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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Module containing functions for calculating first-order greeks
use std::f64::consts::E;

use common::*;
use stats::cnd;

/// Calculates the delta of a call option.
///
/// Delta measures the rate of the theoretical option value with respect to the changes in the underlying asset's price.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
pub fn delta_call(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64) -> f64 {
    let d1 = d1(s0, x, t, r, q, sigma);
    let cnd = cnd(d1);
    let e = E.powf(-(q * t));
    return e * cnd;
}

/// Calculates the delta of a put options
///
/// Delta measures the rate of the theoretical option value with respect to the changes in the underlying asset's price.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
pub fn delta_put(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64) -> f64 {
    let d1 = d1(s0, x, t, r, q, sigma);
    let cnd = cnd(d1);
    let e = E.powf(-(q * t));
    return e * (cnd - 1.0);
}

/// Calculates the lambda of a call option, also known as Omega
///
/// Omega is the percentage of change in an option's value with respect to the percentage change in the underlying price.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
/// * `v` - value or current price of the option
pub fn lambda_call(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64, v: f64) -> f64 {
    let delta = delta_call(s0, x, t, r, q, sigma);
    return lambda(s0, v, delta);
}

/// Calculates the lambda of a put option, also known as Omega
///
/// Omega is the percentage of change in an option's value with respect to the percentage change in the underlying price.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
/// * `v` - value or current price of the option
pub fn lambda_put(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64, v: f64) -> f64 {
    let delta = delta_put(s0, x, t, r, q, sigma);
    return lambda(s0, v, delta);
}

fn lambda(s0: f64, v: f64, delta: f64) -> f64 {
    return delta * s0 / v;
}

/// Calculates the Rho of a call option
///
/// Rho measures the sensitivity to the interest rate. Rho is the derivative of the option value with respect to the risk free interest rate.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
pub fn rho_call(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64) -> f64 {
    let d2_cnd = cnd(d2(s0, x, t, r, q, sigma));
    return (1.0 / 100.0) * x * t * E.powf(-r * t) * d2_cnd;
}

/// Calculates the Rho of a put option
///
/// Rho measures the sensitivity to the interest rate. Rho is the derivative of the option value with respect to the risk free interest rate.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
pub fn rho_put(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64) -> f64 {
    let neg_d2_cnd = cnd(-d2(s0, x, t, r, q, sigma));
    return -(1.0 / 100.0) * x * t * E.powf(-r * t) * neg_d2_cnd;
}

/// Calculates the Theta of a call option
///
/// Theta measures the sensitivity of the value of the derivative to the passage of time.
pub fn theta_call(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64, days_per_year: f64) -> f64 {
    let d1 = d1(s0, x, t, r, q, sigma);
    let arg1 = theta_arg_1(s0, t, q, sigma, d1);
    let d2 = d2_d1(t, sigma, d1);
    let arg2 = theta_arg_2(x, t, r, d2);
    let arg3 = theta_arg_3(s0, t, q, d1);
    return (1.0 / days_per_year) * (arg1 - arg2 + arg3);
}

/// Calculates the Theta of a put option
///
/// Theta measures the sensitivity of the value of the derivative to the passage of time.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
/// * `days_per_year` - the number of calendar days in the year
pub fn theta_put(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64, days_per_year: f64) -> f64 {
    let d1 = d1(s0, x, t, r, q, sigma);
    let arg1 = theta_arg_1(s0, t, q, sigma, d1);
    let d2 = d2_d1(t, sigma, d1);
    let arg2 = theta_arg_2(x, t, r, -d2); // d2 is negative for a put
    let arg3 = theta_arg_3(s0, t, q, -d1); // d1 is negative for a put
    return (1.0 / days_per_year) * (arg1 + arg2 - arg3);
}

fn theta_arg_1(s0: f64, t: f64, q: f64, sigma: f64, d1: f64) -> f64 {
    return -(((s0 * sigma * E.powf(-q * t)) / (2.0 * t.sqrt())) * one_over_sqrt_pi() *
             E.powf((-d1.powf(2.0)) / 2.0));
}

fn theta_arg_2(x: f64, t: f64, r: f64, d2: f64) -> f64 {
    return r * x * E.powf(-r * t) * cnd(d2);
}

fn theta_arg_3(s0: f64, t: f64, q: f64, d1: f64) -> f64 {
    return q * s0 * E.powf(-q * t) * cnd(d1);
}

/// Calculates the Vega of a given option
///
/// Vega measures the sensitivity to volatility. Vega is the derivative of the option value with respect to the volatility of the underlying asset.
///
/// # Arguments
/// * `s0` - The underlying price of the option
/// * `x` - The strike price of the option
/// * `t` - time to expiration as a percentage of the year
/// * `r` - continuously compounded risk-free interest rate
/// * `q` - continuously compounded divident yield
/// * `sigma` - volatility
pub fn vega(s0: f64, x: f64, t: f64, r: f64, q: f64, sigma: f64) -> f64 {
    let d1 = d1(s0, x, t, r, q, sigma);
    return vega_d1(s0, t, q, d1);
}

