derivative_pricer/
formulas.rs

1//! Provides Black-Scholes formulas for various securities and greeks.
2//! Provides Black-Scholes formulas for european call and put options, digital call and put options,
3//! forward prices and zero coupon bonds, and greeks of call and put options.
4//! 
5//! Note: the functions in this module use the custome types `Stock` and `NonNegativeFloat` defined in `stock.rs` and `utils.rs`, respectively.
6//! For ease of use, the formulas are also implemented using only the `f64` type in the module `raw_formulas`.
7
8use crate::raw_formulas;
9use crate::utils::NonNegativeFloat;
10use crate::stock::GeometricBrownianMotionStock;
11
12pub fn european_call_option_price(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
13    let ret = raw_formulas::european_call_option_price(f64::from(stock.get_current_state().get_value()), 
14        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
15    NonNegativeFloat::from(ret)
16}
17
18pub fn european_put_option_price(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
19    let ret = raw_formulas::european_put_option_price(f64::from(stock.get_current_state().get_value()), 
20        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
21    NonNegativeFloat::from(ret)
22}
23
24pub fn forward_price(stock: &GeometricBrownianMotionStock, r: f64, time: NonNegativeFloat) -> NonNegativeFloat{
25    let ret = raw_formulas::forward_price(f64::from(stock.get_current_state().get_value()), r, f64::from(time), f64::from(stock.get_divident_rate()));
26    NonNegativeFloat::from(ret)
27}
28
29pub fn digital_call_price(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
30    let ret = raw_formulas::digital_call_price(f64::from(stock.get_current_state().get_value()), 
31    f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
32    NonNegativeFloat::from(ret)
33}
34
35pub fn digital_put_price(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
36    let ret = raw_formulas::digital_put_price(f64::from(stock.get_current_state().get_value()), 
37    f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
38    NonNegativeFloat::from(ret)
39}
40
41pub fn zero_coupon_bond(r: f64,time_to_maturity: NonNegativeFloat) -> NonNegativeFloat{
42    let ret = raw_formulas::zero_coupon_bond(r, f64::from(time_to_maturity));
43    NonNegativeFloat::from(ret)
44}
45
46pub fn call_delta(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
47    let ret = raw_formulas::call_delta(f64::from(stock.get_current_state().get_value()), 
48        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
49    NonNegativeFloat::from(ret)
50}
51
52pub fn call_gamma(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
53    let ret = raw_formulas::call_gamma(f64::from(stock.get_current_state().get_value()), 
54        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
55    NonNegativeFloat::from(ret)
56}
57
58pub fn call_vega(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
59    let ret = raw_formulas::call_vega(f64::from(stock.get_current_state().get_value()), 
60        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
61    NonNegativeFloat::from(ret)
62}
63
64pub fn call_rho(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
65    let ret = raw_formulas::call_rho(f64::from(stock.get_current_state().get_value()), 
66        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
67    NonNegativeFloat::from(ret)
68}
69
70pub fn call_theta(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
71    let ret = raw_formulas::call_theta(f64::from(stock.get_current_state().get_value()), 
72        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
73    NonNegativeFloat::from(ret)
74}
75
76pub fn put_delta(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
77    let ret = raw_formulas::put_delta(f64::from(stock.get_current_state().get_value()), 
78        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
79    NonNegativeFloat::from(ret)
80}
81
82pub fn put_gamma(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
83    let ret = raw_formulas::put_gamma(f64::from(stock.get_current_state().get_value()), 
84        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
85    NonNegativeFloat::from(ret)
86}
87
88pub fn put_vega(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
89    let ret = raw_formulas::put_vega(f64::from(stock.get_current_state().get_value()), 
90        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
91    NonNegativeFloat::from(ret)
92}
93
94pub fn put_rho(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
95    let ret = raw_formulas::put_rho(f64::from(stock.get_current_state().get_value()), 
96        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
97    NonNegativeFloat::from(ret)
98}
99
100pub fn put_theta(stock: &GeometricBrownianMotionStock, strike:NonNegativeFloat, r: f64, time_to_expiry: NonNegativeFloat) -> NonNegativeFloat{
101    let ret = raw_formulas::put_theta(f64::from(stock.get_current_state().get_value()), 
102        f64::from(strike), r, f64::from(time_to_expiry), f64::from(stock.get_volatility()), f64::from(stock.get_divident_rate()));
103    NonNegativeFloat::from(ret)
104}