Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unobserved_components_explanatory model #329

Closed
wants to merge 5 commits into from

Conversation

andreramosfdc
Copy link
Member

Add the unobserved components explanatory model. Unfortunately, including the cycle component results in instability, consistently leading to some hyperparameters being NaN. However, the other components perform well.

nile = CSV.File(StateSpaceModels.NILE) |> DataFrame
steps_ahead = 10
nile_y = nile.flow
X = [0.906 0.247 0.065; 0.443 0.211 0.778; 0.745 0.32 0.081; 0.512 0.011 0.95; 0.253 0.99 0.136; 0.334 0.285 0.345; 0.427 0.078 0.449; 0.867 0.468 0.119; 0.099 0.476 0.04; 0.125 0.157 0.979; 0.692 0.328 0.558; 0.136 0.279 0.91; 0.032 0.022 0.346; 0.35 0.321 0.748; 0.93 0.852 0.115; 0.959 0.023 0.291; 0.774 0.631 0.059; 0.183 0.62 0.563; 0.297 0.434 0.124; 0.15 0.788 0.941; 0.893 0.851 0.298; 0.354 0.623 0.814; 0.131 0.355 0.719; 0.941 0.847 0.823; 0.057 0.719 0.176; 0.245 0.064 0.287; 0.805 0.366 0.027; 0.337 0.28 0.191; 0.823 0.893 0.4; 0.45 0.054 0.463; 0.891 0.661 0.365; 0.711 0.379 0.718; 0.36 0.792 0.0; 0.259 0.398 0.783; 0.39 0.832 0.179; 0.461 0.828 0.061; 0.934 0.575 0.829; 0.753 0.903 0.832; 0.729 0.28 0.71; 0.162 0.505 0.684; 0.637 0.266 0.309; 0.991 0.824 0.73; 0.383 0.371 0.828; 0.618 0.542 0.516; 0.484 0.503 0.101; 0.599 0.443 0.559; 0.453 0.087 0.414; 0.324 0.545 0.931; 0.51 0.596 0.049; 0.656 0.677 0.855; 0.869 0.206 0.392; 0.373 0.78 0.09; 0.692 0.564 0.374; 0.746 0.839 0.649; 0.096 0.342 0.578; 0.459 0.999 0.078; 0.608 0.776 0.99; 0.836 0.719 0.063; 0.283 0.652 0.737; 0.631 0.752 0.883; 0.691 0.081 0.348; 0.821 0.364 0.711; 0.564 0.881 0.045; 0.03 0.6 0.734; 0.091 0.975 0.586; 0.766 0.965 0.567; 0.246 0.14 0.612; 0.096 0.371 0.774; 0.092 0.797 0.055; 0.437 0.096 0.641; 0.231 0.895 0.384; 0.647 0.634 0.325; 0.681 0.612 0.507; 0.122 0.304 0.524; 0.648 0.364 0.023; 0.587 0.294 0.481; 0.377 0.61 0.275; 0.24 0.267 0.63; 0.222 0.24 0.283; 0.509 0.195 0.741; 0.218 0.944 0.804; 0.834 0.72 0.432; 0.563 0.016 0.229; 0.42 0.571 0.976; 0.62 0.31 0.238; 0.292 0.786 0.647; 0.145 0.938 0.018; 0.864 0.918 0.967; 0.926 0.09 0.712; 0.068 0.688 0.407; 0.047 0.739 0.862; 0.426 0.323 0.057; 0.972 0.092 0.697; 0.928 0.761 0.841; 0.077 0.757 0.522; 0.538 0.98 0.388; 0.357 0.843 0.108; 0.471 0.2 0.491; 0.97 0.026 0.486; 0.612 0.999 0.249; 0.539 0.877 0.886; 0.156 0.892 0.617; 0.159 0.57 0.768; 0.485 0.081 0.468; 0.924 0.506 0.68; 0.219 0.068 0.711; 0.457 0.461 0.514; 0.87 0.21 0.412; 0.907 0.305 0.581; 0.688 0.441 0.311]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, what is this time series?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just random exogenous features. The different versions in Julia in tests were generating different numbers even with a fixed seed

Comment on lines 51 to 79
# rj_temp = CSV.File(RJ_TEMPERATURE) |> DataFrame
# rj_tem_values = rj_temp.Values ./ 100
# steps_ahead = 52
# X = rand(length(rj_tem_values) + steps_ahead, 1)
# β = [0.1]
# X_train = X[1:end - steps_ahead, :]
# X_test = X[end - steps_ahead + 1:end, :]
# rj_tem_values += X_train*β
# model = UnobservedComponentsExplanatory(rj_tem_values, X_train; trend = "local level", cycle = "stochastic")
# fit!(model)
# model.hyperparameters
# forec = forecast(model, X_test)

