Skip to content

Commit 10e2fb8

Browse files
Merge pull request #24 from LAMPSPUC/export_simulate
Export simulate
2 parents 26b0417 + 33d551e commit 10e2fb8

File tree

7 files changed

+93
-94
lines changed

7 files changed

+93
-94
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 = "0.2.0"
4+
version = "0.2.1"
55

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

src/StateSpaceLearning.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include("datasets.jl")
1111

1212
const DEFAULT_COMPONENTS_PARAMETERS = ["level", "stochastic_level", "trend", "stochastic_trend", "seasonal", "stochastic_seasonal", "freq_seasonal"]
1313

14-
export fit_model, forecast
14+
export fit_model, forecast, simulate
1515

1616
"""
1717
fit_model(y::Vector{Fl};
@@ -82,20 +82,20 @@ function fit_model(y::Vector{Fl};
8282
end
8383

8484
"""
85-
forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Union{Matrix{Fl}, Missing}=missing)::Vector{Float64} where Fl
85+
forecast(output::Output, steps_ahead::Int; Exogenous_Forecast::Union{Matrix{Fl}, Missing}=missing)::Vector{Float64} where Fl
8686
8787
Returns the forecast for a given number of steps ahead using the provided StateSpaceLearning output and exogenous forecast data.
8888
8989
# Arguments
9090
- `output::Output`: Output object obtained from model fitting.
91-
- `steps_ahead::Int64`: Number of steps ahead for forecasting.
91+
- `steps_ahead::Int`: Number of steps ahead for forecasting.
9292
- `Exogenous_Forecast::Matrix{Fl}`: Exogenous variables forecast (default: zeros(steps_ahead, 0))
9393
9494
# Returns
9595
- `Vector{Float64}`: Vector containing forecasted values.
9696
9797
"""
98-
function forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Vector{Float64} where Fl
98+
function forecast(output::Output, steps_ahead::Int; Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Vector{Float64} where Fl
9999

100100
@assert length(output.components["Exogenous_X"]["Indexes"]) == size(Exogenous_Forecast, 2) "If an exogenous matrix was utilized in the estimation procedure, it must be provided its prediction for the forecast procedure. If no exogenous matrix was utilized, Exogenous_Forecast must be missing"
101101
@assert size(Exogenous_Forecast, 1) == steps_ahead "Exogenous_Forecast must have the same number of rows as steps_ahead"
@@ -107,21 +107,21 @@ function forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Matrix
107107
end
108108

109109
"""
110-
simulate(output::Output, steps_ahead::Int64; N_scenarios::Int64 = 1000, simulate_outliers::Bool = true, Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Matrix{Float64} where Fl
110+
simulate(output::Output, steps_ahead::Int; N_scenarios::Int = 1000, simulate_outliers::Bool = true, Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Matrix{Float64} where Fl
111111
112112
Generate simulations for a given number of steps ahead using the provided StateSpaceLearning output and exogenous forecast data.
113113
114114
# Arguments
115115
- `output::Output`: Output object obtained from model fitting.
116-
- `steps_ahead::Int64`: Number of steps ahead for simulation.
117-
- `N_scenarios::Int64`: Number of scenarios to simulate (default: 1000).
116+
- `steps_ahead::Int`: Number of steps ahead for simulation.
117+
- `N_scenarios::Int`: Number of scenarios to simulate (default: 1000).
118118
- `simulate_outliers::Bool`: If true, simulate outliers (default: true).
119119
- `Exogenous_Forecast::Matrix{Fl}`: Exogenous variables forecast (default: zeros(steps_ahead, 0))
120120
121121
# Returns
122122
- `Matrix{Float64}`: Matrix containing simulated values.
123123
"""
124-
function simulate(output::Output, steps_ahead::Int64, N_scenarios::Int64; simulate_outliers::Bool = true,
124+
function simulate(output::Output, steps_ahead::Int, N_scenarios::Int; simulate_outliers::Bool = true,
125125
innovation_functions::Dict = Dict("stochastic_level" => Dict("create_X" => create_ξ, "component" => "ξ", "args" => (length(output.ε) + steps_ahead + 1, 0)),
126126
"stochastic_trend" => Dict("create_X" => create_ζ, "component" => "ζ", "args" => (length(output.ε) + steps_ahead + 1, 0, 1)),
127127
"stochastic_seasonal" => Dict("create_X" => create_ω, "component" => "ω", "args" => (length(output.ε) + steps_ahead + 1, output.model_input["freq_seasonal"], 0, 1))),

src/estimation_procedure/default_estimation_procedure.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ function get_dummy_indexes(Exogenous_X::Matrix{Fl}) where{Fl}
2525
end
2626

2727
"""
28-
get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int64}}) where{Tl}
28+
get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int}}) where{Tl}
2929
3030
Identifies and returns the indexes of outlier columns that are duplicates of dummy variables in the exogenous matrix.
3131
3232
# Arguments
3333
- `Estimation_X::Matrix{Tl}`: Matrix used for estimation.
34-
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
34+
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
3535
3636
# Returns
3737
- `Vector{Int}`: Vector containing the indexes of outlier columns that are duplicates of dummy variables in the exogenous matrix.
3838
3939
"""
40-
function get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int64}}) where{Tl}
40+
function get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int}}) where{Tl}
4141
if !haskey(components_indexes, "o")
4242
return []
4343
else
@@ -115,7 +115,7 @@ end
115115

116116
"""
117117
fit_lasso(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl}, α::Float64, information_criteria::String,
118-
penalize_exogenous::Bool, components_indexes::Dict{String, Vector{Int64}}, penalty_factor::Vector{Float64};
118+
penalize_exogenous::Bool, components_indexes::Dict{String, Vector{Int}}, penalty_factor::Vector{Float64};
119119
rm_average::Bool = false)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
120120
121121
Fits a Lasso regression model to the provided data and returns coefficients and residuals based on selected criteria.
@@ -126,15 +126,15 @@ end
126126
- `α::Float64`: Elastic net control factor between ridge (α=0) and lasso (α=1) (default: 0.1).
127127
- `information_criteria::String`: Information Criteria method for hyperparameter selection (default: aic).
128128
- `penalize_exogenous::Bool`: Flag for selecting exogenous variables. When false the penalty factor for these variables will be set to 0.
129-
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
129+
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
130130
- `penalty_factor::Vector{Float64}`: Penalty factors for each predictor.
131131
- `rm_average::Bool`: Flag to consider if the intercept will be calculated is the average of the time series (default: false).
132132
133133
# Returns
134134
- `Tuple{Vector{Float64}, Vector{Float64}}`: Tuple containing coefficients and residuals of the fitted Lasso model.
135135
136136
"""
137-
function fit_lasso(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl}, α::Float64, information_criteria::String, penalize_exogenous::Bool, components_indexes::Dict{String, Vector{Int64}}, penalty_factor::Vector{Float64}; rm_average::Bool = false)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
137+
function fit_lasso(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl}, α::Float64, information_criteria::String, penalize_exogenous::Bool, components_indexes::Dict{String, Vector{Int}}, penalty_factor::Vector{Float64}; rm_average::Bool = false)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
138138

139139
outlier_duplicate_columns = get_outlier_duplicate_columns(Estimation_X, components_indexes)
140140
penalty_factor[outlier_duplicate_columns] .= Inf
@@ -168,23 +168,23 @@ end
168168
"""
169169
fit_adalasso(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl}, α::Float64,
170170
information_criteria::String,
171-
components_indexes::Dict{String, Vector{Int64}},
171+
components_indexes::Dict{String, Vector{Int}},
172172
ε::Float64, penalize_exogenous::Bool)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
173173
174174
Fits an Adaptive Lasso (AdaLasso) regression model to the provided data and returns coefficients and residuals.
175175
176176
# Arguments
177177
- `Estimation_X::Matrix{Tl}`: Matrix of predictors for estimation.
178178
- `estimation_y::Vector{Fl}`: Vector of response values for estimation.
179-
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
179+
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
180180
- `estimation_input::Dict`: Dictionary containing the estimation input parameters.
181181
182182
# Returns
183183
- `Tuple{Vector{Float64}, Vector{Float64}}`: Tuple containing coefficients and residuals of the fitted AdaLasso model.
184184
185185
"""
186186
function default_estimation_procedure(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl},
187-
components_indexes::Dict{String, Vector{Int64}}, estimation_input::Dict)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
187+
components_indexes::Dict{String, Vector{Int}}, estimation_input::Dict)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
188188

189189
@assert all([key in keys(estimation_input) for key in ["α", "information_criteria", "ϵ", "penalize_exogenous", "penalize_initial_states"]]) "All estimation input parameters must be set"
190190
α = estimation_input["α"]; information_criteria = estimation_input["information_criteria"];

src/information_criteria.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
"""
2-
get_information(T::Int64, K::Int64, ε::Vector{Float64};
3-
information_criteria::String = "bic", p::Int64 = 0)::Float64
2+
get_information(T::Int, K::Int, ε::Vector{Float64};
3+
information_criteria::String = "bic")::Float64
44
55
Calculates information criterion value based on the provided parameters and residuals.
66
77
# Arguments
8-
- `T::Int64`: Number of observations.
9-
- `K::Int64`: Number of selected predictors.
8+
- `T::Int`: Number of observations.
9+
- `K::Int`: Number of selected predictors.
1010
- `ε::Vector{Float64}`: Vector of residuals.
1111
- `information_criteria::String`: Method for hyperparameter selection (default: "aic").
12-
- `p::Int64`: Number of total predictors (default: 0).
1312
1413
# Returns
1514
- `Float64`: Information criterion value.
1615
1716
"""
18-
function get_information(T::Int64, K::Int64, ε::Vector{Float64}; information_criteria::String = "aic")::Float64
17+
function get_information(T::Int, K::Int, ε::Vector{Float64}; information_criteria::String = "aic")::Float64
1918
if information_criteria == "bic"
2019
return T*log(var(ε)) + K*log(T)
2120
elseif information_criteria == "aic"

0 commit comments

Comments
 (0)