-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
68 lines (58 loc) · 1.79 KB
/
test.cpp
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
#include <iostream>
#include <iomanip>
#include "matrix.hpp"
using namespace std;
random_device rd;
mt19937 generator(rd());
normal_distribution<double> dist(0.0, 1.0);
int main() {
double rho = 0.6;
vector <double> u = {0.7, 3.0, 0.8, 3.0};
vector <double> x = {0.1, 0.5, 0.1, 1.0};
Matrix sigma(4, 4);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
sigma[i][j] = x[i] * x[j];
if (i != j) sigma[i][j] = rho * x[i] * x[j];
}
}
Matrix sigma_cholesky = Cholesky(sigma);
int n = 1000000;
vector<vector<double>> val(n, vector<double>(4));
for (int i = 0; i < n; i++) {
vector <double> ran(4);
for (int j = 0; j < 4; j++) ran[j] = dist(generator);
vector <double> r = rand_multinormal(u, sigma_cholesky, ran);
for (int j = 0; j < 4; j++) val[i][j] = r[j];
}
cout << scientific << setprecision(4);
cout << "average =\n";
vector <double> ave(4);
for (int j = 0; j < 4; j++) {
double sum = 0;
for (int i = 0; i < n; i++) sum += val[i][j];
ave[j] = sum / n;
cout << ave[j] << ' ';
}
cout << "\n\n";
cout << "standard derivation =\n";
vector <double> s(4);
for (int j = 0; j < 4; j++) {
double sum = 0;
for (int i = 0; i < n; i++) sum += (val[i][j] - ave[j]) * (val[i][j] - ave[j]);
s[j] = sum / n;
cout << sqrt(s[j]) << ' ';
}
cout << "\n\n";
cout << "col matrix =\n";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
double sum = 0;
for (int k = 0; k < n; k++) sum += (val[k][i] - ave[i]) * (val[k][j] - ave[j]);
cout << sum / n / sqrt(s[i] * s[j]) << ' ';
}
cout << '\n';
}
cout << endl;
return 0;
}