1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! # black_scholes
//! A Black Scholes option pricing library.
extern crate special;
extern crate nrfind;
use std::f64::consts::PI;
use std::f64::consts::SQRT_2;
use special::Error;
#[macro_use]
#[cfg(test)]
extern crate approx;

fn cum_norm(x:f64)->f64 {
    (x/SQRT_2).erf()*0.5+0.5
}
fn inc_norm(x:f64)->f64 {
    (-x.powi(2)/2.0).exp()/(PI.sqrt()*SQRT_2)
}

pub fn call_discount(s:f64, k:f64, discount:f64, sqrt_maturity_sigma:f64)->f64{
    if sqrt_maturity_sigma>0.0{
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        s*cum_norm(d1)-k*discount*cum_norm(d1-sqrt_maturity_sigma)
    }
    else{
        if s>k {s-k} else {0.0}
    }
}

/// Returns BS call option formula.
///
/// # Examples
///
/// ```
/// let stock = 5.0;
/// let strike = 4.5;
/// let rate = 0.05;
/// let sigma=0.3;
/// let maturity=1.0;
/// assert_eq!(0.9848721043419868, black_scholes::call(stock, strike, rate, sigma, maturity));
/// ```
pub fn call(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    call_discount(s, k, (-rate*maturity).exp(), maturity.sqrt()*sigma)
}


pub fn call_delta(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_maturity_sigma=maturity.sqrt()*sigma;
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        cum_norm(d1)
    }
    else{
        if s>k {1.0} else {0.0}
    }
}


pub fn call_gamma(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_maturity_sigma=maturity.sqrt()*sigma;    
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        inc_norm(d1)/(s*sqrt_maturity_sigma)
    }
    else{
        0.0
    }
}

pub fn call_vega(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_maturity_sigma=maturity.sqrt()*sigma;    
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        s*inc_norm(d1)*sqrt_maturity_sigma/sigma
    }
    else{
        0.0
    }
}
pub fn call_theta(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_t=maturity.sqrt();
    let sqrt_maturity_sigma=sqrt_t*sigma;    
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        -s*inc_norm(d1)*sigma/(2.0*sqrt_t)-rate*k*discount*cum_norm(d1-sqrt_maturity_sigma)
    }
    else{
        0.0
    }
}

pub fn put_discount(s:f64, k:f64, discount:f64, sqrt_maturity_sigma:f64)->f64{
    if sqrt_maturity_sigma>0.0{
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        k*discount*cum_norm(sqrt_maturity_sigma-d1)-s*cum_norm(-d1)
    }
    else{
        if k>s {k-s} else {0.0}
    }
}

/// Returns BS put option formula.
///
/// # Examples
///
/// ```
/// let stock = 5.0;
/// let strike = 4.5;
/// let rate = 0.05;
/// let sigma = 0.3;
/// let maturity = 1.0;
/// assert_eq!(0.2654045145951993, black_scholes::put(stock, strike, rate, sigma, maturity));
/// ```
pub fn put(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    put_discount(s, k, (-rate*maturity).exp(), maturity.sqrt()*sigma)
}

pub fn put_delta(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_maturity_sigma=maturity.sqrt()*sigma;
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();  
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        return cum_norm(d1)-1.0;
    }
    else{
        return if k>s {-1.0} else {0.0};
    }
}
pub fn put_gamma(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    call_gamma(s, k, rate, sigma, maturity)//same as call
}

pub fn put_vega(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    call_vega(s, k, rate, sigma, maturity) //same as call
}

pub fn put_theta(s:f64, k:f64, rate:f64, sigma:f64, maturity:f64)->f64{
    let sqrt_t=maturity.sqrt();
    let sqrt_maturity_sigma=sqrt_t*sigma;    
    if sqrt_maturity_sigma>0.0{
        let discount=(-rate*maturity).exp();
        let d1=(s/(k*discount)).ln()/sqrt_maturity_sigma+0.5*sqrt_maturity_sigma;
        -s*inc_norm(d1)*sigma/(2.0*sqrt_t)+rate*k*discount*cum_norm(-d1+sqrt_maturity_sigma)
    }
    else{
        0.0
    }
}

pub fn call_iv(price:f64, s:f64, k:f64, rate:f64, maturity:f64, initial_guess:f64)->f64{
    let obj_fn=|sigma|call(s, k, rate, sigma, maturity)-price;
    let dfn=|sigma|call_vega(s, k, rate, sigma, maturity);
    let precision=0.00000001;
    let iterations=20;
    nrfind::find_root(&obj_fn, &dfn, initial_guess, precision, iterations).unwrap()
}
pub fn put_iv(price:f64, s:f64, k:f64, rate:f64, maturity:f64, initial_guess:f64)->f64{
    let obj_fn=|sigma|put(s, k, rate, sigma, maturity)-price;
    let dfn=|sigma|put_vega(s, k, rate, sigma, maturity);
    let precision=0.00000001;
    let iterations=20;
    nrfind::find_root(&obj_fn, &dfn, initial_guess, precision, iterations).unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn call_formula_works() {
        assert_eq!(call(5.0, 4.5, 0.05, 0.3, 1.0), 0.9848721043419868);
    }
    #[test]
    fn call_formula_works_with_zero_vol() {
        assert_eq!(call(5.0, 4.5, 0.05, 0.3, 0.0), 0.5);
    }
    #[test]
    fn put_formula_works() {
        assert_eq!(put(5.0, 4.5, 0.05, 0.3, 1.0), 0.2654045145951993);
    }
    #[test]
    fn put_formula_works_with_zero_vol() {
        assert_eq!(put(5.0, 4.5, 0.05, 0.3, 0.0), 0.0);
    }
    #[test]
    fn call_iv_works(){
        let sigma=0.2;
        let initial_guess=0.5;
        let s=5.0;
        let k=4.5;
        let rate=0.05;
        let maturity=1.0;
        let price=call(s, k, rate, sigma, maturity);
        assert_abs_diff_eq!(call_iv(price, s, k, rate, maturity , initial_guess), sigma, epsilon=0.00000001);
    }
    #[test]
    fn put_iv_works(){
        let sigma=0.2;
        let initial_guess=0.5;
        let s=5.0;
        let k=4.5;
        let rate=0.05;
        let maturity=1.0;
        let price=put(s, k, rate, sigma, maturity);
        assert_abs_diff_eq!(put_iv(price, s, k, rate, maturity , initial_guess), sigma, epsilon=0.00000001);
    }
}