From 6058199b57af2da0112519d8cd2b8cde2ed92355 Mon Sep 17 00:00:00 2001 From: andre_ramos Date: Wed, 30 Apr 2025 18:02:16 -0300 Subject: [PATCH 1/2] fix deterministic cycle simulation --- Project.toml | 2 +- src/models/structural_model.jl | 43 ++++++++++++++++++++------------- test/models/structural_model.jl | 10 ++++++++ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index d123464..5d7942b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StateSpaceLearning" uuid = "971c4b7c-2c4e-4bac-8525-e842df3cde7b" authors = ["andreramosfc "] -version = "2.0.3" +version = "2.0.4" [deps] Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" diff --git a/src/models/structural_model.jl b/src/models/structural_model.jl index e834fab..2be6dd3 100644 --- a/src/models/structural_model.jl +++ b/src/models/structural_model.jl @@ -444,7 +444,7 @@ function create_deterministic_cycle( )::Matrix where {Fl<:AbstractFloat} λ = 2 * pi * (1:T) / c_period cycle1_matrix = hcat(cos.(λ), sin.(λ)) - return cycle1_matrix + return round.(cycle1_matrix, digits=5) end """ @@ -1500,24 +1500,33 @@ function simulate_states( if model.cycle_period != 0 cycles_t = zeros(AbstractFloat, length(model.cycle_period)) for i in eachindex(model.cycle_period) - ϕ_cos = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][1:2:end] - ϕ_sin = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][2:2:end] + λ = 2 * pi * (1:(T + steps_ahead)) / model.cycle_period[i] - cycle_t = - dot( - model.output.components["c1_$(model.cycle_period[i])"]["Coefs"], - [cos(λ[t]), sin(λ[t])], - ) + - sum( - ϕ_cos[j] * cos(λ[t]) + ϕ_sin[j] * sin(λ[t]) for - j in eachindex(ϕ_cos) - ) + - sum( - stochastic_cycles_cos_set[i][j] * cos(λ[t]) + - stochastic_cycles_sin_set[i][j] * sin(λ[t]) for - j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)]) - ) + if model.stochastic_cycle + ϕ_cos = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][1:2:end] + ϕ_sin = model.output.components["ϕ_$(model.cycle_period[i])"]["Coefs"][2:2:end] + cycle_t = + dot( + model.output.components["c1_$(model.cycle_period[i])"]["Coefs"], + [cos(λ[t]), sin(λ[t])], + ) + + sum( + ϕ_cos[j] * cos(λ[t]) + ϕ_sin[j] * sin(λ[t]) for + j in eachindex(ϕ_cos) + ) + + sum( + stochastic_cycles_cos_set[i][j] * cos(λ[t]) + + stochastic_cycles_sin_set[i][j] * sin(λ[t]) for + j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)]) + ) + else + cycle_t = + dot( + model.output.components["c1_$(model.cycle_period[i])"]["Coefs"], + [cos(λ[t]), sin(λ[t])], + ) + end cycles_t[i] = cycle_t end else diff --git a/test/models/structural_model.jl b/test/models/structural_model.jl index 9b7278c..505d57d 100644 --- a/test/models/structural_model.jl +++ b/test/models/structural_model.jl @@ -810,6 +810,16 @@ end model6, 10, forecast_dynamic_exog_coefs ), ) == 10 + + model7 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="deterministic", cycle_period=[12, 6, 4, 3, 12/5, 2]) + StateSpaceLearning.fit!(model7) + forecast7 = trunc.(StateSpaceLearning.forecast(model7, 18); digits=3) + @test length(forecast7) == 18 + + model8 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="stochastic", cycle_period=[12, 6, 4, 3, 12/5, 2]) + StateSpaceLearning.fit!(model8) + forecast8 = trunc.(StateSpaceLearning.forecast(model8, 18); digits=3) + @test length(forecast8) == 18 end @testset "Function: simulate" begin From 14b5a05edd03f18245a6142cdab7b57c07a2b406 Mon Sep 17 00:00:00 2001 From: andre_ramos Date: Wed, 30 Apr 2025 18:09:12 -0300 Subject: [PATCH 2/2] fix blue format --- src/models/structural_model.jl | 10 ++++------ test/models/structural_model.jl | 8 ++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/models/structural_model.jl b/src/models/structural_model.jl index 2be6dd3..33af43c 100644 --- a/src/models/structural_model.jl +++ b/src/models/structural_model.jl @@ -1500,7 +1500,6 @@ function simulate_states( if model.cycle_period != 0 cycles_t = zeros(AbstractFloat, length(model.cycle_period)) for i in eachindex(model.cycle_period) - λ = 2 * pi * (1:(T + steps_ahead)) / model.cycle_period[i] if model.stochastic_cycle @@ -1521,11 +1520,10 @@ function simulate_states( j in eachindex(stochastic_cycles_cos_set[i][1:(t - T)]) ) else - cycle_t = - dot( - model.output.components["c1_$(model.cycle_period[i])"]["Coefs"], - [cos(λ[t]), sin(λ[t])], - ) + cycle_t = dot( + model.output.components["c1_$(model.cycle_period[i])"]["Coefs"], + [cos(λ[t]), sin(λ[t])], + ) end cycles_t[i] = cycle_t end diff --git a/test/models/structural_model.jl b/test/models/structural_model.jl index 505d57d..b2b226d 100644 --- a/test/models/structural_model.jl +++ b/test/models/structural_model.jl @@ -811,12 +811,16 @@ end ), ) == 10 - model7 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="deterministic", cycle_period=[12, 6, 4, 3, 12/5, 2]) + model7 = StateSpaceLearning.StructuralModel( + y3; seasonal="none", cycle="deterministic", cycle_period=[12, 6, 4, 3, 12 / 5, 2] + ) StateSpaceLearning.fit!(model7) forecast7 = trunc.(StateSpaceLearning.forecast(model7, 18); digits=3) @test length(forecast7) == 18 - model8 = StateSpaceLearning.StructuralModel(y3; seasonal="none", cycle="stochastic", cycle_period=[12, 6, 4, 3, 12/5, 2]) + model8 = StateSpaceLearning.StructuralModel( + y3; seasonal="none", cycle="stochastic", cycle_period=[12, 6, 4, 3, 12 / 5, 2] + ) StateSpaceLearning.fit!(model8) forecast8 = trunc.(StateSpaceLearning.forecast(model8, 18); digits=3) @test length(forecast8) == 18