@@ -6,10 +6,12 @@ use nalgebra::{DMatrix, DVector, Dyn, Owned};
66
77use crate :: quant:: {
88 pricing:: bsm:: { BSMCoc , BSMPricer } ,
9- r#trait:: PricerExt ,
10- OptionType ,
9+ r#trait:: { CalibrationLossExt , PricerExt } ,
10+ CalibrationLossScore , OptionType ,
1111} ;
1212
13+ use super :: heston:: CalibrationHistory ;
14+
1315#[ derive( Clone , Debug ) ]
1416pub struct BSMParams {
1517 /// Implied volatility
@@ -51,10 +53,14 @@ pub struct BSMCalibrator {
5153 pub tau : f64 ,
5254 /// Option type
5355 pub option_type : OptionType ,
56+ /// Levenberg-Marquardt algorithm residauls.
57+ calibration_history : RefCell < Vec < CalibrationHistory < BSMParams > > > ,
5458 /// Derivate matrix.
5559 derivates : RefCell < Vec < Vec < f64 > > > ,
5660}
5761
62+ impl CalibrationLossExt for BSMCalibrator { }
63+
5864impl BSMCalibrator {
5965 pub fn calibrate ( & self ) {
6066 println ! ( "Initial guess: {:?}" , self . params) ;
@@ -117,6 +123,23 @@ impl LeastSquaresProblem<f64, Dyn, Dyn> for BSMCalibrator {
117123 OptionType :: Put => c_model[ idx] = put,
118124 }
119125
126+ self
127+ . calibration_history
128+ . borrow_mut ( )
129+ . push ( CalibrationHistory {
130+ residuals : c_model. clone ( ) - self . c_market . clone ( ) ,
131+ call_put : vec ! [ ( call, put) ] . into ( ) ,
132+ params : self . params . clone ( ) . into ( ) ,
133+ loss_scores : CalibrationLossScore {
134+ mae : self . mae ( & c_model, & self . c_market ) ,
135+ mse : self . mse ( & c_model, & self . c_market ) ,
136+ rmse : self . rmse ( & c_model, & self . c_market ) ,
137+ mpe : self . mpe ( & c_model, & self . c_market ) ,
138+ mape : self . mape ( & c_model, & self . c_market ) ,
139+ mspe : self . mspe ( & c_model, & self . c_market ) ,
140+ rmspe : self . rmspe ( & c_model, & self . c_market ) ,
141+ } ,
142+ } ) ;
120143 derivates. push ( pricer. derivatives ( ) ) ;
121144 }
122145
0 commit comments