|
1 |
| -using ScoreDrivenModels, Plots, DelimitedFiles, Dates, Random |
| 1 | +using Dates, DelimitedFiles, Plots, Random, ScoreDrivenModels |
2 | 2 |
|
| 3 | +# Define dates and load historical Affluent Natural Energy data |
3 | 4 | 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 |
10 | 22 | estimation_stats = fit_stats(f)
|
11 | 23 |
|
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) |
13 | 26 |
|
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) |
0 commit comments