Skip to content

Commit b1723e7

Browse files
authored
Merge pull request #279 from LAMPSPUC/auto_arima
Auto arima
2 parents a1cc1ab + 3ac04a4 commit b1723e7

22 files changed

+534
-40
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StateSpaceModels"
22
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
33
authors = ["raphaelsaavedra <[email protected]>, guilhermebodin <[email protected]>, mariohsouto"]
4-
version = "0.5.16"
4+
version = "0.5.17"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
@@ -12,6 +12,7 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1212
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
1313
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1414
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
15+
SeasonalTrendLoess = "42fb36cb-998a-4034-bf40-4eee476c43a1"
1516
ShiftedArrays = "1277b4bf-5013-50f5-be3d-901d8477a67a"
1617
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1718
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
@@ -23,6 +24,7 @@ Optim = "0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 1.2"
2324
OrderedCollections = "1"
2425
Polynomials = "1"
2526
RecipesBase = "1"
27+
SeasonalTrendLoess = "0.1"
2628
ShiftedArrays = "1"
2729
StatsBase = "0.27, 0.28, 0.29, 0.30, 0.31, 0.32, 0.33"
2830
julia = "1"

docs/src/manual.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ In any case if the documentation explains the procedures and indicates if there
7676

7777
```
7878
auto_ets
79+
auto_arima
7980
```
8081

8182
### Implementing a custom model

src/StateSpaceModels.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using Optim
1313
using OrderedCollections
1414
using RecipesBase
1515
using StatsBase
16+
using SeasonalTrendLoess
1617

1718
abstract type StateSpaceModel end
1819

@@ -21,7 +22,8 @@ include("datasets.jl")
2122
include("hyperparameters.jl")
2223
include("systems.jl")
2324
include("kalman_filter_and_smoother.jl")
24-
25+
include("statistical_tests/kpss.jl")
26+
include("statistical_tests/canova_hensen.jl")
2527
include("filters/univariate_kalman_filter.jl")
2628
include("filters/multivariate_kalman_filter.jl")
2729
include("filters/scalar_kalman_filter.jl")
@@ -81,6 +83,7 @@ export UnivariateKalmanFilter
8183
export UnobservedComponents
8284

8385
# Exported functions
86+
export auto_arima
8487
export auto_ets
8588
export cross_validation
8689
export constrain_box!

src/fit.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function fit!(
3030
filter::KalmanFilter=default_filter(model),
3131
optimizer::Optimizer=default_optimizer(model),
3232
)
33+
isfitted(model) && return model
3334
@assert has_fit_methods(typeof(model))
3435
initial_unconstrained_hyperparameter = handle_optim_initial_hyperparameters(model)
3536
# TODO Should there be a try catch?
@@ -144,26 +145,30 @@ function fill_results!(model::StateSpaceModel, llk::Fl, std_err::Vector{Fl}) whe
144145
model.results.model_name = model_name(model)
145146
model.results.coef_table = coef_table
146147
model.results.llk = llk
147-
model.results.aic = AIC(num_hyperparameters, llk)
148-
model.results.aicc = AICc(n_obs, num_hyperparameters, llk)
149-
model.results.bic = BIC(n_obs, num_hyperparameters, llk)
148+
model.results.aic = aic(num_hyperparameters, llk)
149+
model.results.aicc = aicc(n_obs, num_hyperparameters, llk)
150+
model.results.bic = bic(n_obs, num_hyperparameters, llk)
150151
model.results.num_observations = n_obs
151152
model.results.num_hyperparameters = num_hyperparameters
152153
return model
153154
end
154155

155-
function AIC(k::Int, llk::Fl) where Fl
156+
function aic(k::Int, llk::Fl) where Fl
156157
return convert(Fl, 2 * k - 2 * llk)
157158
end
158159

