Skip to content

Commit

Permalink
change to blue style
Browse files Browse the repository at this point in the history
  • Loading branch information
andre_ramos committed Oct 27, 2024
1 parent 3b3d44a commit 53ceade
Show file tree
Hide file tree
Showing 20 changed files with 1,514 additions and 743 deletions.
2 changes: 1 addition & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
style = "yas" # Optional predefined style
style = "blue" # Optional predefined style
25 changes: 13 additions & 12 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ using Documenter
include("../src/StateSpaceLearning.jl")

# Set up to run docstrings with jldoctest
DocMeta.setdocmeta!(StateSpaceLearning, :DocTestSetup, :(using StateSpaceLearning);
recursive=true)
DocMeta.setdocmeta!(
StateSpaceLearning, :DocTestSetup, :(using StateSpaceLearning); recursive=true
)

makedocs(;
modules=[StateSpaceLearning],
doctest=true,
clean=true,
checkdocs=:none,
format=Documenter.HTML(; mathengine=Documenter.MathJax2()),
sitename="StateSpaceLearning.jl",
authors="André Ramos",
pages=["Home" => "index.md", "manual.md"],)
modules=[StateSpaceLearning],
doctest=true,
clean=true,
checkdocs=:none,
format=Documenter.HTML(; mathengine=Documenter.MathJax2()),
sitename="StateSpaceLearning.jl",
authors="André Ramos",
pages=["Home" => "index.md", "manual.md"],
)

