Skip to content

Forecasting recession using Support Vector Machines (SVM) in R

Notifications You must be signed in to change notification settings

rogerjbos/svm_recession

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

title author date output
README
Roger J Bos, CFA
12/12/2019
html_document

Installation

library(devtools)
install_github("rogerjbos/svm_recession)
library(svm.recession)

Data and sources

The sample data will automatically load and be stored in the object dat. Here are the important variables.

fwdstate is 1 if the next month is in a recession and -1 otherwise. The model is fwdstate ~ payems + sp500 + yld_curve + man_prod where each of the four independant variables includes the current value plus the most recent 11 lagged months, for a total of 48 variables.

Running the example


library(svm.recession)
# load the sample data (should happen automatically -- object named `dat`)

# Set best_cost to NA to determine best_cost
best_cost <- 1120 
# Lag the independent variables by 12 months so there is no look ahead bias
mylag <- 12
# Determine which factors to include in the model
factors <- "vintage|fwdstate|payems|sp500|yld_curve|man_prod"

# Define `fwdstate`
dat[, fwdstate := ifelse(recession==1, 1, -1)]
fmla = formula(fwdstate ~ .)
cols <- grep(factors, names(dat), value = TRUE)
mydat <- dat[, ..cols]

# Run the backtest
b1 <- bt(mydat = mydat, fmla = fmla, mylag = mylag, best_cost = best_cost, flip = TRUE)

# Model error rate
b1$error

# Investment strategy preformance
b1$stats  

# Model best cost
b1$best_cost

# First few predictions
tail(b1$ans)

# Plot the results
layout(1:2)
plot(b1$ans$fwdstate ~ b1$ans$vintage, type='l', lty='dotted', 
     main='NBER Recessions', xlab='', ylab='Recessions')
lines(b1$ans$forecast ~ b1$ans$vintage, type='s', col='red')
plot(b1$ans$prob ~ b1$ans$vintage, type='l', col='red',
      main='Forecast Recession Probabilities', ylab='Probability')
lines(b1$ans$fwdstate ~ b1$ans$vintage, type='s')

Investment Strategy

The backtest measures the accuracy of the SVM predictions versus the actual recession markers as determined by the NBER.

From an investment strategy standpoint, we go long the S&P 500 when the recession is not forecasted and switch to cash when recession is forecasted. This provides a modest outperformance versus a buy and hold strategy.

References

This code was inspired by the paper Nowcasting Recessions using the SVM Machine Learning Algorithm by James, Abu-Mostafa, and Qiao. Available on SSRN at https://ssrn.com/abstract=3316917.

About

Forecasting recession using Support Vector Machines (SVM) in R

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages