Skip to content

Commit db66a25

Browse files
improve examples (#107)
1 parent ff89261 commit db66a25

File tree

4 files changed

+85
-35
lines changed

4 files changed

+85
-35
lines changed

examples/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
88

99
[compat]
1010
Plots = "= 1.4.3"
11-
ScoreDrivenModels = "= 0.1.1"
11+
ScoreDrivenModels = "= 0.1.1"

examples/ane.jl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
using ScoreDrivenModels, Plots, DelimitedFiles, Dates, Random
1+
using Dates, DelimitedFiles, Plots, Random, ScoreDrivenModels
22

3+
# Define dates and load historical Affluent Natural Energy data
34
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);
5+
y = vec(readdlm("../test/data/ane_northeastern.csv"))
6+
y_train = y[1:400]
7+
y_test = y[401:460]
8+
9+
# Set RNG seed to guarantee consistent results
10+
Random.seed!(123)
11+
12+
# Specify GAS model: a lognormal model with time-varying μ, constant σ, and lags 1 and 12
13+
gas = Model([1, 2, 11, 12], [1, 2, 11, 12], LogNormal, 0.0; time_varying_params=[1])
14+
15+
# Obtain initial parameters to start the GAS recursion
16+
initial_params = dynamic_initial_params(y_train, gas)
17+
18+
# Fit specified model to historical data using initial parameters
19+
f = ScoreDrivenModels.fit!(gas, y_train; initial_params=initial_params)
20+
21+
# Get estimation statistics
1022
estimation_stats = fit_stats(f)
1123

12-
forec = ScoreDrivenModels.forecast(y_train, gas, 60; S=1_000, initial_params=initial_params)
24+
# Simulate 1000 future scenarios and obtain the 5% and 95% quantiles in each time period
25+
forec = forecast(y_train, gas, 60; S=1000, initial_params=initial_params)
1326

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)
27+
# Plot results
28+
plot(dates[401:460], forec.observation_scenarios, color="grey", width=0.05, label="", ylims=(0, 70))
29+
plot!(dates[360:460], y[360:460], label="ANE", color="black", xlabel="Months", ylabel="GWmed", legend=:topright)
30+
plot!(dates[401:460], forec.observation_quantiles, label=["Quantiles" "" ""], color="red", line=:dash)

examples/cpichg.jl

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

5-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2]);
6-
f = ScoreDrivenModels.fit!(gas, y);
3+
# Load historical Consumer Price Index data
4+
y = vec(readdlm("../test/data/cpichg.csv"))
75

8-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2]);
9-
f = fit!(gas, y; verbose=2);
6+
# Set RNG seed to guarantee consistent results
7+
Random.seed!(123)
108

11-
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1; 2]);
12-
f = fit!(gas, y; opt_method=LBFGS(gas, 5));
9+
# Specify GAS model: a student's t model with location scale transformation
10+
# (see /src/distributions/non_native_dists.jl in the repository)
11+
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
1312

13+
# Fit specified model to historical data
14+
f = fit!(gas, y)
15+
16+
# Next, we show examples using nondefault keyword arguments
17+
# 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])
19+
20+
# In this example, we set verbose to 2, prompting the package to output optimization results
21+
f = fit!(gas, y; verbose=2)
22+
23+
# Re-define `gas` once again for another example
24+
gas = Model(1, 1, TDistLocationScale, 0.0, time_varying_params=[1, 2])
25+
26+
# In this example, we set the optimization method to LBFGS with 5 initial points
27+
f = fit!(gas, y; opt_method=LBFGS(gas, 5))
28+
29+
# Get estimation statistics
1430
estimation_stats = fit_stats(f)
1531

16-
forec = forecast(y, gas, 12);
32+
# Perform forecast via simulations for 12 time periods ahead
33+
forec = forecast(y, gas, 12)
34+
35+
# We can access the parameter forecasts in `forec.parameter_forecasts`
1736
forec.parameter_forecast
18-
forec.observation_scenarios
1937

20-
param = score_driven_recursion(gas, y)
38+
# Similarly, we can access the simulated observation scenarios
39+
forec.observation_scenarios

examples/garch.jl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using ScoreDrivenModels, Statistics, DelimitedFiles
1+
using DelimitedFiles, ScoreDrivenModels, Statistics
22

3-
# Overwrite link interface to use IdentityLink for Normal distribution.
3+
# This is a more advanced example. We want to specify a GARCH model using the GAS framework.
4+
# To that end, we need to overwrite the default link-unlink interface as it would lead to
5+
# a different parametrization.
46
function ScoreDrivenModels.link!(param_tilde::Matrix{T}, ::Type{Normal}, param::Matrix{T}, t::Int) where T
57
param_tilde[t, 1] = link(IdentityLink, param[t, 1])
68
param_tilde[t, 2] = link(IdentityLink, param[t, 2])
@@ -17,12 +19,28 @@ function ScoreDrivenModels.jacobian_link!(aux::AuxiliaryLinAlg{T}, ::Type{Normal
1719
return
1820
end
1921

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));
22+
# Load (TODO: what is this data?) data
23+
y = vec(readdlm("../test/data/BG96.csv"))
24+
25+
# Set initial parameters as observed mean and variance of the series
26+
initial_params = [mean(y) var(y)]
27+
28+
# We can set upper and lower bounds for the parameters
29+
ub = [1.0, 1.0, 0.5, 1.0]
30+
lb = [-1.0, 0.0, 0.0, 0.5]
31+
32+
# Specify GAS model: a normal model with time-varying σ and lag 1
33+
gas = Model(1, 1, Normal, 1.0, time_varying_params = [2])
34+
35+
# Set initial point for the optimization
36+
initial_point = [0.0, 0.5, 0.25, 0.75]
37+
38+
# Fit specified model to historical data
39+
f = fit!(
40+
gas, y;
41+
initial_params = initial_params,
42+
opt_method = IPNewton(gas, [initial_point]; ub=ub, lb=lb)
43+
)
44+
45+
# Get estimation statistics
2846
estimation_stats = fit_stats(f)

0 commit comments

Comments
 (0)