Skip to content

Commit

Permalink
Merge pull request #270 from LAMPSPUC/naive_models
Browse files Browse the repository at this point in the history
Naive models
  • Loading branch information
guilhermebodin authored May 12, 2021
2 parents 809189e + 8c671ec commit 4abe75b
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StateSpaceModels"
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
authors = ["raphaelsaavedra <[email protected]>, guilhermebodin <[email protected]>, mariohsouto"]
version = "0.5.15"
version = "0.5.16"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
288 changes: 144 additions & 144 deletions datasets/airpassengers.csv
Original file line number Diff line number Diff line change
@@ -1,145 +1,145 @@
month,passengers
1949-01,112
1949-02,118
1949-03,132
1949-04,129
1949-05,121
1949-06,135
1949-07,148
1949-08,148
1949-09,136
1949-10,119
1949-11,104
1949-12,118
1950-01,115
1950-02,126
1950-03,141
1950-04,135
1950-05,125
1950-06,149
1950-07,170
1950-08,170
1950-09,158
1950-10,133
1950-11,114
1950-12,140
1951-01,145
1951-02,150
1951-03,178
1951-04,163
1951-05,172
1951-06,178
1951-07,199
1951-08,199
1951-09,184
1951-10,162
1951-11,146
1951-12,166
1952-01,171
1952-02,180
1952-03,193
1952-04,181
1952-05,183
1952-06,218
1952-07,230
1952-08,242
1952-09,209
1952-10,191
1952-11,172
1952-12,194
1953-01,196
1953-02,196
1953-03,236
1953-04,235
1953-05,229
1953-06,243
1953-07,264
1953-08,272
1953-09,237
1953-10,211
1953-11,180
1953-12,201
1954-01,204
1954-02,188
1954-03,235
1954-04,227
1954-05,234
1954-06,264
1954-07,302
1954-08,293
1954-09,259
1954-10,229
1954-11,203
1954-12,229
1955-01,242
1955-02,233
1955-03,267
1955-04,269
1955-05,270
1955-06,315
1955-07,364
1955-08,347
1955-09,312
1955-10,274
1955-11,237
1955-12,278
1956-01,284
1956-02,277
1956-03,317
1956-04,313
1956-05,318
1956-06,374
1956-07,413
1956-08,405
1956-09,355
1956-10,306
1956-11,271
1956-12,306
1957-01,315
1957-02,301
1957-03,356
1957-04,348
1957-05,355
1957-06,422
1957-07,465
1957-08,467
1957-09,404
1957-10,347
1957-11,305
1957-12,336
1958-01,340
1958-02,318
1958-03,362
1958-04,348
1958-05,363
1958-06,435
1958-07,491
1958-08,505
1958-09,404
1958-10,359
1958-11,310
1958-12,337
1959-01,360
1959-02,342
1959-03,406
1959-04,396
1959-05,420
1959-06,472
1959-07,548
1959-08,559
1959-09,463
1959-10,407
1959-11,362
1959-12,405
1960-01,417
1960-02,391
1960-03,419
1960-04,461
1960-05,472
1960-06,535
1960-07,622
1960-08,606
1960-09,508
1960-10,461
1960-11,390
1960-12,432
1949-01-01,112
1949-02-01,118
1949-03-01,132
1949-04-01,129
1949-05-01,121
1949-06-01,135
1949-07-01,148
1949-08-01,148
1949-09-01,136
1949-10-01,119
1949-11-01,104
1949-12-01,118
1950-01-01,115
1950-02-01,126
1950-03-01,141
1950-04-01,135
1950-05-01,125
1950-06-01,149
1950-07-01,170
1950-08-01,170
1950-09-01,158
1950-10-01,133
1950-11-01,114
1950-12-01,140
1951-01-01,145
1951-02-01,150
1951-03-01,178
1951-04-01,163
1951-05-01,172
1951-06-01,178
1951-07-01,199
1951-08-01,199
1951-09-01,184
1951-10-01,162
1951-11-01,146
1951-12-01,166
1952-01-01,171
1952-02-01,180
1952-03-01,193
1952-04-01,181
1952-05-01,183
1952-06-01,218
1952-07-01,230
1952-08-01,242
1952-09-01,209
1952-10-01,191
1952-11-01,172
1952-12-01,194
1953-01-01,196
1953-02-01,196
1953-03-01,236
1953-04-01,235
1953-05-01,229
1953-06-01,243
1953-07-01,264
1953-08-01,272
1953-09-01,237
1953-10-01,211
1953-11-01,180
1953-12-01,201
1954-01-01,204
1954-02-01,188
1954-03-01,235
1954-04-01,227
1954-05-01,234
1954-06-01,264
1954-07-01,302
1954-08-01,293
1954-09-01,259
1954-10-01,229
1954-11-01,203
1954-12-01,229
1955-01-01,242
1955-02-01,233
1955-03-01,267
1955-04-01,269
1955-05-01,270
1955-06-01,315
1955-07-01,364
1955-08-01,347
1955-09-01,312
1955-10-01,274
1955-11-01,237
1955-12-01,278
1956-01-01,284
1956-02-01,277
1956-03-01,317
1956-04-01,313
1956-05-01,318
1956-06-01,374
1956-07-01,413
1956-08-01,405
1956-09-01,355
1956-10-01,306
1956-11-01,271
1956-12-01,306
1957-01-01,315
1957-02-01,301
1957-03-01,356
1957-04-01,348
1957-05-01,355
1957-06-01,422
1957-07-01,465
1957-08-01,467
1957-09-01,404
1957-10-01,347
1957-11-01,305
1957-12-01,336
1958-01-01,340
1958-02-01,318
1958-03-01,362
1958-04-01,348
1958-05-01,363
1958-06-01,435
1958-07-01,491
1958-08-01,505
1958-09-01,404
1958-10-01,359
1958-11-01,310
1958-12-01,337
1959-01-01,360
1959-02-01,342
1959-03-01,406
1959-04-01,396
1959-05-01,420
1959-06-01,472
1959-07-01,548
1959-08-01,559
1959-09-01,463
1959-10-01,407
1959-11-01,362
1959-12-01,405
1960-01-01,417
1960-02-01,391
1960-03-01,419
1960-04-01,461
1960-05-01,472
1960-06-01,535
1960-07-01,622
1960-08-01,606
1960-09-01,508
1960-10-01,461
1960-11-01,390
1960-12-01,432
10 changes: 10 additions & 0 deletions docs/src/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ LocalLinearTrend
MultivariateBasicStructural
```

## Naive models

Naive models are not state space models but are good benchmarks for forecasting, for this reason we implemented them here.

```@docs
Naive
SeasonalNaive
ExperimentalSeasonalNaive
```

## Automatic forecasting

Some models have various parameters and modelling options. The package provides simple functions that
Expand Down
4 changes: 4 additions & 0 deletions src/StateSpaceModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ include("models/sarima.jl")
include("models/linear_regression.jl")
include("models/unobserved_components.jl")
include("models/exponential_smoothing.jl")
include("models/naive_models.jl")
include("models/dar.jl")

include("visualization/forecast.jl")
Expand All @@ -56,6 +57,7 @@ include("visualization/diagnostics.jl")

# Exported types and structs
export BasicStructural
export ExperimentalSeasonalNaive
export BasicStructuralExplanatory
export DAR
export ExponentialSmoothing
Expand All @@ -69,9 +71,11 @@ export LocalLevelCycle
export LocalLevelExplanatory
export LocalLinearTrend
export MultivariateBasicStructural
export Naive
export Optimizer
export SARIMA
export ScalarKalmanFilter
export SeasonalNaive
export StateSpaceModel
export UnivariateKalmanFilter
export UnobservedComponents
Expand Down
8 changes: 8 additions & 0 deletions src/kalman_filter_and_smoother.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ function assert_possible_to_filter(model::StateSpaceModel)
error("Model has not been estimated yet, please use `fit!`.")
end

get_standard_residuals(fo::FilterOutput) = get_standard_innovations(fo)
function get_standard_innovations(fo::FilterOutput)
v = get_innovations(fo)
F = get_innovations_variance(fo)
@assert size(v, 2) == 1 # Must be univariate
return v[:, 1] ./ sqrt.(F[1, 1, :])
end

"""
get_innovations
Expand Down
4 changes: 3 additions & 1 deletion src/models/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ num_series(model::StateSpaceModel) = num_series(system(model))
system(model::StateSpaceModel) = model.system
isunivariate(model::StateSpaceModel) = isa(model.system.y, Vector)
model_name(model::StateSpaceModel) = "$(typeof(model))"
length_observations(model::StateSpaceModel) = length(model.system.y)
observations(model::StateSpaceModel) = model.system.y

function lagmat(y::Vector{Fl}, k::Int) where Fl
X = Matrix{Fl}(undef, length(y) - k, k)
for i in 1:k
X[:, i] = lag(y, i)[k + 1:end]
end
return X
end
end
Loading

0 comments on commit 4abe75b

Please sign in to comment.