159-
function AICc(n::Int, k::Int, llk::Fl) where Fl
160-
return convert(Fl, AIC(k, llk) + (2 * k * (k + 1) / (n - k - 1)))
160+
function aicc(n::Int, k::Int, llk::Fl) where Fl
161+
return convert(Fl, aic(k, llk) + (2 * k * (k + 1) / (n - k - 1)))
161162
end
162163

163-
function BIC(n::Int, k::Int, llk::Fl) where Fl
164+
function bic(n::Int, k::Int, llk::Fl) where Fl
164165
return convert(Fl, log(n) * k - 2 * llk)
165166
end
166167

168+
aic(model::StateSpaceModel) = model.results.aic
169+
aicc(model::StateSpaceModel) = model.results.aicc
170+
bic(model::StateSpaceModel) = model.results.bic
171+
167172
function build_coef_table(model::StateSpaceModel, std_err::Vector{Fl}) where Fl
168173
all_coef = get_constrained_values(model)
169174
all_std_err = handle_std_err(model, std_err)

src/forecast.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function forecast(
3939
# Associate with the model hyperparameters
4040
forecasting_model.hyperparameters = model_hyperparameters
4141
# Perform the kalman filter
42-
fo = kalman_filter(forecasting_model)
42+
fo = kalman_filter(forecasting_model; filter = filter)
4343
# fill forecast matrices
4444
expected_value = Vector{Vector{Fl}}(undef, steps_ahead)
4545
covariance = Vector{Matrix{Fl}}(undef, steps_ahead)
@@ -82,7 +82,7 @@ function forecast(
8282
# Associate with the model hyperparameters
8383
forecasting_model.hyperparameters = model_hyperparameters
8484
# Perform the kalman filter
85-
fo = kalman_filter(forecasting_model)
85+
fo = kalman_filter(forecasting_model; filter = filter)
8686
# fill forecast matrices
8787
expected_value = Vector{Vector{Fl}}(undef, steps_ahead)
8888
covariance = Vector{Matrix{Fl}}(undef, steps_ahead)
@@ -119,7 +119,7 @@ function simulate_scenarios(
119119
)
120120
# Query the type of model elements
121121
Fl = typeof_model_elements(model)
122-
fo = kalman_filter(model)
122+
fo = kalman_filter(model; filter = filter)
123123
last_state = fo.a[end]
124124
num_series = size(model.system.y, 2)
125125

src/models/basicstructural.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ component. It is defined by:
1717
# Example
1818
```jldoctest
1919
julia> model = BasicStructural(rand(100), 12)
20-
BasicStructural model
20+
BasicStructural
2121
```
2222
2323
# References

src/models/basicstructural_explanatory.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It is defined by:
1717
# Example
1818
```jldoctest
1919
julia> model = BasicStructuralExplanatory(rand(100), 12, rand(100, 2))
20-
BasicStructuralExplanatory model
20+
BasicStructuralExplanatory
2121
```
2222
2323
# References

src/models/basicstructural_multivariate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ It is defined by:
1818
# Example
1919
```jldoctest
2020
julia> model = MultivariateBasicStructural(rand(100, 2), 12)
21-
MultivariateBasicStructural model
21+
MultivariateBasicStructural
2222
```
2323
2424
# References

src/models/dar.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ A Dynamic Autorregressive model is defined by:
2222
# Example
2323
```jldoctest
2424
julia> model = DAR(randn(100), 2)
25-
DAR model
25+
DAR
2626
```
2727
"""
2828
mutable struct DAR <: StateSpaceModel

src/models/exponential_smoothing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,6 @@ function auto_ets(y::Vector{Fl}; seasonal::Int = 0) where Fl
325325
fit!(m3)
326326
push!(models, m3)
327327
push!(models_aic, m3.results.aic)
328-
best_aic_idx = findmin(models_aic)[2] # index of the best BIC
328+
best_aic_idx = findmin(models_aic)[2] # index of the best bic
329329
return models[best_aic_idx]
330330
end

0 commit comments

Comments
 (0)