Skip to content

Commit 4940f7a

Browse files
Merge pull request #309 from LAMPSPUC/gb/fix_simulation_bsex
Fix basic structural with exogenous variables simulation
2 parents bea472c + 2b455cd commit 4940f7a

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StateSpaceModels"
22
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
33
authors = ["raphaelsaavedra <[email protected]>, guilhermebodin <[email protected]>, mariohsouto"]
4-
version = "0.6.2"
4+
version = "0.6.3"
55

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

3839
[targets]
39-
test = ["CSV", "DataFrames", "Test"]
40+
test = ["CSV", "DataFrames", "Random", "Test"]

src/models/basicstructural_explanatory.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,17 @@ function simulate(
196196
standard_ε = randn(n)
197197
standard_η = randn(n + 1, size(sys.Q[1], 1))
198198

199+
num_exogenous = size(model.exogenous, 2)
200+
@assert num_exogenous == size(new_exogenous, 2) "You must have the same number of exogenous variables of the model."
201+
199202
# The first state of the simulation is the update of a_0
200203
alpha[1, :] .= initial_state
201-
sys.Z[1][14:end] .= new_exogenous[1, :]
204+
sys.Z[1][end-num_exogenous+1:end] .= new_exogenous[1, :]
202205
y[1] = dot(sys.Z[1], initial_state) + sys.d[1] + chol_H * standard_ε[1]
203206
alpha[2, :] = sys.T[1] * initial_state + sys.c[1] + sys.R[1] * chol_Q.L * standard_η[1, :]
204207
# Simulate scenarios
205208
for t in 2:n
206-
sys.Z[t][14:end] .= new_exogenous[t, :]
209+
sys.Z[t][end-num_exogenous+1:end] .= new_exogenous[t, :]
207210
y[t] = dot(sys.Z[t], alpha[t, :]) + sys.d[t] + chol_H * standard_ε[t]
208211
alpha[t + 1, :] = sys.T[t] * alpha[t, :] + sys.c[t] + sys.R[t] * chol_Q.L * standard_η[t, :]
209212
end

test/models/basicstructural_explanatory.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
@test has_fit_methods(BasicStructuralExplanatory)
33
y = CSV.File(StateSpaceModels.AIR_PASSENGERS) |> DataFrame
44
logap = log.(y.passengers)
5-
X = rand(length(logap), 2)
6-
model = BasicStructuralExplanatory(logap, 10, X)
7-
fit!(model)
8-
model.results
5+
X = ones(length(logap), 2)
96
model = BasicStructuralExplanatory(logap, 12, X)
107
fit!(model)
11-
model.results
8+
model = BasicStructuralExplanatory(logap, 10, X)
9+
fit!(model)
1210
# forecasting
1311
# For a fixed forecasting explanatory the variance must not decrease
1412
forec = forecast(model, ones(10, 2))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CSV
22
using DataFrames
33
using LinearAlgebra
4+
using Random
45
using RecipesBase
56
using StateSpaceModels
67
using Statistics

0 commit comments

Comments
 (0)