Skip to content

Commit f1bad07

Browse files
Merge pull request #67 from LAMPSPUC/fix_deterministic_cycle_simulation
fix deterministic cycle simulation
2 parents 2b828bf + 14b5a05 commit f1bad07

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

Project.toml

+1-1
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 <andreramosfdc@gmail.com>"]
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

+21-14
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,31 @@ 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]
15051503
λ = 2 * pi * (1:(T + steps_ahead)) / model.cycle_period[i]
15061504

1507-
cycle_t =
1508-
dot(
1505+
if model.stochastic_cycle
1506+
ϕ_cos = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][1:2:end]
1507+
ϕ_sin = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][2:2:end]
1508+
cycle_t =
1509+
dot(
1510+
model.output.components["c1_$(model.cycle_period[i])"]["Coefs"],
1511+
[cos(λ[t]), sin(λ[t])],
1512+
) +
1513+
sum(
1514+
ϕ_cos[j] * cos(λ[t]) + ϕ_sin[j] * sin(λ[t]) for
1515+
j in eachindex(ϕ_cos)
1516+
) +
1517+
sum(
1518+
stochastic_cycles_cos_set[i][j] * cos(λ[t]) +
1519+
stochastic_cycles_sin_set[i][j] * sin(λ[t]) for
1520+
j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)])
1521+
)
1522+
else
1523+
cycle_t = dot(
15091524
model.output.components["c1_$(model.cycle_period[i])"]["Coefs"],
15101525
[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)])
15201526
)
1527+
end
15211528
cycles_t[i] = cycle_t
15221529
end
15231530
else

test/models/structural_model.jl

+14
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,20 @@ end
810810
model6, 10, forecast_dynamic_exog_coefs
811811
),
812812
) == 10
813+
814+
model7 = StateSpaceLearning.StructuralModel(
815+
y3; seasonal="none", cycle="deterministic", cycle_period=[12, 6, 4, 3, 12 / 5, 2]
816+
)
817+
StateSpaceLearning.fit!(model7)
818+
forecast7 = trunc.(StateSpaceLearning.forecast(model7, 18); digits=3)
819+
@test length(forecast7) == 18
820+
821+
model8 = StateSpaceLearning.StructuralModel(
822+
y3; seasonal="none", cycle="stochastic", cycle_period=[12, 6, 4, 3, 12 / 5, 2]
823+
)
824+
StateSpaceLearning.fit!(model8)
825+
forecast8 = trunc.(StateSpaceLearning.forecast(model8, 18); digits=3)
826+
@test length(forecast8) == 18
813827
end
814828

815829
@testset "Function: simulate" begin

0 commit comments

Comments
 (0)