-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_seq_simulation_forecasting.py
685 lines (530 loc) · 22.6 KB
/
event_seq_simulation_forecasting.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 7 21:11:54 2022
@author: dama-f
"""
import numpy as np
import pickle
from scipy.stats import multivariate_normal
import os
import sys
from scaled_modified_forward_backward_recursion import modified_FB
from event_sequence_covariates import compute_transition_mat_eventSeqCov
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), \
"../../PHMC-VAR/src/")))
from utils import compute_LL, compute_prediction_probs, compute_means, \
performance_metrics
#/////////////////////////////////////////////////////////////////////////////
## @fn
# @brief
#
# @return TxM matrix of prbabilities Gamma.
#
def compute_gamma_standard_FB_algo(M, LL, B, Pi):
#the modified FB algorithm for NH-MSVAR
(_, _, Gamma, _) = modified_FB(M, LL, B, Pi)
return Gamma
## @fn compute_trans_probs
#
def compute_trans_probs(A, Y_params, M, series_len, covariates, kappa):
#assertion
assert(series_len == covariates.shape[0])
# nb covariates
nb_cov = covariates.shape[1]
B = compute_transition_mat_eventSeqCov(A, Y_params["phi"], \
Y_params["delta1"], Y_params["psi"], \
Y_params["delta2"], [covariates], \
[kappa], M, nb_cov, [series_len])[0]
return B
## @fn
# @brief Compute state probabilities at forecast horizons
#
# @param B HxMxM matrix of transition probabilities at forecast horizons
#
def compute_state_probs_at_forecast(Gamma_T, B, H):
#assertion
assert(B.shape[0] == H)
#number of states
M = Gamma_T.shape[0]
#output
state_probs = np.zeros(shape=(H, M), dtype=np.float64)
#normalization of Gamma_T
Gamma_T = Gamma_T / np.sum(Gamma_T)
#--initial case h = 0
for k in range(M):
state_probs[0, k] = np.sum(B[0, :, k] * Gamma_T)
state_probs[0, :] = state_probs[0, :] / np.sum(state_probs[0, :])
#--for h = 1, ..., H-1
for h in range(1, H):
for k in range(M):
state_probs[h, k] = np.sum(B[h, :, k] * state_probs[h-1, :])
state_probs[h, :] = state_probs[h, :] / np.sum(state_probs[h, :])
#--assertions
assert(np.sum(np.isnan(state_probs)) == 0)
assert(np.sum(state_probs < 0.) == 0)
assert(np.sum(state_probs > 1.) == 0)
return state_probs
#/////////////////////////////////////////////////////////////////////////////
# FORECASTING
#/////////////////////////////////////////////////////////////////////////////
#------------------------------------------BEGIN OPTIMAL POINT FORECAST
## @forecasting_one_seq
# @brief Compute H-step ahead forecasting on the given sequence.
# At each forecast horizon h, the expectation of X_{T+h}, knowing its own
# past values and those of covariates Y_t, is computed.
#
# @param ar_coefficients
# @param ar_intercepts
# @param pred_probs H x K array probabilities of states at forecast horizons
# @param past_values order x dimension array of initial values
# @param H
#
# @return A Hxdimension array of predicted values
#
def forecasting_one_seq(ar_coefficients, ar_intercepts, pred_probs, \
past_values, H):
#---hyper-parameters
nb_regimes = len(ar_coefficients)
order = len(ar_coefficients[0])
dimension = ar_intercepts[0].shape[0]
#assertions
assert(past_values.shape == (order, dimension))
assert(pred_probs.shape == (H, nb_regimes))
#---total X values
total_X = np.zeros(shape=(order+H, dimension), dtype=np.float64)
total_X[0:order, :] = past_values
#---forecasting begins
for t in range(order, H+order, 1):
#---compute the conditional means of X_t within each regime
# nb_regimes x dimension array
means = compute_means(ar_coefficients, ar_intercepts, \
total_X[t-order:t, :], nb_regimes, order, \
dimension)
#---prediction: weighted sum of conditional means
for i in range(nb_regimes):
means[i, :] = means[i, :] * pred_probs[t-order, i]
total_X[t, :] = np.sum(means, axis=0)
return total_X[order:, :]
#/////////////////////////////////////////////////////////////////////////////
# OPTIMAL H-STEP AHEAD POINT FORECASTING
#/////////////////////////////////////////////////////////////////////////////
## @fn
#
# @param covariates (T-order) x nb_covariates array of covariates data Y_t
# where T stands for the correponding time series length.
#
def eventSeqCov_sliding_forecasting(model_file, time_series, H, covariates, \
kappa, scaler=None, \
use_cov_at_forecast=False):
print("----------------LOG-INFO: eventSeqCov_sliding_forecasting")
print("with data scaling = ", True if (scaler != None) else False)
print("use_cov_at_forecast = ", use_cov_at_forecast)
print()
#model loading
infile = open(model_file, 'rb')
phmc_var = pickle.load(infile)
infile.close()
#required phmc_var parameters
innovation = "gaussian"
A = phmc_var[1]
Pi = phmc_var[2]
ar_coefficients = phmc_var[5]
ar_intercepts = phmc_var[7]
sigma = phmc_var[6]
Y_params = phmc_var[9]
#hyper-parameters
order = len(ar_coefficients[0])
M = A.shape[0]
#assertions
assert(np.sum(A < 0.0) == 0)
assert(np.sum(np.isnan(A)) == 0)
#---compute the time dependent transition probabilities using covariates Y_t
T = time_series.shape[0]
# (T-order) x M x M array
B = compute_trans_probs(A, Y_params, M, (T-order), covariates, kappa)
#---data standardization
standardization = True if (scaler != None) else False
if(standardization):
stand_timeseries = scaler.transform(time_series)
else:
stand_timeseries = time_series
#---H-step sliding forecasts
#first projection time-step
projec_t = order + 1
nb_projec = 0
#end projection time-step
end_t = T - H
#outputs initialization
total_predictions = stand_timeseries[0:(order+2), :]
while(projec_t < end_t):
#---data splitting
all_past_values = stand_timeseries[0:(projec_t+1), :]
order_past_values = stand_timeseries[(projec_t+1-order):(projec_t+1), :]
#---ll from order to projection time
LL = compute_LL(ar_coefficients, ar_intercepts, sigma, innovation, \
all_past_values)
#assertion
assert(LL.shape[0] == (projec_t+1-order))
#---gamma_T
#index of the last covariable considered in gamma computing
last_cov_used = LL.shape[0] - 1
Gamma_T = compute_gamma_standard_FB_algo(M, LL, B[0:(last_cov_used+1)], \
Pi)[-1, :]
#---states's probability at forecast horizons
if(use_cov_at_forecast):
state_probs_at_forecast = \
compute_state_probs_at_forecast(Gamma_T, \
B[(last_cov_used+1):(last_cov_used+H+1)], H)
else:
state_probs_at_forecast = compute_prediction_probs(A, Gamma_T, H)
#---forecasting
h_step_predictions = forecasting_one_seq(ar_coefficients, \
ar_intercepts, \
state_probs_at_forecast, \
order_past_values, H)
#assertion
assert(h_step_predictions.shape[0] == H)
#---forecast error computing
total_predictions = np.vstack((total_predictions, h_step_predictions))
#----next projection time, used in prognostic machine health
projec_t = projec_t + H
nb_projec += 1
#assertion
assert(total_predictions.shape[0] == (nb_projec*H + order + 2))
#---back to original scale
if(standardization):
original_scale_predictions = scaler.inverse_transform(total_predictions)
else:
original_scale_predictions = total_predictions
return original_scale_predictions
## @fn
# @brief Compute several estimates of H-step ahead forecast error.
#
# @param begin_t Must be strictly greater than order.
# @param L Sliding windows size
# @param set_of_scalers List of scalers to be used to standardize time series.
# One scaler per time series.
#
# @return Three arrays where lines correspond to estimations of forecast
# error metrics and columns correspond to data dimensions.
#
def eventSeqCov_sliding_forecasting_error(model_file, set_of_time_series, \
H, begin_t, L, set_of_covariates, \
set_of_kappa, set_of_scalers=None, \
use_cov_at_forecast=False):
print("----------------LOG-INFO: eventSeqCov_sliding_forecasting_error")
print("H={}, begin_t={}, L={}".format(H, begin_t, L))
print("with data scaling = ", True if (set_of_scalers != None) else False)
print("use_cov_at_forecast = ", use_cov_at_forecast)
print()
#model loading
infile = open(model_file, 'rb')
phmc_var = pickle.load(infile)
infile.close()
#required phmc_var parameters
innovation = "gaussian"
A = phmc_var[1]
Pi = phmc_var[2]
ar_coefficients = phmc_var[5]
ar_intercepts = phmc_var[7]
sigma = phmc_var[6]
Y_params = phmc_var[9]
#hyper-parameters
order = len(ar_coefficients[0])
M = A.shape[0]
#assertions
assert(np.sum(A < 0.0) == 0)
assert(np.sum(np.isnan(A)) == 0)
assert(begin_t > order)
#---forecasting
#nb time series
N = len(set_of_time_series)
#time series's length without initial values
series_len = [set_of_time_series[s].shape[0] - order for s in range(N)]
#data must be standardized
standardization = True if (set_of_scalers != None) else False
#outputs
total_MBias = []
total_RMSE = []
total_NRMSE = []
total_MAPE = []
for s in range(N):
#time series length
T_s = set_of_time_series[s].shape[0]
#---first projection time-step
projec_t = begin_t
#---end projection time-step
end_t = T_s - H
#---roling H-step prediction over the s^th time series
Bias = []
NBias = [] #normalized bias
#---data standardization
if(standardization):
stand_timeseries_s = set_of_scalers[s].transform(set_of_time_series[s])
else:
stand_timeseries_s = set_of_time_series[s]
#---compute the time dependent transition probabilities
B = compute_trans_probs(A, Y_params, M, series_len[s], \
set_of_covariates[s], set_of_kappa[s])
while(projec_t < end_t):
#---data splitting
all_past_values = stand_timeseries_s[0:(projec_t+1), :]
order_past_values = stand_timeseries_s[(projec_t+1-order):(projec_t+1), :]
#---ll from order to projection time
LL = compute_LL(ar_coefficients, ar_intercepts, sigma, innovation, \
all_past_values)
#assertion
assert(LL.shape[0] == (projec_t+1-order))
#---gamma_T
#index of the last covariable considered in prediction
last_cov_used = LL.shape[0] - 1
Gamma_T = compute_gamma_standard_FB_algo(M, LL, \
B[0:(last_cov_used+1)], Pi)[-1, :]
#---states's probability at forecast horizons
if(use_cov_at_forecast):
state_probs_at_forecast = \
compute_state_probs_at_forecast(Gamma_T, \
B[(last_cov_used+1):(last_cov_used+H+1)], H)
else:
state_probs_at_forecast = compute_prediction_probs(A, Gamma_T, H)
#---forecasting
predictions = forecasting_one_seq(ar_coefficients, ar_intercepts, \
state_probs_at_forecast, \
order_past_values, H)
#assertion
assert(predictions.shape[0] == H)
#---back to original scale
if(standardization):
original_scale_predictions = set_of_scalers[s].inverse_transform(predictions)
else:
original_scale_predictions = predictions
#---forecast error computing
obs_x = set_of_time_series[s][(projec_t+H), :]
pred_x = original_scale_predictions[-1, :]
Bias.append( (obs_x - pred_x) )
NBias.append( (obs_x - pred_x)/obs_x )
#----next projection time, used in prognostic machine health
projec_t = projec_t + L
#NB: from what number of projections it is pertinent to compute RMSE, etc
#We chose 10
if(len(Bias) >= 10):
#---forecast error of the s^th time series
(MBias_, RMSE_, NRMSE_, NMAE_) = performance_metrics(Bias, NBias)
total_MBias.append(MBias_)
total_RMSE.append(RMSE_)
total_NRMSE.append(NRMSE_)
total_MAPE.append(NMAE_)
else:
print("s={}, nb_projections = {}".format(s, len(Bias)))
total_MBias = np.vstack(total_MBias)
total_RMSE = np.vstack(total_RMSE)
total_NRMSE = np.vstack(total_NRMSE)
total_MAPE = np.vstack(total_MAPE)
return (total_MBias, total_RMSE, total_NRMSE, total_MAPE)
#/////////////////////////////////////////////////////////////////////////////
# MODEL RESIDUAL COMPUTING
#/////////////////////////////////////////////////////////////////////////////
## @fn
# @brief Compute model residuals
#
def eventSeqCov_compute_model_residuals(model_file, set_of_time_series, begin_t,\
set_of_covariates, set_of_kappa, \
set_of_scalers=None, \
use_cov_at_forecast=False):
print("----------------LOG-INFO: eventSeqCov_compute_model_residuals")
print("begin_t={}".format(begin_t))
print("with data scaling = ", True if (set_of_scalers != None) else False)
print("use_cov_at_forecast = ", use_cov_at_forecast)
print()
#model loading
infile = open(model_file, 'rb')
phmc_var = pickle.load(infile)
infile.close()
#required phmc_var parameters
innovation = "gaussian"
A = phmc_var[1]
Pi = phmc_var[2]
ar_coefficients = phmc_var[5]
ar_intercepts = phmc_var[7]
sigma = phmc_var[6]
Y_params = phmc_var[9]
#hyper-parameters
order = len(ar_coefficients[0])
M = A.shape[0]
#assertions
assert(np.sum(A < 0.0) == 0)
assert(np.sum(np.isnan(A)) == 0)
#---forecasting
#nb time series
N = len(set_of_time_series)
#time series's length without initial values
series_len = [set_of_time_series[s].shape[0] - order for s in range(N)]
#data must be standardized
standardization = True if (set_of_scalers != None) else False
#outputs
set_of_residuals = []
H = 1
for s in range(N):
#time series length
T_s = set_of_time_series[s].shape[0]
#---first projection time-step
projec_t = begin_t
#---end projection time-step
end_t = T_s - H
#---roling 1-step prediction over the s^th time series
Bias = []
#---data standardization
if(standardization):
stand_timeseries_s = set_of_scalers[s].transform(set_of_time_series[s])
else:
stand_timeseries_s = set_of_time_series[s]
#---compute the time dependent transition probabilities
B = compute_trans_probs(A, Y_params, M, series_len[s], \
set_of_covariates[s], set_of_kappa[s])
while(projec_t < end_t):
#---data splitting
all_past_values = stand_timeseries_s[0:(projec_t+1), :]
order_past_values = stand_timeseries_s[(projec_t+1-order):(projec_t+1), :]
#---ll from order to projection time
LL = compute_LL(ar_coefficients, ar_intercepts, sigma, innovation, \
all_past_values)
#assertion
assert(LL.shape[0] == (projec_t+1-order))
#---gamma_T
#index of the last covariable considered in prediction
last_cov_used = LL.shape[0] - 1
Gamma_T = compute_gamma_standard_FB_algo(M, LL, \
B[0:(last_cov_used+1)], Pi)[-1, :]
#---states's probability at forecast horizons
if(use_cov_at_forecast):
state_probs_at_forecast = \
compute_state_probs_at_forecast(Gamma_T, \
B[(last_cov_used+1):(last_cov_used+H+1)], H)
else:
state_probs_at_forecast = compute_prediction_probs(A, Gamma_T, H)
#---forecasting
predictions = forecasting_one_seq(ar_coefficients, ar_intercepts, \
state_probs_at_forecast, \
order_past_values, H)
#assertion
assert(predictions.shape[0] == H)
#---residual at projec_t+1 computed at training data scale
obs_x = stand_timeseries_s[(projec_t+H), :]
pred_x = predictions[-1, :]
Bias.append( (obs_x - pred_x) )
#----next projection time, used in prognostic machine health
projec_t = projec_t + 1
# model residuals for the s^th time series
set_of_residuals.append(np.array(Bias))
return set_of_residuals
#/////////////////////////////////////////////////////////////////////////////
# SIMULATION: Generate synthetic data from model
#/////////////////////////////////////////////////////////////////////////////
"""
## @fn event_seq_simulation
#
# @return
# * Simulated event sequence
# * Associated priors
#
def event_seq_simulation(sampling_times):
#load hawkes_learner from pickle file, then simulate it
pass
## @fn Simulation
# @brief
#
# @param init_values The first initial values. A dxdim matrix where d is
# VAR process order and dim is time series dimension
# @param L Length of the simulated sequence
# @param coefficients
# @param intercept
# @param sigma
# @param A
# @param Pi
# @param innovation
# @param sampling_times Time series sampling times
#
# @return Simulated couple (timeseries, state_seq)
#
def simulation(init_values, L, coefficients, intercept, sigma, A, Pi, \
innovation, sampling_times=[]):
#-----hyper-parameters
#RS-AR order
order = len(coefficients[0])
#time series dimension
dim = init_values.shape[1]
#number of classes
K = A.shape[0]
assert((init_values.shape[0] == order) and (init_values.shape[1] == dim))
#-----event sequence simulation and weights building
####(event_seq, weights) = event_seq_simulation(sampling_times)
weights = np.ones(dtype=np.float64, shape=(L,K))
#-----output initialization
total_X = np.ones(shape=(L,dim), dtype=np.float64) * np.nan
total_X = np.vstack((init_values, total_X))
selec_states = np.ones(shape=L, dtype=np.int32) * (-1)
#-----initial state probabilities: t=0
states_probs_t = Pi[0, :] * weights[0, :]
states_probs_t = states_probs_t / np.sum(states_probs_t)
#-----simulation starts
for t in range(order, L+order, 1):
# conditional mean within each state
cond_mean = compute_means(coefficients, intercept, \
total_X[t-order:t, :], K, order, dim)
# sample the conditional
(s, sample) = cond_density_sampling(cond_mean, sigma, innovation, \
states_probs_t)
selec_states[t-order] = s
total_X[t, :] = sample
# state probabilities at t+1
if((t+1-order) < L):
states_probs_t = A[selec_states[t-order], :] * weights[t+1-order, :]
states_probs_t = states_probs_t / np.sum(states_probs_t)
return (total_X, selec_states)
## @fn synthetic_data_generation
# @brief
#
#
def synthetic_data_generation(model_file, innovation, list_L, list_init_values=[]):
#----model loading
infile = open(model_file, 'rb')
model = pickle.load(infile)
infile.close()
#----required model parameters
A = model[1]
Pi = model[2]
ar_coefficients = model[5]
ar_intercepts = model[7]
sigma = model[6]
psi = model[8]
#assertions
assert(np.sum(A < 0.0) == 0)
assert(np.sum(np.isnan(A)) == 0)
#----hyper-parameters
order = len(ar_coefficients[0])
X_dim = ar_coefficients[0][0].shape[0]
#----output
syn_data = []
N = len(list_L)
# if initial values are given
if(len(list_init_values) != 0):
given_init_val = True
assert( len(list_init_values) == N )
else:
given_init_val = False
#----Simulation begins
for n in range(N):
if(given_init_val):
init_values = list_init_values[n]
else:
#initial values simulation
init_values = np.zeros(shape=(order,X_dim), dtype=np.float64)
for j in range(1, order+1):
init_values[order-j, :] = \
multivariate_normal.rvs(psi["means"][j-1], psi["covar"], 1)
#data simulation
syn_data.append( simulation(init_values, list_L[n], ar_coefficients, \
ar_intercepts, sigma, A, Pi, innovation) )
return syn_data
"""