pub fn vega_d1(s0: f64, t: f64, q: f64, d1: f64) -> f64 {
    let mult1 = (1.0 / 100.0) * s0 * E.powf(-(q * t)) * t.sqrt();
    let mult2 = one_over_sqrt_pi();
    let mult3 = E.powf((-d1.powf(2.0) / 2.0));
    return mult1 * mult2 * mult3;
}

#[cfg(test)]
mod tests {

    use greeks::*;
    use value::*;

    const UNDERLYING: f64 = 64.68;
    const STRIKE: f64 = 65.00;
    const VOL: f64 = 0.5051;
    const INTEREST_RATE: f64 = 0.0150;
    const DIV_YIELD: f64 = 0.0210;
    const DAYS_PER_YEAR: f64 = 365.0;
    const TIME_TO_EXPIRY: f64 = 23.0 / DAYS_PER_YEAR;

    const E_CALL_DELTA: f64 = 0.5079;
    const E_PUT_DELTA: f64 = -0.4908;
    const E_LAMBDA_PUT: f64 = -3.0759;
    const E_LAMBDA_CALL: f64 = 3.3936;
    const E_RHO_CALL: f64 = 0.0187;
    const E_RHO_PUT: f64 = -0.0222;
    const E_THETA_CALL: f64 = -0.0703;
    const E_THETA_PUT: f64 = -0.0714;
    const E_VEGA: f64 = 0.0647;

    #[test]
    fn test_delta_call() {
        let call_delta = delta_call(UNDERLYING,
                                    STRIKE,
                                    TIME_TO_EXPIRY,
                                    INTEREST_RATE,
                                    DIV_YIELD,
                                    VOL);
        let abs = (call_delta - E_CALL_DELTA).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_delta_put() {
        let put_delta = delta_put(UNDERLYING,
                                  STRIKE,
                                  TIME_TO_EXPIRY,
                                  INTEREST_RATE,
                                  DIV_YIELD,
                                  VOL);
        let abs = (put_delta - E_PUT_DELTA).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_lambda_put() {
        // Abitrary change in underlying at expiry
        let price = put_at_expiry(UNDERLYING - 10.0, STRIKE);
        let lambda = lambda_put(UNDERLYING,
                                STRIKE,
                                TIME_TO_EXPIRY,
                                INTEREST_RATE,
                                DIV_YIELD,
                                VOL,
                                price);
        println!("{}", lambda);
        let abs = (lambda - E_LAMBDA_PUT).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_lambda_call() {
        // abitrary change in underlying at expiry
        let price = call_at_expiry(UNDERLYING + 10.0, STRIKE);
        let lambda = lambda_call(UNDERLYING,
                                 STRIKE,
                                 TIME_TO_EXPIRY,
                                 INTEREST_RATE,
                                 DIV_YIELD,
                                 VOL,
                                 price);
        let abs = (lambda - E_LAMBDA_CALL).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_rho_call() {
        let rho_call = rho_call(UNDERLYING,
                                STRIKE,
                                TIME_TO_EXPIRY,
                                INTEREST_RATE,
                                DIV_YIELD,
                                VOL);
        let abs = (rho_call - E_RHO_CALL).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_rho_put() {
        let rho_put = rho_put(UNDERLYING,
                              STRIKE,
                              TIME_TO_EXPIRY,
                              INTEREST_RATE,
                              DIV_YIELD,
                              VOL);
        let abs = (rho_put - E_RHO_PUT).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_theta_call() {
        let theta_call = theta_call(UNDERLYING,
                                    STRIKE,
                                    TIME_TO_EXPIRY,
                                    INTEREST_RATE,
                                    DIV_YIELD,
                                    VOL,
                                    DAYS_PER_YEAR);
        let abs = (theta_call - E_THETA_CALL).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_theta_put() {
        let theta_put = theta_put(UNDERLYING,
                                  STRIKE,
                                  TIME_TO_EXPIRY,
                                  INTEREST_RATE,
                                  DIV_YIELD,
                                  VOL,
                                  DAYS_PER_YEAR);
        let abs = (theta_put - E_THETA_PUT).abs();
        assert!(abs < 0.001);
    }

    #[test]
    fn test_vega() {
        let vega = vega(UNDERLYING,
                        STRIKE,
                        TIME_TO_EXPIRY,
                        INTEREST_RATE,
                        DIV_YIELD,
                        VOL);
        let abs = (vega - E_VEGA).abs();
        assert!(abs < 0.001);
    }
}