-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_walk.cpp
More file actions
46 lines (36 loc) · 961 Bytes
/
random_walk.cpp
File metadata and controls
46 lines (36 loc) · 961 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
39
40
41
42
43
44
45
46
#include <iostream>
#include <random>
#include <vector>
#include <fstream>
using namespace std;
void save_path(const vector<double>& A, const string& filename) {
ofstream out(filename);
for (int i = 0; i < A.size(); ++i) {
out << i << "," << A[i] << "\n";
}
}
vector<double> brownian_motion(int n, double drift, double diffusion_coefficient){
random_device rd;
mt19937 gen(rd());
vector<double> Z(n, 0.0);
double dt = 1.0/n;
normal_distribution<double> dist(0.0, 1.0);
for (int i = 0; i < n; ++i) {
double z = dist(gen);
Z[i] = z;
}
vector<double> W(n+1,0.0);
for (int i = 1; i<n+1; i++){
W[i]=W[i-1]+sqrt(dt)*Z[i-1];
}
vector<double> BM(n+1,0.0);
for (int i = 0; i<n+1; i++){
BM[i]=drift*i*dt + diffusion_coefficient*W[i];
}
return BM;
}
int main() {
auto BM = brownian_motion(50, 1.0, 0.5);
save_path(BM, "plot.csv");
return 0;
}