Skip to content

Commit 8935f7e

Browse files
Merge pull request #29 from LAMPSPUC/separate_fit_model_add_format
separate fit_model and add code format
2 parents 65acdd7 + 03e5b55 commit 8935f7e

32 files changed

+1794
-1369
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
.DS_Store
2-
Manifest.toml
2+
Manifest.toml
3+
4+
local_test/
5+
paper_tests/m4_test/metrics_results/
6+
paper_tests/m4_test/results_ARIMA/
7+
paper_tests/m4_test/results_SSL/
8+
paper_tests/simulation_test/results_simulation_raw/

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.3.0"
4+
version = "1.0.0"
55

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

README.md

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,31 @@ using StateSpaceLearning
1313

1414
y = randn(100)
1515

16-
#Fit Model
17-
output = StateSpaceLearning.fit_model(y)
18-
19-
#Main output options
20-
model_input = output.model_input # Model inputs that were utilized to build the regression matrix.
21-
Create_X = output.Create_X # The function utilized to build the regression matrix.
22-
X = output.X # High Dimension Regression utilized in the estimation.
23-
coefs = output.coefs # High Dimension Regression coefficients estimated in the estimation.
24-
ε = output.ε # Residuals of the model.
25-
fitted = output.fitted # Fit in Sample of the model.
26-
components = output.components # Dictionary containing information about each component of the model, each component has the keys: "Values" (The value of the component in each timestamp) , "Coefs" (The coefficients estimated for each element of the component) and "Indexes" (The indexes of the elements of the component in the high dimension regression "X").
27-
residuals_variances = output.residuals_variances # Dictionary containing the estimated variances for the innovations components (that is the information that can be utilized to initialize the state space model).
28-
valid_indexes = output.valid_indexes # Vector containing valid indexes of the time series (non valid indexes represent NaN values in the time series).
29-
30-
#Forecast
16+
# Instantiate Model
17+
model = StructuralModel(y)
18+
19+
# Fit Model
20+
fit!(model)
21+
22+
# Point Forecast
3123
prediction = StateSpaceLearning.forecast(output, 12) #Gets a 12 steps ahead prediction
3224

25+
# Scenarios Path Simulation
26+
simulation = StateSpaceLearning.simulate(output, 12, 1000) #Gets 1000 scenarios path of 12 steps ahead predictions
3327
```
3428

35-
## Fit Arguments
29+
## StructuralModel Arguments
3630

37-
* `y::Vector{Fl}`: Vector of data.
38-
* `model_input::Dict`: Dictionary containing the model input parameters (default: Dict("level" => true, "stochastic\_level" => true, "trend" => true, "stochastic\_trend" => true, "seasonal" => true, "stochastic\_seasonal" => true, "freq\_seasonal" => 12, "outlier" => true, , "ζ\_ω\_threshold" => 12)).
39-
* `model_functions::Dict`: Dictionary containing the model functions (default: Dict("create\_X" => create\_X, "get\_components\_indexes" => get\_components\_indexes, "get\_variances" => get\_variances)).
40-
* `estimation_input::Dict`: Dictionary containing the estimation input parameters (default: Dict("α" => 0.1, "information\_criteria" => "aic", ψ => 0.05, "penalize\_exogenous" => true, "penalize\_initial\_states" => true)).
41-
* `estimation_function::Function`: Estimation function (default: default\_estimation\_procedure).
42-
* `Exogenous_X::Union{Matrix{Fl}, Missing}`: Exogenous variables matrix (default: missing).
31+
* `y::Vector`: Vector of data.
32+
* `level::Bool`: Boolean where to consider intercept in the model (default: true)
33+
* `stochastic_level::Bool`: Boolean where to consider stochastic level component in the model (default: true)
34+
* `trend::Bool`: Boolean where to consider trend component in the model (default: true)
35+
* `stochastic_trend::Bool`: Boolean where to consider stochastic trend component in the model (default: true)
36+
* `seasonal::Bool`: Boolean where to consider seasonal component in the model (default: true)
37+
* `stochastic_seasonal::Bool`: Boolean where to consider stochastic seasonal component in the model (default: true)
38+
* `freq_seasonal::Int`: Seasonal frequency to be considered in the model (default: 12)
39+
* `outlier::Bool`: Boolean where to consider outlier component in the model (default: true)
40+
* `ζ_ω_threshold::Int`: Argument to stabilize `stochastic trend` and `stochastic seasonal` components (default: 12)
4341

4442
## Features
4543

@@ -66,8 +64,9 @@ airp = CSV.File(StateSpaceLearning.AIR_PASSENGERS) |> DataFrame
6664
log_air_passengers = log.(airp.passengers)
6765
steps_ahead = 30
6866

69-
output = StateSpaceLearning.fit_model(log_air_passengers)
70-
prediction_log = StateSpaceLearning.forecast(output, steps_ahead) # arguments are the output of the fitted model and number of steps ahead the user wants to forecast
67+
model = StructuralModel(log_air_passengers)
68+
fit!(model)
69+
prediction_log = StateSpaceLearning.forecast(model, steps_ahead) # arguments are the output of the fitted model and number of steps ahead the user wants to forecast
7170
prediction = exp.(prediction_log)
7271

7372
plot(airp.passengers, w=2 , color = "Black", lab = "Historical", legend = :outerbottom)
@@ -77,7 +76,7 @@ plot!(vcat(ones(length(log_air_passengers)).*NaN, prediction), lab = "Forecast",
7776

7877
```julia
7978
N_scenarios = 1000
80-
simulation = StateSpaceLearning.simulate(output, steps_ahead, N_scenarios) # arguments are the output of the fitted model, number of steps ahead the user wants to forecast and number of scenario paths
79+
simulation = StateSpaceLearning.simulate(model, steps_ahead, N_scenarios) # arguments are the output of the fitted model, number of steps ahead the user wants to forecast and number of scenario paths
8180

