Skip to content

Commit

Permalink
Merge pull request #309 from LAMPSPUC/gb/fix_simulation_bsex
Browse files Browse the repository at this point in the history
Fix basic structural with exogenous variables simulation
  • Loading branch information
guilhermebodin authored Jun 11, 2022
2 parents bea472c + 2b455cd commit 4940f7a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StateSpaceModels"
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
authors = ["raphaelsaavedra <raphael.saavedra93@gmail.com>, guilhermebodin <guilherme.b.moraes@gmail.com>, mariohsouto"]
version = "0.6.2"
version = "0.6.3"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down Expand Up @@ -33,7 +33,8 @@ julia = "1"
[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["CSV", "DataFrames", "Test"]
test = ["CSV", "DataFrames", "Random", "Test"]
7 changes: 5 additions & 2 deletions src/models/basicstructural_explanatory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,17 @@ function simulate(
standard_ε = randn(n)
standard_η = randn(n + 1, size(sys.Q[1], 1))

num_exogenous = size(model.exogenous, 2)
@assert num_exogenous == size(new_exogenous, 2) "You must have the same number of exogenous variables of the model."

# The first state of the simulation is the update of a_0
alpha[1, :] .= initial_state
sys.Z[1][14:end] .= new_exogenous[1, :]
sys.Z[1][end-num_exogenous+1:end] .= new_exogenous[1, :]
y[1] = dot(sys.Z[1], initial_state) + sys.d[1] + chol_H * standard_ε[1]
alpha[2, :] = sys.T[1] * initial_state + sys.c[1] + sys.R[1] * chol_Q.L * standard_η[1, :]
# Simulate scenarios
for t in 2:n
sys.Z[t][14:end] .= new_exogenous[t, :]
sys.Z[t][end-num_exogenous+1:end] .= new_exogenous[t, :]
y[t] = dot(sys.Z[t], alpha[t, :]) + sys.d[t] + chol_H * standard_ε[t]
alpha[t + 1, :] = sys.T[t] * alpha[t, :] + sys.c[t] + sys.R[t] * chol_Q.L * standard_η[t, :]
end
Expand Down
8 changes: 3 additions & 5 deletions test/models/basicstructural_explanatory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
@test has_fit_methods(BasicStructuralExplanatory)
y = CSV.File(StateSpaceModels.AIR_PASSENGERS) |> DataFrame
logap = log.(y.passengers)
X = rand(length(logap), 2)
model = BasicStructuralExplanatory(logap, 10, X)
fit!(model)
model.results
X = ones(length(logap), 2)
model = BasicStructuralExplanatory(logap, 12, X)
fit!(model)
model.results
model = BasicStructuralExplanatory(logap, 10, X)
fit!(model)
# forecasting
# For a fixed forecasting explanatory the variance must not decrease
forec = forecast(model, ones(10, 2))
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CSV
using DataFrames
using LinearAlgebra
using Random
using RecipesBase
using StateSpaceModels
using Statistics
Expand Down

2 comments on commit 4940f7a

@guilhermebodin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

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/62173

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:

git tag -a v0.6.3 -m "<description of version>" 4940f7a09c5efcd43ffb12fd3f0fb4f9967ace74
git push origin v0.6.3

Please sign in to comment.