Skip to content

Commit e779144

Browse files
Fix backtest (#136)
* Update diagnostics and bump version * Fix backtest * add visualization forecast test
1 parent a047701 commit e779144

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ScoreDrivenModels"
22
uuid = "4a87933e-d659-11e9-0e65-7f40dedd4a3a"
33
authors = ["guilhermebodin <[email protected]>, raphaelsaavedra <[email protected]>"]
4-
version = "0.1.8"
4+
version = "0.1.9"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

src/backtest.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TODO
4040
"""
4141
function backtest(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int, start_idx::Int;
4242
S::Int = 10_000,
43-
initial_params::Matrix{T} = stationary_initial_params(gas),
43+
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
4444
opt_method = NelderMead(gas, DEFAULT_NUM_SEEDS)) where T
4545
num_mle = length(y) - start_idx - steps_ahead
4646
b = Backtest(num_mle, steps_ahead)
@@ -49,8 +49,10 @@ function backtest(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int,
4949
gas_to_fit = deepcopy(gas)
5050
y_to_fit = y[1:start_idx - 1 + i]
5151
y_to_verify = y[start_idx + i:start_idx - 1 + i + steps_ahead]
52-
ScoreDrivenModels.fit!(gas_to_fit, y_to_fit; initial_params=initial_params, opt_method=opt_method)
53-
forec = forecast(y_to_fit, gas_to_fit, steps_ahead; S=S, initial_params=initial_params)
52+
fit!(gas_to_fit, y_to_fit; initial_params=initial_params, opt_method=opt_method)
53+
forec = initial_params !== DEFAULT_INITIAL_PARAM ?
54+
forecast(y_to_fit, gas_to_fit, steps_ahead; S=S, initial_params=initial_params) :
55+
forecast(y_to_fit, gas_to_fit, steps_ahead; S=S)
5456
abs_errors = evaluate_abs_error(y_to_verify, forec.observation_forecast)
5557
crps_scores = evaluate_crps(y_to_verify, forec.observation_scenarios)
5658
b.abs_errors[:, i] = abs_errors

src/visualization/forecast.jl

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
#TODO
1+
RecipesBase.@recipe function f(y::Vector, forec::Forecast)
2+
n = length(y)
3+
steps_ahead = length(forec.observation_forecast)
4+
forec_idx = collect(n + 1: n + steps_ahead)
5+
fillrange_color = :steelblue
6+
# Plot the series
7+
@series begin
8+
seriestype := :path
9+
seriescolor := "black"
10+
return y
11+
end
12+
# Plot the forecast
13+
@series begin
14+
seriescolor := "blue"
15+
return forec_idx, forec.observation_forecast
16+
end
17+
# Plot the prediction interval
18+
q975 = Vector{Float64}(undef, steps_ahead)
19+
q025 = Vector{Float64}(undef, steps_ahead)
20+
q900 = Vector{Float64}(undef, steps_ahead)
21+
q100 = Vector{Float64}(undef, steps_ahead)
22+
for i in 1:steps_ahead
23+
q975[i] = quantile(forec.observation_scenarios[i, :], 0.975)
24+
q025[i] = quantile(forec.observation_scenarios[i, :], 0.025)
25+
q900[i] = quantile(forec.observation_scenarios[i, :], 0.9)
26+
q100[i] = quantile(forec.observation_scenarios[i, :], 0.1)
27+
end
28+
@series begin
29+
seriestype := :path
30+
seriescolor := fillrange_color
31+
fillcolor := fillrange_color
32+
fillalpha := 0.5
33+
fillrange := q975
34+
label := ""
35+
return forec_idx, q025
36+
end
37+
@series begin
38+
seriestype := :path
39+
seriescolor := fillrange_color
40+
fillcolor := fillrange_color
41+
fillalpha := 0.5
42+
fillrange := q900
43+
label := ""
44+
return forec_idx, q100
45+
end
46+
end

test/test_visualization.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@
1616
bac = backtest(gas, simulation, 10, 4985)
1717
rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), bac, "name")
1818
@test length(rec) == 2
19+
20+
simulation = simulate_GAS_1_1(Normal, 0.0, ω, A, B, 1)
21+
gas = ScoreDrivenModels.Model(1, 1, Normal, 0.0)
22+
fit!(gas, simulation)
23+
forec = forecast(simulation, gas, 50)
24+
rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), simulation, forec)
25+
@test length(rec) == 4
1926
end

0 commit comments

Comments
 (0)