1
- export simulate
1
+ export simulate, forecast
2
2
3
- function simulate (gas :: GAS{D, T} , n :: Int , s :: Int ) where {D, T}
4
- scenarios_series = Matrix {T} (undef, n, s)
3
+ """
4
+ simulate(serie::Vector{T}, gas::GAS{D, T}, N::Int, S::Int, kwargs...) where {D, T}
5
5
6
- for i in 1 : s
7
- serie, params = simulate (gas, n)
8
- scenarios_series[:, i] = serie
6
+ Generate scenarios for the future of a time series by updating the GAS recursion `N` times and taking
7
+ a sample of the distribution until generate `S` scenarios.
8
+ """
9
+ function simulate (serie:: Vector{T} , gas:: GAS{D, T} , N:: Int , S:: Int ;
10
+ initial_params:: Matrix{T} = stationary_initial_params (gas)) where {D, T}
11
+ # Filter params estimated on the time series
12
+ params = score_driven_recursion (gas, serie; initial_params = initial_params)
13
+
14
+ biggest_lag = number_of_lags (gas)
15
+
16
+ params_simulation = params[(end - biggest_lag): (end - 1 ), :]
17
+ # Create scenarios matrix
18
+ scenarios = Matrix {T} (undef, N, S)
19
+ for scenario in 1 : s
20
+ sim, param = simulate_recursion (gas, N + biggest_lag; initial_params = params_simulation)
21
+ scenarios[:, scenario] = sim[biggest_lag + 1 : end ]
9
22
end
10
- return scenarios_series
11
- end
12
23
13
- function simulate (gas:: GAS{D, T} , n:: Int ) where {D, T}
14
- initial_params = stationary_initial_params (gas)
15
- return simulate (gas, n, initial_params)
24
+ return scenarios
16
25
end
17
26
18
- function simulate (gas:: GAS{D, T} , n:: Int , initial_params:: Matrix{T} ) where {D, T}
19
- # Allocations
20
- serie = zeros (n)
21
- n_params = num_params (D)
22
- param = Matrix {T} (undef, n, n_params)
23
- param_tilde = Matrix {T} (undef, n, n_params)
24
- scores_tilde = Matrix {T} (undef, n, n_params)
25
-
26
- aux = AuxiliaryLinAlg {T} (n_params)
27
27
28
- # Auxiliary Allocation
29
- param_dist = zeros (T, 1 , n_params)
28
+ """
29
+ forecast(serie::Vector{T}, gas::GAS{D, T}, N::Int; kwargs...) where {D, T}
30
+
31
+ Forecast future values of a time series by updating the GAS recursion `N` times and
32
+ taking the mean of the distribution at each time.
33
+ """
34
+ function forecast (serie:: Vector{T} , gas:: GAS{D, T} , N:: Int ;
35
+ initial_params:: Matrix{T} = stationary_initial_params (gas)) where {D, T}
36
+ # Filter params estimated on the time series
37
+ params = score_driven_recursion (gas, serie; initial_params = initial_params)
30
38
31
39
biggest_lag = number_of_lags (gas)
32
40
33
- # initial_values
34
- for t in 1 : biggest_lag
35
- for p in 1 : n_params
36
- param[t, p] = initial_params[t, p]
37
- end
38
- link! (param_tilde, D, param, t)
39
- # Sample
40
- updated_dist = update_dist (D, param, t)
41
- serie[t] = sample_observation (updated_dist)
42
- score_tilde! (scores_tilde, serie[t], D, param, aux, gas. scaling, t)
43
- end
44
-
45
- update_param_tilde! (param_tilde, gas. ω, gas. A, gas. B, scores_tilde, biggest_lag)
46
- unlink! (param, D, param_tilde, biggest_lag + 1 )
47
- updated_dist = update_dist (D, param, biggest_lag + 1 )
48
- serie[biggest_lag + 1 ] = sample_observation (updated_dist)
49
-
50
- for i in biggest_lag + 1 : n- 1
51
- # update step
52
- univariate_score_driven_update! (param, param_tilde, scores_tilde, serie[i], aux, gas, i)
53
- # Sample from the updated distribution
54
- unlink! (param, D, param_tilde, i + 1 )
55
- updated_dist = update_dist (D, param, i + 1 )
56
- serie[i + 1 ] = sample_observation (updated_dist)
57
- end
58
- update_param! (param, param_tilde, D, n)
41
+ params_simulation = params[(end - biggest_lag): (end - 1 ), :]
42
+ # Create scenarios matrix
43
+ forec = Vector {T} (undef, N)
44
+ sim, param = simulate_recursion (gas, N + biggest_lag; initial_params = params_simulation, update = mean)
45
+ forec = sim[biggest_lag + 1 : end ]
59
46
60
- return serie, param
47
+ return forec
61
48
end
0 commit comments