forked from Liberxue/cqf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binomial_tree.rs
237 lines (213 loc) · 7.49 KB
/
binomial_tree.rs
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
use crate::models::{OptionParameters, OptionPricingModel};
// <https://www.kent.ac.uk/learning/documents/slas-documents/Binomial_models.pdf >
// <https://www.le.ac.uk/users/dsgp1/COURSES/DERIVATE/BINOPTION.PDF >
pub struct BinomialTreeModel {
/// Number of steps in the binomial tree model.
pub steps: usize,
/// Epsilon value for numerical differentiation.
pub epsilon: f64,
}
enum OptionType {
Call,
Put,
}
impl BinomialTreeModel {
/// Creates a new `BinomialTreeModel` with a specified number of steps and epsilon.
///
/// # Arguments
///
/// * `steps` - Number of steps in the binomial tree model.
/// * `epsilon` - Epsilon value for numerical differentiation.
pub fn new(steps: usize, epsilon: f64) -> Self {
Self { steps, epsilon }
}
/// Initializes the prices vector for call or put options.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
/// * `option_type` - A value indicating the type of option (`Call` or `Put`).
///
/// # Returns
///
/// A vector containing the prices of the option at each node.
fn initialize_prices(&self, params: &OptionParameters, option_type: OptionType) -> Vec<f64> {
let n = self.steps; // Number of steps in the binomial tree
let dt = params.t / (n as f64); // Time step size
let u = f64::exp(params.sigma * (dt as f64).sqrt()); // Up factor
let d = 1.0 / u; // Down factor
// Terminal prices
(0..=n)
.map(|i| {
let price = params.s * u.powi((n - i) as i32) * d.powi(i as i32);
match option_type {
OptionType::Call => (price - params.k).max(0.0),
OptionType::Put => (params.k - price).max(0.0),
}
})
.collect()
}
/// Performs backward induction to calculate option price.
///
/// # Arguments
///
/// * `prices` - A mutable vector containing the prices of the option at each node.
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated option price.
fn backward_induction(&self, prices: &mut Vec<f64>, params: &OptionParameters) -> f64 {
let n = self.steps; // Number of steps in the binomial tree
let dt = params.t / (n as f64); // Time step size
let u = f64::exp(params.sigma * (dt as f64).sqrt()); // Up factor
let d = 1.0 / u; // Down factor
let q = (f64::exp(params.r * dt as f64) - d) / (u - d); // Risk-neutral probability
for j in (0..n).rev() {
for i in 0..=j {
prices[i] =
f64::exp(-params.r * dt as f64) * (q * prices[i] + (1.0 - q) * prices[i + 1]);
}
}
prices[0]
}
}
impl Default for BinomialTreeModel {
fn default() -> Self {
Self {
steps: 100,
epsilon: 1e-5,
} // Default number of steps is 100 and epsilon is 1e-5
}
}
impl OptionPricingModel for BinomialTreeModel {
/// Calculates the call option price using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated call option price.
fn call_price(&self, params: &OptionParameters) -> f64 {
let mut prices = self.initialize_prices(params, OptionType::Call);
self.backward_induction(&mut prices, params)
}
/// Calculates the put option price using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated put option price.
fn put_price(&self, params: &OptionParameters) -> f64 {
let mut prices = self.initialize_prices(params, OptionType::Put);
self.backward_induction(&mut prices, params)
}
/// Calculates the delta of the option using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated delta.
fn delta(&self, params: &OptionParameters) -> f64 {
let n = self.steps;
let dt = params.t / (n as f64);
let u = f64::exp(params.sigma * (dt as f64).sqrt());
let d = 1.0 / u;
let up_params = OptionParameters {
s: params.s * u,
..params.clone()
};
let down_params = OptionParameters {
s: params.s * d,
..params.clone()
};
let delta_up = self.call_price(&up_params);
let delta_down = self.call_price(&down_params);
(delta_up - delta_down) / (params.s * (u - d))
}
/// Calculates the gamma of the option using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated gamma.
fn gamma(&self, params: &OptionParameters) -> f64 {
let n = self.steps;
let dt = params.t / (n as f64);
let u = f64::exp(params.sigma * (dt as f64).sqrt());
let d = 1.0 / u;
let delta_up = self.delta(&OptionParameters {
s: params.s * u,
..params.clone()
});
let delta_down = self.delta(&OptionParameters {
s: params.s * d,
..params.clone()
});
(delta_up - delta_down) / (0.5 * params.s * (u - d))
}
/// Calculates the theta of the option using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated theta.
fn theta(&self, params: &OptionParameters) -> f64 {
let new_params = OptionParameters {
t: params.t - self.epsilon,
..params.clone()
};
let call_price_t1 = self.call_price(params);
let call_price_t2 = self.call_price(&new_params);
(call_price_t2 - call_price_t1) / self.epsilon
}
/// Calculates the vega of the option using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated vega.
fn vega(&self, params: &OptionParameters) -> f64 {
let call_price_sigma1 = self.call_price(params);
let call_price_sigma2 = self.call_price(&OptionParameters {
sigma: params.sigma + self.epsilon,
..params.clone()
});
(call_price_sigma2 - call_price_sigma1) / self.epsilon
}
/// Calculates the rho of the option using the binomial tree model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated rho.
fn rho(&self, params: &OptionParameters) -> f64 {
let new_params = OptionParameters {
r: params.r + self.epsilon,
..params.clone()
};
let call_price_r1 = self.call_price(params);
let call_price_r2 = self.call_price(&new_params);
(call_price_r2 - call_price_r1) / self.epsilon
}
}