-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvasicek_model.cpp
More file actions
38 lines (32 loc) · 923 Bytes
/
vasicek_model.cpp
File metadata and controls
38 lines (32 loc) · 923 Bytes
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
#include <iostream>
#include <random>
#include <vector>
using namespace std;
vector<double> generate_random_sequence(int n)
{
random_device rd;
mt19937 gen(rd());
normal_distribution<double> dist(0.0, 1.0);
vector<double> Z(n, 0.0);
for (int i = 0; i < n; ++i)
{
double z = dist(gen);
Z[i] = z;
}
return Z;
}
vector<double> short_rate(int n, double r0, double T, double volatility, double alpha, double b)
{
vector<double> Z = generate_random_sequence(n);
vector<double> r(n + 1);
double dt = T / n;
r[0] = r0;
double memory_coeff = exp(-alpha * dt);
double mean_reversion_coeff = b * (1 - memory_coeff);
double stochastic_coeff = volatility * sqrt((1 - exp(-2 * alpha * dt)) / (2 * alpha));
for (int i = 0; i < n; ++i)
{
r[i + 1] = r[i] * memory_coeff + mean_reversion_coeff + stochastic_coeff * Z[i];
}
return r;
}