8281
plot(airp.passengers, w=2 , color = "Black", lab = "Historical", legend = :outerbottom)
8382
for s in 1:N_scenarios-1
@@ -99,11 +98,12 @@ using Plots
9998
airp = CSV.File(StateSpaceLearning.AIR_PASSENGERS) |> DataFrame
10099
log_air_passengers = log.(airp.passengers)
101100

102-
output = StateSpaceLearning.fit_model(log_air_passengers)
101+
model = StructuralModel(log_air_passengers)
102+
fit!(model)
103103

104-
level = output.components["μ1"]["Values"] + output.components["ξ"]["Values"]
105-
slope = output.components["ν1"]["Values"] + output.components["ζ"]["Values"]
106-
seasonal = output.components["γ1"]["Values"] + output.components["ω"]["Values"]
104+
level = model.output.components["μ1"]["Values"] + model.output.components["ξ"]["Values"]
105+
slope = model.output.components["ν1"]["Values"] + model.output.components["ζ"]["Values"]
106+
seasonal = model.output.components["γ1"]["Values"] + model.output.components["ω"]["Values"]
107107
trend = level + slope
108108

109109
plot(trend, w=2 , color = "Black", lab = "Trend Component", legend = :outerbottom)
@@ -133,9 +133,10 @@ X = rand(length(log_air_passengers), 10) # Create 10 exogenous features
133133

134134
y = log_air_passengers + X[:, 1:3]*β # add to the log_air_passengers series a contribution from only 3 exogenous features.
135135

136-
output = StateSpaceLearning.fit_model(y; Exogenous_X = X, estimation_input = Dict("α" => 1.0, "information_criteria" => "bic", "ε" => 0.05, "penalize_exogenous" => true, "penalize_initial_states" => true))
136+
model = StructuralModel(y; Exogenous_X = X)
137+
fit!(model; α = 1.0, information_criteria = "bic", ϵ = 0.05, penalize_exogenous = true, penalize_initial_states = true)
137138

138-
Selected_exogenous = output.components["Exogenous_X"]["Selected"]
139+
Selected_exogenous = model.output.components["Exogenous_X"]["Selected"]
139140

