{ "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": null, "id": "47c20bb7", "metadata": { "id": "47c20bb7" }, "outputs": [], "source": [ "!pip install yfinance # to install, remove # and run the cell\n", "import yfinance as yf # to import" ] }, { "cell_type": "code", "execution_count": 3, "id": "479eb94a", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "479eb94a", "outputId": "f4bb78f6-0070-43fd-d54a-339ddec9e1c4" }, "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": 4, "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": 5, "id": "d16cf519", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 455 }, "id": "d16cf519", "outputId": "de9b7b39-5378-4ccf-ff78-3c9e03266ecd" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " TSLA SPY\n", "Date \n", "2011-02-01 -0.008714 0.034737\n", "2011-03-01 0.161574 -0.004206\n", "2011-04-01 -0.005405 0.033431\n", "2011-05-01 0.092029 -0.011214\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.034737 | \n", "
2011-03-01 | \n", "0.161574 | \n", "-0.004206 | \n", "
2011-04-01 | \n", "-0.005405 | \n", "0.033431 | \n", "
2011-05-01 | \n", "0.092029 | \n", "-0.011214 | \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", "