{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"id": "a6be4a95",
"metadata": {
"id": "a6be4a95"
},
"source": [
"# Running the First Regression in Python"
]
},
{
"cell_type": "markdown",
"id": "b7890b49",
"metadata": {
"id": "b7890b49"
},
"source": [
"Suppose this is your first time to write the code. Perhaps, you want to run a simple regression using two series of asset prices to fin the equity beta. Let's use a step-by-step approach to complete the task.\n",
"\n",
" Step 1: Download two assets' prices from the web\n",
" Step 2: Put them onto a matrix form\n",
" Step 3: Run the OLS\n",
" Step 4: Plot data"
]
},
{
"cell_type": "markdown",
"id": "d844db66",
"metadata": {
"id": "d844db66"
},
"source": [
"### Step 1: Download data\n",
"We will use yahoo finance package (https://pypi.org/project/yfinance/) to download Yahoo Finance data from the web. We need to (1) install and (2) import this package."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "47c20bb7",
"metadata": {
"id": "47c20bb7",
"outputId": "6cce99f7-51f6-48f1-cca6-99832e742033",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Collecting yfinance\n",
" Downloading yfinance-0.1.85-py2.py3-none-any.whl (29 kB)\n",
"Collecting requests>=2.26\n",
" Downloading requests-2.28.1-py3-none-any.whl (62 kB)\n",
"\u001b[K |████████████████████████████████| 62 kB 1.7 MB/s \n",
"\u001b[?25hRequirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.4.4)\n",
"Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.21.6)\n",
"Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance) (0.0.11)\n",
"Requirement already satisfied: pandas>=0.24.0 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.3.5)\n",
"Requirement already satisfied: lxml>=4.5.1 in /usr/local/lib/python3.7/dist-packages (from yfinance) (4.9.1)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2.8.2)\n",
"Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24.0->yfinance) (2022.6)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24.0->yfinance) (1.15.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.10)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2022.9.24)\n",
"Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (2.1.1)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.26->yfinance) (1.24.3)\n",
"Installing collected packages: requests, yfinance\n",
" Attempting uninstall: requests\n",
" Found existing installation: requests 2.23.0\n",
" Uninstalling requests-2.23.0:\n",
" Successfully uninstalled requests-2.23.0\n",
"Successfully installed requests-2.28.1 yfinance-0.1.85\n"
]
}
],
"source": [
"!pip install yfinance # to install, remove # and run the cell\n",
"import yfinance as yf # to import"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "479eb94a",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "479eb94a",
"outputId": "9d5e8c3c-b4f5-4e0d-b820-a4f4cde8a026"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n"
]
}
],
"source": [
"# download\n",
"mystock = yf.download(\"TSLA\", start=\"2011-01-01\", end=\"2022-05-31\", interval='1mo')['Adj Close'].rename('TSLA')\n",
"index = yf.download(\"SPY\", start=\"2011-01-01\", end=\"2022-05-31\", interval='1mo')['Adj Close'].rename('SPY')"
]
},
{
"cell_type": "markdown",
"id": "9621734b",
"metadata": {
"id": "9621734b"
},
"source": [
"### Step 2: Put two time series onto a matrix\n",
"We need pandas module, so let's install and import it. https://pandas.pydata.org/"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6b7ec623",
"metadata": {
"id": "6b7ec623"
},
"outputs": [],
"source": [
"#!pip install pandas # Actually, you have this alread when you isntalled Anaconda.\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d16cf519",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 455
},
"id": "d16cf519",
"outputId": "3a0a7543-42b2-4ea9-cbbb-3975cb1c1a61"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" TSLA SPY\n",
"Date \n",
"2011-02-01 -0.008714 0.034738\n",
"2011-03-01 0.161574 -0.004206\n",
"2011-04-01 -0.005405 0.033432\n",
"2011-05-01 0.092029 -0.011215\n",
"2011-06-01 -0.033510 -0.021720\n",
"... ... ...\n",
"2022-01-01 -0.113609 -0.049413\n",
"2022-02-01 -0.070768 -0.029517\n",
"2022-03-01 0.238009 0.034377\n",
"2022-04-01 -0.191945 -0.084935\n",
"2022-05-01 -0.129197 0.002257\n",
"\n",
"[136 rows x 2 columns]"
],
"text/html": [
"\n",
"
\n", " | TSLA | \n", "SPY | \n", "
---|---|---|
Date | \n", "\n", " | \n", " |
2011-02-01 | \n", "-0.008714 | \n", "0.034738 | \n", "
2011-03-01 | \n", "0.161574 | \n", "-0.004206 | \n", "
2011-04-01 | \n", "-0.005405 | \n", "0.033432 | \n", "
2011-05-01 | \n", "0.092029 | \n", "-0.011215 | \n", "
2011-06-01 | \n", "-0.033510 | \n", "-0.021720 | \n", "
... | \n", "... | \n", "... | \n", "
2022-01-01 | \n", "-0.113609 | \n", "-0.049413 | \n", "
2022-02-01 | \n", "-0.070768 | \n", "-0.029517 | \n", "
2022-03-01 | \n", "0.238009 | \n", "0.034377 | \n", "
2022-04-01 | \n", "-0.191945 | \n", "-0.084935 | \n", "
2022-05-01 | \n", "-0.129197 | \n", "0.002257 | \n", "
136 rows × 2 columns
\n", "