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..33af43c 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,31 @@ 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( + 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])], - ) + - 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)]) ) + end cycles_t[i] = cycle_t end else diff --git a/test/models/structural_model.jl b/test/models/structural_model.jl index 9b7278c..b2b226d 100644 --- a/test/models/structural_model.jl +++ b/test/models/structural_model.jl @@ -810,6 +810,20 @@ 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