<jupyter_start><jupyter_text>Local-Global-Trend(LGT) Example<jupyter_code>import pandas as pd
import numpy as np
from orbit.models.lgt import LGTMAP, LGTAggregated, LGTFull
from orbit.diagnostics.plot import plot_predicted_data
from orbit.diagnostics.plot import plot_predicted_components<jupyter_output><empty_output><jupyter_text>Data *iclaims_example* is a dataset containing the weekly initial claims for US unemployment benefits against a few related google trend queries from Jan 2010 - June 2018. This aims to mimick the dataset from the paper "Predicting the Present with Bayesian Structural Time Series" by SCOTT and VARIAN (2014).Number of claims are obtained from [Federal Reserve Bank of St. Louis](https://fred.stlouisfed.org/series/ICNSA) while google queries are obtained through [Google Trends API](https://trends.google.com/trends/?geo=US).<jupyter_code>DATA_FILE = "./data/iclaims_example.csv"
date_col = 'week'
response_col = 'claims'
df = pd.read_csv(DATA_FILE, parse_dates=[date_col])
df.dtypes<jupyter_output><empty_output><jupyter_text>We preprocess the data by log transformation to turn model from `additive` to `multiplicative`.<jupyter_code>df[['claims', 'trend.unemploy', 'trend.filling', 'trend.job']] =\
    df[['claims', 'trend.unemploy', 'trend.filling', 'trend.job']].apply(np.log)
df.head(5)<jupyter_output><empty_output><jupyter_text>Train / Test Split<jupyter_code>test_size=52
train_df=df[:-test_size]
test_df=df[-test_size:]
train_df.head(5)<jupyter_output><empty_output><jupyter_text>MAP Fit and Predict<jupyter_code>lgt=LGTMAP(
    response_col=response_col,
    date_col=date_col,
    seasonality=52,
    seed=8888,
)
%%time
lgt.fit(df=train_df)
predicted_df = lgt.predict(df=test_df)
plot_predicted_data(training_actual_df=train_df, predicted_df=predicted_df, 
                    date_col=date_col, actual_col=response_col, 
                    pred_col='prediction', 
                    test_actual_df=test_df)<jupyter_output><empty_output><jupyter_text>MCMC Fit and Predict<jupyter_code>lgt=LGTFull(
    response_col=response_col,
    date_col=date_col,
    seasonality=52,
    seed=8888,
    prediction_percentiles=[5, 95],
)
%%time
lgt.fit(df=train_df)
predicted_df = lgt.predict(df=test_df)
predicted_df.tail(5)
plot_predicted_data(training_actual_df=train_df, predicted_df=predicted_df, 
                    date_col=lgt.date_col, actual_col=lgt.response_col, pred_col='prediction', 
                    test_actual_df=test_df)<jupyter_output><empty_output><jupyter_text>Fit with Regressors & Decmpose Prediction  Fit with regressors<jupyter_code>lgt=LGTFull(
    response_col=response_col,
    regressor_col=['trend.unemploy', 'trend.filling'],
    date_col='week',
    seasonality=52,
    seed=8888,
    prediction_percentiles=[5, 95],
)
%%time
lgt.fit(df=train_df)<jupyter_output>WARNING:pystan:Maximum (flat) parameter count (1000) exceeded: skipping diagnostic tests for n_eff and Rhat.
To run all diagnostics call pystan.check_hmc_diagnostics(fit)
WARNING:pystan:1 of 100 iterations ended with a divergence (1 %).
WARNING:pystan:Try running with adapt_delta larger than 0.8 to remove the divergences.<jupyter_text>Decompose Prediction We can use `decompose=True` to decompose prediction into `trend`, `seasonality` and `regression` components. This command only works with point estimate.<jupyter_code># make prediction in both train and test period
predicted_df = lgt.predict(df=df, decompose=True)
predicted_df.tail(5)
plot_predicted_components(predicted_df=predicted_df, date_col=date_col)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10257126886528788, 'fraction_numerical': 0.014812744550027948, 'mean_word_length': 6.129482071713148, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '120321', 'license': 'Apache-2.0', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1217, 'n_words': 252, 'path': 'examples/LGT_Example.ipynb', 'repo_name': 'szmark001/orbit', 'size': '3578'}