Skip to content

Commit 3c67675

Browse files
committed
feat: add calibration history to bsm pricer
1 parent 3188545 commit 3c67675

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/quant/calibration/bsm.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use nalgebra::{DMatrix, DVector, Dyn, Owned};
66

77
use 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)]
1416
pub 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+
5864
impl 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

src/quant/calibration/heston.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ impl From<DVector<f64>> for HestonParams {
5050
}
5151

5252
#[derive(Clone, Debug)]
53-
pub struct CalibrationHistory {
53+
pub struct CalibrationHistory<T> {
5454
pub residuals: DVector<f64>,
5555
pub call_put: DVector<(f64, f64)>,
56-
pub params: HestonParams,
56+
pub params: T,
5757
pub loss_scores: CalibrationLossScore,
5858
}
5959

@@ -77,15 +77,15 @@ pub struct HestonCalibrator {
7777
/// Option type
7878
pub option_type: OptionType,
7979
/// Levenberg-Marquardt algorithm residauls.
80-
calibration_history: RefCell<Vec<CalibrationHistory>>,
80+
calibration_history: RefCell<Vec<CalibrationHistory<HestonParams>>>,
8181
/// Derivate matrix.
8282
derivates: RefCell<Vec<Vec<f64>>>,
8383
}
8484

8585
impl CalibrationLossExt for HestonCalibrator {}
8686

8787
impl HestonCalibrator {
88-
pub fn calibrate(&self) -> Result<Vec<CalibrationHistory>> {
88+
pub fn calibrate(&self) -> Result<Vec<CalibrationHistory<HestonParams>>> {
8989
println!("Initial guess: {:?}", self.params);
9090

9191
let (result, ..) = LevenbergMarquardt::new().minimize(self.clone());

0 commit comments

Comments
 (0)