Skip to content

Commit 33d551e

Browse files
author
andre_ramos
committed
change to int
1 parent 6dcd899 commit 33d551e

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

src/StateSpaceLearning.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

src/structs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
- `fitted::Vector`: Fitted values from the model.
1313
- `components::Dict`: Dictionary containing different components.
1414
- `residuals_variances::Dict`: Dictionary storing variances of residuals for different components.
15-
- `T::Int64`: Integer representing a parameter 'T'.
15+
- `T::Int`: Integer representing a parameter 'T'.
1616
- `outlier::Bool`: Boolean indicating the presence of outlier component.
17-
- `valid_indexes::Vector{Int64}`: Vector containing valid indexes (non NaN) of the time series.
18-
- `ζ_ω_threshold::Int64`: ζ_ω_threshold parameter.
17+
- `valid_indexes::Vector{Int}`: Vector containing valid indexes (non NaN) of the time series.
18+
- `ζ_ω_threshold::Int`: ζ_ω_threshold parameter.
1919
- `y::Vector{Fl}`: Vector of data.
2020
2121
"""
@@ -28,5 +28,5 @@ mutable struct Output
2828
fitted::Vector
2929
components::Dict
3030
residuals_variances::Dict
31-
valid_indexes::Vector{Int64}
31+
valid_indexes::Vector{Int}
3232
end

src/utils.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
2-
build_components(X::Matrix{Tl}, coefs::Vector{Float64}, components_indexes::Dict{String, Vector{Int64}}) -> Dict where Tl
2+
build_components(X::Matrix{Tl}, coefs::Vector{Float64}, components_indexes::Dict{String, Vector{Int}}) -> Dict where Tl
33
44
Constructs components dict containing values, indexes and coefficients for each component.
55
66
# Arguments
77
- X::Matrix{Tl}: Input matrix.
88
- coefs::Vector{Float64}: Coefficients.
9-
- components_indexes::Dict{String, Vector{Int64}}: Dictionary mapping component names to their indexes.
9+
- components_indexes::Dict{String, Vector{Int}}: Dictionary mapping component names to their indexes.
1010
1111
# Returns
1212
- components::Dict: Dictionary containing components, each represented by a dictionary with keys:
@@ -15,7 +15,7 @@
1515
- "Values": Values computed from `X` and component coefficients.
1616
1717
"""
18-
function build_components(X::Matrix{Tl}, coefs::Vector{Float64}, components_indexes::Dict{String, Vector{Int64}})::Dict where Tl
18+
function build_components(X::Matrix{Tl}, coefs::Vector{Float64}, components_indexes::Dict{String, Vector{Int}})::Dict where Tl
1919
components = Dict()
2020
for key in keys(components_indexes)
2121
components[key] = Dict()
@@ -30,57 +30,57 @@ function build_components(X::Matrix{Tl}, coefs::Vector{Float64}, components_inde
3030
end
3131

3232
"""
33-
get_fit_and_residuals(estimation_ε::Vector{Float64}, coefs::Vector{Float64}, X::Matrix{Tl}, valid_indexes::Vector{Int64}, T::Int64) -> Tuple{Vector{Float64}, Vector{Float64}} where Tl
33+
get_fit_and_residuals(estimation_ε::Vector{Float64}, coefs::Vector{Float64}, X::Matrix{Tl}, valid_indexes::Vector{Int}, T::Int) -> Tuple{Vector{Float64}, Vector{Float64}} where Tl
3434
3535
Builds complete residuals and fit in sample. Residuals will contain nan values for non valid indexes. Fit in Sample will be a vector of fitted values computed from input data and coefficients (non valid indexes will also be calculated via interpolation).
3636
3737
# Arguments
3838
- `estimation_ε::Vector{Float64}`: Vector of estimation errors.
3939
- `coefs::Vector{Float64}`: Coefficients.
4040
- `X::Matrix{Tl}`: Input matrix.
41-
- `valid_indexes::Vector{Int64}`: Valid indexes.
42-
- `T::Int64`: Length of the original time series.
41+
- `valid_indexes::Vector{Int}`: Valid indexes.
42+
- `T::Int`: Length of the original time series.
4343
4444
# Returns
4545
- Tuple containing:
4646
- `ε::Vector{Float64}`: Vector containing NaN values filled with estimation errors at valid indexes.
4747
- `fitted::Vector{Float64}`: Vector of fitted values computed from input data and coefficients.
4848
4949
"""
50-
function get_fit_and_residuals(estimation_ε::Vector{Float64}, coefs::Vector{Float64}, X::Matrix{Tl}, valid_indexes::Vector{Int64}, T::Int64)::Tuple{Vector{Float64}, Vector{Float64}} where Tl
50+
function get_fit_and_residuals(estimation_ε::Vector{Float64}, coefs::Vector{Float64}, X::Matrix{Tl}, valid_indexes::Vector{Int}, T::Int)::Tuple{Vector{Float64}, Vector{Float64}} where Tl
5151
ε = fill(NaN, T); ε[valid_indexes] = estimation_ε
5252
fitted = X*coefs
5353
return ε, fitted
5454
end
5555

5656
"""
57-
o_size(T::Int64)::Int64
57+
o_size(T::Int)::Int
5858
5959
Calculates the size of outlier matrix based on the input T.
6060
6161
# Arguments
62-
- `T::Int64`: Length of the original time series.
62+
- `T::Int`: Length of the original time series.
6363
6464
# Returns
65-
- `Int64`: Size of o calculated from T.
65+
- `Int`: Size of o calculated from T.
6666
6767
"""
68-
o_size(T::Int64)::Int64 = T
68+
o_size(T::Int)::Int = T
6969

7070
"""
71-
create_o_matrix(T::Int64, steps_ahead::Int64)::Matrix
71+
create_o_matrix(T::Int, steps_ahead::Int)::Matrix
7272
7373
Creates a matrix of outliers based on the input sizes, and the desired steps ahead (this is necessary for the forecast function).
7474
7575
# Arguments
76-
- `T::Int64`: Length of the original time series.
77-
- `steps_ahead::Int64`: Number of steps ahead (for estimation purposes this should be set at 0).
76+
- `T::Int`: Length of the original time series.
77+
- `steps_ahead::Int`: Number of steps ahead (for estimation purposes this should be set at 0).
7878
7979
# Returns
8080
- `Matrix`: Matrix of outliers constructed based on the input sizes.
8181
8282
"""
83-
function create_o_matrix(T::Int64, steps_ahead::Int64)::Matrix
83+
function create_o_matrix(T::Int, steps_ahead::Int)::Matrix
8484
return vcat(Matrix(1.0 * I, T, T), zeros(steps_ahead, T))
8585
end
8686

@@ -97,9 +97,9 @@ handle_missing_values(X::Matrix{Tl}, y::Vector{Fl}) -> Tuple{Vector{Fl}, Matrix{
9797
- Tuple containing:
9898
- `y::Vector{Fl}`: Time series without missing values.
9999
- `X::Matrix{Tl}`: Input matrix without missing values.
100-
- `valid_indexes::Vector{Int64}`: Vector containing valid indexes of the time series.
100+
- `valid_indexes::Vector{Int}`: Vector containing valid indexes of the time series.
101101
"""
102-
function handle_missing_values(X::Matrix{Tl}, y::Vector{Fl})::Tuple{Vector{Fl}, Matrix{Tl}, Vector{Int64}} where {Tl, Fl}
102+
function handle_missing_values(X::Matrix{Tl}, y::Vector{Fl})::Tuple{Vector{Fl}, Matrix{Tl}, Vector{Int}} where {Tl, Fl}
103103

104104
invalid_indexes = unique(vcat([i[1] for i in findall(i -> any(isnan, i), X)], findall(i -> isnan(i), y)))
105105
valid_indexes = setdiff(1:length(y), invalid_indexes)
@@ -134,7 +134,7 @@ fill_innovation_coefs(component::String, output::Output)::Vector{Float64}
134134
# Returns
135135
- `Vector{Float64}`: Vector containing innovation coefficients for the given component.
136136
"""
137-
function fill_innovation_coefs(T::Int64, component::String, output::Output)::Vector{Float64}
137+
function fill_innovation_coefs(T::Int, component::String, output::Output)::Vector{Float64}
138138
inov_comp = zeros(T)
139139
for (i, idx) in enumerate(output.components[component]["Indexes"])
140140
inov_comp[findfirst(i -> i != 0, output.X[:, idx])] = output.components[component]["Coefs"][i]

0 commit comments

Comments
 (0)