Course Project: Trading Strategy based on PCA

Welcome to your course project. This exercise gives you a hands-on experience to use PCA to:

Instructions:

After this assignment you will:

Let's get started!

About iPython Notebooks

iPython Notebooks are interactive coding environments embedded in a webpage. You will be using iPython notebooks in this class. You only need to write code between the ### START CODE HERE ### and ### END CODE HERE ### comments. After writing your code, you can run the cell by either pressing "SHIFT"+"ENTER" or by clicking on "Run Cell" (denoted by a play symbol) in the upper bar of the notebook.

We will often specify "(≈ X lines of code)" in the comments to tell you about how much code you need to write. It is just a rough estimate, so don't feel bad if your code is longer or shorter.

1 - Packages

First, let's run the cell below to import all the packages that you will need during this assignment.

Dataset: daily prices of stocks from S&P 500 index

For this exercise we will be working with S&P 500 Index stock prices dataset. The following cell computes returns based for a subset of S&P 500 index stocks. It starts with stocks price data:

Calculate daily log-returns

Now we are ready to compute Absorption Ratio(AR). We do so by defining a moving look back window over which we collect returns for computing PCA. We start off from the earliest historical data and march forward moving by step_size, which we also choose arbitrary. For each such window we compute PCA and AR, fixing in advance number of components in the enumerator. Specifically, for we use the following hyper-parameters:

Part 1 (Implement exponentially-weighted)

Instructions: Implement exponent_weighting function which returns a sequence of $w_j$ as np.array. See below:

Define sequence of $X_j$ where $j \subset [N, 0]$, an integer taking all values in the interval from 0 to N $$ X_j = e^{-\frac{log(2)}{H} \times \space j}$$ where H is half-life which determines the speed of decay, and $log$ is natural log function Then a sequence of exponentially decaying weights $w_j$ is defined as $$ w_j = \frac{X_j}{ \sum\limits_{j=0}^N X_j } $$

Part 2 (Implement Linear Auto-Encoder)

LinearAutoEncoder class has two fully connected layers and no activation functions between the layers.

Instructions:

Instructions:

Having computed daily (this means the step size is 1) Absorption Ratio times series, we further follow M. Kritzman to make use of AR to define yet another measure: AR Delta. In particular: $$ AR\delta = \frac{AR_{15d} - AR_{1y}}{ AR\sigma_{1y}}$$ We use $AR\delta$ to build simple portfolio trading strategy

Part 3 (AR Delta Trading Strategy)

Instructions: Implement get_weight() function

The AR Delta trading strategy forms a portfolio of EQ and FI, following these simple rules:

Here we compute AR Delta strategy weights using data from the same data set. As expected, the average number of trades per year is very low.

Now that weights have been determined, run the re-balancing strategy using time series of returns and compute

Contrast this with 50 / 50 Equity / Fixed Income ETF strategy performance using the same performance metrics. Use VTI as Equity and AGG as Fixed Income assets.

Part 4 (Calculate performance of backtested strategy)

Instructions:

Implement function backtest_strategy which given a DataFrame of strategy weights and a DataFrame asset returns annualized return, volatility and Sharpe ratio of a strategy.