@@ -17,8 +17,8 @@ function simulate(serie::Vector{T}, gas::Model{D, T}, N::Int, S::Int;
17
17
# Create scenarios matrix
18
18
scenarios = Matrix {T} (undef, N, S)
19
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 ]
20
+ sim, param = simulate_recursion (gas, N + biggest_lag + 1 ; initial_params = params_simulation)
21
+ scenarios[:, scenario] = sim[biggest_lag + 2 : end ]
22
22
end
23
23
24
24
return scenarios
29
29
forecast(serie::Vector{T}, gas::Model{D, T}, N::Int; kwargs...) where {D, T}
30
30
31
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.
32
+ taking the mean of the distribution at each time. You can pass the desired confidence interval
33
+ as a `Vector{T}`. The forecast will be the first column and the confidence intervals are the remaining
34
+ columns.
35
+
36
+ The forecast is build using Monte Carlo method as in Blasques, Francisco, Siem Jan Koopman,
37
+ Katarzyna Lasak and Andre Lucas (2016): "In-Sample Confidence Bounds and Out-of-Sample Forecast
38
+ Bands for Time-Varying Parameters in Observation Driven Models", International Journal of Forecasting, 32(3), 875-887.
39
+
40
+ By default 1000 scenarios are used but one can change by switching the `S` keyword argument.
33
41
"""
34
42
function forecast (serie:: Vector{T} , gas:: Model{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)
43
+ initial_params:: Matrix{T} = stationary_initial_params (gas),
44
+ ci:: Vector{T} = T .([0.95 ]), S:: Int = 1000 ) where {D, T}
38
45
39
- biggest_lag = number_of_lags (gas)
40
-
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 ]
46
+ scenarios = simulate (serie, gas, N, S; initial_params = initial_params)
47
+
48
+ quantiles = get_quantiles (ci)
46
49
50
+ forec = Matrix {T} (undef, N, length (quantiles) + 1 )
51
+
52
+ forec[:, 1 ] = mean (scenarios, dims = 2 )
53
+ for t in 1 : N
54
+ for (i, q) in enumerate (quantiles)
55
+ forec[t, i + 1 ] = quantile (scenarios[t, :], q)
56
+ end
57
+ end
58
+
47
59
return forec
60
+ end
61
+
62
+ function get_quantiles (ci:: Vector{T} ) where T
63
+ @assert all ((ci .< 1.0 ) .& (ci .> 0.0 ))
64
+ quantiles = zeros (2 * length (ci))
65
+ i = 1
66
+ for v in ci
67
+ quantiles[i] = 0.5 + v/ 2
68
+ i += 1
69
+ quantiles[i] = 0.5 - v/ 2
70
+ i += 1
71
+ end
72
+ return quantiles
48
73
end
0 commit comments