deploydocs(; repo="github.com/LAMPSPUC/StateSpaceLearning.jl.git",
push_preview=true)
deploydocs(; repo="github.com/LAMPSPUC/StateSpaceLearning.jl.git", push_preview=true)
60 changes: 43 additions & 17 deletions paper_tests/m4_test/evaluate_model.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
function evaluate_SSL(initialization_df::DataFrame, results_df::DataFrame, input::Dict,
outlier::Bool, α::Float64, H::Int64, sample_size::Int64,
information_criteria::String)
function evaluate_SSL(
initialization_df::DataFrame,
results_df::DataFrame,
input::Dict,
outlier::Bool,
α::Float64,
H::Int64,
sample_size::Int64,
information_criteria::String,
)
normalized_y = input["normalized_train"]
y_train = input["train"]
y_test = input["test"]
Expand All @@ -10,13 +17,26 @@ function evaluate_SSL(initialization_df::DataFrame, results_df::DataFrame, input
T = length(normalized_y)
normalized_y = normalized_y[max(1, T - sample_size + 1):end]

model = StateSpaceLearning.StructuralModel(normalized_y; level=true,
stochastic_level=true, trend=true,
stochastic_trend=true, seasonal=true,
stochastic_seasonal=true, freq_seasonal=12,
outlier=outlier, ζ_ω_threshold=12)
StateSpaceLearning.fit!(model; α=α, information_criteria=information_criteria, ϵ=0.05,
penalize_exogenous=true, penalize_initial_states=true)
model = StateSpaceLearning.StructuralModel(
normalized_y;
level=true,
stochastic_level=true,
trend=true,
stochastic_trend=true,
seasonal=true,
stochastic_seasonal=true,
freq_seasonal=12,
outlier=outlier,
ζ_ω_threshold=12,
)
StateSpaceLearning.fit!(
model;
α=α,
information_criteria=information_criteria,
ϵ=0.05,
penalize_exogenous=true,
penalize_initial_states=true,
)

normalized_prediction = StateSpaceLearning.forecast(model, H)
prediction = de_normalize(normalized_prediction, max_y, min_y)
Expand All @@ -25,11 +45,17 @@ function evaluate_SSL(initialization_df::DataFrame, results_df::DataFrame, input
smape = sMAPE(y_test, prediction)

results_df = vcat(results_df, DataFrame([[mase], [smape]], [:MASE, :sMAPE]))
initialization_df = vcat(initialization_df,
DataFrame([[model.output.residuals_variances["ξ"]],
[model.output.residuals_variances["ω"]],
[model.output.residuals_variances["ε"]],
[model.output.residuals_variances["ζ"]]],
[, , , ]))
initialization_df = vcat(
initialization_df,
DataFrame(
[
[model.output.residuals_variances["ξ"]],
[model.output.residuals_variances["ω"]],
[model.output.residuals_variances["ε"]],
[model.output.residuals_variances["ζ"]],
],
[, , , ],
),
)
return initialization_df, results_df
end
end
60 changes: 43 additions & 17 deletions paper_tests/m4_test/m4_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ function append_results(filepath, results_df)
return CSV.write(filepath, results_df)
end

function run_config(results_table::DataFrame, outlier::Bool, information_criteria::String,
α::Float64, save_init::Bool, sample_size::Int64)
function run_config(
results_table::DataFrame,
outlier::Bool,
information_criteria::String,
α::Float64,
save_init::Bool,
sample_size::Int64,
)
NAIVE_sMAPE = 14.427 #M4 Paper
NAIVE_MASE = 1.063 #M4 Paper

Expand All @@ -47,9 +53,16 @@ function run_config(results_table::DataFrame, outlier::Bool, information_criteri
initialization_df = DataFrame()
end

initialization_df, results_df = evaluate_SSL(initialization_df, results_df,
dict_vec[i], outlier, α, H,
sample_size, information_criteria)
initialization_df, results_df = evaluate_SSL(
initialization_df,
results_df,
dict_vec[i],
outlier,
α,
H,
sample_size,
information_criteria,
)

if i in [10000, 20000, 30000, 40000, 48000]
!save_init ? append_results(filepath, results_df) : nothing
Expand All @@ -61,13 +74,24 @@ function run_config(results_table::DataFrame, outlier::Bool, information_criteri

mase = trunc(mean(results_df[:, :MASE]); digits=3)
smape = trunc(mean(results_df[:, :sMAPE]); digits=3)
owa = trunc(mean([mean(results_df[:, :sMAPE]) / NAIVE_sMAPE,
mean(results_df[:, :MASE]) / NAIVE_MASE]); digits=3)
name = outlier ? "SSL-O ($(information_criteria), α = $(α))" :
"SSL ($(information_criteria), α = $(α))"
results_table = vcat(results_table,
DataFrame("Names" => ["$name"], "MASE" => [mase],
"sMAPE" => [smape], "OWA" => [owa]))
owa = trunc(
mean([
mean(results_df[:, :sMAPE]) / NAIVE_sMAPE,
mean(results_df[:, :MASE]) / NAIVE_MASE,
]);
digits=3,
)
name = if outlier
"SSL-O ($(information_criteria), α = $(α))"
else
"SSL ($(information_criteria), α = $(α))"
end
results_table = vcat(
results_table,
DataFrame(
"Names" => ["$name"], "MASE" => [mase], "sMAPE" => [smape], "OWA" => [owa]
),
)
return results_table
end

Expand All @@ -78,13 +102,15 @@ function main()
for information_criteria in ["aic", "bic"]
for α in vcat([0.0], collect(0.1:0.2:0.9), [1.0])
@info "Running SSL with outlier = $outlier, information_criteria = $information_criteria, α = "
results_table = run_config(results_table, outlier, information_criteria, α,
false, 60)
results_table = run_config(
results_table, outlier, information_criteria, α, false, 60
)
end
end
end
return CSV.write("paper_tests/m4_test/metrics_results/SSL_METRICS_RESULTS.csv",
results_table)
return CSV.write(
"paper_tests/m4_test/metrics_results/SSL_METRICS_RESULTS.csv", results_table
)
end

function create_dirs()
Expand Down Expand Up @@ -114,4 +140,4 @@ create_dirs()

main()

run_config(DataFrame(), false, "aic", 0.1, true, 2794)#max sample size
run_config(DataFrame(), false, "aic", 0.1, true, 2794)#max sample size
2 changes: 1 addition & 1 deletion paper_tests/m4_test/prepare_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ function build_train_test_dict(df_train::DataFrame, df_test::DataFrame)
push!(dict_vec, train_test_dict[key])
end
return dict_vec
end
end
Loading

0 comments on commit 53ceade

Please sign in to comment.