Skip to content

Commit ff89261

Browse files
New examples (#106)
* update examples Co-authored-by: Raphael Saavedra <[email protected]>
1 parent 134d5d2 commit ff89261

File tree

4 files changed

+53
-72
lines changed

4 files changed

+53
-72
lines changed

examples/Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[deps]
2+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
3+
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
4+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
5+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
6+
ScoreDrivenModels = "4a87933e-d659-11e9-0e65-7f40dedd4a3a"
7+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
8+
9+
[compat]
10+
Plots = "= 1.4.3"
11+
ScoreDrivenModels = "= 0.1.1"

examples/ane.jl

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
using ScoreDrivenModels, Plots
2-
3-
# Convert data to vector
4-
y = Vector{Float64}(vec(readdlm("./test/data/ena_northeastern.csv")'))
5-
y_train = y[1:400]
6-
y_test = y[401:end]
7-
8-
# Specify model: here we use lag 1 for trend characterization and lag 12 for seasonality characterization
9-
gas = Model([1, 2, 11, 12], [1, 2, 11, 12], LogNormal, 0.0; time_varying_params = [1])
10-
11-
# Define initial_params with
12-
initial_params = dynamic_initial_params(y_train, gas)
13-
14-
# Estimate the model via MLE
15-
f = fit!(gas, y_train; initial_params = initial_params)
16-
17-
# Compare observations and in-sample estimates
18-
plot(y_train, label = "In-sample ANE")
19-
20-
# Forecasts with 95% confidence interval
21-
forecast = forecast_quantiles(y_train, gas, 60; S = 1_000, initial_params = initial_params)
22-
23-
plot(forecast.scenarios, color = "grey", width = 0.05, label = "")
24-
plot!(y[360:460], label = "ANE", color = "black", xlabel = "Months", ylabel = "GWmed", legend = :topright)
25-
plot!(forecast.quantiles, label = ["Quantiles" "" ""], color = "red", line = :dash)
26-
1+
using ScoreDrivenModels, Plots, DelimitedFiles, Dates, Random
2+
3+
dates = collect(Date(1961):Month(1):Date(2000, 12))
4+
Random.seed!(123);
5+
y = vec(readdlm("../test/data/ane_northeastern.csv"));
6+
y_train = y[1:400];
7+
gas = Model([1, 2, 11, 12], [1, 2, 11, 12], LogNormal, 0.0; time_varying_params=[1]);
8+
initial_params = dynamic_initial_params(y_train, gas);
9+
f = ScoreDrivenModels.fit!(gas, y_train; initial_params=initial_params);
10+
estimation_stats = fit_stats(f)
11+
12+
forec = ScoreDrivenModels.forecast(y_train, gas, 60; S=1_000, initial_params=initial_params)
13+
14+
y_test = y[401:460]
15+
p2 = plot(dates[401:460], forec.observation_scenarios, color="grey", width=0.05, label="", ylims=(0, 70))
16+
plot!(p2, dates[360:460], y[360:460], label="ANE", color="black", xlabel="Months", ylabel="GWmed", legend=:topright)
17+
plot!(p2, dates[401:460], forec.observation_quantiles, label=["Quantiles" "" ""], color="red", line=:dash)

examples/cpichg.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
using ScoreDrivenModels, DelimitedFiles
1+
using ScoreDrivenModels, DelimitedFiles, Random, Statistics
2+
Random.seed!(123);
3+
y = vec(readdlm("../test/data/cpichg.csv"));
24

3-
y = readdlm("./test/data/cpichg.csv")[:]
5+
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2]);
6+
f = ScoreDrivenModels.fit!(gas, y);
47

5-
# Define a model where only \mu and \sigma^2 is varying over time
6-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params = [1; 2])
8+
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2]);
9+
f = fit!(gas, y; verbose=2);
710

8-
# Estimate the model
9-
f = fit!(gas, y)
11+
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1; 2]);
12+
f = fit!(gas, y; opt_method=LBFGS(gas, 5));
1013

11-
estimation_stats = fit_stats(f)
14+
estimation_stats = fit_stats(f)
15+
16+
forec = forecast(y, gas, 12);
17+
forec.parameter_forecast
18+
forec.observation_scenarios
19+
20+
param = score_driven_recursion(gas, y)

examples/garch.jl

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,12 @@ function ScoreDrivenModels.jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{Normal
1717
return
1818
end
1919

20-
# Data from [Bollerslev and Ghysels (JBES 1996)](https://doi.org/10.2307/1392425). Obtained from [ARCHModels.jl](https://github.com/s-broda/ARCHModels.jl)
21-
y = readdlm("./test/data/BG96.csv")[:]
22-
23-
# Evaluate some approximate initial params
24-
initial_params = [mean(y) var(y)]
25-
26-
# There are 2 possibilities to estimate the model.
27-
# The first one is to understand that in the GARCH estimation process usually one
28-
# defines that the parameters ω, α_0, α_1 and β_1 are constrained. The problem is
29-
# that the equivalent GAS model relies on
30-
# ω_1 = ω
31-
# ω_2 = α_0
32-
# A_1 = α_1
33-
# B_1 - A_1 = β_1
34-
# Here there is no easy way of adding a constraint B_1 - A_1 >= 0. An alternative is to define
35-
# bounds assuring the upper bound of A_1 is smaller than the lower bound for B_1.
36-
37-
# Define upper bounds and lower bounds for parameters ω_1, ω_2, A_1, B_1
38-
ub = [1.0; 1.0; 0.5; 1.0]
39-
lb = [-1.0; 0.0; 0.0; 0.5]
40-
41-
# Define a model where only \sigma^2 is varying over time
42-
gas = Model(1, 1, Normal, 1.0, time_varying_params = [2])
43-
44-
# Give an initial_point in the interior of the bounds.
45-
initial_point = [0.0; 0.5; 0.25; 0.75]
46-
47-
# Estimate the model
48-
f_ip_newton = fit(gas, y; initial_params = initial_params,
49-
opt_method = IPNewton(gas, [initial_point]; ub = ub, lb = lb))
50-
51-
estimation_stats_ip_newton = fit_stats(f_ip_newton)
52-
53-
# Another way to estimate the model is to rely on luck and give many random initial_points
54-
# for a non constrained optimization
55-
f_nelder_mead = fit(gas, y; initial_params = initial_params,
56-
opt_method = NelderMead(gas, 100))
57-
58-
estimation_stats_nelder_mead = fit_stats(f_nelder_mead)
20+
y = vec(readdlm("../test/data/BG96.csv"));
21+
initial_params = [mean(y) var(y)];
22+
ub = [1.0, 1.0, 0.5, 1.0];
23+
lb = [-1.0, 0.0, 0.0, 0.5];
24+
gas = Model(1, 1, Normal, 1.0, time_varying_params = [2]);
25+
initial_point = [0.0, 0.5, 0.25, 0.75];
26+
f = fit!(gas, y; initial_params = initial_params,
27+
opt_method = IPNewton(gas, [initial_point]; ub = ub, lb = lb));
28+
estimation_stats = fit_stats(f)

0 commit comments

Comments
 (0)