Skip to content

Commit 04383f9

Browse files
committed
fix: bonds
1 parent 1776c21 commit 04383f9

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/quant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::Display;
22

3+
pub mod bonds;
34
pub mod calibration;
45
pub mod pricing;
56
pub mod strategies;

src/quant/bonds/cir.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::quant::r#trait::Pricer;
1+
use crate::quant::r#trait::{PricerExt, TimeExt};
22

33
/// CIR model for zero-coupon bond pricing
44
/// dR(t) = theta(mu - R(t))dt + sigma * sqrt(R(t))dW(t)
@@ -21,7 +21,7 @@ pub struct CIR {
2121
pub expiration: Option<chrono::NaiveDate>,
2222
}
2323

24-
impl Pricer for CIR {
24+
impl PricerExt for CIR {
2525
fn calculate_price(&self) -> f64 {
2626
let tau = self.calculate_tau_in_days();
2727

@@ -34,16 +34,21 @@ impl Pricer for CIR {
3434

3535
A * (self.r_t * B).exp()
3636
}
37+
}
3738

39+
impl TimeExt for CIR {
40+
fn calculate_tau_in_days(&self) -> f64 {
41+
self.tau
42+
}
3843
fn tau(&self) -> Option<f64> {
3944
Some(self.tau)
4045
}
4146

42-
fn eval(&self) -> Option<chrono::NaiveDate> {
43-
self.eval
47+
fn eval(&self) -> chrono::NaiveDate {
48+
self.eval.unwrap()
4449
}
4550

46-
fn expiration(&self) -> Option<chrono::NaiveDate> {
47-
self.expiration
51+
fn expiration(&self) -> chrono::NaiveDate {
52+
self.expiration.unwrap()
4853
}
4954
}

src/quant/bonds/hull_white.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use chrono::{Datelike, Utc};
22

3-
use crate::quant::r#trait::Pricer;
3+
use crate::quant::r#trait::{PricerExt, TimeExt};
44

55
/// Hull-White model for zero-coupon bond pricing
66
/// dR(t) = (theta(t) - aR(t))dt + sigma(t)dW(t)
@@ -23,7 +23,7 @@ pub struct HullWhite {
2323
pub expiration: Option<chrono::NaiveDate>,
2424
}
2525

26-
impl Pricer for HullWhite {
26+
impl PricerExt for HullWhite {
2727
/// Calculate the price of the zero-coupon bond (unstable)
2828
fn calculate_price(&self) -> f64 {
2929
let tau = self.calculate_tau_in_years();
@@ -44,16 +44,18 @@ impl Pricer for HullWhite {
4444

4545
A * (-B * self.r_t).exp()
4646
}
47+
}
4748

49+
impl TimeExt for HullWhite {
4850
fn tau(&self) -> Option<f64> {
4951
Some(self.tau)
5052
}
5153

52-
fn eval(&self) -> Option<chrono::NaiveDate> {
53-
self.eval
54+
fn eval(&self) -> chrono::NaiveDate {
55+
self.eval.unwrap()
5456
}
5557

56-
fn expiration(&self) -> Option<chrono::NaiveDate> {
57-
self.expiration
58+
fn expiration(&self) -> chrono::NaiveDate {
59+
self.expiration.unwrap()
5860
}
5961
}

src/quant/bonds/vasicek.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::quant::r#trait::Pricer;
1+
use crate::quant::r#trait::{PricerExt, TimeExt};
22

33
/// Vasicek model for zero-coupon bond pricing
44
/// dR(t) = theta(mu - R(t))dt + sigma dW(t)
@@ -21,7 +21,7 @@ pub struct Vasicek {
2121
pub expiration: Option<chrono::NaiveDate>,
2222
}
2323

24-
impl Pricer for Vasicek {
24+
impl PricerExt for Vasicek {
2525
fn calculate_price(&self) -> f64 {
2626
let tau = self.calculate_tau_in_days();
2727

@@ -31,16 +31,18 @@ impl Pricer for Vasicek {
3131

3232
(A - B * self.r_t).exp()
3333
}
34+
}
3435

36+
impl TimeExt for Vasicek {
3537
fn tau(&self) -> Option<f64> {
3638
Some(self.tau)
3739
}
3840

39-
fn eval(&self) -> Option<chrono::NaiveDate> {
40-
self.eval
41+
fn eval(&self) -> chrono::NaiveDate {
42+
self.eval.unwrap()
4143
}
4244

43-
fn expiration(&self) -> Option<chrono::NaiveDate> {
44-
self.expiration
45+
fn expiration(&self) -> chrono::NaiveDate {
46+
self.expiration.unwrap()
4547
}
4648
}

0 commit comments

Comments
 (0)