Skip to content

Commit 21614e5

Browse files
Cv (#139)
* change from ANE to NIE * change from backtest to cross_validation
1 parent 8bb101a commit 21614e5

File tree

8 files changed

+18
-20
lines changed

8 files changed

+18
-20
lines changed

examples/cpichg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ gas_t = Model(1, 1, TDistLocationScale, 1.0; time_varying_params = [1])
4343
gas_t_1_2 = Model(1, 2, TDistLocationScale, 1.0; time_varying_params = [1])
4444
steps_ahead = 50
4545
first_idx = 150
46-
b_t = backtest(gas_t, y, steps_ahead, first_idx)
47-
b_t_1_2 = backtest(gas_t_1_2, y, steps_ahead, first_idx)
46+
b_t = cross_validation(gas_t, y, steps_ahead, first_idx)
47+
b_t_1_2 = cross_validation(gas_t_1_2, y, steps_ahead, first_idx)
4848
plot(b_t, "GAS(1, 1) Student t")
4949
plot!(b_t_1_2, "GAS(1, 2) Student t"; legend = :topleft)

examples/nie.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Dates, DelimitedFiles, Plots, Random, ScoreDrivenModels
33
# Define dates and load historical Affluent Natural Energy data
44
dates = collect(Date(1961):Month(1):Date(2000, 12))
55
y = vec(readdlm("../test/data/nie_northeastern.csv"))
6+
67
y_train = y[1:400]
78
y_test = y[401:460]
89

@@ -26,9 +27,6 @@ plot(f)
2627
forec = forecast(y_train, gas, 60; S=1000, initial_params=initial_params)
2728

2829
# Plot results
29-
plotly()
30-
plot(dates[401:460], forec.observation_scenarios, color="grey", w=0.05, label="", ylims=(0, 70));
31-
plot!(dates[360:460], y[360:460], label="NIE", color="black", xlabel="Months", ylabel="GWmed", legend=:topright);
30+
plot(dates[401:460], forec.observation_scenarios, color="grey", w=0.05, label="", ylims=(0, 70))
31+
plot!(dates[360:460], y[360:460], label="NIE", color="black", xlabel="Months", ylabel="GWmed", legend=:topright)
3232
plot!(dates[401:460], forec.observation_quantiles, label=["Quantiles" "" ""], color="red", line=:dash)
33-
34-
plot(dates[1:400], y_train, label = "in-sample NIE", ylabel = "GWmed", xlabel = "Months", color = "black")

src/ScoreDrivenModels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include("simulate.jl")
2020
include("diagnostics.jl")
2121
include("univariate_score_driven_recursion.jl")
2222
include("MLE.jl")
23-
include("backtest.jl")
23+
include("cross_validation.jl")
2424
include("prints.jl")
2525

2626
# Optimization methods
@@ -47,6 +47,6 @@ include("distributions/weibull.jl")
4747

4848
include("visualization/forecast.jl")
4949
include("visualization/residuals.jl")
50-
include("visualization/backtest.jl")
50+
include("visualization/cross_validation.jl")
5151

5252
end

src/backtest.jl renamed to src/cross_validation.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export backtest
1+
export cross_validation
22

3-
struct Backtest
3+
struct CrossValidation
44
abs_errors::Matrix{Float64}
55
mae::Vector{Float64}
66
crps_scores::Matrix{Float64}
77
mean_crps::Vector{Float64}
8-
function Backtest(n::Int, steps_ahead::Int)
8+
function CrossValidation(n::Int, steps_ahead::Int)
99
abs_errors = Matrix{Float64}(undef, steps_ahead, n)
1010
crps_scores = Matrix{Float64}(undef, steps_ahead, n)
1111
mae = Vector{Float64}(undef, steps_ahead)
@@ -38,14 +38,14 @@ end
3838
"""
3939
TODO
4040
"""
41-
function backtest(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int, start_idx::Int;
41+
function cross_validation(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int, start_idx::Int;
4242
S::Int = 10_000,
4343
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
46-
b = Backtest(num_mle, steps_ahead)
46+
b = CrossValidation(num_mle, steps_ahead)
4747
for i in 1:num_mle
48-
println("Backtest: step $i of $num_mle")
48+
println("CrossValidation: step $i of $num_mle")
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]

src/visualization/backtest.jl renamed to src/visualization/cross_validation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RecipesBase.@recipe function f(b::ScoreDrivenModels.Backtest, name::String)
1+
RecipesBase.@recipe function f(b::ScoreDrivenModels.CrossValidation, name::String)
22
xguide := "lead times"
33
@series begin
44
seriestype := :path

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ include("test_initial_params.jl")
1111
include("test_diagnostics.jl")
1212
include("test_estimate.jl")
1313
include("test_simulate.jl")
14-
include("test_backtest.jl")
14+
include("test_cross_validation.jl")
1515
include("test_visualization.jl")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@testset "Backtest" begin
1+
@testset "CrossValidation" begin
22
ω = [0.1, 0.1]
33
A = [0.5 0; 0 0.5]
44
B = [0.5 0; 0 0.5]
55
simulation = simulate_GAS_1_1(Normal, 0.0, ω, A, B, 1)
66
gas = ScoreDrivenModels.Model(1, 1, Normal, 0.0)
7-
bac = backtest(gas, simulation, 10, 4985)
7+
bac = cross_validation(gas, simulation, 10, 4985)
88
end

test/test_visualization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
B = [0.5 0; 0 0.5]
1414
simulation = simulate_GAS_1_1(Normal, 0.0, ω, A, B, 1)
1515
gas = ScoreDrivenModels.Model(1, 1, Normal, 0.0)
16-
bac = backtest(gas, simulation, 10, 4985)
16+
bac = cross_validation(gas, simulation, 10, 4985)
1717
rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), bac, "name")
1818
@test length(rec) == 2
1919

0 commit comments

Comments
 (0)