Skip to content

Commit

Permalink
feat: add bond price of vasicek and cir
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Oct 2, 2024
1 parent 40f9e28 commit ff5b08f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/quant/bonds/cir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::quant::r#trait::Price;
/// where R(t) is the short rate.
#[derive(Default, Debug)]
pub struct CIR {
/// Initial short rate
/// Short rate
pub r_t: f64,
/// Long-term mean of the short rate
pub theta: f64,
Expand All @@ -23,7 +23,16 @@ pub struct CIR {

impl Price for CIR {
fn price(&self) -> f64 {
todo!()
let tau = self.calculate_tau_in_days();

let h = (self.theta.powi(2) + 2.0 * self.sigma.powi(2)).sqrt();
let A = ((2.0 * h * ((self.theta + h) * (tau / 2.0)).exp())
/ (2.0 * h + (self.theta + h) * ((h * tau).exp() - 1.0)))
.powf((2.0 * self.theta * self.mu) / (self.sigma.powi(2)));
let B =
(2.0 * ((h * tau).exp() - 1.0)) / (2.0 * h + (self.theta + h) * ((h * tau).exp() - 1.0));

A * (self.r_t * B).exp()
}

fn tau(&self) -> Option<f64> {
Expand Down
10 changes: 7 additions & 3 deletions src/quant/bonds/vasicek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ pub struct Vasicek {

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
let tau = self.calculate_tau_in_days();

let B = (1.0 - (-self.theta * tau).exp()) / self.theta;
let A = (self.mu - (self.sigma.powi(2) / (2.0 * self.theta.powi(2)))) * (B - tau)
- (self.sigma.powi(2) / (4.0 * self.theta)) * B.powi(2);

(A - B * self.r_t).exp()
}

fn tau(&self) -> Option<f64> {
Expand Down

0 comments on commit ff5b08f

Please sign in to comment.