Skip to content

Commit cd04152

Browse files
author
andre_ramos
committed
format in bluestyle
1 parent 1f56ed6 commit cd04152

File tree

4 files changed

+259
-141
lines changed

4 files changed

+259
-141
lines changed

paper_tests/m4_test/m4_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ end
140140

141141
create_dirs()
142142

143-
main()
143+
main()

src/fit_forecast.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,7 @@ function forecast(
121121
@assert size(Exogenous_Forecast, 1) == steps_ahead "Exogenous_Forecast must have the same number of rows as steps_ahead"
122122

123123
Exogenous_X = model.X[:, exog_idx]
124-
complete_matrix = create_X(
125-
model,
126-
Exogenous_X,
127-
steps_ahead,
128-
Exogenous_Forecast,
129-
)
124+
complete_matrix = create_X(model, Exogenous_X, steps_ahead, Exogenous_Forecast)
130125

131126
if typeof(model.output) == Output
132127
return AbstractFloat.(

src/models/structural_model.jl

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mutable struct StructuralModel <: StateSpaceLearningModel
6060
seasonal::Bool
6161
stochastic_seasonal::Bool
6262
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}}
6464
cycle_matrix::Vector{Matrix}
6565
stochastic_cycle::Bool
6666
outlier::Bool
@@ -78,7 +78,7 @@ mutable struct StructuralModel <: StateSpaceLearningModel
7878
seasonal::Bool=true,
7979
stochastic_seasonal::Bool=true,
8080
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,
8282
dumping_cycle::Float64=1.0,
8383
stochastic_cycle::Bool=false,
8484
outlier::Bool=true,
@@ -104,13 +104,13 @@ mutable struct StructuralModel <: StateSpaceLearningModel
104104
cycle_matrix = Vector{Matrix}(undef, length(cycle_period))
105105
for i in eachindex(cycle_period)
106106
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])
108108
cycle_matrix[i] = [A B; -B A]
109109
end
110110
else
111111
cycle_matrix = Vector{Matrix}(undef, 1)
112112
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)
114114
cycle_matrix[1] = [A B; -B A]
115115
end
116116
else
@@ -182,7 +182,8 @@ end
182182
- `Int`: Size of ζ calculated from T.
183183
184184
"""
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))
186187

187188
"""
188189
ω_size(T::Int, s::Int, stochastic_start::Int)::Int
@@ -198,8 +199,8 @@ end
198199
- `Int`: Size of ω calculated from T.
199200
200201
"""
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))
203204

204205
"""
205206
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
229230
# Returns
230231
- `Int`: Size of ϕ calculated from T.
231232
"""
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)
233235

234236
"""
235237
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
248250
function create_ξ(T::Int, steps_ahead::Int, stochastic_start::Int)::Matrix
249251
stochastic_start = max(2, stochastic_start)
250252
ξ_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+
)
252257
ξ_matrix[ones_indexes] .= 1
253258
return ξ_matrix[:, 1:(end - 1)]
254259
end
@@ -268,10 +273,12 @@ create_ζ(T::Int, steps_ahead::Int, ζ_ω_threshold::Int, stochastic_start::Int)
268273
- `Matrix`: Matrix of innovations ζ constructed based on the input sizes.
269274
270275
"""
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
272279
stochastic_start = max(2, stochastic_start)
273280
ζ_matrix = zeros(T + steps_ahead, T - stochastic_start)
274-
281+
275282
for t in 2:(T + steps_ahead)
276283
if t < T
277284
len = t - stochastic_start
@@ -298,11 +305,13 @@ create_ω(T::Int, freq_seasonal::Int, steps_ahead::Int, ζ_ω_threshold::Int, st
298305
- `Matrix`: Matrix of innovations ω constructed based on the input sizes.
299306
300307
"""
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
302311
stochastic_start = max(2, stochastic_start)
303312
ω_matrix_size = T - freq_seasonal + 1
304313
stochastic_start_diff = max(0, stochastic_start - freq_seasonal)
305-
314+
306315
ω_matrix = zeros(T + steps_ahead, ω_matrix_size - stochastic_start_diff)
307316
for t in (freq_seasonal + 1):(T + steps_ahead)
308317
ωₜ_coefs = zeros(ω_matrix_size)
@@ -313,12 +322,11 @@ function create_ω(T::Int, freq_seasonal::Int, steps_ahead::Int, ζ_ω_threshold
313322
1
314323
ωₜ_coefs[lag₂[0 .< lag₂ .<= ω_matrix_size + (freq_seasonal - 1)] .- (freq_seasonal - 1)] .=
315324
-1
316-
ω_matrix[t, :] = ωₜ_coefs[1+stochastic_start_diff:end]
325+
ω_matrix[t, :] = ωₜ_coefs[(1 + stochastic_start_diff):end]
317326
end
318327
return ω_matrix[:, 1:(end - ζ_ω_threshold)]
319328
end
320329

321-
322330
"""
323331
create_o_matrix(T::Int, steps_ahead::Int, stochastic_start::Int)::Matrix
324332
@@ -358,18 +366,22 @@ create_ϕ(X_cycle::Matrix, T::Int, steps_ahead::Int64, ζ_ω_threshold::Int, sto
358366
# Returns
359367
- `Matrix`: Matrix of innovations ϕ constructed based on the input sizes.
360368
"""
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
363372
num_cols = 2 * (T - stochastic_start + 1)
364373
X = Matrix{Float64}(undef, T + steps_ahead, num_cols)
365374

366375
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+
)
369382
end
370383

