Skip to content

Commit 626456a

Browse files
author
Raphael Saavedra
authored
Rename Model to ScoreDrivenModel (#145)
* Rename `Model` to `ScoreDrivenModel` * Bump (breaking change)
1 parent feb92dc commit 626456a

27 files changed

+169
-169
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.11"
4+
version = "0.2.0"
55

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

bench/benchmark_estimate.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ scaling = 0.0
77
A = [0.2 0; 0 0.2]
88
B = [0.2 0; 0 0.2]
99
simulation = simulate_GAS_1_1(LogNormal, scaling, ω, A, B, 1)
10-
gas = Model(1, 1, LogNormal, scaling)
10+
gas = ScoreDrivenModel(1, 1, LogNormal, scaling)
1111
gas.ω = ω
1212
gas.A[1] = A
1313
gas.B[1] = B
@@ -29,7 +29,7 @@ scaling = 0.5
2929
A = [0.2 0; 0 0.2]
3030
B = [0.2 0; 0 0.2]
3131
simulation = simulate_GAS_1_1(LogNormal, scaling, ω, A, B, 1)
32-
gas = Model(1, 1, LogNormal, scaling)
32+
gas = ScoreDrivenModel(1, 1, LogNormal, scaling)
3333
gas.ω = ω
3434
gas.A[1] = A
3535
gas.B[1] = B
@@ -51,7 +51,7 @@ scaling = 1.0
5151
A = [0.2 0; 0 0.2]
5252
B = [0.2 0; 0 0.2]
5353
simulation = simulate_GAS_1_1(LogNormal, scaling, ω, A, B, 1)
54-
gas = Model(1, 1, LogNormal, scaling)
54+
gas = ScoreDrivenModel(1, 1, LogNormal, scaling)
5555
gas.ω = ω
5656
gas.A[1] = A
5757
gas.B[1] = B
@@ -66,4 +66,4 @@ gas.B[1] = B
6666
# maximum time: 5.374 ms (58.97% GC)
6767
# --------------
6868
# samples: 3247
69-
# evals/sample: 1
69+
# evals/sample: 1

docs/src/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ y = Vector{Float64}(vec(inflow'))
1818

1919
# Specify model: here we use lag 1 for trend characterization and
2020
# lag 12 for seasonality characterization
21-
gas = Model([1, 12], [1, 12], LogNormal, 0.0)
21+
gas = ScoreDrivenModel([1, 12], [1, 12], LogNormal, 0.0)
2222

2323
# Estimate the model via MLE
2424
fit!(gas, y)

docs/src/manual.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It provides a high-level presentation of the theory behind score-driven models a
66
## Model Specification
77

88
```@docs
9-
Model
9+
ScoreDrivenModel
1010
```
1111

1212
## Optimization Algorithms

examples/cpichg.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ y = vec(readdlm("../test/data/cpichg.csv"))
88

99
# Specify GAS model: a student's t model with location scale transformation
1010
# (see /src/distributions/non_native_dists.jl in the repository)
11-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
11+
gas = ScoreDrivenModel(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
1212

1313
# Fit specified model to historical data
1414
f = fit!(gas, y)
1515

1616
# Next, we show examples using nondefault keyword arguments
1717
# Note that we need to re-define `gas` since its parameters were populated by `fit!`
18-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
18+
gas = ScoreDrivenModel(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
1919

2020
# In this example, we set verbose to 2, prompting the package to output optimization results
2121
f = fit!(gas, y; verbose=2)
2222

2323
# Re-define `gas` once again for another example
24-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
24+
gas = ScoreDrivenModel(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
2525

2626
# In this example, we set the optimization method to LBFGS with 5 initial points
2727
f = fit!(gas, y; opt_method=LBFGS(gas, 5))
@@ -39,11 +39,11 @@ forec.parameter_forecast
3939
# Similarly, we can access the simulated observation scenarios
4040
forec.observation_scenarios
4141

42-
gas_t = Model(1, 1, TDistLocationScale, 1.0; time_varying_params = [1])
43-
gas_t_1_2 = Model(1, 2, TDistLocationScale, 1.0; time_varying_params = [1])
42+
gas_t = ScoreDrivenModel(1, 1, TDistLocationScale, 1.0; time_varying_params = [1])
43+
gas_t_1_2 = ScoreDrivenModel(1, 2, TDistLocationScale, 1.0; time_varying_params = [1])
4444
steps_ahead = 50
4545
first_idx = 150
4646
b_t = cross_validation(gas_t, y, steps_ahead, first_idx)
4747
b_t_1_2 = cross_validation(gas_t_1_2, y, steps_ahead, first_idx)
4848
plot(b_t, "GAS(1, 1) Student t")
49-
plot!(b_t_1_2, "GAS(1, 2) Student t"; legend = :topleft)
49+
plot!(b_t_1_2, "GAS(1, 2) Student t"; legend = :topleft)

examples/garch.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ub = [1.0, 1.0, 0.5, 1.0]
3030
lb = [-1.0, 0.0, 0.0, 0.5]
3131

3232
# Specify GAS model: a normal model with time-varying σ and lag 1
33-
gas = Model(1, 1, Normal, 1.0, time_varying_params = [2])
33+
gas = ScoreDrivenModel(1, 1, Normal, 1.0, time_varying_params = [2])
3434

3535
# Set initial point for the optimization
3636
initial_point = [0.0, 0.5, 0.25, 0.75]

examples/introduction.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ScoreDrivenModels
22

3-
Model(1, 2, LogNormal, 0.5)
3+
ScoreDrivenModel(1, 2, LogNormal, 0.5)
44

5-
Model(1, 2, LogNormal, 0.5; time_varying_params = [1])
5+
ScoreDrivenModel(1, 2, LogNormal, 0.5; time_varying_params = [1])
66

7-
Model([1, 12], [1, 12], LogNormal, 0.5)
7+
ScoreDrivenModel([1, 12], [1, 12], LogNormal, 0.5)

examples/nie.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ y_test = y[401:460]
1111
Random.seed!(123)
1212

1313
# Specify GAS model: a lognormal model with time-varying μ, constant σ, and lags 4 and 12
14-
gas = Model([1,2,3,4], [1,2,3,10,11,12], LogNormal, 1.0; time_varying_params=[1])
14+
gas = ScoreDrivenModel([1,2,3,4], [1,2,3,10,11,12], LogNormal, 1.0; time_varying_params=[1])
1515

1616
# Obtain initial parameters to start the GAS recursion
1717
initial_params = dynamic_initial_params(y_train, gas)

src/MLE.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function results(f::Fitted{D, T}) where {D, T}
3838
estim_results = eval_coefs_stats(f)
3939
np = length(f.unknowns)
4040
jarquebera_p_value = pvalue(JarqueBeraTest(f.quantile_residuals))
41-
return EstimationStats{D, T}(f.num_obs, f.llk, f.aic, f.bic, np,
41+
return EstimationStats{D, T}(f.num_obs, f.llk, f.aic, f.bic, np,
4242
jarquebera_p_value, estim_results)
4343
end
4444

@@ -47,7 +47,7 @@ function eval_coefs_stats(f::Fitted{D, T}) where {D, T}
4747
inv_H = inv(f.numerical_hessian)
4848
vars = diag(inv_H)
4949
for i in eachindex(vars)
50-
if vars[i] <= VARIANCE_ZERO
50+
if vars[i] <= VARIANCE_ZERO
5151
vars[i] = VARIANCE_ZERO
5252
end
5353
end
@@ -70,7 +70,7 @@ mutable struct AuxEstimation{T <: AbstractFloat}
7070
function AuxEstimation{T}() where T
7171
return new(
7272
Vector{Vector{T}}(undef, 0), #psi
73-
Vector{Matrix{T}}(undef, 0),
73+
Vector{Matrix{T}}(undef, 0),
7474
Vector{T}(undef, 0), # loglikelihood
7575
Vector{Optim.OptimizationResults}(undef, 0) # opt_result
7676
)
@@ -87,15 +87,15 @@ end
8787

8888
function update_aux_estimation!(aux_est::AuxEstimation{T}, func::Optim.TwiceDifferentiable,
8989
opt_result::Optim.OptimizationResults) where T
90-
90+
9191
push!(aux_est.numerical_hessian, Optim.hessian!(func, opt_result.minimizer))
9292
push!(aux_est.psi, opt_result.minimizer)
9393
push!(aux_est.loglikelihood, -opt_result.minimum)
9494
push!(aux_est.opt_result, opt_result)
9595
return
9696
end
9797

98-
function fit!(gas::Model{D, T}, y::Vector{T};
98+
function fit!(gas::ScoreDrivenModel{D, T}, y::Vector{T};
9999
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
100100
opt_method::AbstractOptimizationMethod = NelderMead(gas, DEFAULT_NUM_SEEDS),
101101
verbose::Int = DEFAULT_VERBOSE,
@@ -111,23 +111,23 @@ function fit!(gas::Model{D, T}, y::Vector{T};
111111
# Number of initial_points and number of params to estimate
112112
n_initial_points = length(opt_method.initial_points)
113113
n = length(y)
114-
114+
115115
unknowns = find_unknowns(gas)
116116
n_unknowns = length(unknowns)
117-
117+
118118
# Check if the model has no unknowns
119119
check_model_estimated(n_unknowns) && return gas
120120

121121
# Create a copy of the model to estimate
122122
gas_fit = deepcopy(gas)
123-
123+
124124
# optimize for each initial_point
125125
aux_est = AuxEstimation{T}()
126126

127127
for i = 1:n_initial_points
128-
try
129-
func = TwiceDifferentiable(psi_tilde -> log_lik(psi_tilde, y, gas_fit,
130-
initial_params, unknowns, n),
128+
try
129+
func = TwiceDifferentiable(psi_tilde -> log_lik(psi_tilde, y, gas_fit,
130+
initial_params, unknowns, n),
131131
opt_method.initial_points[i])
132132
opt_result = optimize(func, opt_method, verbose, i, time_limit_sec)
133133
update_aux_estimation!(aux_est, func, opt_result)
@@ -141,7 +141,7 @@ function fit!(gas::Model{D, T}, y::Vector{T};
141141
end
142142
end
143143

144-
if isempty(aux_est.loglikelihood)
144+
if isempty(aux_est.loglikelihood)
145145
verbose >= 1 && println("No initial point converged.")
146146
return
147147
end
@@ -162,9 +162,9 @@ function fit!(gas::Model{D, T}, y::Vector{T};
162162
fill_psitilde!(gas, coefs, unknowns)
163163

164164
# Calculate pearson residuals
165-
pearson_res = isnan(initial_params[1]) ?
165+
pearson_res = isnan(initial_params[1]) ?
166166
quantile_residuals(y, gas) :
167167
quantile_residuals(y, gas; initial_params = initial_params)
168168

169169
return Fitted{D, T}(n, unknowns, aic, bic, best_llk, coefs, num_hessian, pearson_res)
170-
end
170+
end

src/cross_validation.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
"""
3939
TODO
4040
"""
41-
function cross_validation(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahead::Int, start_idx::Int;
41+
function cross_validation(gas::ScoreDrivenModel{<: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
@@ -50,7 +50,7 @@ function cross_validation(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahe
5050
y_to_fit = y[1:start_idx - 1 + i]
5151
y_to_verify = y[start_idx + i:start_idx - 1 + i + steps_ahead]
5252
fit!(gas_to_fit, y_to_fit; initial_params=initial_params, opt_method=opt_method)
53-
forec = initial_params !== DEFAULT_INITIAL_PARAM ?
53+
forec = initial_params !== DEFAULT_INITIAL_PARAM ?
5454
forecast(y_to_fit, gas_to_fit, steps_ahead; S=S, initial_params=initial_params) :
5555
forecast(y_to_fit, gas_to_fit, steps_ahead; S=S)
5656
abs_errors = evaluate_abs_error(y_to_verify, forec.observation_forecast)
@@ -63,4 +63,4 @@ function cross_validation(gas::Model{<:Distribution, T}, y::Vector{T}, steps_ahe
6363
b.mean_crps[i] = mean(b.crps_scores[i, :])
6464
end
6565
return b
66-
end
66+
end

0 commit comments

Comments
 (0)