-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgarch.rs
257 lines (230 loc) · 7.69 KB
/
garch.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::models::{OptionParameters, OptionPricingModel};
/// A GARCH(1,1) model for option pricing.
pub struct GarchModel {
/// Number of steps in the model.
pub steps: usize,
/// GARCH model parameters.
pub omega: f64,
pub alpha: f64,
pub beta: f64,
/// Epsilon value for numerical differentiation.
pub epsilon: f64,
}
impl GarchModel {
/// Creates a new `GarchModel` with specified parameters.
///
/// # Arguments
///
/// * `steps` - Number of steps in the model.
/// * `omega` - GARCH model parameter omega.
/// * `alpha` - GARCH model parameter alpha.
/// * `beta` - GARCH model parameter beta.
pub fn new(steps: usize, omega: f64, alpha: f64, beta: f64, epsilon: f64) -> Self {
Self {
steps,
omega,
alpha,
beta,
epsilon,
}
}
}
impl Default for GarchModel {
fn default() -> Self {
Self {
steps: 100,
omega: 0.1,
alpha: 0.1,
beta: 0.8,
epsilon: 1e-5,
}
}
}
impl OptionPricingModel for GarchModel {
/// Calculates the call option price using the GARCH(1,1) 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 n = self.steps; // Number of steps in the binomial tree
let dt = params.t / (n as f64); // Time step size
let mut sigma2 = vec![params.sigma * params.sigma; n + 1];
let mut prices = vec![0.0; n + 1];
let mut u = vec![0.0; n + 1];
let mut d = vec![0.0; n + 1];
let mut q = vec![0.0; n + 1];
for i in 1..=n {
sigma2[i] =
self.omega + self.alpha * params.sigma * params.sigma + self.beta * sigma2[i - 1];
u[i] = f64::exp(sigma2[i].sqrt() * (dt as f64).sqrt());
d[i] = 1.0 / u[i];
q[i] = (f64::exp(params.r * dt as f64) - d[i]) / (u[i] - d[i]);
}
for i in 0..=n {
prices[i] = (params.s * u[n - i].powi(i as i32) * d[n - i].powi((n - i) as i32)
- params.k)
.max(0.0);
}
for j in (0..n).rev() {
for i in 0..=j {
prices[i] = f64::exp(-params.r * dt as f64)
* (q[j + 1] * prices[i] + (1.0 - q[j + 1]) * prices[i + 1]);
}
}
prices[0]
}
/// Calculates the put option price using the GARCH(1,1) 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 n = self.steps; // Number of steps in the binomial tree
let dt = params.t / (n as f64); // Time step size
let mut sigma2 = vec![params.sigma * params.sigma; n + 1];
let mut prices = vec![0.0; n + 1];
let mut u = vec![0.0; n + 1];
let mut d = vec![0.0; n + 1];
let mut q = vec![0.0; n + 1];
for i in 1..=n {
sigma2[i] =
self.omega + self.alpha * params.sigma * params.sigma + self.beta * sigma2[i - 1];
u[i] = f64::exp(sigma2[i].sqrt() * (dt as f64).sqrt());
d[i] = 1.0 / u[i];
q[i] = (f64::exp(params.r * dt as f64) - d[i]) / (u[i] - d[i]);
}
for i in 0..=n {
prices[i] = (params.k
- params.s * u[n - i].powi(i as i32) * d[n - i].powi((n - i) as i32))
.max(0.0);
}
for j in (0..n).rev() {
for i in 0..=j {
prices[i] = f64::exp(-params.r * dt as f64)
* (q[j + 1] * prices[i] + (1.0 - q[j + 1]) * prices[i + 1]);
}
}
prices[0]
}
/// Calculates the delta of the option using the GARCH(1,1) 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 sigma2 = params.sigma * params.sigma;
let u = f64::exp(sigma2.sqrt() * (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 call_up = self.call_price(&up_params);
let call_down = self.call_price(&down_params);
let delta = (call_up - call_down) / (params.s * (u - d));
delta.min(1.0).max(-1.0)
}
/// Calculates the gamma of the option using the GARCH(1,1) 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 up_params = OptionParameters {
s: params.s * u,
..params.clone()
};
let down_params = OptionParameters {
s: params.s * d,
..params.clone()
};
let delta_up = self.delta(&up_params);
let delta_down = self.delta(&down_params);
(delta_up - delta_down) / (0.5 * params.s * (u - d))
}
/// Calculates the theta of the option using the GARCH(1,1) model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated theta.
fn theta(&self, params: &OptionParameters) -> f64 {
let epsilon = 1e-5;
let new_params = OptionParameters {
t: params.t - 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) / epsilon
}
/// Calculates the vega of the option using the GARCH(1,1) model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated vega.
fn vega(&self, params: &OptionParameters) -> f64 {
let epsilon = self.epsilon;
let new_params = OptionParameters {
sigma: params.sigma + epsilon,
..params.clone()
};
let call_price_sigma1 = self.call_price(params);
let call_price_sigma2 = self.call_price(&new_params);
(call_price_sigma2 - call_price_sigma1) / epsilon
}
/// Calculates the rho of the option using the GARCH(1,1) model.
///
/// # Arguments
///
/// * `params` - A reference to `OptionParameters` containing the parameters for the option.
///
/// # Returns
///
/// The calculated rho.
fn rho(&self, params: &OptionParameters) -> f64 {
let epsilon = self.epsilon;
let new_params = OptionParameters {
r: params.r + epsilon,
..params.clone()
};
let call_price_r1 = self.call_price(params);
let call_price_r2 = self.call_price(&new_params);
let rho = (call_price_r2 - call_price_r1) / epsilon;
rho.max(0.0)
}
}