140141
```
141142

@@ -155,9 +156,10 @@ log_air_passengers = log.(airp.passengers)
155156
airpassengers = Float64.(airp.passengers)
156157
log_air_passengers[60:72] .= NaN
157158

158-
output = StateSpaceLearning.fit_model(log_air_passengers)
159+
model = StructuralModel(log_air_passengers)
160+
fit!(model)
159161

160-
fitted_completed_missing_values = ones(144).*NaN; fitted_completed_missing_values[60:72] = exp.(output.fitted[60:72])
162+
fitted_completed_missing_values = ones(144).*NaN; fitted_completed_missing_values[60:72] = exp.(model.output.fitted[60:72])
161163
real_removed_valued = ones(144).*NaN; real_removed_valued[60:72] = deepcopy(airp.passengers[60:72])
162164
airpassengers[60:72] .= NaN
163165

@@ -183,8 +185,10 @@ log_air_passengers[60] = 10
183185
log_air_passengers[30] = 1
184186
log_air_passengers[100] = 2
185187

186-
output = StateSpaceLearning.fit_model(log_air_passengers)
187-
detected_outliers = findall(i -> i != 0, output.components["o"]["Coefs"])
188+
model = StructuralModel(log_air_passengers)
189+
fit!(model)
190+
191+
detected_outliers = findall(i -> i != 0, model.output.components["o"]["Coefs"])
188192

189193
plot(log_air_passengers, w=2 , color = "Black", lab = "Historical", legend = :outerbottom)
190194
scatter!([detected_outliers], log_air_passengers[detected_outliers], lab = "Detected Outliers")
@@ -203,15 +207,17 @@ using StateSpaceModels
203207
airp = CSV.File(StateSpaceLearning.AIR_PASSENGERS) |> DataFrame
204208
log_air_passengers = log.(airp.passengers)
205209

206-
output = StateSpaceLearning.fit_model(log_air_passengers)
207-
residuals_variances = output.residuals_variances
210+
model = StructuralModel(log_air_passengers)
211+
fit!(model)
208212

209-
model = BasicStructural(log_air_passengers, 12)
210-
set_initial_hyperparameters!(model, Dict("sigma2_ε" => residuals_variances["ε"],
213+
residuals_variances = model.output.residuals_variances
214+
215+
ss_model = BasicStructural(log_air_passengers, 12)
216+
set_initial_hyperparameters!(ss_model, Dict("sigma2_ε" => residuals_variances["ε"],
211217
"sigma2_ξ" =>residuals_variances["ξ"],
212218
"sigma2_ζ" =>residuals_variances["ζ"],
213219
"sigma2_ω" =>residuals_variances["ω"]))
214-
fit!(model)
220+
StateSpaceModels.fit!(ss_model)
215221
```
216222

217223
## Paper Results Reproducibility

docs/make.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@ using Documenter
33
include("../src/StateSpaceLearning.jl")
44

55
# Set up to run docstrings with jldoctest
6-
DocMeta.setdocmeta!(
7-
StateSpaceLearning, :DocTestSetup, :(using StateSpaceLearning); recursive=true
8-
)
6+
DocMeta.setdocmeta!(StateSpaceLearning, :DocTestSetup, :(using StateSpaceLearning);
7+
recursive=true)
98

109
makedocs(;
11-
modules=[StateSpaceLearning],
12-
doctest=true,
13-
clean=true,
14-
checkdocs=:none,
15-
format=Documenter.HTML(mathengine=Documenter.MathJax2()),
16-
sitename="StateSpaceLearning.jl",
17-
authors="André Ramos",
18-
pages=[
19-
"Home" => "index.md", "manual.md",
20-
"adapting_package.md"
21-
],
22-
)
10+
modules=[StateSpaceLearning],
11+
doctest=true,
12+
clean=true,
13+
checkdocs=:none,
14+
format=Documenter.HTML(; mathengine=Documenter.MathJax2()),
15+
sitename="StateSpaceLearning.jl",
16+
authors="André Ramos",
17+
pages=["Home" => "index.md", "manual.md"],)
2318

24-
deploydocs(
25-
repo="github.com/LAMPSPUC/StateSpaceLearning.jl.git",
26-
push_preview = true
27-
)
19+
deploydocs(; repo="github.com/LAMPSPUC/StateSpaceLearning.jl.git",
20+
push_preview=true)

docs/src/adapting_package.md

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)