-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcir_model.cpp
More file actions
34 lines (29 loc) · 739 Bytes
/
cir_model.cpp
File metadata and controls
34 lines (29 loc) · 739 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
#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;
for (int i = 0; i < n; ++i)
{
r[i + 1] = r[i] + alpha * (b - r[i]) * dt + volatility * sqrt(max(r[i], 0.0) * dt) * Z[i];
}
return r;
}