{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Rolling Regression\n", "Author: Thomas Wiecki\n", "\n", "* [Pairs trading](https://www.quantopian.com/posts/pairs-trading-algorithm-1) is a famous technique in algorithmic trading that plays two stocks against each other.\n", "* For this to work, stocks must be correlated (cointegrated).\n", "* One common example is the price of gold (GLD) and the price of gold mining operations (GFI)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import pandas as pd\n", "import numpy as np\n", "import pymc3 as pm\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets load the prices of GFI and GLD." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | GFI | \n", "GLD | \n", "
---|---|---|
Date | \n", "\n", " | \n", " |
2010-01-04 | \n", "13.55 | \n", "109.82 | \n", "
2010-01-05 | \n", "13.51 | \n", "109.88 | \n", "
2010-01-06 | \n", "13.70 | \n", "110.71 | \n", "
2010-01-07 | \n", "13.63 | \n", "111.07 | \n", "
2010-01-08 | \n", "13.72 | \n", "111.52 | \n", "