@@ -60,7 +60,7 @@ mutable struct StructuralModel <: StateSpaceLearningModel
60
60
seasonal:: Bool
61
61
stochastic_seasonal:: Bool
62
62
freq_seasonal:: Union{Int,Vector{Int}}
63
- cycle_period:: Union{Union{Int, <:AbstractFloat},Vector{Int}, Vector{<:AbstractFloat}}
63
+ cycle_period:: Union{Union{Int,<:AbstractFloat},Vector{Int},Vector{<:AbstractFloat}}
64
64
cycle_matrix:: Vector{Matrix}
65
65
stochastic_cycle:: Bool
66
66
outlier:: Bool
@@ -78,7 +78,7 @@ mutable struct StructuralModel <: StateSpaceLearningModel
78
78
seasonal:: Bool = true ,
79
79
stochastic_seasonal:: Bool = true ,
80
80
freq_seasonal:: Union{Int,Vector{Int}} = 12 ,
81
- cycle_period:: Union{Union{Int, <:AbstractFloat},Vector{Int}, Vector{<:AbstractFloat}} = 0 ,
81
+ cycle_period:: Union{Union{Int,<:AbstractFloat},Vector{Int},Vector{<:AbstractFloat}} = 0 ,
82
82
dumping_cycle:: Float64 = 1.0 ,
83
83
stochastic_cycle:: Bool = false ,
84
84
outlier:: Bool = true ,
@@ -104,13 +104,13 @@ mutable struct StructuralModel <: StateSpaceLearningModel
104
104
cycle_matrix = Vector {Matrix} (undef, length (cycle_period))
105
105
for i in eachindex (cycle_period)
106
106
A = dumping_cycle * cos (2 * pi / cycle_period[i])
107
- B = dumping_cycle * sin (2 * pi / cycle_period[i])
107
+ B = dumping_cycle * sin (2 * pi / cycle_period[i])
108
108
cycle_matrix[i] = [A B; - B A]
109
109
end
110
110
else
111
111
cycle_matrix = Vector {Matrix} (undef, 1 )
112
112
A = dumping_cycle * cos (2 * pi / cycle_period)
113
- B = dumping_cycle * sin (2 * pi / cycle_period)
113
+ B = dumping_cycle * sin (2 * pi / cycle_period)
114
114
cycle_matrix[1 ] = [A B; - B A]
115
115
end
116
116
else
182
182
- `Int`: Size of ζ calculated from T.
183
183
184
184
"""
185
- ζ_size (T:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Int = max (0 , T - ζ_ω_threshold - max (2 , stochastic_start))
185
+ ζ_size (T:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Int =
186
+ max (0 , T - ζ_ω_threshold - max (2 , stochastic_start))
186
187
187
188
"""
188
189
ω_size(T::Int, s::Int, stochastic_start::Int)::Int
198
199
- `Int`: Size of ω calculated from T.
199
200
200
201
"""
201
- ω_size (T:: Int , s:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Int = max ( 0 , T - ζ_ω_threshold - s + 1 - max ( 0 , max ( 2 , stochastic_start) - s))
202
-
202
+ ω_size (T:: Int , s:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Int =
203
+ max ( 0 , T - ζ_ω_threshold - s + 1 - max ( 0 , max ( 2 , stochastic_start) - s))
203
204
204
205
"""
205
206
o_size(T::Int, stochastic_start::Int)::Int
@@ -229,7 +230,8 @@ o_size(T::Int, stochastic_start::Int)::Int = T - max(1, stochastic_start) + 1
229
230
# Returns
230
231
- `Int`: Size of ϕ calculated from T.
231
232
"""
232
- ϕ_size (T:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ) = 2 * (T - max (2 , stochastic_start) + 1 ) - (ζ_ω_threshold * 2 )
233
+ ϕ_size (T:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ) =
234
+ 2 * (T - max (2 , stochastic_start) + 1 ) - (ζ_ω_threshold * 2 )
233
235
234
236
"""
235
237
create_ξ(T::Int, steps_ahead::Int, stochastic_start::Int)::Matrix
@@ -248,7 +250,10 @@ o_size(T::Int, stochastic_start::Int)::Int = T - max(1, stochastic_start) + 1
248
250
function create_ξ (T:: Int , steps_ahead:: Int , stochastic_start:: Int ):: Matrix
249
251
stochastic_start = max (2 , stochastic_start)
250
252
ξ_matrix = zeros (T + steps_ahead, T - stochastic_start + 1 )
251
- ones_indexes = findall (I -> Tuple (I)[1 ] - (stochastic_start - 2 ) > Tuple (I)[2 ], CartesianIndices ((T + steps_ahead, T - stochastic_start)))
253
+ ones_indexes = findall (
254
+ I -> Tuple (I)[1 ] - (stochastic_start - 2 ) > Tuple (I)[2 ],
255
+ CartesianIndices ((T + steps_ahead, T - stochastic_start)),
256
+ )
252
257
ξ_matrix[ones_indexes] .= 1
253
258
return ξ_matrix[:, 1 : (end - 1 )]
254
259
end
@@ -268,10 +273,12 @@ create_ζ(T::Int, steps_ahead::Int, ζ_ω_threshold::Int, stochastic_start::Int)
268
273
- `Matrix`: Matrix of innovations ζ constructed based on the input sizes.
269
274
270
275
"""
271
- function create_ζ (T:: Int , steps_ahead:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Matrix
276
+ function create_ζ (
277
+ T:: Int , steps_ahead:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int
278
+ ):: Matrix
272
279
stochastic_start = max (2 , stochastic_start)
273
280
ζ_matrix = zeros (T + steps_ahead, T - stochastic_start)
274
-
281
+
275
282
for t in 2 : (T + steps_ahead)
276
283
if t < T
277
284
len = t - stochastic_start
@@ -298,11 +305,13 @@ create_ω(T::Int, freq_seasonal::Int, steps_ahead::Int, ζ_ω_threshold::Int, st
298
305
- `Matrix`: Matrix of innovations ω constructed based on the input sizes.
299
306
300
307
"""
301
- function create_ω (T:: Int , freq_seasonal:: Int , steps_ahead:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Matrix
308
+ function create_ω (
309
+ T:: Int , freq_seasonal:: Int , steps_ahead:: Int , ζ_ω_threshold:: Int , stochastic_start:: Int
310
+ ):: Matrix
302
311
stochastic_start = max (2 , stochastic_start)
303
312
ω_matrix_size = T - freq_seasonal + 1
304
313
stochastic_start_diff = max (0 , stochastic_start - freq_seasonal)
305
-
314
+
306
315
ω_matrix = zeros (T + steps_ahead, ω_matrix_size - stochastic_start_diff)
307
316
for t in (freq_seasonal + 1 ): (T + steps_ahead)
308
317
ωₜ_coefs = zeros (ω_matrix_size)
@@ -313,12 +322,11 @@ function create_ω(T::Int, freq_seasonal::Int, steps_ahead::Int, ζ_ω_threshold
313
322
1
314
323
ωₜ_coefs[lag₂[0 .< lag₂ .<= ω_matrix_size + (freq_seasonal - 1 )] .- (freq_seasonal - 1 )] .=
315
324
- 1
316
- ω_matrix[t, :] = ωₜ_coefs[1 + stochastic_start_diff: end ]
325
+ ω_matrix[t, :] = ωₜ_coefs[( 1 + stochastic_start_diff) : end ]
317
326
end
318
327
return ω_matrix[:, 1 : (end - ζ_ω_threshold)]
319
328
end
320
329
321
-
322
330
"""
323
331
create_o_matrix(T::Int, steps_ahead::Int, stochastic_start::Int)::Matrix
324
332
@@ -358,18 +366,22 @@ create_ϕ(X_cycle::Matrix, T::Int, steps_ahead::Int64, ζ_ω_threshold::Int, sto
358
366
# Returns
359
367
- `Matrix`: Matrix of innovations ϕ constructed based on the input sizes.
360
368
"""
361
- function create_ϕ (c_matrix:: Matrix , T:: Int , steps_ahead:: Int64 , ζ_ω_threshold:: Int , stochastic_start:: Int ):: Matrix
362
-
369
+ function create_ϕ (
370
+ c_matrix:: Matrix , T:: Int , steps_ahead:: Int64 , ζ_ω_threshold:: Int , stochastic_start:: Int
371
+ ):: Matrix
363
372
num_cols = 2 * (T - stochastic_start + 1 )
364
373
X = Matrix {Float64} (undef, T + steps_ahead, num_cols)
365
374
366
375
for (idx, t) in enumerate (stochastic_start: T)
367
- X[:, 2 * (idx - 1 ) + 1 ] = vcat (zeros (t - 1 ), c_matrix[1 : T - t + 1 + steps_ahead, 1 ])
368
- X[:, 2 * (idx - 1 ) + 2 ] = vcat (zeros (t - 1 ), c_matrix[1 : T - t + 1 + steps_ahead, 2 ])
376
+ X[:, 2 * (idx - 1 ) + 1 ] = vcat (
377
+ zeros (t - 1 ), c_matrix[1 : (T - t + 1 + steps_ahead), 1 ]
378
+ )
379
+ X[:, 2 * (idx - 1 ) + 2 ] = vcat (
380
+ zeros (t - 1 ), c_matrix[1 : (T - t + 1 + steps_ahead), 2 ]
381
+ )
369
382
end
370
383
371
- return X[:, 1 : end - (ζ_ω_threshold * 2 )]
372
-
384
+ return X[:, 1 : (end - (ζ_ω_threshold * 2 ))]
373
385
end
374
386
375
387
"""
@@ -385,13 +397,15 @@ end
385
397
# Returns
386
398
- `Vector{Matrix}`: Deterministic cycle matrix constructed based on the input parameters.
387
399
"""
388
- function create_deterministic_cycle_matrix (cycle_matrix:: Vector{Matrix} , T:: Int , steps_ahead:: Int ):: Vector{Matrix}
400
+ function create_deterministic_cycle_matrix (
401
+ cycle_matrix:: Vector{Matrix} , T:: Int , steps_ahead:: Int
402
+ ):: Vector{Matrix}
389
403
deterministic_cycle_matrix = Vector {Matrix} (undef, length (cycle_matrix))
390
404
for (idx, c_matrix) in enumerate (cycle_matrix)
391
- X_cycle = Matrix {Float64} (undef, T+ steps_ahead, 2 )
392
- cycle_matrix_term = c_matrix ^ 0
405
+ X_cycle = Matrix {Float64} (undef, T + steps_ahead, 2 )
406
+ cycle_matrix_term = c_matrix^ 0
393
407
X_cycle[1 , :] = cycle_matrix_term[1 , :]
394
- for t in 2 : T + steps_ahead
408
+ for t in 2 : (T + steps_ahead)
395
409
cycle_matrix_term *= c_matrix
396
410
X_cycle[t, :] = cycle_matrix_term[1 , :]
397
411
end
@@ -426,7 +440,7 @@ function create_initial_states_Matrix(
426
440
level:: Bool ,
427
441
trend:: Bool ,
428
442
seasonal:: Bool ,
429
- deterministic_cycle_matrix:: Vector{Matrix}
443
+ deterministic_cycle_matrix:: Vector{Matrix} ,
430
444
):: Matrix
431
445
initial_states_matrix = zeros (T + steps_ahead, 0 )
432
446
if level
@@ -517,7 +531,11 @@ function create_X(
517
531
) where {Fl<: AbstractFloat ,Tl<: AbstractFloat }
518
532
T = size (Exogenous_X, 1 )
519
533
520
- ξ_matrix = stochastic_level ? create_ξ (T, steps_ahead, stochastic_start) : zeros (T + steps_ahead, 0 )
534
+ ξ_matrix = if stochastic_level
535
+ create_ξ (T, steps_ahead, stochastic_start)
536
+ else
537
+ zeros (T + steps_ahead, 0 )
538
+ end
521
539
ζ_matrix = if stochastic_trend
522
540
create_ζ (T, steps_ahead, ζ_ω_threshold, stochastic_start)
523
541
else
@@ -527,19 +545,30 @@ function create_X(
527
545
ω_matrix = zeros (T + steps_ahead, 0 )
528
546
if stochastic_seasonal
529
547
for s in freq_seasonal
530
- ω_matrix = hcat (ω_matrix, create_ω (T, s, steps_ahead, ζ_ω_threshold, stochastic_start))
548
+ ω_matrix = hcat (
549
+ ω_matrix, create_ω (T, s, steps_ahead, ζ_ω_threshold, stochastic_start)
550
+ )
531
551
end
532
552
end
533
553
534
- deterministic_cycle_matrix = create_deterministic_cycle_matrix (cycle_matrix, T, steps_ahead)
554
+ deterministic_cycle_matrix = create_deterministic_cycle_matrix (
555
+ cycle_matrix, T, steps_ahead
556
+ )
535
557
ϕ_matrix = zeros (T + steps_ahead, 0 )
536
558
if stochastic_cycle
537
559
for c_matrix in deterministic_cycle_matrix
538
- ϕ_matrix = hcat (ϕ_matrix, create_ϕ (c_matrix, T, steps_ahead, ζ_ω_threshold, stochastic_start))
560
+ ϕ_matrix = hcat (
561
+ ϕ_matrix,
562
+ create_ϕ (c_matrix, T, steps_ahead, ζ_ω_threshold, stochastic_start),
563
+ )
539
564
end
540
565
end
541
566
542
- o_matrix = outlier ? create_o_matrix (T, steps_ahead, stochastic_start) : zeros (T + steps_ahead, 0 )
567
+ o_matrix = if outlier
568
+ create_o_matrix (T, steps_ahead, stochastic_start)
569
+ else
570
+ zeros (T + steps_ahead, 0 )
571
+ end
543
572
544
573
initial_states_matrix = create_initial_states_Matrix (
545
574
T, freq_seasonal, steps_ahead, level, trend, seasonal, deterministic_cycle_matrix
@@ -580,7 +609,6 @@ function create_X(
580
609
steps_ahead:: Int = 0 ,
581
610
Exogenous_Forecast:: Matrix{Tl} = zeros (steps_ahead, size (Exogenous_X, 2 )),
582
611
) where {Fl<: AbstractFloat ,Tl<: AbstractFloat }
583
-
584
612
return create_X (
585
613
model. level,
586
614
model. stochastic_level,
@@ -655,15 +683,19 @@ function get_components_indexes(model::StructuralModel)::Dict
655
683
end
656
684
657
685
if model. stochastic_level
658
- ξ_indexes = collect ((FINAL_INDEX + 1 ): (FINAL_INDEX + ξ_size (T, model. stochastic_start)))
686
+ ξ_indexes = collect (
687
+ (FINAL_INDEX + 1 ): (FINAL_INDEX + ξ_size (T, model. stochastic_start))
688
+ )
659
689
FINAL_INDEX += length (ξ_indexes)
660
690
else
661
691
ξ_indexes = Int[]
662
692
end
663
693
664
694
if model. stochastic_trend
665
695
ζ_indexes = collect (
666
- (FINAL_INDEX + 1 ): (FINAL_INDEX + ζ_size (T, model. ζ_ω_threshold, model. stochastic_start))
696
+ (FINAL_INDEX + 1 ): (FINAL_INDEX + ζ_size (
697
+ T, model. ζ_ω_threshold, model. stochastic_start
698
+ )),
667
699
)
668
700
FINAL_INDEX += length (ζ_indexes)
669
701
else
@@ -674,7 +706,9 @@ function get_components_indexes(model::StructuralModel)::Dict
674
706
if model. stochastic_seasonal
675
707
for s in model. freq_seasonal
676
708
ω_s_indexes = collect (
677
- (FINAL_INDEX + 1 ): (FINAL_INDEX + ω_size (T, s, model. ζ_ω_threshold, model. stochastic_start))
709
+ (FINAL_INDEX + 1 ): (FINAL_INDEX + ω_size (
710
+ T, s, model. ζ_ω_threshold, model. stochastic_start
711
+ )),
678
712
)
679
713
FINAL_INDEX += length (ω_s_indexes)
680
714
push! (ω_indexes, ω_s_indexes)
@@ -687,7 +721,9 @@ function get_components_indexes(model::StructuralModel)::Dict
687
721
if model. stochastic_cycle
688
722
for _ in eachindex (model. cycle_matrix)
689
723
ϕ_i_indexes = collect (
690
- (FINAL_INDEX + 1 ): (FINAL_INDEX + ϕ_size (T, model. ζ_ω_threshold, model. stochastic_start))
724
+ (FINAL_INDEX + 1 ): (FINAL_INDEX + ϕ_size (
725
+ T, model. ζ_ω_threshold, model. stochastic_start
726
+ )),
691
727
)
692
728
FINAL_INDEX += length (ϕ_i_indexes)
693
729
push! (ϕ_indexes, ϕ_i_indexes)
@@ -697,7 +733,9 @@ function get_components_indexes(model::StructuralModel)::Dict
697
733
end
698
734
699
735
if model. outlier
700
- o_indexes = collect ((FINAL_INDEX + 1 ): (FINAL_INDEX + o_size (T, model. stochastic_start)))
736
+ o_indexes = collect (
737
+ (FINAL_INDEX + 1 ): (FINAL_INDEX + o_size (T, model. stochastic_start))
738
+ )
701
739
FINAL_INDEX += length (o_indexes)
702
740
else
703
741
o_indexes = Int[]
@@ -723,7 +761,6 @@ function get_components_indexes(model::StructuralModel)::Dict
723
761
if model. stochastic_seasonal
724
762
components_indexes_dict[" ω_$s " ] = ω_indexes[i]
725
763
end
726
-
727
764
end
728
765
729
766
if ! isempty (model. cycle_matrix)
@@ -733,7 +770,7 @@ function get_components_indexes(model::StructuralModel)::Dict
733
770
components_indexes_dict[" ϕ_$i " ] = ϕ_indexes[i]
734
771
end
735
772
end
736
- end
773
+ end
737
774
738
775
return components_indexes_dict
739
776
end
@@ -876,8 +913,16 @@ function get_innovation_simulation_X(
876
913
return create_ω (length (model. y) + steps_ahead + 1 , s, 0 , 1 , model. stochastic_start)
877
914
elseif occursin (" ϕ_" , innovation)
878
915
i = parse (Int, split (innovation, " _" )[2 ])
879
- deterministic_cycle_matrix = create_deterministic_cycle_matrix (model. cycle_matrix, length (model. y), steps_ahead)
880
- return create_ϕ (deterministic_cycle_matrix[i], length (model. y), steps_ahead, model. ζ_ω_threshold, model. stochastic_start)
916
+ deterministic_cycle_matrix = create_deterministic_cycle_matrix (
917
+ model. cycle_matrix, length (model. y), steps_ahead
918
+ )
919
+ return create_ϕ (
920
+ deterministic_cycle_matrix[i],
921
+ length (model. y),
922
+ steps_ahead,
923
+ model. ζ_ω_threshold,
924
+ model. stochastic_start,
925
+ )
881
926
end
882
927
end
883
928
0 commit comments