-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add backtest and time_limit_sec * Add backtest
- Loading branch information
1 parent
4a70667
commit dfae6d5
Showing
10 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "ScoreDrivenModels" | ||
uuid = "4a87933e-d659-11e9-0e65-7f40dedd4a3a" | ||
authors = ["guilhermebodin <[email protected]>, raphaelsaavedra <[email protected]>"] | ||
version = "0.1.5" | ||
version = "0.1.6" | ||
|
||
[deps] | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export backtest | ||
|
||
struct Backtest | ||
abs_errors::Matrix{Float64} | ||
crps_scores::Matrix{Float64} | ||
function Backtest(n::Int, steps_ahead::Int) | ||
abs_errors = Matrix{Float64}(undef, n, steps_ahead) | ||
crps_scores = Matrix{Float64}(undef, n, steps_ahead) | ||
return new(abs_errors, crps_scores) | ||
end | ||
end | ||
|
||
discrete_crps_indicator_function(val::T, z::T) where {T} = val < z | ||
function crps(val::T, scenarios::Vector{T}) where {T} | ||
sorted_scenarios = sort(scenarios) | ||
m = length(scenarios) | ||
crps_score = zero(T) | ||
for i = 1:m | ||
crps_score += | ||
(sorted_scenarios[i] - val) * | ||
(m * discrete_crps_indicator_function(val, sorted_scenarios[i]) - i + 0.5) | ||
end | ||
return (2 / m^2) * crps_score | ||
end | ||
evaluate_abs_error(y::Vector{T}, forecast::Vector{T}) where T = abs.(y - forecast) | ||
function evaluate_crps(y::Vector{T}, scenarios::Matrix{T}) where {T} | ||
crps_scores = Vector{T}(undef, length(y)) | ||
for k = 1:length(y) | ||
crps_scores[k] = crps(y[k], scenarios[k, :]) | ||
end | ||
return crps_scores | ||
end | ||
|
||
""" | ||
TODO | ||
""" | ||
function backtest(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int, start_idx::Int; | ||
S::Int = 1000, | ||
initial_params::Matrix{T} = stationary_initial_params(gas), | ||
opt_method = NelderMead(gas, DEFAULT_NUM_SEEDS)) where T | ||
num_mle = length(y) - start_idx - steps_ahead | ||
backtest = Backtest(num_mle, steps_ahead) | ||
for i in 1:num_mle | ||
println("Backtest: step $i of $num_mle") | ||
gas_to_fit = deepcopy(gas) | ||
y_to_fit = y[1:start_idx - 1 + i] | ||
y_to_verify = y[start_idx + i:start_idx - 1 + i + steps_ahead] | ||
ScoreDrivenModels.fit!(gas_to_fit, y_to_fit; initial_params=initial_params, opt_method=opt_method) | ||
forec = forecast(y_to_fit, gas_to_fit, steps_ahead; S=S, initial_params=initial_params) | ||
abs_errors = evaluate_abs_error(y_to_verify, forec.observation_forecast) | ||
crps_scores = evaluate_crps(y_to_verify, forec.observation_scenarios) | ||
backtest.abs_errors[i, :] = abs_errors | ||
backtest.crps_scores[i, :] = crps_scores | ||
end | ||
return backtest | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@testset "Backtest" begin | ||
ω = [0.1, 0.1] | ||
A = [0.5 0; 0 0.5] | ||
B = [0.5 0; 0 0.5] | ||
simulation = simulate_GAS_1_1(Normal, 0.0, ω, A, B, 1) | ||
gas = ScoreDrivenModels.Model(1, 1, Normal, 0.0) | ||
bac = backtest(gas, simulation, 10, 4985) | ||
end |
dfae6d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
dfae6d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/29770
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: