Skip to content

Commit 6058199

Browse files
author
andre_ramos
committed
fix deterministic cycle simulation
1 parent 2be51bd commit 6058199

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StateSpaceLearning"
22
uuid = "971c4b7c-2c4e-4bac-8525-e842df3cde7b"
33
authors = ["andreramosfc <[email protected]>"]
4-
version = "2.0.3"
4+
version = "2.0.4"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

src/models/structural_model.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ function create_deterministic_cycle(
444444
)::Matrix where {Fl<:AbstractFloat}
445445
λ = 2 * pi * (1:T) / c_period
446446
cycle1_matrix = hcat(cos.(λ), sin.(λ))
447-
return cycle1_matrix
447+
return round.(cycle1_matrix, digits=5)
448448
end
449449

450450
"""
@@ -1500,24 +1500,33 @@ function simulate_states(
15001500
if model.cycle_period != 0
15011501
cycles_t = zeros(AbstractFloat, length(model.cycle_period))
15021502
for i in eachindex(model.cycle_period)
1503-
ϕ_cos = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][1:2:end]
1504-
ϕ_sin = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][2:2:end]
1503+
15051504
λ = 2 * pi * (1:(T + steps_ahead)) / model.cycle_period[i]
15061505

1507-
cycle_t =
1508-
dot(
1509-
model.output.components["c1_$(model.cycle_period[i])"]["Coefs"],
1510-
[cos(λ[t]), sin(λ[t])],
1511-
) +
1512-
sum(
1513-
ϕ_cos[j] * cos(λ[t]) + ϕ_sin[j] * sin(λ[t]) for
1514-
j in eachindex(ϕ_cos)
1515-
) +
1516-
sum(
1517-
stochastic_cycles_cos_set[i][j] * cos(λ[t]) +
1518-
stochastic_cycles_sin_set[i][j] * sin(λ[t]) for
1519-
j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)])
1520-
)
1506+
if model.stochastic_cycle
1507+
ϕ_cos = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][1:2:end]
1508+
ϕ_sin = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][2:2:end]
1509+
cycle_t =
1510+
dot(
1511+
model.output.components["c1_$(model.cycle_period[i])"]["Coefs"],
1512+
[cos(λ[t]), sin(λ[t])],
1513+
) +
1514+
sum(
1515+
ϕ_cos[j] * cos(λ[t]) + ϕ_sin[j] * sin(λ[t]) for
1516+
j in eachindex(ϕ_cos)
1517+
) +
1518+
sum(
1519+
stochastic_cycles_cos_set[i][j] * cos(λ[t]) +
1520+
stochastic_cycles_sin_set[i][j] * sin(λ[t]) for
1521+
j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)])
1522+
)
1523+
else
1524+
cycle_t =
1525+
dot(
1526+
model.output.components["c1_$(model.cycle_period[i])"]["Coefs"],
1527+
[cos(λ[t]), sin(λ[t])],
1528+
)
1529+
end
15211530
cycles_t[i] = cycle_t
15221531
end
15231532
else

test/models/structural_model.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,16 @@ end
810810
model6, 10, forecast_dynamic_exog_coefs
811811
),
812812
) == 10
813+
814+
model7 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="deterministic", cycle_period=[12, 6, 4, 3, 12/5, 2])
815+
StateSpaceLearning.fit!(model7)
816+
forecast7 = trunc.(StateSpaceLearning.forecast(model7, 18); digits=3)
817+
@test length(forecast7) == 18
818+
819+
model8 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="stochastic", cycle_period=[12, 6, 4, 3, 12/5, 2])
820+
StateSpaceLearning.fit!(model8)
821+
forecast8 = trunc.(StateSpaceLearning.forecast(model8, 18); digits=3)
822+
@test length(forecast8) == 18
813823
end
814824

815825
@testset "Function: simulate" begin

0 commit comments

Comments
 (0)