{
"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"
},
"outputs": [],
"source": [
"!pip install yfinance -q # 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": "40940da6-cf99-4f08-c050-224ecdfce62c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"YF.download() has changed argument auto_adjust default to True\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"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')['Close']\n",
"index = yf.download(\"SPY\", start=\"2011-01-01\", end=\"2022-05-31\", interval='1mo')['Close']"
]
},
{
"cell_type": "code",
"source": [
"mystock"
],
"metadata": {
"id": "heFPJAtXjqSe",
"outputId": "1df5f728-d635-4b82-ce3b-85f853585486",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 455
}
},
"id": "heFPJAtXjqSe",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Ticker TSLA\n",
"Date \n",
"2011-01-01 1.606667\n",
"2011-02-01 1.592667\n",
"2011-03-01 1.850000\n",
"2011-04-01 1.840000\n",
"2011-05-01 2.009333\n",
"... ...\n",
"2022-01-01 312.239990\n",
"2022-02-01 290.143341\n",
"2022-03-01 359.200012\n",
"2022-04-01 290.253326\n",
"2022-05-01 252.753326\n",
"\n",
"[137 rows x 1 columns]"
],
"text/html": [
"\n",
"
| Ticker | \n", "TSLA | \n", "
|---|---|
| Date | \n", "\n", " |
| 2011-01-01 | \n", "1.606667 | \n", "
| 2011-02-01 | \n", "1.592667 | \n", "
| 2011-03-01 | \n", "1.850000 | \n", "
| 2011-04-01 | \n", "1.840000 | \n", "
| 2011-05-01 | \n", "2.009333 | \n", "
| ... | \n", "... | \n", "
| 2022-01-01 | \n", "312.239990 | \n", "
| 2022-02-01 | \n", "290.143341 | \n", "
| 2022-03-01 | \n", "359.200012 | \n", "
| 2022-04-01 | \n", "290.253326 | \n", "
| 2022-05-01 | \n", "252.753326 | \n", "
137 rows × 1 columns
\n", "| Ticker | \n", "SPY | \n", "
|---|---|
| Date | \n", "\n", " |
| 2011-01-01 | \n", "99.595802 | \n", "
| 2011-02-01 | \n", "103.055534 | \n", "
| 2011-03-01 | \n", "102.622070 | \n", "
| 2011-04-01 | \n", "106.052910 | \n", "
| 2011-05-01 | \n", "104.863586 | \n", "
| ... | \n", "... | \n", "
| 2022-01-01 | \n", "430.576294 | \n", "
| 2022-02-01 | \n", "417.866943 | \n", "
| 2022-03-01 | \n", "432.231873 | \n", "
| 2022-04-01 | \n", "395.520264 | \n", "
| 2022-05-01 | \n", "396.413055 | \n", "
137 rows × 1 columns
\n", "| Ticker | \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.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", "