{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Expanding returns\n", "\n", "```{note}\n", "The code below might need to be modified to work as of Feb 2023. [The fix is here.](https://github.com/LeDataSciFi/ledatascifi-2024/issues/6)\n", "```\n", "\n", "## The problem\n", "\n", "You know the charts that show cumulative returns if you'd bought and held a stock since some long ago date? Let's make one!\n", "\n", "This is called \"expanding returns\" because you get the total returns from day 0 to day N, then from day 0 to day N+1, and so on; the window is expanding instead of having a fixed number of units or containing a specific increment of time." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download the returns\n", "\n", "We need a dataset with firm, date, and the daily return. Let's build it:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#!pip install pandas_datareader # uncomment and run this ONE TIME ONLY to install pandas data reader" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import pandas_datareader as pdr # you might need to install this (see above)\n", "from datetime import datetime\n", "import yfinance as yf\n", "\n", "# choose your firms and dates \n", "stocks = ['SBUX','AAPL','MSFT']\n", "start = datetime(1980, 1, 1)\n", "end = datetime(2022, 7, 31)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip}\n", "The code in the next block is explained more thoroughly in `handouts/factor_loading_simple.ipynb` in the textbook repo because that file prints the status of the data throughout. Looking at this might help.\n", "```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", " | Firm | \n", "Date | \n", "Adj Close | \n", "ret | \n", "
---|---|---|---|---|
0 | \n", "AAPL | \n", "1980-12-12 | \n", "0.100040 | \n", "NaN | \n", "
1 | \n", "AAPL | \n", "1980-12-15 | \n", "0.094820 | \n", "-0.052171 | \n", "
2 | \n", "AAPL | \n", "1980-12-16 | \n", "0.087861 | \n", "-0.073398 | \n", "
3 | \n", "AAPL | \n", "1980-12-17 | \n", "0.090035 | \n", "0.024751 | \n", "
4 | \n", "AAPL | \n", "1980-12-18 | \n", "0.092646 | \n", "0.028992 | \n", "
5 | \n", "AAPL | \n", "1980-12-19 | \n", "0.098300 | \n", "0.061029 | \n", "
6 | \n", "AAPL | \n", "1980-12-22 | \n", "0.103084 | \n", "0.048670 | \n", "
7 | \n", "AAPL | \n", "1980-12-23 | \n", "0.107434 | \n", "0.042199 | \n", "
8 | \n", "AAPL | \n", "1980-12-24 | \n", "0.113088 | \n", "0.052628 | \n", "
9 | \n", "AAPL | \n", "1980-12-26 | \n", "0.123527 | \n", "0.092310 | \n", "
10 | \n", "AAPL | \n", "1980-12-29 | \n", "0.125267 | \n", "0.014083 | \n", "
11 | \n", "AAPL | \n", "1980-12-30 | \n", "0.122222 | \n", "-0.024304 | \n", "
12 | \n", "AAPL | \n", "1980-12-31 | \n", "0.118743 | \n", "-0.028468 | \n", "
13 | \n", "AAPL | \n", "1981-01-02 | \n", "0.120048 | \n", "0.010988 | \n", "
14 | \n", "AAPL | \n", "1981-01-05 | \n", "0.117438 | \n", "-0.021738 | \n", "