# TODO check with other software maybe statsmodels
# @test loglike(model) ≈ -1032.40953 atol = 1e-5 rtol = 1e-5
# filt = kalman_filter(model)
# forec = forecast(model, X_test)
# @test monotone_forecast_variance(forec)
# smoother = kalman_smoother(model)
# alpha = get_smoothed_state(smoother)
# @test maximum(alpha[:, 1]) >= 296
# @test minimum(alpha[:, 1]) <= 296
# @test maximum(alpha[:, 2]) >= 7.5
# @test minimum(alpha[:, 2]) <= -7.5

# # Testing that it does not break
# rj_temp = CSV.File(RJ_TEMPERATURE) |> DataFrame
# model = UnobservedComponents(rj_temp.Values; trend = "smooth trend", cycle = "stochastic")
# fit!(model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comments

Comment on lines 1 to 107
# "linear deterministic trend",
# "linear trend",
"local linear trend",
# "damped local lineaar trend",
# "random walk with drift"
"smooth trend"
]
return true
end
function parse_trend(trend::String)
validate_trend(trend)
(has_irregular, has_trend, stochastic_trend,
has_slope, stochastic_slope) = (false, false, false, false, false)
if trend == "local level"
(has_irregular, has_trend, stochastic_trend,
has_slope, stochastic_slope) = (true, true, true, false, false)
elseif trend == "random walk"
(has_irregular, has_trend, stochastic_trend,
has_slope, stochastic_slope) = (false, true, true, false, false)
elseif trend == "local linear trend"
(has_irregular, has_trend, stochastic_trend,
has_slope, stochastic_slope) = (true, true, true, true, true)
elseif trend == "smooth trend"
(has_irregular, has_trend, stochastic_trend,
has_slope, stochastic_slope) = (true, true, false, true, true)
end
# Validate trend booleans
if !has_trend && stochastic_trend
error("Invalid trend specification.")
end
if !has_trend && has_slope
error("Invalid trend specification.")
end
if !has_slope && stochastic_slope
error("Invalid trend specification.")
end
return (has_irregular, has_trend, stochastic_trend, has_slope, stochastic_slope)
end
function validate_seasonal(seasonal::String)
if seasonal == "no"
return true
else
spl = split(seasonal)
# TODO better error messaage
# Maybe a no string can be also valid
@assert length(spl) == 2
@assert spl[1] in ["deterministic", "stochastic"]
return true
end
end
function parse_seasonal(seasonal::String)
validate_seasonal(seasonal)
if seasonal == "no"
has_seasonal, stochastic_seasonal, seasonal_freq = (false, false, 0)
return has_seasonal, stochastic_seasonal, seasonal_freq
else
spl = split(seasonal)
stochastic_seasonal = spl[1] == "stochastic"
seasonal_freq = parse(Int, spl[2])
has_seasonal = seasonal_freq == 0 ? false : true
return has_seasonal, stochastic_seasonal, seasonal_freq
end
end
function validate_cycle(cycle::String)
if cycle == "no"
return true
else
spl = split(cycle)
# TODO better error messaage
if length(spl) == 1
@assert spl[1] in ["deterministic", "stochastic"]
return true
elseif length(spl) == 2
@assert spl[1] in ["deterministic", "stochastic"]
@assert spl[2] == "damped"
return true
end
end
return false
end
function parse_cycle(cycle::String)
validate_cycle(cycle)
if cycle == "no"
has_cycle, stochastic_cycle, damped_cycle = (false, false, false)
return has_cycle, stochastic_cycle, damped_cycle
else
spl = split(cycle)
has_cycle = true
# TODO better error messaage
# Maybe a no string can be also valid
if length(spl) == 1
stochastic_cycle = spl[1] == "stochastic"
return has_cycle, stochastic_cycle, false
elseif length(spl) == 2
stochastic_cycle = spl[1] == "stochastic"
damped_cycle = spl[2] == "damped"
return has_cycle, stochastic_cycle, damped_cycle
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these are duplicates from Unobserved Components. Honestly, I think that the explanatory variables should be implemented in the Unobserved Components model. We could use the TimeVariant filter even if we don't have explanatory variables, I believe this is the best way of not having to maintain two nearly identical codebases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will implement this change and submit again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants