Skip to content

Instantly share code, notes, and snippets.

@18182324
Created January 4, 2023 18:43
Show Gist options
  • Save 18182324/39dd71f479d3cde62bb63a4fa375a5d9 to your computer and use it in GitHub Desktop.
Save 18182324/39dd71f479d3cde62bb63a4fa375a5d9 to your computer and use it in GitHub Desktop.
Statistical models to Predict Recession
#Markov-Switching Vector Autoregression (MSVAR) model
import numpy as np
import statsmodels.tsa.api as smt
# Set the number of lags to consider
nlags = 4
# Create the model
model = smt.MSVAR(data, lags=nlags)
# Fit the model
results = model.fit()
# Print the predicted probabilities of a recession
print(results.predicted_probabilities[:, 1])
############################################
#Dynamic Conditional Correlation (DCC)
import numpy as np
import statsmodels.tsa.api as smt
# Set the number of lags to consider
nlags = 4
# Create the model
model = smt.DCC(data, lags=nlags)
# Fit the model
results = model.fit()
# Print the predicted probabilities of a recession
print(results.predicted_probabilities[:, 1])
###########################################
#Here is an example of Python code for the Dynamic Conditional Correlation (DCC) model
#using unemployment rate, consumer sentiment, stock market volatility (S&P 500), and corporate interest rates as variables.
#This code assumes that the data is stored in a CSV file called "recession_data.csv"
import numpy as np
import pandas as pd
import statsmodels.tsa.api as smt
# Load the data
data = pd.read_csv("recession_data.csv")
# Set the number of lags to consider
nlags = 4
# Create the model
model = smt.DCC(data[['unemployment_rate', 'consumer_sentiment', 'sp500_volatility', 'corporate_interest_rates']], lags=nlags)
# Fit the model
results = model.fit()
# Print the predicted probabilities of a recession
print(results.predicted_probabilities[:, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment