{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(GLM-rolling-regression)=\n", "# Rolling Regression\n", "\n", ":::{post} Jan 28, 2023\n", ":tags: generalized linear model, regression \n", ":category: intermediate\n", ":author: Thomas Wiecki\n", ":::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* [Pairs trading](https://en.wikipedia.org/wiki/Pairs_trade?oldformat=true) 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": {}, "outputs": [], "source": [ "import os\n", "import warnings\n", "\n", "import arviz as az\n", "import matplotlib.pyplot as plt\n", "import matplotlib.ticker as mticker\n", "import numpy as np\n", "import pandas as pd\n", "import pymc as pm\n", "import xarray as xr\n", "\n", "from matplotlib import MatplotlibDeprecationWarning\n", "\n", "warnings.filterwarnings(action=\"ignore\", category=MatplotlibDeprecationWarning)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "RANDOM_SEED = 8927\n", "rng = np.random.default_rng(RANDOM_SEED)\n", "%config InlineBackend.figure_format = 'retina'\n", "az.style.use(\"arviz-darkgrid\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets load the prices of GFI and GLD." ] }, { "cell_type": "code", "execution_count": 3, "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", "