-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hull_white and hull_white_2f
- Loading branch information
Showing
41 changed files
with
478 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod bonds; | ||
pub mod options; | ||
pub mod r#trait; | ||
pub mod volatility; | ||
pub mod yahoo; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod cir; | ||
pub mod hull_white; | ||
pub mod vasicek; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::quant::r#trait::Price; | ||
|
||
/// CIR model for zero-coupon bond pricing | ||
/// dR(t) = theta(mu - R(t))dt + sigma * sqrt(R(t))dW(t) | ||
/// where R(t) is the short rate. | ||
#[derive(Default, Debug)] | ||
pub struct CIR { | ||
/// Initial short rate | ||
pub r_t: f64, | ||
/// Long-term mean of the short rate | ||
pub theta: f64, | ||
/// Mean reversion speed | ||
pub mu: f64, | ||
/// Volatility | ||
pub sigma: f64, | ||
/// Maturity of the bond in days | ||
pub tau: f64, | ||
/// Evaluation date | ||
pub eval: Option<chrono::NaiveDate>, | ||
/// Expiration date | ||
pub expiration: Option<chrono::NaiveDate>, | ||
} | ||
|
||
impl Price for CIR { | ||
fn price(&self) -> f64 { | ||
todo!() | ||
} | ||
|
||
fn tau(&self) -> Option<f64> { | ||
Some(self.tau) | ||
} | ||
|
||
fn eval(&self) -> Option<chrono::NaiveDate> { | ||
self.eval | ||
} | ||
|
||
fn expiration(&self) -> Option<chrono::NaiveDate> { | ||
self.expiration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::quant::r#trait::Price; | ||
|
||
/// Hull-White model for zero-coupon bond pricing | ||
/// dR(t) = (theta(t) - aR(t))dt + sigma(t)dW(t) | ||
/// where R(t) is the short rate. | ||
#[derive(Debug)] | ||
pub struct HullWhite { | ||
/// Short rate | ||
pub r_t: f64, | ||
/// Long-term mean of the short rate | ||
pub theta: fn(f64) -> f64, | ||
/// Mean reversion speed | ||
pub alpha: f64, | ||
/// Volatility | ||
pub sigma: f64, | ||
/// Maturity of the bond in days | ||
pub tau: f64, | ||
/// Evaluation date | ||
pub eval: Option<chrono::NaiveDate>, | ||
/// Expiration date | ||
pub expiration: Option<chrono::NaiveDate>, | ||
} | ||
|
||
impl Price for HullWhite { | ||
fn price(&self) -> f64 { | ||
0.0 | ||
} | ||
|
||
fn tau(&self) -> Option<f64> { | ||
Some(self.tau) | ||
} | ||
|
||
fn eval(&self) -> Option<chrono::NaiveDate> { | ||
self.eval | ||
} | ||
|
||
fn expiration(&self) -> Option<chrono::NaiveDate> { | ||
self.expiration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::quant::r#trait::Price; | ||
|
||
/// Vasicek model for zero-coupon bond pricing | ||
/// dR(t) = theta(mu - R(t))dt + sigma dW(t) | ||
/// where R(t) is the short rate. | ||
#[derive(Default, Debug)] | ||
pub struct Vasicek { | ||
/// Short rate | ||
pub r_t: f64, | ||
/// Long-term mean of the short rate | ||
pub theta: f64, | ||
/// Mean reversion speed | ||
pub mu: f64, | ||
/// Volatility | ||
pub sigma: f64, | ||
/// Maturity of the bond in days | ||
pub tau: f64, | ||
/// Evaluation date | ||
pub eval: Option<chrono::NaiveDate>, | ||
/// Expiration date | ||
pub expiration: Option<chrono::NaiveDate>, | ||
} | ||
|
||
impl Price for Vasicek { | ||
fn price(&self) -> f64 { | ||
// Itt definiálhatod a Vasicek modell árképzési képletét | ||
// Placeholder érték visszaadása | ||
0.0 | ||
} | ||
|
||
fn tau(&self) -> Option<f64> { | ||
Some(self.tau) | ||
} | ||
|
||
fn eval(&self) -> Option<chrono::NaiveDate> { | ||
self.eval | ||
} | ||
|
||
fn expiration(&self) -> Option<chrono::NaiveDate> { | ||
self.expiration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use chrono::Local; | ||
use nalgebra::DVector; | ||
|
||
/// Pricer trait. | ||
pub(crate) trait Pricer { | ||
/// Calculate the price of an option. | ||
fn calculate_price(&mut self); | ||
/// Update the parameters. | ||
fn update_params(&mut self, params: DVector<f64>); | ||
/// Update strike price. | ||
fn update_strike(&mut self, k: f64); | ||
/// Prices. | ||
fn prices(&self) -> (f64, f64); | ||
/// Derivatives. | ||
fn derivates(&self) -> Vec<f64>; | ||
} | ||
|
||
/// Price an instrument. | ||
pub trait Price { | ||
/// Calculate the price of an instrument. | ||
fn price(&self) -> f64; | ||
|
||
/// Calculate the valuation date of an instrument. | ||
fn calculate_tau_in_days(&self) -> f64 { | ||
if let Some(tau) = self.tau() { | ||
tau | ||
} else { | ||
let eval = self | ||
.eval() | ||
.unwrap_or_else(|| Local::now().naive_local().into()); | ||
let expiration = self.expiration().unwrap(); | ||
(expiration - eval).num_days() as f64 | ||
} | ||
} | ||
|
||
/// Calculate the valuation date of an instrument. | ||
fn calculate_tau_in_years(&self) -> f64 { | ||
if let Some(tau) = self.tau() { | ||
tau | ||
} else { | ||
let eval = self | ||
.eval() | ||
.unwrap_or_else(|| Local::now().naive_local().into()); | ||
let expiration = self.expiration().unwrap(); | ||
(expiration - eval).num_days() as f64 / 365.0 | ||
} | ||
} | ||
|
||
fn tau(&self) -> Option<f64>; | ||
fn eval(&self) -> Option<chrono::NaiveDate>; | ||
fn expiration(&self) -> Option<chrono::NaiveDate>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.