{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GFIGLD
Date
2010-01-0413.55109.82
2010-01-0513.51109.88
2010-01-0613.70110.71
2010-01-0713.63111.07
2010-01-0813.72111.52
\n", "