371-
return X[:, 1:end - (ζ_ω_threshold * 2)]
372-
384+
return X[:, 1:(end - (ζ_ω_threshold * 2))]
373385
end
374386

375387
"""
@@ -385,13 +397,15 @@ end
385397
# Returns
386398
- `Vector{Matrix}`: Deterministic cycle matrix constructed based on the input parameters.
387399
"""
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}
389403
deterministic_cycle_matrix = Vector{Matrix}(undef, length(cycle_matrix))
390404
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
393407
X_cycle[1, :] = cycle_matrix_term[1, :]
394-
for t in 2:T+steps_ahead
408+
for t in 2:(T + steps_ahead)
395409
cycle_matrix_term *= c_matrix
396410
X_cycle[t, :] = cycle_matrix_term[1, :]
397411
end
@@ -426,7 +440,7 @@ function create_initial_states_Matrix(
426440
level::Bool,
427441
trend::Bool,
428442
seasonal::Bool,
429-
deterministic_cycle_matrix::Vector{Matrix}
443+
deterministic_cycle_matrix::Vector{Matrix},
430444
)::Matrix
431445
initial_states_matrix = zeros(T + steps_ahead, 0)
432446
if level
@@ -517,7 +531,11 @@ function create_X(
517531
) where {Fl<:AbstractFloat,Tl<:AbstractFloat}
518532
T = size(Exogenous_X, 1)
519533

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
521539
ζ_matrix = if stochastic_trend
522540
create_ζ(T, steps_ahead, ζ_ω_threshold, stochastic_start)
523541
else
@@ -527,19 +545,30 @@ function create_X(
527545
ω_matrix = zeros(T + steps_ahead, 0)
528546
if stochastic_seasonal
529547
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+
)
531551
end
532552
end
533553

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+
)
535557
ϕ_matrix = zeros(T + steps_ahead, 0)
536558
if stochastic_cycle
537559
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+
)
539564
end
540565
end
541566

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
543572

544573
initial_states_matrix = create_initial_states_Matrix(
545574
T, freq_seasonal, steps_ahead, level, trend, seasonal, deterministic_cycle_matrix
@@ -580,7 +609,6 @@ function create_X(
580609
steps_ahead::Int=0,
581610
Exogenous_Forecast::Matrix{Tl}=zeros(steps_ahead, size(Exogenous_X, 2)),
582611
) where {Fl<:AbstractFloat,Tl<:AbstractFloat}
583-
584612
return create_X(
585613
model.level,
586614
model.stochastic_level,
@@ -655,15 +683,19 @@ function get_components_indexes(model::StructuralModel)::Dict
655683
end
656684

657685
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+
)
659689
FINAL_INDEX += length(ξ_indexes)
660690
else
661691
ξ_indexes = Int[]
662692
end
663693

664694
if model.stochastic_trend
665695
ζ_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+
)),
667699
)
668700
FINAL_INDEX += length(ζ_indexes)
669701
else
@@ -674,7 +706,9 @@ function get_components_indexes(model::StructuralModel)::Dict
674706
if model.stochastic_seasonal
675707
for s in model.freq_seasonal
676708
ω_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+
)),
678712
)
679713
FINAL_INDEX += length(ω_s_indexes)
680714
push!(ω_indexes, ω_s_indexes)
@@ -687,7 +721,9 @@ function get_components_indexes(model::StructuralModel)::Dict
687721
if model.stochastic_cycle
688722
for _ in eachindex(model.cycle_matrix)
689723
ϕ_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+
)),
691727
)
692728
FINAL_INDEX += length(ϕ_i_indexes)
693729
push!(ϕ_indexes, ϕ_i_indexes)
@@ -697,7 +733,9 @@ function get_components_indexes(model::StructuralModel)::Dict
697733
end
698734

699735
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+
)
701739
FINAL_INDEX += length(o_indexes)
702740
else
703741
o_indexes = Int[]
@@ -723,7 +761,6 @@ function get_components_indexes(model::StructuralModel)::Dict
723761
if model.stochastic_seasonal
724762
components_indexes_dict["ω_$s"] = ω_indexes[i]
725763
end
726-
727764
end
728765

729766
if !isempty(model.cycle_matrix)
@@ -733,7 +770,7 @@ function get_components_indexes(model::StructuralModel)::Dict
733770
components_indexes_dict["ϕ_$i"] = ϕ_indexes[i]
734771
end
735772
end
736-
end
773+
end
737774

738775
return components_indexes_dict
739776
end
@@ -876,8 +913,16 @@ function get_innovation_simulation_X(
876913
return create_ω(length(model.y) + steps_ahead + 1, s, 0, 1, model.stochastic_start)
877914
elseif occursin("ϕ_", innovation)
878915
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+
)
881926
end
882927
end
883928

0 commit comments

Comments
 (0)