-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharima-sim.cpp
More file actions
282 lines (223 loc) · 8.26 KB
/
arima-sim.cpp
File metadata and controls
282 lines (223 loc) · 8.26 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//This program will take the parameters set at the beginning of the main function (around line 230) and output a two column csv, the first being the simulated magnitudues
//and the second being the direction.
#include <iostream>
#include <vector>
#include <random>
#include <fstream>
#include <cmath>
static constexpr double bad_number = -9999.0;
static constexpr double epsilon = 0.000000000000000001;
static constexpr double pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
bool isFilterOk(double value)
{
return !(std::abs(value - bad_number) < epsilon);
}
void applyAutoregressiveFilter(std::vector<double>& arimaWindStream, std::vector<double>& autoregressiveParameters)
{
std::vector<double> out(arimaWindStream.size() + autoregressiveParameters.size());
int nf = (int)autoregressiveParameters.size();
int nx = (int)arimaWindStream.size();
double sum;
double tmp;
bool goodStatus = true;
for (int i = 0; i < nx; i++)
{
goodStatus = true;
sum = arimaWindStream[(unsigned int)i];
for (int j = 0; j < nf; j++)
{
tmp = out[(unsigned int)(nf + i - j - 1)];
if (isFilterOk(tmp))
{
sum += tmp * autoregressiveParameters[(unsigned int)j];
}
else
{
out[(unsigned int)(nf + i)] = bad_number;
goodStatus = false;
break;
}
}
if (goodStatus)
{
out[(unsigned int)(nf + i)] = sum;
}
else
{
continue;
}
}
out.erase(out.begin(), out.begin() + nf); // Delete junk data
arimaWindStream = out;
}
void applyMovingAverageFilter(std::vector<double>& arimaWindStream, std::vector<double>& movingAverageParameters)
{
int nshift = 0;
int nf = (int)movingAverageParameters.size();
int nx = (int)arimaWindStream.size();
std::vector<double> out(arimaWindStream.size() + nf);
double z;
double tmp;
bool goodStatus = true;
for (int i = 0; i < (int)(arimaWindStream.size()); i++)
{
goodStatus = true;
z = 0;
if (i + nshift - (nf - 1) < 0 || i + nshift >= nx)
{
out[(unsigned int)i] = bad_number;
continue;
}
for (int j = std::max(0, nshift + i - nx); j < std::min(nf, i + nshift + 1); j++)
{
tmp = arimaWindStream[(unsigned int)(i + nshift - j)];
if (isFilterOk(tmp))
{
z += movingAverageParameters[(unsigned int)j] * tmp;
}
else
{
out[(unsigned int)i] = bad_number;
goodStatus = false;
}
}
if (goodStatus)
{
out[(unsigned int)i] = z;
}
else
{
continue;
}
}
out.erase(out.begin(), out.begin() + nf); // Delete junk data
arimaWindStream = out;
}
std::vector<double> derive_autocorrelation(const std::vector<double>& ar_coeffs, const std::vector<double>& ma_coeffs, size_t k) {
size_t p = ar_coeffs.size();
size_t q = ma_coeffs.size();
size_t max_lag = std::max(p, q + k);
std::vector<double> acf(max_lag + 1, 0.0);
// Calculate autocorrelation coefficients using Yule-Walker equations for AR part
for (size_t l = 1; l <= p; ++l) {
acf[l] = ar_coeffs[l - 1];
for (size_t j = 1; j < l; ++j) {
acf[l] += ar_coeffs[j - 1] * acf[l - j];
}
}
// Calculate autocorrelation coefficients using invertibility conditions for MA part
for (size_t l = 1; l <= q && l <= k; ++l) {
for (size_t j = 1; j < l; ++j) {
acf[l] += ma_coeffs[j - 1] * acf[l - j];
}
}
return acf;
}
double von_mises(double mean, double kappa, std::mt19937& generator) {
std::uniform_real_distribution<double> uniform_dist(0.0, 1.0);
std::normal_distribution<double> normal_dist(0.0, 1.0);
double tau = 1.0 + std::sqrt(1.0 + 4.0 * kappa * kappa);
double rho = (tau - std::sqrt(2.0 * tau)) / (2.0 * kappa);
double r = (1.0 + rho * rho) / (2.0 * rho);
while (true) {
double u1 = uniform_dist(generator);
double z = std::cos(pi * u1);
double f = (1.0 + r * z) / (r + z);
double c = kappa * (r - f);
double u2 = uniform_dist(generator);
if (u2 < c * (2.0 - c) || u2 <= c * std::exp(1.0 - c)) {
double u3 = uniform_dist(generator);
double theta = mean + std::copysign(1.0, u3 - 0.5) * std::acos(f);
return theta;
}
}
}
std::vector<double> ArrayToVector(double* arr, size_t arr_len) {
return std::vector<double>(arr, arr + arr_len);
}
// Function to simulate ARIMA process
std::vector<double> simulateARIMA(double *phi, int phi_size, double *theta, int theta_size, int num_samples, double sig2, double meanwind) {
// Initialize variables
std::vector<double> simulated_series(num_samples, 0.0);
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0, 1.0);
// Generate ARIMA process
// Initialize with random noise
int k = 30;
//std::vector<double> acf = derive_autocorrelation(phivec, thetavec, k);
//std::vector<double> acf = { 1.000000,0.975384,0.941925,0.913408,0.890211,0.872260,0.858886,0.847181,0.834872,0.822290,0.810373,0.797528,0.784385,0.771865,0.759759,0.748905,
// 0.737786,0.725777,0.713362,0.701186,0.689194,0.676517,0.663835,0.652762,0.642148,0.631309,0.621892,0.612033,0.600785,0.587710,0.575404,0.563168,0.551136,0.541185,0.531647,0.521629 };
std::vector<double> arvec, mavec, acfvec;
arvec = ArrayToVector(phi, phi_size);
mavec = ArrayToVector(theta, theta_size);
acfvec = derive_autocorrelation(arvec, mavec, k);
double cc = 1.0;
for (int i = 0; i < sizeof(*phi); i++)
cc -= phi[i] * acfvec[i];
//cc = sqrt(cc);
double scfac = sig2 / cc;
scfac = sqrt(scfac);
for (int i = 0; i < num_samples; i++)
{
double sample = distribution(generator);
//sample = sample / sig2;
sample = sample * scfac;
simulated_series[i] = sample;
//file << MagWind << sample << '\n'
// std::cout << sample << std::endl;
}
applyAutoregressiveFilter(simulated_series, arvec);
//applyMovingAverageFilter(simulated_series, thetavec);
for (int i = 0; i < simulated_series.size(); i++){
simulated_series[i] = simulated_series[i]+meanwind;
}
return simulated_series;
}
int main() {
//mean and sd of wind magnitude
double sig2 = 0.0;
double meanwind = 5.0;
// set all ARIMA coefficients
double phi[] = { 1.17, -0.321, 0.0785, -0.0182, -0.00601, 0.0724 }; // Autoregressive coefficients
int phi_size = 6;
double theta[]{ 0.1 }; // Moving average coefficients
int theta_size = 1;
//von mises input varaibles
double angle_input = 0.0; //degrees east of north
double vmmean = pi * (-angle_input + 90) / 180.0; //radians north of east
double vmkappa = 1.0;
// Number of samples to simulate
int num_samples = 1000;
// Simulate ARIMA process
std::vector<double> simulated_series = simulateARIMA(phi, phi_size, theta, theta_size, num_samples, sig2, meanwind);
std::random_device rd;
std::mt19937 generator(rd());
std::mt19937 gen(rd());
// Output simulated series
//for (int i = 0; i < num_samples; ++i) {lear
// std::cout << simulated_series[i] << std::endl;
//}
std::ofstream outfile("output.txt");
for (int i = 0; i < num_samples; i++)
{
double sample = simulated_series[i];
double vmsample = von_mises(vmmean, vmkappa, generator);
int vmdegreesint;
double vmdegrees = 90 - (vmsample / pi) * 180;
if (vmdegrees < 0) {
vmdegrees += 360.0;
}
if (sample < 0.0)
{
vmdegrees = vmdegrees + 180.0;
sample = -sample;
}
vmdegreesint = int(vmdegrees);
vmdegreesint = vmdegreesint % 360;
// sample = sample * sig2;
//sample = sample * cc;
outfile << sample << "," << vmdegreesint << std::endl;
//file << MagWind << sample << '\n';
// std::cout << sample << std::endl;
}
}