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

Components breakdown for forecast #44

Closed
vandonova opened this issue Aug 23, 2021 · 2 comments
Closed

Components breakdown for forecast #44

vandonova opened this issue Aug 23, 2021 · 2 comments

Comments

@vandonova
Copy link

Hi,

I'm looking to break down my forecast into its components. I saw your function plot_components however, it only uses the train data to create the components.

In my example, my training data never has values on Saturdays. In that case,

params = {'forecast_horizon': 1,
 'fit_algorithm_dict': {'fit_algorithm': 'elastic_net',
  'fit_algorithm_params': {'l1_ratio': [0.1, 0.5, 0.7, 0.9, 0.95, 0.99, 1]}},
 'changepoints_dict': {'method': 'auto',
  'regularization_strength': None,
  'actual_changepoint_min_distance': '7D',
  'potential_changepoint_distance': '3D',
  'yearly_seasonality_order': 6},
}
model = SimpleSilverkiteEstimator(**params)
model.fit(data)
predictions = model.predict(data)
model.plot_components()

return a weekly component plot that just connects Friday and Sunday with a straight line. (Note that hovering on 5, Saturday, does not show any data).
image
image

When predicting, I want to include Saturdays in my forecast and predict their value based on the fitted coefficients for 'sin1_tow_weekly', 'cos1_tow_weekly', 'sin2_tow_weekly', 'cos2_tow_weekly', 'sin3_tow_weekly', 'cos3_tow_weekly'.

Is there an easy way to retrieve the components for predictions? Alternatively, how would I access the design matrix x_mat for the forecast instead of the fitted data?

Thank you in advance!
Vicky

@sayanpatra
Copy link
Contributor

Hi @vandonova,

You can retrieve the components for training as follows:

fig = result.model[-1].plot_components()  # Generates and stores the components
result.model[-1].silverkite_diagnostics.components

Currently there is no easy way to return the components for the forecasts, but it can be computed as follows.

Assumption: result is the output from the pipeline

import patsy

estimator = result.forecast.estimator
trained_model= estimator.model_dict
fut_df = result.forecast.df[["ts", "actual"]].rename(columns={"actual":"y"})

# Build Silverkite features
features_df_fut = estimator.silverkite._SilverkiteForecast__build_silverkite_features(
    df=fut_df,
    time_col=trained_model["time_col"],
    origin_for_time_vars=trained_model["origin_for_time_vars"],
    daily_event_df_dict=trained_model["daily_event_df_dict"],
    changepoint_values=trained_model["changepoint_values"],
    continuous_time_col=trained_model["continuous_time_col"],
    growth_func=trained_model["growth_func"],
    fs_func=trained_model["fs_func"],
    seasonality_changepoint_result=trained_model["seasonality_changepoint_result"],
    changepoint_dates=trained_model["trend_changepoint_dates"]
)

# Add autoregression terms
if trained_model["autoreg_func"] is not None:
    past_df = trained_model["df"].copy()
    df = pd.DataFrame({value_col: [np.nan]*fut_df.shape[0]})
    df.index = fut_df.index
    autoreg_df = estimator.silverkite._SilverkiteForecast__build_autoreg_features(
        df=df,
        value_col=trained_model["value_col"],
        autoreg_func=trained_model["autoreg_func"],
        phase="predict",
        past_df=past_df[[value_col]])
    features_df_fut = pd.concat(
        [features_df_fut, autoreg_df],
        axis=1,
        sort=False)

# Compute the design matrix
(x_mat_predict,) = patsy.build_design_matrices(
    [trained_model["x_design_info"]],
    data=features_df_fut,
    return_type="dataframe")

# Compute features df ( this is `x_mat_predict` multiplied by the training coefficients)
feature_df_predict = (trained_model["ml_model"].coef_* x_mat_predict)

# Calculate components
diagnostics = estimator.silverkite_diagnostics
components_predict = diagnostics.get_silverkite_components(
                fut_df,
                diagnostics.time_col,
                diagnostics.value_col,
                feature_df_predict)

Hope this helps. Have a good weekend.

@vandonova
Copy link
Author

Thanks @sayanpatra this is exactly what I needed.

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

No branches or pull requests

2 participants