Skip to content

Commit 4abe75b

Browse files
Merge pull request #270 from LAMPSPUC/naive_models
Naive models
2 parents 809189e + 8c671ec commit 4abe75b

File tree

11 files changed

+461
-157
lines changed

11 files changed

+461
-157
lines changed

Project.toml

Lines changed: 1 addition & 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.15"
4+
version = "0.5.16"
55

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

datasets/airpassengers.csv

Lines changed: 144 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,145 @@
11
month,passengers
2-
1949-01,112
3-
1949-02,118
4-
1949-03,132
5-
1949-04,129
6-
1949-05,121
7-
1949-06,135
8-
1949-07,148
9-
1949-08,148
10-
1949-09,136
11-
1949-10,119
12-
1949-11,104
13-
1949-12,118
14-
1950-01,115
15-
1950-02,126
16-
1950-03,141
17-
1950-04,135
18-
1950-05,125
19-
1950-06,149
20-
1950-07,170
21-
1950-08,170
22-
1950-09,158
23-
1950-10,133
24-
1950-11,114
25-
1950-12,140
26-
1951-01,145
27-
1951-02,150
28-
1951-03,178
29-
1951-04,163
30-
1951-05,172
31-
1951-06,178
32-
1951-07,199
33-
1951-08,199
34-
1951-09,184
35-
1951-10,162
36-
1951-11,146
37-
1951-12,166
38-
1952-01,171
39-
1952-02,180
40-
1952-03,193
41-
1952-04,181
42-
1952-05,183
43-
1952-06,218
44-
1952-07,230
45-
1952-08,242
46-
1952-09,209
47-
1952-10,191
48-
1952-11,172
49-
1952-12,194
50-
1953-01,196
51-
1953-02,196
52-
1953-03,236
53-
1953-04,235
54-
1953-05,229
55-
1953-06,243
56-
1953-07,264
57-
1953-08,272
58-
1953-09,237
59-
1953-10,211
60-
1953-11,180
61-
1953-12,201
62-
1954-01,204
63-
1954-02,188
64-
1954-03,235
65-
1954-04,227
66-
1954-05,234
67-
1954-06,264
68-
1954-07,302
69-
1954-08,293
70-
1954-09,259
71-
1954-10,229
72-
1954-11,203
73-
1954-12,229
74-
1955-01,242
75-
1955-02,233
76-
1955-03,267
77-
1955-04,269
78-
1955-05,270
79-
1955-06,315
80-
1955-07,364
81-
1955-08,347
82-
1955-09,312
83-
1955-10,274
84-
1955-11,237
85-
1955-12,278
86-
1956-01,284
87-
1956-02,277
88-
1956-03,317
89-
1956-04,313
90-
1956-05,318
91-
1956-06,374
92-
1956-07,413
93-
1956-08,405
94-
1956-09,355
95-
1956-10,306
96-
1956-11,271
97-
1956-12,306
98-
1957-01,315
99-
1957-02,301
100-
1957-03,356
101-
1957-04,348
102-
1957-05,355
103-
1957-06,422
104-
1957-07,465
105-
1957-08,467
106-
1957-09,404
107-
1957-10,347
108-
1957-11,305
109-
1957-12,336
110-
1958-01,340
111-
1958-02,318
112-
1958-03,362
113-
1958-04,348
114-
1958-05,363
115-
1958-06,435
116-
1958-07,491
117-
1958-08,505
118-
1958-09,404
119-
1958-10,359
120-
1958-11,310
121-
1958-12,337
122-
1959-01,360
123-
1959-02,342
124-
1959-03,406
125-
1959-04,396
126-
1959-05,420
127-
1959-06,472
128-
1959-07,548
129-
1959-08,559
130-
1959-09,463
131-
1959-10,407
132-
1959-11,362
133-
1959-12,405
134-
1960-01,417
135-
1960-02,391
136-
1960-03,419
137-
1960-04,461
138-
1960-05,472
139-
1960-06,535
140-
1960-07,622
141-
1960-08,606
142-
1960-09,508
143-
1960-10,461
144-
1960-11,390
145-
1960-12,432
2+
1949-01-01,112
3+
1949-02-01,118
4+
1949-03-01,132
5+
1949-04-01,129
6+
1949-05-01,121
7+
1949-06-01,135
8+
1949-07-01,148
9+
1949-08-01,148
10+
1949-09-01,136
11+
1949-10-01,119
12+
1949-11-01,104
13+
1949-12-01,118
14+
1950-01-01,115
15+
1950-02-01,126
16+
1950-03-01,141
17+
1950-04-01,135
18+
1950-05-01,125
19+
1950-06-01,149
20+
1950-07-01,170
21+
1950-08-01,170
22+
1950-09-01,158
23+
1950-10-01,133
24+
1950-11-01,114
25+
1950-12-01,140
26+
1951-01-01,145
27+
1951-02-01,150
28+
1951-03-01,178
29+
1951-04-01,163
30+
1951-05-01,172
31+
1951-06-01,178
32+
1951-07-01,199
33+
1951-08-01,199
34+
1951-09-01,184
35+
1951-10-01,162
36+
1951-11-01,146
37+
1951-12-01,166
38+
1952-01-01,171
39+
1952-02-01,180
40+
1952-03-01,193
41+
1952-04-01,181
42+
1952-05-01,183
43+
1952-06-01,218
44+
1952-07-01,230
45+
1952-08-01,242
46+
1952-09-01,209
47+
1952-10-01,191
48+
1952-11-01,172
49+
1952-12-01,194
50+
1953-01-01,196
51+
1953-02-01,196
52+
1953-03-01,236
53+
1953-04-01,235
54+
1953-05-01,229
55+
1953-06-01,243
56+
1953-07-01,264
57+
1953-08-01,272
58+
1953-09-01,237
59+
1953-10-01,211
60+
1953-11-01,180
61+
1953-12-01,201
62+
1954-01-01,204
63+
1954-02-01,188
64+
1954-03-01,235
65+
1954-04-01,227
66+
1954-05-01,234
67+
1954-06-01,264
68+
1954-07-01,302
69+
1954-08-01,293
70+
1954-09-01,259
71+
1954-10-01,229
72+
1954-11-01,203
73+
1954-12-01,229
74+
1955-01-01,242
75+
1955-02-01,233
76+
1955-03-01,267
77+
1955-04-01,269
78+
1955-05-01,270
79+
1955-06-01,315
80+
1955-07-01,364
81+
1955-08-01,347
82+
1955-09-01,312
83+
1955-10-01,274
84+
1955-11-01,237
85+
1955-12-01,278
86+
1956-01-01,284
87+
1956-02-01,277
88+
1956-03-01,317
89+
1956-04-01,313
90+
1956-05-01,318
91+
1956-06-01,374
92+
1956-07-01,413
93+
1956-08-01,405
94+
1956-09-01,355
95+
1956-10-01,306
96+
1956-11-01,271
97+
1956-12-01,306
98+
1957-01-01,315
99+
1957-02-01,301
100+
1957-03-01,356
101+
1957-04-01,348
102+
1957-05-01,355
103+
1957-06-01,422
104+
1957-07-01,465
105+
1957-08-01,467
106+
1957-09-01,404
107+
1957-10-01,347
108+
1957-11-01,305
109+
1957-12-01,336
110+
1958-01-01,340
111+
1958-02-01,318
112+
1958-03-01,362
113+
1958-04-01,348
114+
1958-05-01,363
115+
1958-06-01,435
116+
1958-07-01,491
117+
1958-08-01,505
118+
1958-09-01,404
119+
1958-10-01,359
120+
1958-11-01,310
121+
1958-12-01,337
122+
1959-01-01,360
123+
1959-02-01,342
124+
1959-03-01,406
125+
1959-04-01,396
126+
1959-05-01,420
127+
1959-06-01,472
128+
1959-07-01,548
129+
1959-08-01,559
130+
1959-09-01,463
131+
1959-10-01,407
132+
1959-11-01,362
133+
1959-12-01,405
134+
1960-01-01,417
135+
1960-02-01,391
136+
1960-03-01,419
137+
1960-04-01,461
138+
1960-05-01,472
139+
1960-06-01,535
140+
1960-07-01,622
141+
1960-08-01,606
142+
1960-09-01,508
143+
1960-10-01,461
144+
1960-11-01,390
145+
1960-12-01,432

docs/src/manual.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ LocalLinearTrend
5757
MultivariateBasicStructural
5858
```
5959

60+
## Naive models
61+
62+
Naive models are not state space models but are good benchmarks for forecasting, for this reason we implemented them here.
63+
64+
```@docs
65+
Naive
66+
SeasonalNaive
67+
ExperimentalSeasonalNaive
68+
```
69+
6070
## Automatic forecasting
6171

6272
Some models have various parameters and modelling options. The package provides simple functions that

src/StateSpaceModels.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ include("models/sarima.jl")
4747
include("models/linear_regression.jl")
4848
include("models/unobserved_components.jl")
4949
include("models/exponential_smoothing.jl")
50+
include("models/naive_models.jl")
5051
include("models/dar.jl")
5152

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

5758
# Exported types and structs
5859
export BasicStructural
60+
export ExperimentalSeasonalNaive
5961
export BasicStructuralExplanatory
6062
export DAR
6163
export ExponentialSmoothing
@@ -69,9 +71,11 @@ export LocalLevelCycle
6971
export LocalLevelExplanatory
7072
export LocalLinearTrend
7173
export MultivariateBasicStructural
74+
export Naive
7275
export Optimizer
7376
export SARIMA
7477
export ScalarKalmanFilter
78+
export SeasonalNaive
7579
export StateSpaceModel
7680
export UnivariateKalmanFilter
7781
export UnobservedComponents

src/kalman_filter_and_smoother.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ function assert_possible_to_filter(model::StateSpaceModel)
8080
error("Model has not been estimated yet, please use `fit!`.")
8181
end
8282

83+
get_standard_residuals(fo::FilterOutput) = get_standard_innovations(fo)
84+
function get_standard_innovations(fo::FilterOutput)
85+
v = get_innovations(fo)
86+
F = get_innovations_variance(fo)
87+
@assert size(v, 2) == 1 # Must be univariate
88+
return v[:, 1] ./ sqrt.(F[1, 1, :])
89+
end
90+
8391
"""
8492
get_innovations
8593

src/models/common.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ num_series(model::StateSpaceModel) = num_series(system(model))
44
system(model::StateSpaceModel) = model.system
55
isunivariate(model::StateSpaceModel) = isa(model.system.y, Vector)
66
model_name(model::StateSpaceModel) = "$(typeof(model))"
7+
length_observations(model::StateSpaceModel) = length(model.system.y)
8+
observations(model::StateSpaceModel) = model.system.y
79

810
function lagmat(y::Vector{Fl}, k::Int) where Fl
911
X = Matrix{Fl}(undef, length(y) - k, k)
1012
for i in 1:k
1113
X[:, i] = lag(y, i)[k + 1:end]
1214
end
1315
return X
14-
end
16+
end

0 commit comments

Comments
 (0)