Principal Component Analysis

The buzzword at the moment is Big Data where you have to make sense of lots of observations, but the problem we’ll discuss here is Wide Data where you have lots of observables. Another way of describing this is having too many dimensions. The question we will try to address […]

Shiller’s CAPE and long-term S&P returns

We can also plot the decade ahead S&P 500 return vs cyclically adjusted price earnings today to see if Shiller’s CAPE provides a lead indicator of future returns.

This shows Shiller’s CAPE on the x-axis and S&P 500 return over the following decade on the y axis. The […]

Making Smart Beta Portfolios in R

Here we explore smart beta and how to build portfolios which implement smart beta in R. Smart beta is what people call algorithms that construct portfolios that are intended to beat market cap weighted benchmarks without a human selecting stocks and bonds. So we will begin by explaining what market […]

Bayesian Rolling Regression

See the DLM for stocks page for an introduction to dynamic linear models. Here we can apply the same library but wrapped up in a convenient function called dlm.rolling.regression() which takes only two parameters, two or more independent variables in X and the single dependent variable in y. To test […]

Hierarchical Linear Models

I’m a huge fan of the statistician Andrew Gelman. He explains statistics in such an intuitive way, and it was his book “Bayesian Data Analysis” ┬áthat first opened my eyes to what is possible with Bayesian models, and how to implement them in practice. In Bayesian Data Analysis he gives […]

Recession Shading

As an example we can plot the S&P 500 using the getshiller() function that we have described here.

This function gets the NBER recession data from your local cache of FRED data (see here for the function definition) and builds a data frame with the recession start and end […]

Dynamic linear model for stocks

Using the Dynamic Linear Model (dlm) package and the excellent book by Petris, Petrone and Campagnoli here is an example of dynamic regression using a simple DLM. In case you’re not familiar with DLMs they assume that one models observables (such as prices, GDP etc.) and hidden state variables separately […]

Download and Cache FRED data

The Federal Reserve produces and maintains an amazing resource called FRED: the Federal Reserve Economic Database. I find it useful to maintain a cached RData file with my favourite FRED data. Then I use two functions: update_fred() and get_fred(). These depend on the quantmod and zoo libraries. In case you […]

Extrapolate the US Unemployment Rate

The quantmod library can download data from St. Louis Fed database FRED so we can use it to get the US unemployment rate.

We will assume the rate over a short period follows an exponential decay falling to a long-term rate of 4%.

We can use nls() to […]

Rolling regression and rolling correlation

Rolling Regression In the Linear model for two asset return series example we found that the S&P 500 had a beta of -1 to Treasury returns. Let’s see if that relationship is stable over time. First we get the two ETF series from Yahoo. We convert to daily log returns. […]