Skip to content

Commit

Permalink
Merge pull request #24 from LAMPSPUC/export_simulate
Browse files Browse the repository at this point in the history
Export simulate
  • Loading branch information
andreramosfdc authored Aug 26, 2024
2 parents 26b0417 + 33d551e commit 10e2fb8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 94 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 = "0.2.0"
version = "0.2.1"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
16 changes: 8 additions & 8 deletions src/StateSpaceLearning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include("datasets.jl")

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

export fit_model, forecast
export fit_model, forecast, simulate

"""
fit_model(y::Vector{Fl};
Expand Down Expand Up @@ -82,20 +82,20 @@ function fit_model(y::Vector{Fl};
end

"""
forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Union{Matrix{Fl}, Missing}=missing)::Vector{Float64} where Fl
forecast(output::Output, steps_ahead::Int; Exogenous_Forecast::Union{Matrix{Fl}, Missing}=missing)::Vector{Float64} where Fl
Returns the forecast for a given number of steps ahead using the provided StateSpaceLearning output and exogenous forecast data.
# Arguments
- `output::Output`: Output object obtained from model fitting.
- `steps_ahead::Int64`: Number of steps ahead for forecasting.
- `steps_ahead::Int`: Number of steps ahead for forecasting.
- `Exogenous_Forecast::Matrix{Fl}`: Exogenous variables forecast (default: zeros(steps_ahead, 0))
# Returns
- `Vector{Float64}`: Vector containing forecasted values.
"""
function forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Vector{Float64} where Fl
function forecast(output::Output, steps_ahead::Int; Exogenous_Forecast::Matrix{Fl}=zeros(steps_ahead, 0))::Vector{Float64} where Fl

@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"
@assert size(Exogenous_Forecast, 1) == steps_ahead "Exogenous_Forecast must have the same number of rows as steps_ahead"
Expand All @@ -107,21 +107,21 @@ function forecast(output::Output, steps_ahead::Int64; Exogenous_Forecast::Matrix
end

"""
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
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
Generate simulations for a given number of steps ahead using the provided StateSpaceLearning output and exogenous forecast data.
# Arguments
- `output::Output`: Output object obtained from model fitting.
- `steps_ahead::Int64`: Number of steps ahead for simulation.
- `N_scenarios::Int64`: Number of scenarios to simulate (default: 1000).
- `steps_ahead::Int`: Number of steps ahead for simulation.
- `N_scenarios::Int`: Number of scenarios to simulate (default: 1000).
- `simulate_outliers::Bool`: If true, simulate outliers (default: true).
- `Exogenous_Forecast::Matrix{Fl}`: Exogenous variables forecast (default: zeros(steps_ahead, 0))
# Returns
- `Matrix{Float64}`: Matrix containing simulated values.
"""
function simulate(output::Output, steps_ahead::Int64, N_scenarios::Int64; simulate_outliers::Bool = true,
function simulate(output::Output, steps_ahead::Int, N_scenarios::Int; simulate_outliers::Bool = true,
innovation_functions::Dict = Dict("stochastic_level" => Dict("create_X" => create_ξ, "component" => "ξ", "args" => (length(output.ε) + steps_ahead + 1, 0)),
"stochastic_trend" => Dict("create_X" => create_ζ, "component" => "ζ", "args" => (length(output.ε) + steps_ahead + 1, 0, 1)),
"stochastic_seasonal" => Dict("create_X" => create_ω, "component" => "ω", "args" => (length(output.ε) + steps_ahead + 1, output.model_input["freq_seasonal"], 0, 1))),
Expand Down
18 changes: 9 additions & 9 deletions src/estimation_procedure/default_estimation_procedure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ function get_dummy_indexes(Exogenous_X::Matrix{Fl}) where{Fl}
end

"""
get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int64}}) where{Tl}
get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int}}) where{Tl}
Identifies and returns the indexes of outlier columns that are duplicates of dummy variables in the exogenous matrix.
# Arguments
- `Estimation_X::Matrix{Tl}`: Matrix used for estimation.
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
# Returns
- `Vector{Int}`: Vector containing the indexes of outlier columns that are duplicates of dummy variables in the exogenous matrix.
"""
function get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int64}}) where{Tl}
function get_outlier_duplicate_columns(Estimation_X::Matrix{Tl}, components_indexes::Dict{String, Vector{Int}}) where{Tl}
if !haskey(components_indexes, "o")
return []
else
Expand Down Expand Up @@ -115,7 +115,7 @@ end

"""
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};
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}
Fits a Lasso regression model to the provided data and returns coefficients and residuals based on selected criteria.
Expand All @@ -126,15 +126,15 @@ end
- `α::Float64`: Elastic net control factor between ridge (α=0) and lasso (α=1) (default: 0.1).
- `information_criteria::String`: Information Criteria method for hyperparameter selection (default: aic).
- `penalize_exogenous::Bool`: Flag for selecting exogenous variables. When false the penalty factor for these variables will be set to 0.
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
- `penalty_factor::Vector{Float64}`: Penalty factors for each predictor.
- `rm_average::Bool`: Flag to consider if the intercept will be calculated is the average of the time series (default: false).
# Returns
- `Tuple{Vector{Float64}, Vector{Float64}}`: Tuple containing coefficients and residuals of the fitted Lasso model.
"""
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}
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}

outlier_duplicate_columns = get_outlier_duplicate_columns(Estimation_X, components_indexes)
penalty_factor[outlier_duplicate_columns] .= Inf
Expand Down Expand Up @@ -168,23 +168,23 @@ end
"""
fit_adalasso(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl}, α::Float64,
information_criteria::String,
components_indexes::Dict{String, Vector{Int64}},
components_indexes::Dict{String, Vector{Int}},
ε::Float64, penalize_exogenous::Bool)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
Fits an Adaptive Lasso (AdaLasso) regression model to the provided data and returns coefficients and residuals.
# Arguments
- `Estimation_X::Matrix{Tl}`: Matrix of predictors for estimation.
- `estimation_y::Vector{Fl}`: Vector of response values for estimation.
- `components_indexes::Dict{String, Vector{Int64}}`: Dictionary containing indexes for different components.
- `components_indexes::Dict{String, Vector{Int}}`: Dictionary containing indexes for different components.
- `estimation_input::Dict`: Dictionary containing the estimation input parameters.
# Returns
- `Tuple{Vector{Float64}, Vector{Float64}}`: Tuple containing coefficients and residuals of the fitted AdaLasso model.
"""
function default_estimation_procedure(Estimation_X::Matrix{Tl}, estimation_y::Vector{Fl},
components_indexes::Dict{String, Vector{Int64}}, estimation_input::Dict)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}
components_indexes::Dict{String, Vector{Int}}, estimation_input::Dict)::Tuple{Vector{Float64}, Vector{Float64}} where {Tl, Fl}

@assert all([key in keys(estimation_input) for key in ["α", "information_criteria", "ϵ", "penalize_exogenous", "penalize_initial_states"]]) "All estimation input parameters must be set"
α = estimation_input["α"]; information_criteria = estimation_input["information_criteria"];
Expand Down
11 changes: 5 additions & 6 deletions src/information_criteria.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"""
get_information(T::Int64, K::Int64, ε::Vector{Float64};
information_criteria::String = "bic", p::Int64 = 0)::Float64
get_information(T::Int, K::Int, ε::Vector{Float64};
information_criteria::String = "bic")::Float64
Calculates information criterion value based on the provided parameters and residuals.
# Arguments
- `T::Int64`: Number of observations.
- `K::Int64`: Number of selected predictors.
- `T::Int`: Number of observations.
- `K::Int`: Number of selected predictors.
- `ε::Vector{Float64}`: Vector of residuals.
- `information_criteria::String`: Method for hyperparameter selection (default: "aic").
- `p::Int64`: Number of total predictors (default: 0).
# Returns
- `Float64`: Information criterion value.
"""
function get_information(T::Int64, K::Int64, ε::Vector{Float64}; information_criteria::String = "aic")::Float64
function get_information(T::Int, K::Int, ε::Vector{Float64}; information_criteria::String = "aic")::Float64
if information_criteria == "bic"
return T*log(var(ε)) + K*log(T)
elseif information_criteria == "aic"
Expand Down
Loading

0 comments on commit 10e2fb8

Please sign in to comment.