Skip to content

Commit

Permalink
Merge pull request #44 from LAMPSPUC/univariate_distributions
Browse files Browse the repository at this point in the history
change simulation to univariate distributions
  • Loading branch information
andreramosfdc authored Jan 3, 2025
2 parents 1e95af3 + 3e8858c commit e3366b0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StateSpaceLearning"
uuid = "971c4b7c-2c4e-4bac-8525-e842df3cde7b"
authors = ["andreramosfc <[email protected]>"]
version = "1.2.0"
version = "1.3.0"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
59 changes: 45 additions & 14 deletions src/fit_forecast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,25 @@ function simulate(
@assert seasonal_innovation_simulation >= 0 "seasonal_innovation_simulation must be a non-negative integer"
@assert isfitted(model) "Model must be fitted before simulation"

prediction = StateSpaceLearning.forecast(
model, steps_ahead; Exogenous_Forecast=Exogenous_Forecast
)
prediction = forecast(model, steps_ahead; Exogenous_Forecast=Exogenous_Forecast)

is_univariate = typeof(model.output) == StateSpaceLearning.Output
is_univariate = typeof(model.output) == Output

simulation_X = zeros(steps_ahead, 0)
valid_indexes =
is_univariate ? model.output.valid_indexes : model.output[1].valid_indexes
components_matrix = zeros(length(valid_indexes), 0)
N_components = 1

model_innovations = StateSpaceLearning.get_model_innovations(model)
model_innovations = get_model_innovations(model)
for innovation in model_innovations
simulation_X = hcat(
simulation_X,
StateSpaceLearning.get_innovation_simulation_X(model, innovation, steps_ahead)[
get_innovation_simulation_X(model, innovation, steps_ahead)[
(end - steps_ahead):(end - 1), (end - steps_ahead + 1):end
],
)
comp = StateSpaceLearning.fill_innovation_coefs(model, innovation, valid_indexes)
comp = fill_innovation_coefs(model, innovation, valid_indexes)
components_matrix = hcat(components_matrix, comp)
N_components += 1
end
Expand All @@ -242,7 +240,11 @@ function simulate(
end

if seasonal_innovation_simulation == 0
= cov(components_matrix)
= if is_univariate
Diagonal([var(components_matrix[:, i]) for i in 1:N_components])
else
Diagonal([var(components_matrix[:, i]) for i in 1:N_mv_components])
end
for i in 1:steps_ahead
MV_dist_vec[i] = if is_univariate
MvNormal(zeros(N_components), ∑)
Expand Down Expand Up @@ -270,12 +272,27 @@ function simulate(
end
else
start_seasonal_term = (size(components_matrix, 1) % seasonal_innovation_simulation)
for i in 1:steps_ahead
= cov(
components_matrix[
(i + start_seasonal_term):seasonal_innovation_simulation:end, :,
],
)
for i in 1:seasonal_innovation_simulation
= if is_univariate
Diagonal([
var(
components_matrix[
(i + start_seasonal_term):seasonal_innovation_simulation:end,
j,
],
) for j in 1:N_components
])
else
Diagonal([
var(
components_matrix[
(i + start_seasonal_term):seasonal_innovation_simulation:end,
j,
],
) for j in 1:N_mv_components
])
end

MV_dist_vec[i] = if is_univariate
MvNormal(zeros(N_components), ∑)
else
Expand Down Expand Up @@ -313,6 +330,20 @@ function simulate(
end
end
end
for i in (seasonal_innovation_simulation + 1):steps_ahead
MV_dist_vec[i] = MV_dist_vec[i - seasonal_innovation_simulation]
if model.outlier
if is_univariate
o_noises[i, :] = o_noises[i - seasonal_innovation_simulation, :]
else
for j in eachindex(model.output)
o_noises[j][i, :] = o_noises[j][
i - seasonal_innovation_simulation, :,
]
end
end
end
end
end

simulation = if is_univariate
Expand Down

0 comments on commit e3366b0

Please sign in to comment.