<jupyter_start><jupyter_text>* Get the dataset from the Stata Press publishing house on their website.* This gives a pandas series of the RGNP, and the index annotates the dates.<jupyter_code>dta = pd.read_stata('https://www.stata-press.com/data/r14/rgnp.dta').iloc[1:]
dta.index = pd.DatetimeIndex(dta.date, freq='QS')
dta_hamilton = dta.rgnp<jupyter_output><empty_output><jupyter_text>* Domestic recessions and expansions model.* The model will include transition probabilities between these two regimes and predict probabilities of expansion or recession at each time point.<jupyter_code># Plot the data
dta_hamilton.plot(title='Growth rate of Real GNP', figsize=(16, 6))<jupyter_output><empty_output><jupyter_text>* Fit the 4th order Markov switching model.* Specify two regimes. * Get the model fitted via maximum likelihood estimation to the RGNP data.* Set switching_ar=False because the statsmodels implementation defaults toswitching autoregressive coefficients.<jupyter_code># Fit the model
mod_hamilton = sm.tsa.MarkovAutoregression(dta_hamilton, k_regimes=2, order=4, switching_ar=False)
res_hamilton = mod_hamilton.fit()<jupyter_output><empty_output><jupyter_text>* See the regime transition parameters at the bottom of the same output.<jupyter_code>print(res_hamilton.summary())<jupyter_output>Markov Switching Model Results                         
================================================================================
Dep. Variable:                     rgnp   No. Observations:                  131
Model:             MarkovAutoregression   Log Likelihood                -181.263
Date:                  Tue, 30 Nov 2021   AIC                            380.527
Time:                          03:29:08   BIC                            406.404
Sample:                      04-01-1951   HQIC                           391.042
                           - 10-01-1984                                         
Covariance Type:                 approx                                         
                             Regime 0 parameters                              
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
----------------------------------[...]<jupyter_text>* See the lengths of recession and expansion.* The output array is in financial quarters.* Therefore, a recession is expected to take about four quarters (1 year) and an expansion 10 quarters (two and a half years).<jupyter_code>res_hamilton.expected_durations<jupyter_output><empty_output><jupyter_text>* Plot the probability of recession at each point in time.<jupyter_code>from pandas_datareader.data import DataReader
from datetime import datetime
usrec = DataReader('USREC', 'fred', start=datetime(1947, 1, 1), end=datetime(2013, 4, 1))<jupyter_output><empty_output><jupyter_text>* This gives a DataFrame in which recessions are indicated. * Here are the first five rows.* In the first five rows, there was no recession according to the National Bureau of Economic Research (NBER) indicators.<jupyter_code>usrec.head()<jupyter_output><empty_output><jupyter_text>* Plot NBER recession indicators against the model regime predictions.* This gives actual recession data against model predictions.<jupyter_code>_, ax = plt.subplots(1, figsize=(16, 6))
ax.plot(res_hamilton.filtered_marginal_probabilities[0])
ax.fill_between(
    usrec.index, 0, 1, where=usrec['USREC'].values,
    color='gray', alpha=0.3
)
ax.set(
    xlim=(dta_hamilton.index[4], dta_hamilton.index[-1]),
    ylim=(0, 1),
    title='Filtered probability of recession'
);<jupyter_output><empty_output><jupyter_text>* See there seems to be quite a good match between the model predictions andactual recession indicators.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12936880542514345, 'fraction_numerical': 0.02634324465310381, 'mean_word_length': 3.145945945945946, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '3880', 'license': 'MIT', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1026, 'n_words': 318, 'path': 'Probabilistic Models for TS/Markov Switching Autoregression Model.ipynb', 'repo_name': 'thirasit/Time-series-analysis-with-ML', 'size': '3834'}