{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 40 Practices on Stock Data Processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Packages" ] }, { "cell_type": "code", "execution_count": 134, "metadata": { "tags": [ "remove-output" ], "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "library(data.table) # the best data manipulation package\n", "library(stringr) # process strings\n", "library(magrittr) # pipe operator, e.g., \"%>%\"\n", "library(lubridate) # for date and time processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} About the Code Comments\n", "For instruction purpose, I tried to provide as many comments as possible, which may render the code unnecessarily long and scary. For example (Question 6):\n", "\n", "```{code-block} python\n", "data[\n", " # filter stocks\n", " retx>=0.05 | retx<=-0.05,\n", " ][, \n", " # create `direction` to indicate price change direction\n", " ':='(direction=ifelse(retx>=0.05, 'up5%+', 'down5%-'))\n", " ][, \n", " # count the stocks\n", " .(num_stk=uniqueN(permno)),\n", " keyby=.(date, direction)\n", " ][1:5]\n", "```\n", "\n", "However, if you take out the comments and rearrange the code, it'll be much more friendly:\n", "\n", "```{code-block} python\n", "data[retx>=0.05 | retx<=-0.05,\n", " ][, ':='(direction=ifelse(retx>=0.05, 'up5%+', 'down5%-'))\n", " ][, .(num_stk=uniqueN(permno)),\n", " keyby=.(date, direction)\n", " ][1:5]\n", "```\n", "\n", "````" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inspect the Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We select the price and trading data of all US common stocks from the CRSP database between 2023-01-01 and 2023-06-30. Each row represents a stock's daily observation. There're in total 4221 unique stocks with about 465k rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} Observation vs. Row\n", "In my book, I use \"observation\" and \"row\" interchangeably, which is common in the field of data science.\n", "\n", "As a related note, \"field,\" \"column,\" \"variable,\" and \"feature\" are also interchangeably to indicate a column of a table. While statisticians prefer \"variable\", data scientists use \"feature\" more often. \"Field\" and \"column\" are often seen in the context of database.\n", "```" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 14</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>date</th><th scope=col>ticker</th><th scope=col>open</th><th scope=col>high</th><th scope=col>low</th><th scope=col>close</th><th scope=col>cap</th><th scope=col>dlyfacprc</th><th scope=col>vol</th><th scope=col>prcvol</th><th scope=col>retx</th><th scope=col>industry</th><th scope=col>is_sp500</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><chr></th><th scope=col><lgl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>11790</td><td>2023-01-03</td><td>ALCO</td><td>23.91</td><td>24.02</td><td>23.42</td><td>23.83</td><td>180941.2</td><td>1</td><td>115286</td><td>2747265</td><td>-0.001675744</td><td>Agriculture, Forestry, Fishing and Hunting</td><td>FALSE</td></tr>\n", "\t<tr><td>11790</td><td>2023-01-04</td><td>ALCO</td><td>23.81</td><td>24.37</td><td>23.68</td><td>23.99</td><td>182156.1</td><td>1</td><td> 46646</td><td>1119038</td><td> 0.006714226</td><td>Agriculture, Forestry, Fishing and Hunting</td><td>FALSE</td></tr>\n", "\t<tr><td>11790</td><td>2023-01-05</td><td>ALCO</td><td>23.75</td><td>24.06</td><td>23.53</td><td>23.79</td><td>180637.5</td><td>1</td><td> 61673</td><td>1467201</td><td>-0.008336807</td><td>Agriculture, Forestry, Fishing and Hunting</td><td>FALSE</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 14\n", "\\begin{tabular}{llllllllllllll}\n", " permno & date & ticker & open & high & low & close & cap & dlyfacprc & vol & prcvol & retx & industry & is\\_sp500\\\\\n", " <dbl> & <date> & <chr> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <chr> & <lgl>\\\\\n", "\\hline\n", "\t 11790 & 2023-01-03 & ALCO & 23.91 & 24.02 & 23.42 & 23.83 & 180941.2 & 1 & 115286 & 2747265 & -0.001675744 & Agriculture, Forestry, Fishing and Hunting & FALSE\\\\\n", "\t 11790 & 2023-01-04 & ALCO & 23.81 & 24.37 & 23.68 & 23.99 & 182156.1 & 1 & 46646 & 1119038 & 0.006714226 & Agriculture, Forestry, Fishing and Hunting & FALSE\\\\\n", "\t 11790 & 2023-01-05 & ALCO & 23.75 & 24.06 & 23.53 & 23.79 & 180637.5 & 1 & 61673 & 1467201 & -0.008336807 & Agriculture, Forestry, Fishing and Hunting & FALSE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 14\n", "\n", "| permno <dbl> | date <date> | ticker <chr> | open <dbl> | high <dbl> | low <dbl> | close <dbl> | cap <dbl> | dlyfacprc <dbl> | vol <dbl> | prcvol <dbl> | retx <dbl> | industry <chr> | is_sp500 <lgl> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| 11790 | 2023-01-03 | ALCO | 23.91 | 24.02 | 23.42 | 23.83 | 180941.2 | 1 | 115286 | 2747265 | -0.001675744 | Agriculture, Forestry, Fishing and Hunting | FALSE |\n", "| 11790 | 2023-01-04 | ALCO | 23.81 | 24.37 | 23.68 | 23.99 | 182156.1 | 1 | 46646 | 1119038 | 0.006714226 | Agriculture, Forestry, Fishing and Hunting | FALSE |\n", "| 11790 | 2023-01-05 | ALCO | 23.75 | 24.06 | 23.53 | 23.79 | 180637.5 | 1 | 61673 | 1467201 | -0.008336807 | Agriculture, Forestry, Fishing and Hunting | FALSE |\n", "\n" ], "text/plain": [ " permno date ticker open high low close cap dlyfacprc vol \n", "1 11790 2023-01-03 ALCO 23.91 24.02 23.42 23.83 180941.2 1 115286\n", "2 11790 2023-01-04 ALCO 23.81 24.37 23.68 23.99 182156.1 1 46646\n", "3 11790 2023-01-05 ALCO 23.75 24.06 23.53 23.79 180637.5 1 61673\n", " prcvol retx industry is_sp500\n", "1 2747265 -0.001675744 Agriculture, Forestry, Fishing and Hunting FALSE \n", "2 1119038 0.006714226 Agriculture, Forestry, Fishing and Hunting FALSE \n", "3 1467201 -0.008336807 Agriculture, Forestry, Fishing and Hunting FALSE " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# read data\n", "data = readRDS(\"data/crsp.rds\")\n", "\n", "# convert to data.table\n", "setDT(data) \n", "\n", "# print the first 3 rows\n", "data[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- This is \"panel datak,\" meaning it contains multiple stocks (cross-sectional) with each stock has multiple observations ordered by date (time-series).\n", "\n", "- Each combination of `permno` (stock ID) and `date` determines a unique observation, i.e., they're the key.\n", "\n", "- The dataset is first sorted by `permno`, and then `date`\n", "\n", "- Column definition:\n", "\n", " - `permno`: The unique ID provided by CRSP.\n", " - `date`: Date\n", " - `ticker`: Ticker. Note that the ticker of a stock can change, and hence it's not recommended to use `ticker` as stock ID.\n", " - `industry`: The industry classification determined using the first two digits of NAICS industry classification code. See the [official document](https://www.census.gov/naics/?58967?yearbck=2022) for more details.\n", " - `is_sp500`: `TRUE` if the stock is in the S&P 500 list by the mid 2023; `FALSE` if not.\n", " - `open`: Daily open\n", " - `high`: Daily high\n", " - `low`: Daily low\n", " - `cap`: Daily market capital (in $1000)\n", " - `dlyfacprc`: Daily factor to adjust for price. (We won't use it in the practice.)\n", " - `vol`: Trading volume **(number of shares traded)**\n", " - `prcvol`: **Dollar** trading volume, calculated as the number of shares traded multiplied by the price per share.\n", " - `retx`: Daily return (excluding dividend)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{warning}\n", "Without specifying otherwise, we mean `prcvol` (dollar trading volume) whenever we say trading volume. The reason is simple: only dollar trading volume are comparable across different stocks.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Find out all stocks whose ticker contains `\"COM\"`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use the `str_detect` function (from the `stringr` package) to find out all ticksers that have a `\"COM\"`. But that's only half the way. Without deduplicating (the `unique()` function), we will end up with numerous duplicate observations whose `permno` and `ticker` are the same but only differ in `date.`\n", "\n", "We provide no arguments to `unique()`, meaning R needs to consider all columns." ] }, { "cell_type": "code", "execution_count": 136, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 6 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>ticker</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><chr></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>77178</td><td>QCOM</td></tr>\n", "\t<tr><td>20421</td><td>COMS</td></tr>\n", "\t<tr><td>14239</td><td>COMM</td></tr>\n", "\t<tr><td>19136</td><td>MCOM</td></tr>\n", "\t<tr><td>92716</td><td>DCOM</td></tr>\n", "\t<tr><td>20916</td><td>COMP</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 6 × 2\n", "\\begin{tabular}{ll}\n", " permno & ticker\\\\\n", " <dbl> & <chr>\\\\\n", "\\hline\n", "\t 77178 & QCOM\\\\\n", "\t 20421 & COMS\\\\\n", "\t 14239 & COMM\\\\\n", "\t 19136 & MCOM\\\\\n", "\t 92716 & DCOM\\\\\n", "\t 20916 & COMP\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 6 × 2\n", "\n", "| permno <dbl> | ticker <chr> |\n", "|---|---|\n", "| 77178 | QCOM |\n", "| 20421 | COMS |\n", "| 14239 | COMM |\n", "| 19136 | MCOM |\n", "| 92716 | DCOM |\n", "| 20916 | COMP |\n", "\n" ], "text/plain": [ " permno ticker\n", "1 77178 QCOM \n", "2 20421 COMS \n", "3 14239 COMM \n", "4 19136 MCOM \n", "5 92716 DCOM \n", "6 20916 COMP " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[str_detect(ticker, 'COM'), .(permno, ticker)\n", " ] %>% unique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution explained:**\n", "\n", "- First, R executes `str_detect(ticker, 'COM')` to filter out the needed rows.\n", "- Then, it only keeps two columns, `.(permno, ticker)`. By then, we have many duplicates.\n", "- Lastly, we use `unique()` to remove the duplicates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How many stocks on each day have positive/negative returns?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we want to count the stocks *on each day*, We need to first group all stocks by `date` and then by the return sign (positive vs. negative). Next, we need to generate two counts: one for the up (positive return) and the other for the down (negative). We leverage the `sign` of `retx` (return) to determine the direction of price change." ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>direction</th><th scope=col>num</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>-1</td><td>1843</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 0</td><td> 97</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 1</td><td>1834</td></tr>\n", "\t<tr><td>2023-01-04</td><td>-1</td><td> 993</td></tr>\n", "\t<tr><td>2023-01-04</td><td> 0</td><td> 103</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & direction & num\\\\\n", " <date> & <dbl> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & -1 & 1843\\\\\n", "\t 2023-01-03 & 0 & 97\\\\\n", "\t 2023-01-03 & 1 & 1834\\\\\n", "\t 2023-01-04 & -1 & 993\\\\\n", "\t 2023-01-04 & 0 & 103\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | direction <dbl> | num <int> |\n", "|---|---|---|\n", "| 2023-01-03 | -1 | 1843 |\n", "| 2023-01-03 | 0 | 97 |\n", "| 2023-01-03 | 1 | 1834 |\n", "| 2023-01-04 | -1 | 993 |\n", "| 2023-01-04 | 0 | 103 |\n", "\n" ], "text/plain": [ " date direction num \n", "1 2023-01-03 -1 1843\n", "2 2023-01-03 0 97\n", "3 2023-01-03 1 1834\n", "4 2023-01-04 -1 993\n", "5 2023-01-04 0 103" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# step 1: create a new column \"direction\" which indicates the direction of the price change\n", "data_q2 = data[, ':='(direction = sign(retx))]\n", "data_q2 = data_q2[!is.na(direction)]\n", "\n", "# step 2: count the stocks with +/- returns on each day\n", "data_q2[, .(num=uniqueN(permno)), keyby=.(date, direction)][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- On each day we have three observations (rows), one for each direction (1, 0, and -1) of the price change. For example, on 2023-01-03, there're 1844 stocks with positive return.\n", "\n", "**Execution Explained:**\n", "\n", "- In Step 1, we create a new column `direction` to indicate whether we're counting the up/down/no-move stocks for each day. Its value is 1, 0, or -1, depending on the sign of `retx`.\n", "\n", "- In Step 2, I group all observations by `date` (since we want statistics for each day) and then `direction`. `uniqueN()` is used to count the unique stock IDs within each group." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, we don't need to split the code explicitly. One great thing about `data.table` is that it can chain multiple computation steps together using `[`. In the following code, I put the creation of `direction` in the `key` argument." ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>direction</th><th scope=col>num</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>-1</td><td>1843</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 0</td><td> 97</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 1</td><td>1834</td></tr>\n", "\t<tr><td>2023-01-04</td><td>-1</td><td> 993</td></tr>\n", "\t<tr><td>2023-01-04</td><td> 0</td><td> 103</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & direction & num\\\\\n", " <date> & <dbl> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & -1 & 1843\\\\\n", "\t 2023-01-03 & 0 & 97\\\\\n", "\t 2023-01-03 & 1 & 1834\\\\\n", "\t 2023-01-04 & -1 & 993\\\\\n", "\t 2023-01-04 & 0 & 103\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | direction <dbl> | num <int> |\n", "|---|---|---|\n", "| 2023-01-03 | -1 | 1843 |\n", "| 2023-01-03 | 0 | 97 |\n", "| 2023-01-03 | 1 | 1834 |\n", "| 2023-01-04 | -1 | 993 |\n", "| 2023-01-04 | 0 | 103 |\n", "\n" ], "text/plain": [ " date direction num \n", "1 2023-01-03 -1 1843\n", "2 2023-01-03 0 97\n", "3 2023-01-03 1 1834\n", "4 2023-01-04 -1 993\n", "5 2023-01-04 0 103" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# chain the two steps together (same results)\n", "data[, .(num=uniqueN(permno)),\n", " keyby=.(date, direction=sign(retx))\n", " ][!is.na(direction)][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} You're encourage to chain the steps!\n", "As you can see, the chained version saves a lot of key strokes for you. Personally, I usually chain dozens of lines of code when I prototype. Once you get familar with `data.table`, the chained brackets will not look confusing but elegant and straightforward to you.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How many stocks on each day have positive/negative returns (only select stocks with market capital larger than $100M)?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q3 is very similar to Q2, the only change is to use `cap` to filter stocks by market capital. Note that `cap` is in 1000 dollars." ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>direction</th><th scope=col>num</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>-1</td><td>1577</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 0</td><td> 72</td></tr>\n", "\t<tr><td>2023-01-03</td><td> 1</td><td>1454</td></tr>\n", "\t<tr><td>2023-01-04</td><td>-1</td><td> 806</td></tr>\n", "\t<tr><td>2023-01-04</td><td> 0</td><td> 68</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & direction & num\\\\\n", " <date> & <dbl> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & -1 & 1577\\\\\n", "\t 2023-01-03 & 0 & 72\\\\\n", "\t 2023-01-03 & 1 & 1454\\\\\n", "\t 2023-01-04 & -1 & 806\\\\\n", "\t 2023-01-04 & 0 & 68\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | direction <dbl> | num <int> |\n", "|---|---|---|\n", "| 2023-01-03 | -1 | 1577 |\n", "| 2023-01-03 | 0 | 72 |\n", "| 2023-01-03 | 1 | 1454 |\n", "| 2023-01-04 | -1 | 806 |\n", "| 2023-01-04 | 0 | 68 |\n", "\n" ], "text/plain": [ " date direction num \n", "1 2023-01-03 -1 1577\n", "2 2023-01-03 0 72\n", "3 2023-01-03 1 1454\n", "4 2023-01-04 -1 806\n", "5 2023-01-04 0 68" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# we only add the condition \"cap>=1e5\" to the first step\n", "# the rest is the same\n", "\n", "data[cap>=1e5, \n", " .(num=uniqueN(permno)),\n", " keyby=.(date, direction=sign(retx))\n", " ][!is.na(direction)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How many stocks are there in each industry on each day?" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>num</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td> 64</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>190</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td> 9</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & num\\\\\n", " <date> & <chr> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 64\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 190\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 9\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | industry <chr> | num <int> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 64 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 190 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 9 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry num\n", "1 Accommodation and Food Services 64\n", "2 Administrative and Support and Waste Management and Remediation Services 190\n", "3 Agriculture, Forestry, Fishing and Hunting 9" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, .(num=uniqueN(permno)),\n", " keyby=.(date, industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- We first group by `date` and `industry`, then we count the unique stock IDs within each gruop using `uniqueN()`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rank industries by their average daily number of stocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This question is very similar to the previous one. But instead of counting stock numbers for each day, we will generate an average daily stock number through out the whole sample period and use that to rank the industries." ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>industry</th><th scope=col>num_stk</th></tr>\n", "\t<tr><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>Manufacturing </td><td>1276</td></tr>\n", "\t<tr><td>Finance and Insurance </td><td> 781</td></tr>\n", "\t<tr><td>Professional, Scientific, and Technical Services</td><td> 299</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " industry & num\\_stk\\\\\n", " <chr> & <dbl>\\\\\n", "\\hline\n", "\t Manufacturing & 1276\\\\\n", "\t Finance and Insurance & 781\\\\\n", "\t Professional, Scientific, and Technical Services & 299\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| industry <chr> | num_stk <dbl> |\n", "|---|---|\n", "| Manufacturing | 1276 |\n", "| Finance and Insurance | 781 |\n", "| Professional, Scientific, and Technical Services | 299 |\n", "\n" ], "text/plain": [ " industry num_stk\n", "1 Manufacturing 1276 \n", "2 Finance and Insurance 781 \n", "3 Professional, Scientific, and Technical Services 299 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # step 1: count the number of stocks in each industry on each day\n", " .(num_stk=uniqueN(permno)), keyby=.(industry, date)\n", " # step 2: get the average number of stocks in each industry\n", " ][, .(num_stk=round(mean(num_stk))), keyby=.(industry)\n", " # step 3: sort the industries descendingly by the average number of stocks\n", " ][order(-num_stk)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained**\n", "\n", "- We first count the number of stocks for each industry on each day, as in the previous problem.\n", "\n", "- We then compute the average stock number across the sample period. Note this time we group by `industry` instead of `industry` and `date`, this is because we want to average across the whole sample period.\n", "\n", "- `order(-num_stk)` sort the results. the minus sign `-` indicates sorting desendingly. Without `-` (the default) the sort is ascending." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## On each day, is the industry with the most stocks also generates the largest trading volume?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some industries contain a lot of stocks, but it may not generate the largest trading volume (in dollars) because the component stocks may be very small. Let's check if this claim is correct or not." ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 2 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol</th><th scope=col>num_stk</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>4899714547</td><td> 64</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>8977070704</td><td>190</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 2 × 4\n", "\\begin{tabular}{llll}\n", " date & industry & prcvol & num\\_stk\\\\\n", " <date> & <chr> & <dbl> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 4899714547 & 64\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 8977070704 & 190\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 2 × 4\n", "\n", "| date <date> | industry <chr> | prcvol <dbl> | num_stk <int> |\n", "|---|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 4899714547 | 64 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 8977070704 | 190 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", " prcvol num_stk\n", "1 4899714547 64 \n", "2 8977070704 190 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# for each industry, compute the total trading volume and stock number on each day\n", "# we use `prcvol` as the trading volume\n", "data_tmp = data[, \n", " .(prcvol=sum(prcvol), num_stk=uniqueN(permno)), \n", " keyby=.(date, industry)]\n", " \n", "data_tmp[1:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- `prcvol`: Total price trading volume\n", "- `num_stk`: number of stocks in this industry." ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 2 × 6</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol</th><th scope=col>num_stk</th><th scope=col>is_max_num_stk</th><th scope=col>is_max_prcvol</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><int></th><th scope=col><lgl></th><th scope=col><lgl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>4899714547</td><td> 64</td><td>FALSE</td><td>FALSE</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>8977070704</td><td>190</td><td>FALSE</td><td>FALSE</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 2 × 6\n", "\\begin{tabular}{llllll}\n", " date & industry & prcvol & num\\_stk & is\\_max\\_num\\_stk & is\\_max\\_prcvol\\\\\n", " <date> & <chr> & <dbl> & <int> & <lgl> & <lgl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 4899714547 & 64 & FALSE & FALSE\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 8977070704 & 190 & FALSE & FALSE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 2 × 6\n", "\n", "| date <date> | industry <chr> | prcvol <dbl> | num_stk <int> | is_max_num_stk <lgl> | is_max_prcvol <lgl> |\n", "|---|---|---|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 4899714547 | 64 | FALSE | FALSE |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 8977070704 | 190 | FALSE | FALSE |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", " prcvol num_stk is_max_num_stk is_max_prcvol\n", "1 4899714547 64 FALSE FALSE \n", "2 8977070704 190 FALSE FALSE " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# for each day and each industry, we create two new columns:\n", "# - `is_max_num_stk` indicates if this industry has the largest number of stocks\n", "# - `is_max_prcvol` indicates if this industry has the largest trading volume\n", "\n", "data_tmp = data_tmp[, \n", " ':='(is_max_num_stk = num_stk == max(num_stk),\n", " is_max_prcvol = prcvol == max(prcvol)),\n", " keyby=.(date)]\n", " \n", "data_tmp[1:2]" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 6</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol</th><th scope=col>num_stk</th><th scope=col>is_max_num_stk</th><th scope=col>is_max_prcvol</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><int></th><th scope=col><lgl></th><th scope=col><lgl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Manufacturing</td><td>120272008757</td><td>1271</td><td>TRUE</td><td>TRUE</td></tr>\n", "\t<tr><td>2023-01-04</td><td>Manufacturing</td><td>122987139599</td><td>1275</td><td>TRUE</td><td>TRUE</td></tr>\n", "\t<tr><td>2023-01-05</td><td>Manufacturing</td><td>113584385443</td><td>1271</td><td>TRUE</td><td>TRUE</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 6\n", "\\begin{tabular}{llllll}\n", " date & industry & prcvol & num\\_stk & is\\_max\\_num\\_stk & is\\_max\\_prcvol\\\\\n", " <date> & <chr> & <dbl> & <int> & <lgl> & <lgl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Manufacturing & 120272008757 & 1271 & TRUE & TRUE\\\\\n", "\t 2023-01-04 & Manufacturing & 122987139599 & 1275 & TRUE & TRUE\\\\\n", "\t 2023-01-05 & Manufacturing & 113584385443 & 1271 & TRUE & TRUE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 6\n", "\n", "| date <date> | industry <chr> | prcvol <dbl> | num_stk <int> | is_max_num_stk <lgl> | is_max_prcvol <lgl> |\n", "|---|---|---|---|---|---|\n", "| 2023-01-03 | Manufacturing | 120272008757 | 1271 | TRUE | TRUE |\n", "| 2023-01-04 | Manufacturing | 122987139599 | 1275 | TRUE | TRUE |\n", "| 2023-01-05 | Manufacturing | 113584385443 | 1271 | TRUE | TRUE |\n", "\n" ], "text/plain": [ " date industry prcvol num_stk is_max_num_stk is_max_prcvol\n", "1 2023-01-03 Manufacturing 120272008757 1271 TRUE TRUE \n", "2 2023-01-04 Manufacturing 122987139599 1275 TRUE TRUE \n", "3 2023-01-05 Manufacturing 113584385443 1271 TRUE TRUE " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# for each day, we select the industry with the largest number of stocks,\n", "# and `is_max_prcvol` will tell us if this industry has the largest trading volume\n", "data_tmp[is_max_num_stk==T][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- Each day only has one observation in the final output. It tells us what's the industry with the largest number of stocks (`is_max_num_stk==T`), and `is_max_prcvol` tells us if this industry generates the largest trading volume.\n", "\n", "- As you can see, \"Manufacturing\" is the industry with the most stocks, and it generates the largest price trading volume." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Chaining everything together, we can rewrite the above code as:" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 6</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol</th><th scope=col>num_stk</th><th scope=col>is_max_num_stk</th><th scope=col>is_max_prcvol</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><int></th><th scope=col><lgl></th><th scope=col><lgl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Manufacturing</td><td>120272008757</td><td>1271</td><td>TRUE</td><td>TRUE</td></tr>\n", "\t<tr><td>2023-01-04</td><td>Manufacturing</td><td>122987139599</td><td>1275</td><td>TRUE</td><td>TRUE</td></tr>\n", "\t<tr><td>2023-01-05</td><td>Manufacturing</td><td>113584385443</td><td>1271</td><td>TRUE</td><td>TRUE</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 6\n", "\\begin{tabular}{llllll}\n", " date & industry & prcvol & num\\_stk & is\\_max\\_num\\_stk & is\\_max\\_prcvol\\\\\n", " <date> & <chr> & <dbl> & <int> & <lgl> & <lgl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Manufacturing & 120272008757 & 1271 & TRUE & TRUE\\\\\n", "\t 2023-01-04 & Manufacturing & 122987139599 & 1275 & TRUE & TRUE\\\\\n", "\t 2023-01-05 & Manufacturing & 113584385443 & 1271 & TRUE & TRUE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 6\n", "\n", "| date <date> | industry <chr> | prcvol <dbl> | num_stk <int> | is_max_num_stk <lgl> | is_max_prcvol <lgl> |\n", "|---|---|---|---|---|---|\n", "| 2023-01-03 | Manufacturing | 120272008757 | 1271 | TRUE | TRUE |\n", "| 2023-01-04 | Manufacturing | 122987139599 | 1275 | TRUE | TRUE |\n", "| 2023-01-05 | Manufacturing | 113584385443 | 1271 | TRUE | TRUE |\n", "\n" ], "text/plain": [ " date industry prcvol num_stk is_max_num_stk is_max_prcvol\n", "1 2023-01-03 Manufacturing 120272008757 1271 TRUE TRUE \n", "2 2023-01-04 Manufacturing 122987139599 1275 TRUE TRUE \n", "3 2023-01-05 Manufacturing 113584385443 1271 TRUE TRUE " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # compute total trading volume and stock number \n", " .(prcvol=sum(prcvol), num_stk=uniqueN(permno)), \n", " keyby=.(date, industry)\n", " ][,\n", " # add indicators\n", " ':='(is_max_num_stk = num_stk == max(num_stk),\n", " is_max_prcvol = prcvol == max(prcvol)),\n", " keyby=.(date)\n", " ][\n", " # only keep the industry with the largest number of stocks\n", " is_max_num_stk==T][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How many stocks on each day have a gain of more than 5% and a loss more than 5%?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to Q2, the key is to group the stocks by `date` and the `direction` of change." ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>direction</th><th scope=col>num_stk</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>down5%-</td><td>260</td></tr>\n", "\t<tr><td>2023-01-03</td><td>up5%+ </td><td>250</td></tr>\n", "\t<tr><td>2023-01-04</td><td>down5%-</td><td> 86</td></tr>\n", "\t<tr><td>2023-01-04</td><td>up5%+ </td><td>499</td></tr>\n", "\t<tr><td>2023-01-05</td><td>down5%-</td><td>313</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & direction & num\\_stk\\\\\n", " <date> & <chr> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & down5\\%- & 260\\\\\n", "\t 2023-01-03 & up5\\%+ & 250\\\\\n", "\t 2023-01-04 & down5\\%- & 86\\\\\n", "\t 2023-01-04 & up5\\%+ & 499\\\\\n", "\t 2023-01-05 & down5\\%- & 313\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | direction <chr> | num_stk <int> |\n", "|---|---|---|\n", "| 2023-01-03 | down5%- | 260 |\n", "| 2023-01-03 | up5%+ | 250 |\n", "| 2023-01-04 | down5%- | 86 |\n", "| 2023-01-04 | up5%+ | 499 |\n", "| 2023-01-05 | down5%- | 313 |\n", "\n" ], "text/plain": [ " date direction num_stk\n", "1 2023-01-03 down5%- 260 \n", "2 2023-01-03 up5%+ 250 \n", "3 2023-01-04 down5%- 86 \n", "4 2023-01-04 up5%+ 499 \n", "5 2023-01-05 down5%- 313 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # filter stocks\n", " retx>=0.05 | retx<=-0.05,\n", " ][, \n", " # create `direction` to indicate price change direction\n", " ':='(direction=ifelse(retx>=0.05, 'up5%+', 'down5%-'))\n", " ][, \n", " # count the stocks\n", " .(num_stk=uniqueN(permno)),\n", " keyby=.(date, direction)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- `retx>=0.05 | retx<=-0.05`: Only select observations with the required retx range. This will reduce the data size for the next step, and hence speed up the execution.\n", "\n", "- `':='(direction=ifelse(retx>=0.05, 'up5%+', 'down5%-'))`: We use `ifelse` to indicate the direction: if `retx>=0.05` is met, then `direction` is \"up5%+\", otherwise, the value is \"down5%-\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What are the daily total trading volume of the top 10 stocks with the largest daily increase and the top 10 stocks with the largest daily decrease?" ] }, { "cell_type": "code", "execution_count": 147, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>bottom10</th><th scope=col>top10</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td> 77247969</td><td> 645893614</td></tr>\n", "\t<tr><td>2023-01-04</td><td>414792118</td><td>1239987199</td></tr>\n", "\t<tr><td>2023-01-05</td><td>582416153</td><td> 257844857</td></tr>\n", "\t<tr><td>2023-01-06</td><td>345656464</td><td> 502542099</td></tr>\n", "\t<tr><td>2023-01-09</td><td>419735680</td><td>1452063611</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & bottom10 & top10\\\\\n", " <date> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & 77247969 & 645893614\\\\\n", "\t 2023-01-04 & 414792118 & 1239987199\\\\\n", "\t 2023-01-05 & 582416153 & 257844857\\\\\n", "\t 2023-01-06 & 345656464 & 502542099\\\\\n", "\t 2023-01-09 & 419735680 & 1452063611\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | bottom10 <dbl> | top10 <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | 77247969 | 645893614 |\n", "| 2023-01-04 | 414792118 | 1239987199 |\n", "| 2023-01-05 | 582416153 | 257844857 |\n", "| 2023-01-06 | 345656464 | 502542099 |\n", "| 2023-01-09 | 419735680 | 1452063611 |\n", "\n" ], "text/plain": [ " date bottom10 top10 \n", "1 2023-01-03 77247969 645893614\n", "2 2023-01-04 414792118 1239987199\n", "3 2023-01-05 582416153 257844857\n", "4 2023-01-06 345656464 502542099\n", "5 2023-01-09 419735680 1452063611" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # sort by date and retx\n", " order(date, retx)\n", " ][, \n", " # compute the total trading volumn of the top/bottom 10\n", " .(bottom10 = sum(prcvol[1:10]), top10 = sum(prcvol[(.N-9):.N])),\n", " keyby=.(date)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- We first sort (ascendingly) by `date` and `retx`, this facilitates us to select the top/bottom 10 stocks based on `retx`.\n", "\n", "- `bottom10 = sum(prcvol[1:10])`: This line compute the total trading volume for the bottom 10 stocks.\n", "\n", " - `prcvol[1:10]`: It selects the first 10 elements of the `prcvol` vector. Remember that previously we've sorted by `retx`, so these 10 observations **also** has the smallest (i.e., most negative) `retx` value .\n", "\n", " - `sum()`: Compute the total trading volume for these 10 observations.\n", "\n", "- `top10 = sum(prcvol[(.N-9):.N])`: Similar to the above, except that we use a special symbol `.N`. `.N` is unique to `data.table`, and it equals to **the number of observations in each group**. Since we have group the observations by `date` (`keyby=.(date)`), then `.N` equals to the number of observations on each day.\n", "\n", " - `prcvol[(.N-9):.N]`: It selects the **last** 10 elements of the `prcvol` vector. Since within each date, the data is sorted ascendingly by `retx`, so `prcvol[(.N-9):.N]` is also the 10 observations with the largest (most positive) `retx`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How many stocks increase by 5% at the time of open and close?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, good news is released to the market over the night when the market is closed. In this case, price can only react to the news when the market opens in the next day. Instead, if the news is released during trading hours, then the market will react to it immediately. Let's count how many stocks fall in these two categories." ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_open</th><th scope=col>num_close</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td> 7</td><td>250</td></tr>\n", "\t<tr><td>2023-01-04</td><td>56</td><td>499</td></tr>\n", "\t<tr><td>2023-01-05</td><td>35</td><td>143</td></tr>\n", "\t<tr><td>2023-01-06</td><td>51</td><td>390</td></tr>\n", "\t<tr><td>2023-01-09</td><td>93</td><td>350</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & num\\_open & num\\_close\\\\\n", " <date> & <int> & <int>\\\\\n", "\\hline\n", "\t 2023-01-03 & 7 & 250\\\\\n", "\t 2023-01-04 & 56 & 499\\\\\n", "\t 2023-01-05 & 35 & 143\\\\\n", "\t 2023-01-06 & 51 & 390\\\\\n", "\t 2023-01-09 & 93 & 350\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | num_open <int> | num_close <int> |\n", "|---|---|---|\n", "| 2023-01-03 | 7 | 250 |\n", "| 2023-01-04 | 56 | 499 |\n", "| 2023-01-05 | 35 | 143 |\n", "| 2023-01-06 | 51 | 390 |\n", "| 2023-01-09 | 93 | 350 |\n", "\n" ], "text/plain": [ " date num_open num_close\n", "1 2023-01-03 7 250 \n", "2 2023-01-04 56 499 \n", "3 2023-01-05 35 143 \n", "4 2023-01-06 51 390 \n", "5 2023-01-09 93 350 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # `pre_close` is the previous day's close price\n", " ':='(pre_close=shift(close, 1)),\n", " keyby=.(permno)\n", " ][,\n", " # create a new column `ret_open` representing the return at the time of open\n", " ':='(ret_open = open/pre_close -1)\n", " ][,\n", " .(num_open=sum(ret_open>=0.05, na.rm=T),\n", " num_close=sum(retx>=0.05, na.rm=T)),\n", " keyby=.(date)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- `num_open`: Number of stocks that increase by 5% on the time of open, i.e., (\"open price\" / \"prev day's close\") - 1 >= 0.05\n", "- `num_close`: Number of stocks that increase by 5% at the time of close, i.e., (\"close price\" / \"prev day's close\") - 1 >= 0.05\n", "\n", "**Execution Explained:**\n", "\n", "- `':='(pre_close=shift(close, 1))`: We use the `shift` function (from `data.table`) to create a new column representing the previous day's close price. `1` means the lag is only one period.\n", "\n", "- `':='(ret_open = open/pre_close -1)`: `ret_open` is the return on the time of open.\n", "\n", "- `num_open=sum(ret_open>=0.05, na.rm=T)`: `num_open` is the number of stocks that increase by 5% on the time of open. The result of `ret_open>=0.05` is a **boolean vector** (whose elements can only be `TRUE` or `FALSE`) with the same length as `ret_open`. `sum(ret_open>=0.05, na.rm=T)` will add up all the elements in this boolean vector, where `TRUE` is treated as 1 and `FALSE` is treated as 0. Basically, what `sum(ret_open>=0.05, na.rm=T)` does is to count the number of elements in `ret_open` that are larger than 0.05. `na.rm=T` is used to ignore `NA`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{tip} \n", "How to **conditionally** count elements in a vector? Suppose we have a vector `x = c(1, 2, 3, 4 ,5)`. How to quickly count the elements in `x` that are no smaller than 3?\n", "\n", "```r\n", "sum(x>=3)\n", "```\n", "````" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the correlation coefficient between trading volume and return?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Does more people trading a stock necessarily push the stock price higher or lower? Let's check this claim!" ] }, { "cell_type": "code", "execution_count": 149, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>corr1</th><th scope=col>corr2</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>0.03158209</td><td>0.005071771</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 2\n", "\\begin{tabular}{ll}\n", " corr1 & corr2\\\\\n", " <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 0.03158209 & 0.005071771\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 2\n", "\n", "| corr1 <dbl> | corr2 <dbl> |\n", "|---|---|\n", "| 0.03158209 | 0.005071771 |\n", "\n" ], "text/plain": [ " corr1 corr2 \n", "1 0.03158209 0.005071771" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Step 1: compute two versions of volume: vol and vol_change\n", "data_q10 = data[order(permno, date)\n", " ][, .(vol, vol_change = vol - shift(vol), retx, cap), \n", " keyby=.(permno)\n", " ] %>% na.omit()\n", " \n", "# Step 2: compute corr\n", "data_q10[, \n", " # compute corr for individual stocks\n", " .(corr1=cor(vol, retx), corr2=cor(vol_change, retx), weight=mean(cap)), \n", " keyby=.(permno)\n", " # compute the weighted average of corr\n", " ][, .(corr1=weighted.mean(corr1, weight, na.rm=T), \n", " corr2=weighted.mean(corr2, weight, na.rm=T))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- Well, the correlation between trading volume and return is very low: from 0.005 to 0.03, depending on how the trading volume is defined.\n", "\n", "**Execution Explained:**\n", "\n", "- In Step 1, we create two versions of trading volume: `vol`, which is its raw value, and `vol_change`, which is the change in vol.\n", "\n", "- We use `na.omit` (from `data.table`) to remove any line that contains an `NA`.\n", "\n", "- In Step 2, we first compute the corr for each individual stock, resulting in `corr1` and `corr2`. We also create a new column `weight` which is the average market capital of each stock. `weight` will be used when we compute the weighted average of corr coefficients.\n", "\n", "- We use `weighted.mean` to compute the weighted average of corr." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the correlation between trading volume and the *magnitude* of return?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From Q10 we've learned that trading volume and return is rarely correlated. However, trading volume may still be correlated with the **magnitude** of return: the more people trade a stock, the larger the price movement (either direction) could be. Let's verify this hypothesis!\n", "\n", "We'll use focus on individual-stock-level (instead of industry-level) this time.\n", "\n", "Instead of using `retx` (return), we'll use `abs(retx)` (the absolute value of `retx`) to represent its magnitude." ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "0.460354725740955" ], "text/latex": [ "0.460354725740955" ], "text/markdown": [ "0.460354725740955" ], "text/plain": [ "[1] 0.4603547" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # compute the individual-level correlation, also compute the average market cap\n", " .(corr=cor(vol, abs(retx)), weight=mean(cap)), keyby=.(permno)\n", " # compute the weighted average of corr across stocks\n", " ][, weighted.mean(corr, weight, na.rm=T)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- Wow! Now the correlation is much strong: jump from less than 0.05 to 0.46!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the correlation between industry-level trading volume and industry-level return?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q11 is an extension of Q10. Instead of focusing on individual stocks, we aggregate stocks within the same industry. Will such aggregation improve the correlation?\n", "\n", "Again, we use the first two digits of `naics` as a proxy of industry." ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 1</caption>\n", "<thead>\n", "\t<tr><th scope=col>corr</th></tr>\n", "\t<tr><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>0.2841868</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 1\n", "\\begin{tabular}{l}\n", " corr\\\\\n", " <dbl>\\\\\n", "\\hline\n", "\t 0.2841868\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 1\n", "\n", "| corr <dbl> |\n", "|---|\n", "| 0.2841868 |\n", "\n" ], "text/plain": [ " corr \n", "1 0.2841868" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: create industry-level volume, return and market cap\n", " ][, .(ind_vol=sum(vol, na.rm=T), ind_ret=weighted.mean(abs(retx), cap, na.rm=T),\n", " ind_cap=sum(cap, na.rm=T)), \n", " keyby=.(industry, date)\n", " # Step 2: calculate the correlation for each industry\n", " ][, .(corr=cor(ind_vol, ind_ret), ind_cap=mean(ind_cap)), keyby=.(industry)\n", " # Step 3: compute the weighted average of corr\n", " ][, .(corr=weighted.mean(corr, ind_cap, na.rm=T))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "- Well, the correlation is weaker 😂.\n", "\n", "**Execution Explained:**\n", "\n", "- In Step 1, we compute the industry-level trading volume, return, and market cap on each day\n", "\n", " - `ind_vol`: Industry volume is defined as the sum of all trading volume within the industry. \n", " \n", " - `ind_ret`: Industry return `ind_ret` is defined as the value weighted average of all stock returns within the industry. We use market capital `cap` as the weight.\n", "\n", " - `ind_cap`: The industry's daily aggregated market capital. We'll use it in the later stage.\n", "\n", "- In Step 2, we compute the correlation coefficient for each industry. Note that we also create a new `ind_cap` defined as `ind_cap=mean(ind_cap)`. It's the average market capital of the industry.\n", "\n", "- In Step 3, we compute the weighted average of corr. We use `ind_cap` calculated in Step 3 as the weight." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the industry distribution of S&P 500 stocks?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem requires us to count the number of S&P 500 stocks in each industry." ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>industry</th><th scope=col>num_stk</th></tr>\n", "\t<tr><th scope=col><chr></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>Manufacturing </td><td>233</td></tr>\n", "\t<tr><td>Finance and Insurance</td><td> 78</td></tr>\n", "\t<tr><td>Information </td><td> 42</td></tr>\n", "\t<tr><td>Retail Trade </td><td> 41</td></tr>\n", "\t<tr><td>Utilities </td><td> 35</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 2\n", "\\begin{tabular}{ll}\n", " industry & num\\_stk\\\\\n", " <chr> & <int>\\\\\n", "\\hline\n", "\t Manufacturing & 233\\\\\n", "\t Finance and Insurance & 78\\\\\n", "\t Information & 42\\\\\n", "\t Retail Trade & 41\\\\\n", "\t Utilities & 35\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 2\n", "\n", "| industry <chr> | num_stk <int> |\n", "|---|---|\n", "| Manufacturing | 233 |\n", "| Finance and Insurance | 78 |\n", "| Information | 42 |\n", "| Retail Trade | 41 |\n", "| Utilities | 35 |\n", "\n" ], "text/plain": [ " industry num_stk\n", "1 Manufacturing 233 \n", "2 Finance and Insurance 78 \n", "3 Information 42 \n", "4 Retail Trade 41 \n", "5 Utilities 35 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # select the S&P 500 stocks\n", " is_sp500==T, \n", " # count the number of stocks in each industry\n", " .(num_stk=uniqueN(permno)), \n", " # group by industry\n", " keyby=.(industry)\n", " ][\n", " # sort descendingly by the number of stocks\n", " order(-num_stk)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(industry-volatility)=\n", "## What's the return volatility of each industry?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first compute industry-level return on each day and then computes its volatility." ] }, { "cell_type": "code", "execution_count": 153, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>industry</th><th scope=col>volatility</th></tr>\n", "\t<tr><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>Public Administration </td><td>0.01895026</td></tr>\n", "\t<tr><td>Real Estate and Rental and Leasing </td><td>0.01705378</td></tr>\n", "\t<tr><td>Mining, Quarrying, and Oil and Gas Extraction</td><td>0.01577619</td></tr>\n", "\t<tr><td>Management of Companies and Enterprises </td><td>0.01576541</td></tr>\n", "\t<tr><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.01453754</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 2\n", "\\begin{tabular}{ll}\n", " industry & volatility\\\\\n", " <chr> & <dbl>\\\\\n", "\\hline\n", "\t Public Administration & 0.01895026\\\\\n", "\t Real Estate and Rental and Leasing & 0.01705378\\\\\n", "\t Mining, Quarrying, and Oil and Gas Extraction & 0.01577619\\\\\n", "\t Management of Companies and Enterprises & 0.01576541\\\\\n", "\t Agriculture, Forestry, Fishing and Hunting & 0.01453754\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 2\n", "\n", "| industry <chr> | volatility <dbl> |\n", "|---|---|\n", "| Public Administration | 0.01895026 |\n", "| Real Estate and Rental and Leasing | 0.01705378 |\n", "| Mining, Quarrying, and Oil and Gas Extraction | 0.01577619 |\n", "| Management of Companies and Enterprises | 0.01576541 |\n", "| Agriculture, Forestry, Fishing and Hunting | 0.01453754 |\n", "\n" ], "text/plain": [ " industry volatility\n", "1 Public Administration 0.01895026\n", "2 Real Estate and Rental and Leasing 0.01705378\n", "3 Mining, Quarrying, and Oil and Gas Extraction 0.01577619\n", "4 Management of Companies and Enterprises 0.01576541\n", "5 Agriculture, Forestry, Fishing and Hunting 0.01453754" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: compute industry return on each day\n", " .(ind_ret=weighted.mean(retx, cap, na.rm=T)), \n", " keyby=.(industry, date)\n", " ][, \n", " # Step 2: compute the standard deviation of industry return\n", " .(volatility=sd(ind_ret)), keyby=.(industry)\n", " # sort results\n", " ][order(-volatility)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: We first group the data by `industry` and `date`, this allows us to compute the industry return on each day. The industry return is calculated as the weighted mean (`weighted.mean`) of the component stocks, where the weight is stock's capital market (`cap`).\n", "\n", "- Step 2: We define return volatility as the standard deviation (`sd`) of the return. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ What's the correlation matrix of different industries?" ] }, { "cell_type": "code", "execution_count": 154, "metadata": { "tags": [ "hide-output" ], "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A matrix: 20 × 20 of type dbl</caption>\n", "<thead>\n", "\t<tr><th></th><th scope=col>Accommodation and Food Services</th><th scope=col>Administrative and Support and Waste Management and Remediation Services</th><th scope=col>Agriculture, Forestry, Fishing and Hunting</th><th scope=col>Arts, Entertainment, and Recreation</th><th scope=col>Construction</th><th scope=col>Educational Services</th><th scope=col>Finance and Insurance</th><th scope=col>Health Care and Social Assistance</th><th scope=col>Information</th><th scope=col>Management of Companies and Enterprises</th><th scope=col>Manufacturing</th><th scope=col>Mining, Quarrying, and Oil and Gas Extraction</th><th scope=col>Other Services (except Public Administration)</th><th scope=col>Professional, Scientific, and Technical Services</th><th scope=col>Public Administration</th><th scope=col>Real Estate and Rental and Leasing</th><th scope=col>Retail Trade</th><th scope=col>Transportation and Warehousing</th><th scope=col>Utilities</th><th scope=col>Wholesale Trade</th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><th scope=row>Accommodation and Food Services</th><td>1.0000000</td><td>0.7823957</td><td>0.5013761</td><td>0.7446452</td><td>0.6078722</td><td>0.4654966</td><td>0.7066154</td><td>0.6463405</td><td>0.6122080</td><td>0.5470300</td><td>0.7633006</td><td>0.5211172</td><td>0.6187736</td><td>0.6538682</td><td>0.2292955</td><td>0.6230375</td><td>0.6628616</td><td>0.6214865</td><td>0.4618819</td><td>0.7677197</td></tr>\n", "\t<tr><th scope=row>Administrative and Support and Waste Management and Remediation Services</th><td>0.7823957</td><td>1.0000000</td><td>0.4870175</td><td>0.7344664</td><td>0.7252820</td><td>0.5375312</td><td>0.7737542</td><td>0.7066700</td><td>0.7159282</td><td>0.6638719</td><td>0.8540851</td><td>0.5505665</td><td>0.7166665</td><td>0.7893084</td><td>0.3274748</td><td>0.7056624</td><td>0.7397659</td><td>0.7403557</td><td>0.4451892</td><td>0.7896287</td></tr>\n", "\t<tr><th scope=row>Agriculture, Forestry, Fishing and Hunting</th><td>0.5013761</td><td>0.4870175</td><td>1.0000000</td><td>0.5675665</td><td>0.4633918</td><td>0.3434987</td><td>0.5934878</td><td>0.5175651</td><td>0.1669638</td><td>0.4734522</td><td>0.4207293</td><td>0.6089541</td><td>0.5641088</td><td>0.3328031</td><td>0.3866789</td><td>0.2557795</td><td>0.3079658</td><td>0.4584330</td><td>0.3518488</td><td>0.5721524</td></tr>\n", "\t<tr><th scope=row>Arts, Entertainment, and Recreation</th><td>0.7446452</td><td>0.7344664</td><td>0.5675665</td><td>1.0000000</td><td>0.6793245</td><td>0.5715538</td><td>0.7458602</td><td>0.6515004</td><td>0.5445807</td><td>0.6643853</td><td>0.6857226</td><td>0.5662939</td><td>0.7366153</td><td>0.6378005</td><td>0.3445050</td><td>0.5983349</td><td>0.5779041</td><td>0.6420355</td><td>0.3554395</td><td>0.7344718</td></tr>\n", "\t<tr><th scope=row>Construction</th><td>0.6078722</td><td>0.7252820</td><td>0.4633918</td><td>0.6793245</td><td>1.0000000</td><td>0.4681835</td><td>0.6700813</td><td>0.5874457</td><td>0.5866864</td><td>0.5886178</td><td>0.7405226</td><td>0.4275301</td><td>0.6750095</td><td>0.6570817</td><td>0.2870273</td><td>0.5991846</td><td>0.6947419</td><td>0.7139696</td><td>0.4077543</td><td>0.8034275</td></tr>\n", "\t<tr><th scope=row>Educational Services</th><td>0.4654966</td><td>0.5375312</td><td>0.3434987</td><td>0.5715538</td><td>0.4681835</td><td>1.0000000</td><td>0.6033540</td><td>0.5072563</td><td>0.3430916</td><td>0.5183743</td><td>0.4615438</td><td>0.3931933</td><td>0.6922868</td><td>0.4145928</td><td>0.4475607</td><td>0.4808919</td><td>0.3831186</td><td>0.3970923</td><td>0.2684934</td><td>0.5589631</td></tr>\n", "\t<tr><th scope=row>Finance and Insurance</th><td>0.7066154</td><td>0.7737542</td><td>0.5934878</td><td>0.7458602</td><td>0.6700813</td><td>0.6033540</td><td>1.0000000</td><td>0.6958050</td><td>0.5172975</td><td>0.7934317</td><td>0.7461947</td><td>0.6772370</td><td>0.7397326</td><td>0.6393567</td><td>0.5000995</td><td>0.6278522</td><td>0.6119276</td><td>0.7016956</td><td>0.4507855</td><td>0.8321860</td></tr>\n", "\t<tr><th scope=row>Health Care and Social Assistance</th><td>0.6463405</td><td>0.7066700</td><td>0.5175651</td><td>0.6515004</td><td>0.5874457</td><td>0.5072563</td><td>0.6958050</td><td>1.0000000</td><td>0.5391532</td><td>0.6388265</td><td>0.7180290</td><td>0.5375165</td><td>0.6963002</td><td>0.6681555</td><td>0.4151832</td><td>0.5667816</td><td>0.6341223</td><td>0.6324254</td><td>0.5353252</td><td>0.7489169</td></tr>\n", "\t<tr><th scope=row>Information</th><td>0.6122080</td><td>0.7159282</td><td>0.1669638</td><td>0.5445807</td><td>0.5866864</td><td>0.3430916</td><td>0.5172975</td><td>0.5391532</td><td>1.0000000</td><td>0.5551209</td><td>0.8110040</td><td>0.2294112</td><td>0.5045470</td><td>0.8297543</td><td>0.1700053</td><td>0.6506830</td><td>0.7794881</td><td>0.6005063</td><td>0.3602825</td><td>0.6048700</td></tr>\n", "\t<tr><th scope=row>Management of Companies and Enterprises</th><td>0.5470300</td><td>0.6638719</td><td>0.4734522</td><td>0.6643853</td><td>0.5886178</td><td>0.5183743</td><td>0.7934317</td><td>0.6388265</td><td>0.5551209</td><td>1.0000000</td><td>0.7038641</td><td>0.5196926</td><td>0.6502163</td><td>0.6242463</td><td>0.4819664</td><td>0.5861686</td><td>0.6148007</td><td>0.6186328</td><td>0.4452484</td><td>0.6842822</td></tr>\n", "\t<tr><th scope=row>Manufacturing</th><td>0.7633006</td><td>0.8540851</td><td>0.4207293</td><td>0.6857226</td><td>0.7405226</td><td>0.4615438</td><td>0.7461947</td><td>0.7180290</td><td>0.8110040</td><td>0.7038641</td><td>1.0000000</td><td>0.4922993</td><td>0.6598291</td><td>0.8158554</td><td>0.3129434</td><td>0.7407663</td><td>0.7728147</td><td>0.7691517</td><td>0.5080767</td><td>0.8038981</td></tr>\n", "\t<tr><th scope=row>Mining, Quarrying, and Oil and Gas Extraction</th><td>0.5211172</td><td>0.5505665</td><td>0.6089541</td><td>0.5662939</td><td>0.4275301</td><td>0.3931933</td><td>0.6772370</td><td>0.5375165</td><td>0.2294112</td><td>0.5196926</td><td>0.4922993</td><td>1.0000000</td><td>0.5161978</td><td>0.3692333</td><td>0.3907405</td><td>0.3370661</td><td>0.3208188</td><td>0.5122547</td><td>0.3717348</td><td>0.5829269</td></tr>\n", "\t<tr><th scope=row>Other Services (except Public Administration)</th><td>0.6187736</td><td>0.7166665</td><td>0.5641088</td><td>0.7366153</td><td>0.6750095</td><td>0.6922868</td><td>0.7397326</td><td>0.6963002</td><td>0.5045470</td><td>0.6502163</td><td>0.6598291</td><td>0.5161978</td><td>1.0000000</td><td>0.6540319</td><td>0.4307723</td><td>0.5786889</td><td>0.5940287</td><td>0.6132417</td><td>0.4639425</td><td>0.7247291</td></tr>\n", "\t<tr><th scope=row>Professional, Scientific, and Technical Services</th><td>0.6538682</td><td>0.7893084</td><td>0.3328031</td><td>0.6378005</td><td>0.6570817</td><td>0.4145928</td><td>0.6393567</td><td>0.6681555</td><td>0.8297543</td><td>0.6242463</td><td>0.8158554</td><td>0.3692333</td><td>0.6540319</td><td>1.0000000</td><td>0.2446779</td><td>0.7704328</td><td>0.7969950</td><td>0.6911916</td><td>0.3839157</td><td>0.7094917</td></tr>\n", "\t<tr><th scope=row>Public Administration</th><td>0.2292955</td><td>0.3274748</td><td>0.3866789</td><td>0.3445050</td><td>0.2870273</td><td>0.4475607</td><td>0.5000995</td><td>0.4151832</td><td>0.1700053</td><td>0.4819664</td><td>0.3129434</td><td>0.3907405</td><td>0.4307723</td><td>0.2446779</td><td>1.0000000</td><td>0.2233009</td><td>0.2342094</td><td>0.2491409</td><td>0.3779179</td><td>0.4332633</td></tr>\n", "\t<tr><th scope=row>Real Estate and Rental and Leasing</th><td>0.6230375</td><td>0.7056624</td><td>0.2557795</td><td>0.5983349</td><td>0.5991846</td><td>0.4808919</td><td>0.6278522</td><td>0.5667816</td><td>0.6506830</td><td>0.5861686</td><td>0.7407663</td><td>0.3370661</td><td>0.5786889</td><td>0.7704328</td><td>0.2233009</td><td>1.0000000</td><td>0.7053585</td><td>0.6030830</td><td>0.3046936</td><td>0.6524080</td></tr>\n", "\t<tr><th scope=row>Retail Trade</th><td>0.6628616</td><td>0.7397659</td><td>0.3079658</td><td>0.5779041</td><td>0.6947419</td><td>0.3831186</td><td>0.6119276</td><td>0.6341223</td><td>0.7794881</td><td>0.6148007</td><td>0.7728147</td><td>0.3208188</td><td>0.5940287</td><td>0.7969950</td><td>0.2342094</td><td>0.7053585</td><td>1.0000000</td><td>0.6410020</td><td>0.4584612</td><td>0.7412964</td></tr>\n", "\t<tr><th scope=row>Transportation and Warehousing</th><td>0.6214865</td><td>0.7403557</td><td>0.4584330</td><td>0.6420355</td><td>0.7139696</td><td>0.3970923</td><td>0.7016956</td><td>0.6324254</td><td>0.6005063</td><td>0.6186328</td><td>0.7691517</td><td>0.5122547</td><td>0.6132417</td><td>0.6911916</td><td>0.2491409</td><td>0.6030830</td><td>0.6410020</td><td>1.0000000</td><td>0.4938618</td><td>0.7756485</td></tr>\n", "\t<tr><th scope=row>Utilities</th><td>0.4618819</td><td>0.4451892</td><td>0.3518488</td><td>0.3554395</td><td>0.4077543</td><td>0.2684934</td><td>0.4507855</td><td>0.5353252</td><td>0.3602825</td><td>0.4452484</td><td>0.5080767</td><td>0.3717348</td><td>0.4639425</td><td>0.3839157</td><td>0.3779179</td><td>0.3046936</td><td>0.4584612</td><td>0.4938618</td><td>1.0000000</td><td>0.5595027</td></tr>\n", "\t<tr><th scope=row>Wholesale Trade</th><td>0.7677197</td><td>0.7896287</td><td>0.5721524</td><td>0.7344718</td><td>0.8034275</td><td>0.5589631</td><td>0.8321860</td><td>0.7489169</td><td>0.6048700</td><td>0.6842822</td><td>0.8038981</td><td>0.5829269</td><td>0.7247291</td><td>0.7094917</td><td>0.4332633</td><td>0.6524080</td><td>0.7412964</td><td>0.7756485</td><td>0.5595027</td><td>1.0000000</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A matrix: 20 × 20 of type dbl\n", "\\begin{tabular}{r|llllllllllllllllllll}\n", " & Accommodation and Food Services & Administrative and Support and Waste Management and Remediation Services & Agriculture, Forestry, Fishing and Hunting & Arts, Entertainment, and Recreation & Construction & Educational Services & Finance and Insurance & Health Care and Social Assistance & Information & Management of Companies and Enterprises & Manufacturing & Mining, Quarrying, and Oil and Gas Extraction & Other Services (except Public Administration) & Professional, Scientific, and Technical Services & Public Administration & Real Estate and Rental and Leasing & Retail Trade & Transportation and Warehousing & Utilities & Wholesale Trade\\\\\n", "\\hline\n", "\tAccommodation and Food Services & 1.0000000 & 0.7823957 & 0.5013761 & 0.7446452 & 0.6078722 & 0.4654966 & 0.7066154 & 0.6463405 & 0.6122080 & 0.5470300 & 0.7633006 & 0.5211172 & 0.6187736 & 0.6538682 & 0.2292955 & 0.6230375 & 0.6628616 & 0.6214865 & 0.4618819 & 0.7677197\\\\\n", "\tAdministrative and Support and Waste Management and Remediation Services & 0.7823957 & 1.0000000 & 0.4870175 & 0.7344664 & 0.7252820 & 0.5375312 & 0.7737542 & 0.7066700 & 0.7159282 & 0.6638719 & 0.8540851 & 0.5505665 & 0.7166665 & 0.7893084 & 0.3274748 & 0.7056624 & 0.7397659 & 0.7403557 & 0.4451892 & 0.7896287\\\\\n", "\tAgriculture, Forestry, Fishing and Hunting & 0.5013761 & 0.4870175 & 1.0000000 & 0.5675665 & 0.4633918 & 0.3434987 & 0.5934878 & 0.5175651 & 0.1669638 & 0.4734522 & 0.4207293 & 0.6089541 & 0.5641088 & 0.3328031 & 0.3866789 & 0.2557795 & 0.3079658 & 0.4584330 & 0.3518488 & 0.5721524\\\\\n", "\tArts, Entertainment, and Recreation & 0.7446452 & 0.7344664 & 0.5675665 & 1.0000000 & 0.6793245 & 0.5715538 & 0.7458602 & 0.6515004 & 0.5445807 & 0.6643853 & 0.6857226 & 0.5662939 & 0.7366153 & 0.6378005 & 0.3445050 & 0.5983349 & 0.5779041 & 0.6420355 & 0.3554395 & 0.7344718\\\\\n", "\tConstruction & 0.6078722 & 0.7252820 & 0.4633918 & 0.6793245 & 1.0000000 & 0.4681835 & 0.6700813 & 0.5874457 & 0.5866864 & 0.5886178 & 0.7405226 & 0.4275301 & 0.6750095 & 0.6570817 & 0.2870273 & 0.5991846 & 0.6947419 & 0.7139696 & 0.4077543 & 0.8034275\\\\\n", "\tEducational Services & 0.4654966 & 0.5375312 & 0.3434987 & 0.5715538 & 0.4681835 & 1.0000000 & 0.6033540 & 0.5072563 & 0.3430916 & 0.5183743 & 0.4615438 & 0.3931933 & 0.6922868 & 0.4145928 & 0.4475607 & 0.4808919 & 0.3831186 & 0.3970923 & 0.2684934 & 0.5589631\\\\\n", "\tFinance and Insurance & 0.7066154 & 0.7737542 & 0.5934878 & 0.7458602 & 0.6700813 & 0.6033540 & 1.0000000 & 0.6958050 & 0.5172975 & 0.7934317 & 0.7461947 & 0.6772370 & 0.7397326 & 0.6393567 & 0.5000995 & 0.6278522 & 0.6119276 & 0.7016956 & 0.4507855 & 0.8321860\\\\\n", "\tHealth Care and Social Assistance & 0.6463405 & 0.7066700 & 0.5175651 & 0.6515004 & 0.5874457 & 0.5072563 & 0.6958050 & 1.0000000 & 0.5391532 & 0.6388265 & 0.7180290 & 0.5375165 & 0.6963002 & 0.6681555 & 0.4151832 & 0.5667816 & 0.6341223 & 0.6324254 & 0.5353252 & 0.7489169\\\\\n", "\tInformation & 0.6122080 & 0.7159282 & 0.1669638 & 0.5445807 & 0.5866864 & 0.3430916 & 0.5172975 & 0.5391532 & 1.0000000 & 0.5551209 & 0.8110040 & 0.2294112 & 0.5045470 & 0.8297543 & 0.1700053 & 0.6506830 & 0.7794881 & 0.6005063 & 0.3602825 & 0.6048700\\\\\n", "\tManagement of Companies and Enterprises & 0.5470300 & 0.6638719 & 0.4734522 & 0.6643853 & 0.5886178 & 0.5183743 & 0.7934317 & 0.6388265 & 0.5551209 & 1.0000000 & 0.7038641 & 0.5196926 & 0.6502163 & 0.6242463 & 0.4819664 & 0.5861686 & 0.6148007 & 0.6186328 & 0.4452484 & 0.6842822\\\\\n", "\tManufacturing & 0.7633006 & 0.8540851 & 0.4207293 & 0.6857226 & 0.7405226 & 0.4615438 & 0.7461947 & 0.7180290 & 0.8110040 & 0.7038641 & 1.0000000 & 0.4922993 & 0.6598291 & 0.8158554 & 0.3129434 & 0.7407663 & 0.7728147 & 0.7691517 & 0.5080767 & 0.8038981\\\\\n", "\tMining, Quarrying, and Oil and Gas Extraction & 0.5211172 & 0.5505665 & 0.6089541 & 0.5662939 & 0.4275301 & 0.3931933 & 0.6772370 & 0.5375165 & 0.2294112 & 0.5196926 & 0.4922993 & 1.0000000 & 0.5161978 & 0.3692333 & 0.3907405 & 0.3370661 & 0.3208188 & 0.5122547 & 0.3717348 & 0.5829269\\\\\n", "\tOther Services (except Public Administration) & 0.6187736 & 0.7166665 & 0.5641088 & 0.7366153 & 0.6750095 & 0.6922868 & 0.7397326 & 0.6963002 & 0.5045470 & 0.6502163 & 0.6598291 & 0.5161978 & 1.0000000 & 0.6540319 & 0.4307723 & 0.5786889 & 0.5940287 & 0.6132417 & 0.4639425 & 0.7247291\\\\\n", "\tProfessional, Scientific, and Technical Services & 0.6538682 & 0.7893084 & 0.3328031 & 0.6378005 & 0.6570817 & 0.4145928 & 0.6393567 & 0.6681555 & 0.8297543 & 0.6242463 & 0.8158554 & 0.3692333 & 0.6540319 & 1.0000000 & 0.2446779 & 0.7704328 & 0.7969950 & 0.6911916 & 0.3839157 & 0.7094917\\\\\n", "\tPublic Administration & 0.2292955 & 0.3274748 & 0.3866789 & 0.3445050 & 0.2870273 & 0.4475607 & 0.5000995 & 0.4151832 & 0.1700053 & 0.4819664 & 0.3129434 & 0.3907405 & 0.4307723 & 0.2446779 & 1.0000000 & 0.2233009 & 0.2342094 & 0.2491409 & 0.3779179 & 0.4332633\\\\\n", "\tReal Estate and Rental and Leasing & 0.6230375 & 0.7056624 & 0.2557795 & 0.5983349 & 0.5991846 & 0.4808919 & 0.6278522 & 0.5667816 & 0.6506830 & 0.5861686 & 0.7407663 & 0.3370661 & 0.5786889 & 0.7704328 & 0.2233009 & 1.0000000 & 0.7053585 & 0.6030830 & 0.3046936 & 0.6524080\\\\\n", "\tRetail Trade & 0.6628616 & 0.7397659 & 0.3079658 & 0.5779041 & 0.6947419 & 0.3831186 & 0.6119276 & 0.6341223 & 0.7794881 & 0.6148007 & 0.7728147 & 0.3208188 & 0.5940287 & 0.7969950 & 0.2342094 & 0.7053585 & 1.0000000 & 0.6410020 & 0.4584612 & 0.7412964\\\\\n", "\tTransportation and Warehousing & 0.6214865 & 0.7403557 & 0.4584330 & 0.6420355 & 0.7139696 & 0.3970923 & 0.7016956 & 0.6324254 & 0.6005063 & 0.6186328 & 0.7691517 & 0.5122547 & 0.6132417 & 0.6911916 & 0.2491409 & 0.6030830 & 0.6410020 & 1.0000000 & 0.4938618 & 0.7756485\\\\\n", "\tUtilities & 0.4618819 & 0.4451892 & 0.3518488 & 0.3554395 & 0.4077543 & 0.2684934 & 0.4507855 & 0.5353252 & 0.3602825 & 0.4452484 & 0.5080767 & 0.3717348 & 0.4639425 & 0.3839157 & 0.3779179 & 0.3046936 & 0.4584612 & 0.4938618 & 1.0000000 & 0.5595027\\\\\n", "\tWholesale Trade & 0.7677197 & 0.7896287 & 0.5721524 & 0.7344718 & 0.8034275 & 0.5589631 & 0.8321860 & 0.7489169 & 0.6048700 & 0.6842822 & 0.8038981 & 0.5829269 & 0.7247291 & 0.7094917 & 0.4332633 & 0.6524080 & 0.7412964 & 0.7756485 & 0.5595027 & 1.0000000\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A matrix: 20 × 20 of type dbl\n", "\n", "| <!--/--> | Accommodation and Food Services | Administrative and Support and Waste Management and Remediation Services | Agriculture, Forestry, Fishing and Hunting | Arts, Entertainment, and Recreation | Construction | Educational Services | Finance and Insurance | Health Care and Social Assistance | Information | Management of Companies and Enterprises | Manufacturing | Mining, Quarrying, and Oil and Gas Extraction | Other Services (except Public Administration) | Professional, Scientific, and Technical Services | Public Administration | Real Estate and Rental and Leasing | Retail Trade | Transportation and Warehousing | Utilities | Wholesale Trade |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Accommodation and Food Services | 1.0000000 | 0.7823957 | 0.5013761 | 0.7446452 | 0.6078722 | 0.4654966 | 0.7066154 | 0.6463405 | 0.6122080 | 0.5470300 | 0.7633006 | 0.5211172 | 0.6187736 | 0.6538682 | 0.2292955 | 0.6230375 | 0.6628616 | 0.6214865 | 0.4618819 | 0.7677197 |\n", "| Administrative and Support and Waste Management and Remediation Services | 0.7823957 | 1.0000000 | 0.4870175 | 0.7344664 | 0.7252820 | 0.5375312 | 0.7737542 | 0.7066700 | 0.7159282 | 0.6638719 | 0.8540851 | 0.5505665 | 0.7166665 | 0.7893084 | 0.3274748 | 0.7056624 | 0.7397659 | 0.7403557 | 0.4451892 | 0.7896287 |\n", "| Agriculture, Forestry, Fishing and Hunting | 0.5013761 | 0.4870175 | 1.0000000 | 0.5675665 | 0.4633918 | 0.3434987 | 0.5934878 | 0.5175651 | 0.1669638 | 0.4734522 | 0.4207293 | 0.6089541 | 0.5641088 | 0.3328031 | 0.3866789 | 0.2557795 | 0.3079658 | 0.4584330 | 0.3518488 | 0.5721524 |\n", "| Arts, Entertainment, and Recreation | 0.7446452 | 0.7344664 | 0.5675665 | 1.0000000 | 0.6793245 | 0.5715538 | 0.7458602 | 0.6515004 | 0.5445807 | 0.6643853 | 0.6857226 | 0.5662939 | 0.7366153 | 0.6378005 | 0.3445050 | 0.5983349 | 0.5779041 | 0.6420355 | 0.3554395 | 0.7344718 |\n", "| Construction | 0.6078722 | 0.7252820 | 0.4633918 | 0.6793245 | 1.0000000 | 0.4681835 | 0.6700813 | 0.5874457 | 0.5866864 | 0.5886178 | 0.7405226 | 0.4275301 | 0.6750095 | 0.6570817 | 0.2870273 | 0.5991846 | 0.6947419 | 0.7139696 | 0.4077543 | 0.8034275 |\n", "| Educational Services | 0.4654966 | 0.5375312 | 0.3434987 | 0.5715538 | 0.4681835 | 1.0000000 | 0.6033540 | 0.5072563 | 0.3430916 | 0.5183743 | 0.4615438 | 0.3931933 | 0.6922868 | 0.4145928 | 0.4475607 | 0.4808919 | 0.3831186 | 0.3970923 | 0.2684934 | 0.5589631 |\n", "| Finance and Insurance | 0.7066154 | 0.7737542 | 0.5934878 | 0.7458602 | 0.6700813 | 0.6033540 | 1.0000000 | 0.6958050 | 0.5172975 | 0.7934317 | 0.7461947 | 0.6772370 | 0.7397326 | 0.6393567 | 0.5000995 | 0.6278522 | 0.6119276 | 0.7016956 | 0.4507855 | 0.8321860 |\n", "| Health Care and Social Assistance | 0.6463405 | 0.7066700 | 0.5175651 | 0.6515004 | 0.5874457 | 0.5072563 | 0.6958050 | 1.0000000 | 0.5391532 | 0.6388265 | 0.7180290 | 0.5375165 | 0.6963002 | 0.6681555 | 0.4151832 | 0.5667816 | 0.6341223 | 0.6324254 | 0.5353252 | 0.7489169 |\n", "| Information | 0.6122080 | 0.7159282 | 0.1669638 | 0.5445807 | 0.5866864 | 0.3430916 | 0.5172975 | 0.5391532 | 1.0000000 | 0.5551209 | 0.8110040 | 0.2294112 | 0.5045470 | 0.8297543 | 0.1700053 | 0.6506830 | 0.7794881 | 0.6005063 | 0.3602825 | 0.6048700 |\n", "| Management of Companies and Enterprises | 0.5470300 | 0.6638719 | 0.4734522 | 0.6643853 | 0.5886178 | 0.5183743 | 0.7934317 | 0.6388265 | 0.5551209 | 1.0000000 | 0.7038641 | 0.5196926 | 0.6502163 | 0.6242463 | 0.4819664 | 0.5861686 | 0.6148007 | 0.6186328 | 0.4452484 | 0.6842822 |\n", "| Manufacturing | 0.7633006 | 0.8540851 | 0.4207293 | 0.6857226 | 0.7405226 | 0.4615438 | 0.7461947 | 0.7180290 | 0.8110040 | 0.7038641 | 1.0000000 | 0.4922993 | 0.6598291 | 0.8158554 | 0.3129434 | 0.7407663 | 0.7728147 | 0.7691517 | 0.5080767 | 0.8038981 |\n", "| Mining, Quarrying, and Oil and Gas Extraction | 0.5211172 | 0.5505665 | 0.6089541 | 0.5662939 | 0.4275301 | 0.3931933 | 0.6772370 | 0.5375165 | 0.2294112 | 0.5196926 | 0.4922993 | 1.0000000 | 0.5161978 | 0.3692333 | 0.3907405 | 0.3370661 | 0.3208188 | 0.5122547 | 0.3717348 | 0.5829269 |\n", "| Other Services (except Public Administration) | 0.6187736 | 0.7166665 | 0.5641088 | 0.7366153 | 0.6750095 | 0.6922868 | 0.7397326 | 0.6963002 | 0.5045470 | 0.6502163 | 0.6598291 | 0.5161978 | 1.0000000 | 0.6540319 | 0.4307723 | 0.5786889 | 0.5940287 | 0.6132417 | 0.4639425 | 0.7247291 |\n", "| Professional, Scientific, and Technical Services | 0.6538682 | 0.7893084 | 0.3328031 | 0.6378005 | 0.6570817 | 0.4145928 | 0.6393567 | 0.6681555 | 0.8297543 | 0.6242463 | 0.8158554 | 0.3692333 | 0.6540319 | 1.0000000 | 0.2446779 | 0.7704328 | 0.7969950 | 0.6911916 | 0.3839157 | 0.7094917 |\n", "| Public Administration | 0.2292955 | 0.3274748 | 0.3866789 | 0.3445050 | 0.2870273 | 0.4475607 | 0.5000995 | 0.4151832 | 0.1700053 | 0.4819664 | 0.3129434 | 0.3907405 | 0.4307723 | 0.2446779 | 1.0000000 | 0.2233009 | 0.2342094 | 0.2491409 | 0.3779179 | 0.4332633 |\n", "| Real Estate and Rental and Leasing | 0.6230375 | 0.7056624 | 0.2557795 | 0.5983349 | 0.5991846 | 0.4808919 | 0.6278522 | 0.5667816 | 0.6506830 | 0.5861686 | 0.7407663 | 0.3370661 | 0.5786889 | 0.7704328 | 0.2233009 | 1.0000000 | 0.7053585 | 0.6030830 | 0.3046936 | 0.6524080 |\n", "| Retail Trade | 0.6628616 | 0.7397659 | 0.3079658 | 0.5779041 | 0.6947419 | 0.3831186 | 0.6119276 | 0.6341223 | 0.7794881 | 0.6148007 | 0.7728147 | 0.3208188 | 0.5940287 | 0.7969950 | 0.2342094 | 0.7053585 | 1.0000000 | 0.6410020 | 0.4584612 | 0.7412964 |\n", "| Transportation and Warehousing | 0.6214865 | 0.7403557 | 0.4584330 | 0.6420355 | 0.7139696 | 0.3970923 | 0.7016956 | 0.6324254 | 0.6005063 | 0.6186328 | 0.7691517 | 0.5122547 | 0.6132417 | 0.6911916 | 0.2491409 | 0.6030830 | 0.6410020 | 1.0000000 | 0.4938618 | 0.7756485 |\n", "| Utilities | 0.4618819 | 0.4451892 | 0.3518488 | 0.3554395 | 0.4077543 | 0.2684934 | 0.4507855 | 0.5353252 | 0.3602825 | 0.4452484 | 0.5080767 | 0.3717348 | 0.4639425 | 0.3839157 | 0.3779179 | 0.3046936 | 0.4584612 | 0.4938618 | 1.0000000 | 0.5595027 |\n", "| Wholesale Trade | 0.7677197 | 0.7896287 | 0.5721524 | 0.7344718 | 0.8034275 | 0.5589631 | 0.8321860 | 0.7489169 | 0.6048700 | 0.6842822 | 0.8038981 | 0.5829269 | 0.7247291 | 0.7094917 | 0.4332633 | 0.6524080 | 0.7412964 | 0.7756485 | 0.5595027 | 1.0000000 |\n", "\n" ], "text/plain": [ " Accommodation and Food Services\n", "Accommodation and Food Services 1.0000000 \n", "Administrative and Support and Waste Management and Remediation Services 0.7823957 \n", "Agriculture, Forestry, Fishing and Hunting 0.5013761 \n", "Arts, Entertainment, and Recreation 0.7446452 \n", "Construction 0.6078722 \n", "Educational Services 0.4654966 \n", "Finance and Insurance 0.7066154 \n", "Health Care and Social Assistance 0.6463405 \n", "Information 0.6122080 \n", "Management of Companies and Enterprises 0.5470300 \n", "Manufacturing 0.7633006 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5211172 \n", "Other Services (except Public Administration) 0.6187736 \n", "Professional, Scientific, and Technical Services 0.6538682 \n", "Public Administration 0.2292955 \n", "Real Estate and Rental and Leasing 0.6230375 \n", "Retail Trade 0.6628616 \n", "Transportation and Warehousing 0.6214865 \n", "Utilities 0.4618819 \n", "Wholesale Trade 0.7677197 \n", " Administrative and Support and Waste Management and Remediation Services\n", "Accommodation and Food Services 0.7823957 \n", "Administrative and Support and Waste Management and Remediation Services 1.0000000 \n", "Agriculture, Forestry, Fishing and Hunting 0.4870175 \n", "Arts, Entertainment, and Recreation 0.7344664 \n", "Construction 0.7252820 \n", "Educational Services 0.5375312 \n", "Finance and Insurance 0.7737542 \n", "Health Care and Social Assistance 0.7066700 \n", "Information 0.7159282 \n", "Management of Companies and Enterprises 0.6638719 \n", "Manufacturing 0.8540851 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5505665 \n", "Other Services (except Public Administration) 0.7166665 \n", "Professional, Scientific, and Technical Services 0.7893084 \n", "Public Administration 0.3274748 \n", "Real Estate and Rental and Leasing 0.7056624 \n", "Retail Trade 0.7397659 \n", "Transportation and Warehousing 0.7403557 \n", "Utilities 0.4451892 \n", "Wholesale Trade 0.7896287 \n", " Agriculture, Forestry, Fishing and Hunting\n", "Accommodation and Food Services 0.5013761 \n", "Administrative and Support and Waste Management and Remediation Services 0.4870175 \n", "Agriculture, Forestry, Fishing and Hunting 1.0000000 \n", "Arts, Entertainment, and Recreation 0.5675665 \n", "Construction 0.4633918 \n", "Educational Services 0.3434987 \n", "Finance and Insurance 0.5934878 \n", "Health Care and Social Assistance 0.5175651 \n", "Information 0.1669638 \n", "Management of Companies and Enterprises 0.4734522 \n", "Manufacturing 0.4207293 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.6089541 \n", "Other Services (except Public Administration) 0.5641088 \n", "Professional, Scientific, and Technical Services 0.3328031 \n", "Public Administration 0.3866789 \n", "Real Estate and Rental and Leasing 0.2557795 \n", "Retail Trade 0.3079658 \n", "Transportation and Warehousing 0.4584330 \n", "Utilities 0.3518488 \n", "Wholesale Trade 0.5721524 \n", " Arts, Entertainment, and Recreation\n", "Accommodation and Food Services 0.7446452 \n", "Administrative and Support and Waste Management and Remediation Services 0.7344664 \n", "Agriculture, Forestry, Fishing and Hunting 0.5675665 \n", "Arts, Entertainment, and Recreation 1.0000000 \n", "Construction 0.6793245 \n", "Educational Services 0.5715538 \n", "Finance and Insurance 0.7458602 \n", "Health Care and Social Assistance 0.6515004 \n", "Information 0.5445807 \n", "Management of Companies and Enterprises 0.6643853 \n", "Manufacturing 0.6857226 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5662939 \n", "Other Services (except Public Administration) 0.7366153 \n", "Professional, Scientific, and Technical Services 0.6378005 \n", "Public Administration 0.3445050 \n", "Real Estate and Rental and Leasing 0.5983349 \n", "Retail Trade 0.5779041 \n", "Transportation and Warehousing 0.6420355 \n", "Utilities 0.3554395 \n", "Wholesale Trade 0.7344718 \n", " Construction\n", "Accommodation and Food Services 0.6078722 \n", "Administrative and Support and Waste Management and Remediation Services 0.7252820 \n", "Agriculture, Forestry, Fishing and Hunting 0.4633918 \n", "Arts, Entertainment, and Recreation 0.6793245 \n", "Construction 1.0000000 \n", "Educational Services 0.4681835 \n", "Finance and Insurance 0.6700813 \n", "Health Care and Social Assistance 0.5874457 \n", "Information 0.5866864 \n", "Management of Companies and Enterprises 0.5886178 \n", "Manufacturing 0.7405226 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.4275301 \n", "Other Services (except Public Administration) 0.6750095 \n", "Professional, Scientific, and Technical Services 0.6570817 \n", "Public Administration 0.2870273 \n", "Real Estate and Rental and Leasing 0.5991846 \n", "Retail Trade 0.6947419 \n", "Transportation and Warehousing 0.7139696 \n", "Utilities 0.4077543 \n", "Wholesale Trade 0.8034275 \n", " Educational Services\n", "Accommodation and Food Services 0.4654966 \n", "Administrative and Support and Waste Management and Remediation Services 0.5375312 \n", "Agriculture, Forestry, Fishing and Hunting 0.3434987 \n", "Arts, Entertainment, and Recreation 0.5715538 \n", "Construction 0.4681835 \n", "Educational Services 1.0000000 \n", "Finance and Insurance 0.6033540 \n", "Health Care and Social Assistance 0.5072563 \n", "Information 0.3430916 \n", "Management of Companies and Enterprises 0.5183743 \n", "Manufacturing 0.4615438 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.3931933 \n", "Other Services (except Public Administration) 0.6922868 \n", "Professional, Scientific, and Technical Services 0.4145928 \n", "Public Administration 0.4475607 \n", "Real Estate and Rental and Leasing 0.4808919 \n", "Retail Trade 0.3831186 \n", "Transportation and Warehousing 0.3970923 \n", "Utilities 0.2684934 \n", "Wholesale Trade 0.5589631 \n", " Finance and Insurance\n", "Accommodation and Food Services 0.7066154 \n", "Administrative and Support and Waste Management and Remediation Services 0.7737542 \n", "Agriculture, Forestry, Fishing and Hunting 0.5934878 \n", "Arts, Entertainment, and Recreation 0.7458602 \n", "Construction 0.6700813 \n", "Educational Services 0.6033540 \n", "Finance and Insurance 1.0000000 \n", "Health Care and Social Assistance 0.6958050 \n", "Information 0.5172975 \n", "Management of Companies and Enterprises 0.7934317 \n", "Manufacturing 0.7461947 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.6772370 \n", "Other Services (except Public Administration) 0.7397326 \n", "Professional, Scientific, and Technical Services 0.6393567 \n", "Public Administration 0.5000995 \n", "Real Estate and Rental and Leasing 0.6278522 \n", "Retail Trade 0.6119276 \n", "Transportation and Warehousing 0.7016956 \n", "Utilities 0.4507855 \n", "Wholesale Trade 0.8321860 \n", " Health Care and Social Assistance\n", "Accommodation and Food Services 0.6463405 \n", "Administrative and Support and Waste Management and Remediation Services 0.7066700 \n", "Agriculture, Forestry, Fishing and Hunting 0.5175651 \n", "Arts, Entertainment, and Recreation 0.6515004 \n", "Construction 0.5874457 \n", "Educational Services 0.5072563 \n", "Finance and Insurance 0.6958050 \n", "Health Care and Social Assistance 1.0000000 \n", "Information 0.5391532 \n", "Management of Companies and Enterprises 0.6388265 \n", "Manufacturing 0.7180290 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5375165 \n", "Other Services (except Public Administration) 0.6963002 \n", "Professional, Scientific, and Technical Services 0.6681555 \n", "Public Administration 0.4151832 \n", "Real Estate and Rental and Leasing 0.5667816 \n", "Retail Trade 0.6341223 \n", "Transportation and Warehousing 0.6324254 \n", "Utilities 0.5353252 \n", "Wholesale Trade 0.7489169 \n", " Information\n", "Accommodation and Food Services 0.6122080 \n", "Administrative and Support and Waste Management and Remediation Services 0.7159282 \n", "Agriculture, Forestry, Fishing and Hunting 0.1669638 \n", "Arts, Entertainment, and Recreation 0.5445807 \n", "Construction 0.5866864 \n", "Educational Services 0.3430916 \n", "Finance and Insurance 0.5172975 \n", "Health Care and Social Assistance 0.5391532 \n", "Information 1.0000000 \n", "Management of Companies and Enterprises 0.5551209 \n", "Manufacturing 0.8110040 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.2294112 \n", "Other Services (except Public Administration) 0.5045470 \n", "Professional, Scientific, and Technical Services 0.8297543 \n", "Public Administration 0.1700053 \n", "Real Estate and Rental and Leasing 0.6506830 \n", "Retail Trade 0.7794881 \n", "Transportation and Warehousing 0.6005063 \n", "Utilities 0.3602825 \n", "Wholesale Trade 0.6048700 \n", " Management of Companies and Enterprises\n", "Accommodation and Food Services 0.5470300 \n", "Administrative and Support and Waste Management and Remediation Services 0.6638719 \n", "Agriculture, Forestry, Fishing and Hunting 0.4734522 \n", "Arts, Entertainment, and Recreation 0.6643853 \n", "Construction 0.5886178 \n", "Educational Services 0.5183743 \n", "Finance and Insurance 0.7934317 \n", "Health Care and Social Assistance 0.6388265 \n", "Information 0.5551209 \n", "Management of Companies and Enterprises 1.0000000 \n", "Manufacturing 0.7038641 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5196926 \n", "Other Services (except Public Administration) 0.6502163 \n", "Professional, Scientific, and Technical Services 0.6242463 \n", "Public Administration 0.4819664 \n", "Real Estate and Rental and Leasing 0.5861686 \n", "Retail Trade 0.6148007 \n", "Transportation and Warehousing 0.6186328 \n", "Utilities 0.4452484 \n", "Wholesale Trade 0.6842822 \n", " Manufacturing\n", "Accommodation and Food Services 0.7633006 \n", "Administrative and Support and Waste Management and Remediation Services 0.8540851 \n", "Agriculture, Forestry, Fishing and Hunting 0.4207293 \n", "Arts, Entertainment, and Recreation 0.6857226 \n", "Construction 0.7405226 \n", "Educational Services 0.4615438 \n", "Finance and Insurance 0.7461947 \n", "Health Care and Social Assistance 0.7180290 \n", "Information 0.8110040 \n", "Management of Companies and Enterprises 0.7038641 \n", "Manufacturing 1.0000000 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.4922993 \n", "Other Services (except Public Administration) 0.6598291 \n", "Professional, Scientific, and Technical Services 0.8158554 \n", "Public Administration 0.3129434 \n", "Real Estate and Rental and Leasing 0.7407663 \n", "Retail Trade 0.7728147 \n", "Transportation and Warehousing 0.7691517 \n", "Utilities 0.5080767 \n", "Wholesale Trade 0.8038981 \n", " Mining, Quarrying, and Oil and Gas Extraction\n", "Accommodation and Food Services 0.5211172 \n", "Administrative and Support and Waste Management and Remediation Services 0.5505665 \n", "Agriculture, Forestry, Fishing and Hunting 0.6089541 \n", "Arts, Entertainment, and Recreation 0.5662939 \n", "Construction 0.4275301 \n", "Educational Services 0.3931933 \n", "Finance and Insurance 0.6772370 \n", "Health Care and Social Assistance 0.5375165 \n", "Information 0.2294112 \n", "Management of Companies and Enterprises 0.5196926 \n", "Manufacturing 0.4922993 \n", "Mining, Quarrying, and Oil and Gas Extraction 1.0000000 \n", "Other Services (except Public Administration) 0.5161978 \n", "Professional, Scientific, and Technical Services 0.3692333 \n", "Public Administration 0.3907405 \n", "Real Estate and Rental and Leasing 0.3370661 \n", "Retail Trade 0.3208188 \n", "Transportation and Warehousing 0.5122547 \n", "Utilities 0.3717348 \n", "Wholesale Trade 0.5829269 \n", " Other Services (except Public Administration)\n", "Accommodation and Food Services 0.6187736 \n", "Administrative and Support and Waste Management and Remediation Services 0.7166665 \n", "Agriculture, Forestry, Fishing and Hunting 0.5641088 \n", "Arts, Entertainment, and Recreation 0.7366153 \n", "Construction 0.6750095 \n", "Educational Services 0.6922868 \n", "Finance and Insurance 0.7397326 \n", "Health Care and Social Assistance 0.6963002 \n", "Information 0.5045470 \n", "Management of Companies and Enterprises 0.6502163 \n", "Manufacturing 0.6598291 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5161978 \n", "Other Services (except Public Administration) 1.0000000 \n", "Professional, Scientific, and Technical Services 0.6540319 \n", "Public Administration 0.4307723 \n", "Real Estate and Rental and Leasing 0.5786889 \n", "Retail Trade 0.5940287 \n", "Transportation and Warehousing 0.6132417 \n", "Utilities 0.4639425 \n", "Wholesale Trade 0.7247291 \n", " Professional, Scientific, and Technical Services\n", "Accommodation and Food Services 0.6538682 \n", "Administrative and Support and Waste Management and Remediation Services 0.7893084 \n", "Agriculture, Forestry, Fishing and Hunting 0.3328031 \n", "Arts, Entertainment, and Recreation 0.6378005 \n", "Construction 0.6570817 \n", "Educational Services 0.4145928 \n", "Finance and Insurance 0.6393567 \n", "Health Care and Social Assistance 0.6681555 \n", "Information 0.8297543 \n", "Management of Companies and Enterprises 0.6242463 \n", "Manufacturing 0.8158554 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.3692333 \n", "Other Services (except Public Administration) 0.6540319 \n", "Professional, Scientific, and Technical Services 1.0000000 \n", "Public Administration 0.2446779 \n", "Real Estate and Rental and Leasing 0.7704328 \n", "Retail Trade 0.7969950 \n", "Transportation and Warehousing 0.6911916 \n", "Utilities 0.3839157 \n", "Wholesale Trade 0.7094917 \n", " Public Administration\n", "Accommodation and Food Services 0.2292955 \n", "Administrative and Support and Waste Management and Remediation Services 0.3274748 \n", "Agriculture, Forestry, Fishing and Hunting 0.3866789 \n", "Arts, Entertainment, and Recreation 0.3445050 \n", "Construction 0.2870273 \n", "Educational Services 0.4475607 \n", "Finance and Insurance 0.5000995 \n", "Health Care and Social Assistance 0.4151832 \n", "Information 0.1700053 \n", "Management of Companies and Enterprises 0.4819664 \n", "Manufacturing 0.3129434 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.3907405 \n", "Other Services (except Public Administration) 0.4307723 \n", "Professional, Scientific, and Technical Services 0.2446779 \n", "Public Administration 1.0000000 \n", "Real Estate and Rental and Leasing 0.2233009 \n", "Retail Trade 0.2342094 \n", "Transportation and Warehousing 0.2491409 \n", "Utilities 0.3779179 \n", "Wholesale Trade 0.4332633 \n", " Real Estate and Rental and Leasing\n", "Accommodation and Food Services 0.6230375 \n", "Administrative and Support and Waste Management and Remediation Services 0.7056624 \n", "Agriculture, Forestry, Fishing and Hunting 0.2557795 \n", "Arts, Entertainment, and Recreation 0.5983349 \n", "Construction 0.5991846 \n", "Educational Services 0.4808919 \n", "Finance and Insurance 0.6278522 \n", "Health Care and Social Assistance 0.5667816 \n", "Information 0.6506830 \n", "Management of Companies and Enterprises 0.5861686 \n", "Manufacturing 0.7407663 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.3370661 \n", "Other Services (except Public Administration) 0.5786889 \n", "Professional, Scientific, and Technical Services 0.7704328 \n", "Public Administration 0.2233009 \n", "Real Estate and Rental and Leasing 1.0000000 \n", "Retail Trade 0.7053585 \n", "Transportation and Warehousing 0.6030830 \n", "Utilities 0.3046936 \n", "Wholesale Trade 0.6524080 \n", " Retail Trade\n", "Accommodation and Food Services 0.6628616 \n", "Administrative and Support and Waste Management and Remediation Services 0.7397659 \n", "Agriculture, Forestry, Fishing and Hunting 0.3079658 \n", "Arts, Entertainment, and Recreation 0.5779041 \n", "Construction 0.6947419 \n", "Educational Services 0.3831186 \n", "Finance and Insurance 0.6119276 \n", "Health Care and Social Assistance 0.6341223 \n", "Information 0.7794881 \n", "Management of Companies and Enterprises 0.6148007 \n", "Manufacturing 0.7728147 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.3208188 \n", "Other Services (except Public Administration) 0.5940287 \n", "Professional, Scientific, and Technical Services 0.7969950 \n", "Public Administration 0.2342094 \n", "Real Estate and Rental and Leasing 0.7053585 \n", "Retail Trade 1.0000000 \n", "Transportation and Warehousing 0.6410020 \n", "Utilities 0.4584612 \n", "Wholesale Trade 0.7412964 \n", " Transportation and Warehousing\n", "Accommodation and Food Services 0.6214865 \n", "Administrative and Support and Waste Management and Remediation Services 0.7403557 \n", "Agriculture, Forestry, Fishing and Hunting 0.4584330 \n", "Arts, Entertainment, and Recreation 0.6420355 \n", "Construction 0.7139696 \n", "Educational Services 0.3970923 \n", "Finance and Insurance 0.7016956 \n", "Health Care and Social Assistance 0.6324254 \n", "Information 0.6005063 \n", "Management of Companies and Enterprises 0.6186328 \n", "Manufacturing 0.7691517 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5122547 \n", "Other Services (except Public Administration) 0.6132417 \n", "Professional, Scientific, and Technical Services 0.6911916 \n", "Public Administration 0.2491409 \n", "Real Estate and Rental and Leasing 0.6030830 \n", "Retail Trade 0.6410020 \n", "Transportation and Warehousing 1.0000000 \n", "Utilities 0.4938618 \n", "Wholesale Trade 0.7756485 \n", " Utilities\n", "Accommodation and Food Services 0.4618819\n", "Administrative and Support and Waste Management and Remediation Services 0.4451892\n", "Agriculture, Forestry, Fishing and Hunting 0.3518488\n", "Arts, Entertainment, and Recreation 0.3554395\n", "Construction 0.4077543\n", "Educational Services 0.2684934\n", "Finance and Insurance 0.4507855\n", "Health Care and Social Assistance 0.5353252\n", "Information 0.3602825\n", "Management of Companies and Enterprises 0.4452484\n", "Manufacturing 0.5080767\n", "Mining, Quarrying, and Oil and Gas Extraction 0.3717348\n", "Other Services (except Public Administration) 0.4639425\n", "Professional, Scientific, and Technical Services 0.3839157\n", "Public Administration 0.3779179\n", "Real Estate and Rental and Leasing 0.3046936\n", "Retail Trade 0.4584612\n", "Transportation and Warehousing 0.4938618\n", "Utilities 1.0000000\n", "Wholesale Trade 0.5595027\n", " Wholesale Trade\n", "Accommodation and Food Services 0.7677197 \n", "Administrative and Support and Waste Management and Remediation Services 0.7896287 \n", "Agriculture, Forestry, Fishing and Hunting 0.5721524 \n", "Arts, Entertainment, and Recreation 0.7344718 \n", "Construction 0.8034275 \n", "Educational Services 0.5589631 \n", "Finance and Insurance 0.8321860 \n", "Health Care and Social Assistance 0.7489169 \n", "Information 0.6048700 \n", "Management of Companies and Enterprises 0.6842822 \n", "Manufacturing 0.8038981 \n", "Mining, Quarrying, and Oil and Gas Extraction 0.5829269 \n", "Other Services (except Public Administration) 0.7247291 \n", "Professional, Scientific, and Technical Services 0.7094917 \n", "Public Administration 0.4332633 \n", "Real Estate and Rental and Leasing 0.6524080 \n", "Retail Trade 0.7412964 \n", "Transportation and Warehousing 0.7756485 \n", "Utilities 0.5595027 \n", "Wholesale Trade 1.0000000 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Step 1: calculate the industry-level return\n", "ret_ind = data[, .(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(industry, date)]\n", "\n", "# Step 2: pivot the industry return table\n", "ret_ind2 = dcast(ret_ind, date~industry, value.var='ret_mkt')\n", "ret_ind2[, ':='(date=NULL)] # remove the date column\n", "\n", "# Step 3: compute the correlation matrix\n", "cor(ret_ind2)\n", "\n", "# -------------------\n", "# The above three steps can be chained together as follows:\n", "\n", "# data[, .(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(industry, date)\n", "# ][, dcast(.SD, date~industry, value.var='ret_mkt')\n", "# ][, ':='(date=NULL)\n", "# ][, cor(.SD)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: Nothing new here.\n", "\n", "- Step 2: We use `cor` to generate the correlation matrix. You may be wondering what's `dcast` doing here. We use `dcast` because it \"pivots\" the table so it can satisfy the requirement of the `cor` function: **each column must represent the returns of one industry.**\n", " \n", " Let's print out `ret_ind` (before `dcast`) and `ret_ind2` (after `dcast`) to get a sense of what's `dcast` doing.\n", "\n", "```r\n", "# (before `dcast`)\n", "print(ret_ind) # column `ret_mkt` stores the returns for *all* industries\n", "\n", "#> industry date ret_mkt\n", "#> 1: Accommodation and Food Services 2023-01-03 0.002703254\n", "#> 2: Accommodation and Food Services 2023-01-04 0.018852215\n", "#> 3: Accommodation and Food Services 2023-01-05 -0.008638118\n", "#> 4: Accommodation and Food Services 2023-01-06 0.023222487\n", "#> 5: Accommodation and Food Services 2023-01-09 -0.001595260\n", "\n", "# (after `dcast`)\n", "print(ret_ind2) # now each column only stores the return of one industry\n", "\n", "#> Transportation and Warehousing Utilities Wholesale Trade\n", "#> 1: 0.001654837 -0.004928265 -0.003529841\n", "#> 2: 0.017020174 0.010504283 0.006552128\n", "#> 3: -0.013090004 -0.019094989 -0.011908121\n", "#> 4: 0.034663817 0.020957761 0.026902487\n", "#> 5: 0.010606304 0.006563325 -0.002127530\n", "```\n", "\n", "As you can see, `dcast` reshapes the table from a \"long\" to a \"wide\" format. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(.SD[1])=\n", "## ⭐️ What's the largest/smallest stocks in S&P 500 on each day?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we measure the size of a stock by its market capital. This problem involves some advanced techniques of `data.table`." ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 6 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>type</th><th scope=col>ticker</th><th scope=col>cap</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>smallest</td><td>MAYS</td><td>9.636480e+04</td></tr>\n", "\t<tr><td>2023-01-03</td><td>largest </td><td>AAPL</td><td>1.981410e+09</td></tr>\n", "\t<tr><td>2023-01-04</td><td>smallest</td><td>YELL</td><td>1.305505e+05</td></tr>\n", "\t<tr><td>2023-01-04</td><td>largest </td><td>AAPL</td><td>2.001847e+09</td></tr>\n", "\t<tr><td>2023-01-05</td><td>smallest</td><td>MAYS</td><td>9.376416e+04</td></tr>\n", "\t<tr><td>2023-01-05</td><td>largest </td><td>AAPL</td><td>1.980618e+09</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 6 × 4\n", "\\begin{tabular}{llll}\n", " date & type & ticker & cap\\\\\n", " <date> & <chr> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & smallest & MAYS & 9.636480e+04\\\\\n", "\t 2023-01-03 & largest & AAPL & 1.981410e+09\\\\\n", "\t 2023-01-04 & smallest & YELL & 1.305505e+05\\\\\n", "\t 2023-01-04 & largest & AAPL & 2.001847e+09\\\\\n", "\t 2023-01-05 & smallest & MAYS & 9.376416e+04\\\\\n", "\t 2023-01-05 & largest & AAPL & 1.980618e+09\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 6 × 4\n", "\n", "| date <date> | type <chr> | ticker <chr> | cap <dbl> |\n", "|---|---|---|---|\n", "| 2023-01-03 | smallest | MAYS | 9.636480e+04 |\n", "| 2023-01-03 | largest | AAPL | 1.981410e+09 |\n", "| 2023-01-04 | smallest | YELL | 1.305505e+05 |\n", "| 2023-01-04 | largest | AAPL | 2.001847e+09 |\n", "| 2023-01-05 | smallest | MAYS | 9.376416e+04 |\n", "| 2023-01-05 | largest | AAPL | 1.980618e+09 |\n", "\n" ], "text/plain": [ " date type ticker cap \n", "1 2023-01-03 smallest MAYS 9.636480e+04\n", "2 2023-01-03 largest AAPL 1.981410e+09\n", "3 2023-01-04 smallest YELL 1.305505e+05\n", "4 2023-01-04 largest AAPL 2.001847e+09\n", "5 2023-01-05 smallest MAYS 9.376416e+04\n", "6 2023-01-05 largest AAPL 1.980618e+09" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: select the S&P 500 stocks\n", " is_sp500==T, .(date, ticker, cap)\n", " # Step 2: sort by date and market cap\n", " ][order(date, cap)\n", " # Step 3: select the smallest and largest stocks on each day\n", " ][, c(type=list(c('smallest', 'largest')), \n", " .SD[c(1,.N)]), \n", " keyby=.(date)\n", " ][1:6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- Each day (e.g., 2023-01-03) has two rows, one for the smallest stock and the other for the largest. The `type` column indicate the type.\n", "\n", "**Execution Explained:**\n", "\n", "- Step 1: We select S&P 500 stocks. `.(date, ticker, cap)` allows us to only include the variable of interests.\n", "\n", "- Step 2: We sort by `date` and `cap`. This is **important**. Because by doing that, in Step 3 where we group by `date`, the first observation in each group will be the smallest stock (measured by `cap`), and the last observation will be the largest stock.\n", "\n", "- Step 3: We first group by `date`. `.SD[c(1,.N)]` will extract the first (`1`) and the last (`.N`) row in each group (`.N` represents the number of rows in the current group). `.SD` (meaning Subset of a Data table) is a special symbol provided by `data.table`, and it represents all the data for the current group. Because in Step 2 we already sort by `date` and `cap`, `.SD[c(1,.N)]` corresponds to the smallest and largest stock.\n", "\n", " - I use `type=list(c('smallest', 'largest'))` to add a new column to indicate which row represents the smallest and which row represents the largest. I also use `c()` to concatenate the newly created `type` column and the columns returned by `.SD`.\n", "\n", " - You can remove `type=list(c('smallest', 'largest'))`, and the result will be the same, just a bit less reading friendly. See below (see how the `type` column disappears):" ] }, { "cell_type": "code", "execution_count": 156, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 6 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>ticker</th><th scope=col>cap</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>MAYS</td><td>9.636480e+04</td></tr>\n", "\t<tr><td>2023-01-03</td><td>AAPL</td><td>1.981410e+09</td></tr>\n", "\t<tr><td>2023-01-04</td><td>YELL</td><td>1.305505e+05</td></tr>\n", "\t<tr><td>2023-01-04</td><td>AAPL</td><td>2.001847e+09</td></tr>\n", "\t<tr><td>2023-01-05</td><td>MAYS</td><td>9.376416e+04</td></tr>\n", "\t<tr><td>2023-01-05</td><td>AAPL</td><td>1.980618e+09</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 6 × 3\n", "\\begin{tabular}{lll}\n", " date & ticker & cap\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & MAYS & 9.636480e+04\\\\\n", "\t 2023-01-03 & AAPL & 1.981410e+09\\\\\n", "\t 2023-01-04 & YELL & 1.305505e+05\\\\\n", "\t 2023-01-04 & AAPL & 2.001847e+09\\\\\n", "\t 2023-01-05 & MAYS & 9.376416e+04\\\\\n", "\t 2023-01-05 & AAPL & 1.980618e+09\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 6 × 3\n", "\n", "| date <date> | ticker <chr> | cap <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | MAYS | 9.636480e+04 |\n", "| 2023-01-03 | AAPL | 1.981410e+09 |\n", "| 2023-01-04 | YELL | 1.305505e+05 |\n", "| 2023-01-04 | AAPL | 2.001847e+09 |\n", "| 2023-01-05 | MAYS | 9.376416e+04 |\n", "| 2023-01-05 | AAPL | 1.980618e+09 |\n", "\n" ], "text/plain": [ " date ticker cap \n", "1 2023-01-03 MAYS 9.636480e+04\n", "2 2023-01-03 AAPL 1.981410e+09\n", "3 2023-01-04 YELL 1.305505e+05\n", "4 2023-01-04 AAPL 2.001847e+09\n", "5 2023-01-05 MAYS 9.376416e+04\n", "6 2023-01-05 AAPL 1.980618e+09" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: select the S&P 500 stocks\n", " is_sp500==T, .(date, ticker, cap)\n", " # Step 2: sort by date and market cap\n", " ][order(date, cap)\n", " # Step 3: select the smallest and largest stocks on each day\n", " ][, .SD[c(1,.N)], \n", " keyby=.(date)\n", " ][1:6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is the ratio of the maximum trading volume to the minimum trading volume in each industry on each day?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem asks us to:\n", "\n", "- On each day and within each industry, find out the maximum and minimum trading volume.\n", "\n", "- Compute the ratio." ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>ratio</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td> 98400.5093</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>2196617.2703</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td> 828.2466</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Arts, Entertainment, and Recreation </td><td> 2789.4402</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Construction </td><td> 12978.0359</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & ratio\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 98400.5093\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 2196617.2703\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 828.2466\\\\\n", "\t 2023-01-03 & Arts, Entertainment, and Recreation & 2789.4402\\\\\n", "\t 2023-01-03 & Construction & 12978.0359\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 3\n", "\n", "| date <date> | industry <chr> | ratio <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 98400.5093 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 2196617.2703 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 828.2466 |\n", "| 2023-01-03 | Arts, Entertainment, and Recreation | 2789.4402 |\n", "| 2023-01-03 | Construction | 12978.0359 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", "4 2023-01-03\n", "5 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", "4 Arts, Entertainment, and Recreation \n", "5 Construction \n", " ratio \n", "1 98400.5093\n", "2 2196617.2703\n", "3 828.2466\n", "4 2789.4402\n", "5 12978.0359" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: sort by date, industry and dollar trading volume\n", " order(date, industry, prcvol)\n", " # Step 2: compute ratio for each industry on each day\n", " ][, .(ratio = prcvol[.N]/prcvol[1]),\n", " keyby=.(date, industry)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- In Step 1: we sort by date, industry and dollar trading volume. This is **important** because if we group by `date` and `industry` later, the first (last) row in each group will have the smallest (largest) trading volume. This technique is also used in the previous problem, and will be used through this book.\n", "\n", "- In Step 2: we first group by `date` and `industry`, and then compute ratio with `ratio = prcvol[.N]/prcvol[1]`. As explained earlier, `prcvol[.N]` (the last row) and `prcvol[1]` (the first row) correspond to the largest and smallest trading volume." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the sum of the dollar trading volume for the top 5 stocks in each industry on each day?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first find out the top 5 stocks with the largest dollar volume in each industry on each day, then we add them up." ] }, { "cell_type": "code", "execution_count": 158, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>2411654851</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>4883788253</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td> 241866864</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & prcvol\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 2411654851\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 4883788253\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 241866864\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | industry <chr> | prcvol <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 2411654851 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 4883788253 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 241866864 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " prcvol \n", "1 2411654851\n", "2 4883788253\n", "3 241866864" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: sort by date, industry and dollar trading volume\n", " order(date, industry, -prcvol)\n", " ][, \n", " # Step 2: get the total trading volume of the top 5 stocks\n", " .(prcvol=sum(prcvol[1:5])), keyby=.(date, industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: We first sort by `date`, `industry` and `-prcvol` (`-prcvol` means sort by `prcvol` **descendingly**). By doing that, when we group by `date` and `industry` in Step 2, `prcvol[1:5]` will correspond to the largest 5 values of `prcvol`.\n", "\n", " - Don't leave out the minus sign in `-prcvol`. Otherwise, `prcvol` will be sort ascendingly and hence `prcvol[1:5]` will correspond to the *smallest* 5 values of `prcvol`.\n", "\n", "- Step 2: Calculate the total trading volume." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ In each industry, what's the daily average return of stocks whose dollar trading volume exceeds the 80th percentile of the industry?" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>avg_ret</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>0.003159201</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>0.001522516</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.003461414</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & avg\\_ret\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 0.003159201\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 0.001522516\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 0.003461414\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | industry <chr> | avg_ret <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 0.003159201 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 0.001522516 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 0.003461414 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " avg_ret \n", "1 0.003159201\n", "2 0.001522516\n", "3 0.003461414" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: only keep rows whose trading volume is in the top 20%\n", " .SD[prcvol>=quantile(prcvol, 0.8)],\n", " keyby=.(date, industry)\n", " # compute the average return\n", " ][, .(avg_ret=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date, industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- In Step 1, we first group by `date` and `industry`. Next, within each group, we only want to keep rows whose dollar trading volume is larger than the 80th percentile, i.e., lines that satisfy `prcvol>=quantile(prcvol, 0.8)`. The results of `prcvol>=quantile(prcvol, 0.8)` is a boolean vector with the same length as `prcvol`, where `TRUE` means the condition is met while `FALSE` means not. We use this boolean vector to select the data: `.SD[prcvol>=quantile(prcvol, 0.8)]`. Remember that `.SD` (Subset of Data.table) is a special symbol in `data.table`, and it represents the current group. Basically, what `.SD[prcvol>=quantile(prcvol, 0.8)]` does is to only keep rows in `.SD` that satisfy the condition.\n", "\n", "- In Step 1, we compute the market-capital-weighted average return." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Extension:**\n", "\n", "If you just want to compute the average return instead of the value-weighted return, the following code is a simpler implementation (note how we use the results of `prcvol>=quantile(prcvol, 0.8)]` to select `retx`):" ] }, { "cell_type": "code", "execution_count": 160, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>avg_ret</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>0.001960478</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>0.001708134</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.010825990</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & avg\\_ret\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 0.001960478\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 0.001708134\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 0.010825990\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | industry <chr> | avg_ret <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 0.001960478 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 0.001708134 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 0.010825990 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " avg_ret \n", "1 0.001960478\n", "2 0.001708134\n", "3 0.010825990" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " .(avg_ret = mean(retx[prcvol>=quantile(prcvol, 0.8)])),\n", " keyby=.(date, industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the correlation coefficient between the average return of the top 10% stocks with the largest dollar trading volume and the bottom 10% stocks with the smallest dollar trading volume?" ] }, { "cell_type": "code", "execution_count": 161, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>ret_top10</th><th scope=col>ret_bottom10</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>-0.004592822</td><td> 0.0007446199</td></tr>\n", "\t<tr><td>2023-01-04</td><td> 0.007174620</td><td> 0.0066312533</td></tr>\n", "\t<tr><td>2023-01-05</td><td>-0.011002616</td><td>-0.0018086251</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & ret\\_top10 & ret\\_bottom10\\\\\n", " <date> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & -0.004592822 & 0.0007446199\\\\\n", "\t 2023-01-04 & 0.007174620 & 0.0066312533\\\\\n", "\t 2023-01-05 & -0.011002616 & -0.0018086251\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | ret_top10 <dbl> | ret_bottom10 <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | -0.004592822 | 0.0007446199 |\n", "| 2023-01-04 | 0.007174620 | 0.0066312533 |\n", "| 2023-01-05 | -0.011002616 | -0.0018086251 |\n", "\n" ], "text/plain": [ " date ret_top10 ret_bottom10 \n", "1 2023-01-03 -0.004592822 0.0007446199\n", "2 2023-01-04 0.007174620 0.0066312533\n", "3 2023-01-05 -0.011002616 -0.0018086251" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data_tmp = data[, \n", " # Step 1: select the returns of the top/bottom 10% stocks by trading volume\n", " .(ret_top10=retx[prcvol>=quantile(prcvol, 0.9)],\n", " cap_top10=cap[prcvol>=quantile(prcvol, 0.9)],\n", " ret_bottom10=retx[prcvol<=quantile(prcvol, 0.1)],\n", " cap_bottom10=cap[prcvol<=quantile(prcvol, 0.1)]), \n", " keyby=.(date)\n", " ][, \n", " # Step 2: compute the weighted average return on each day\n", " .(ret_top10=weighted.mean(ret_top10, cap_top10, na.rm=T),\n", " ret_bottom10=weighted.mean(ret_bottom10, cap_bottom10, na.rm=T)), \n", " keyby=.(date)]\n", " \n", "data_tmp[1:3] # output of Step 2" ] }, { "cell_type": "code", "execution_count": 162, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "0.620066264125022" ], "text/latex": [ "0.620066264125022" ], "text/markdown": [ "0.620066264125022" ], "text/plain": [ "[1] 0.6200663" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data_tmp[, \n", " # Step 3: compute the correlation coefficient\n", " cor(ret_top10, ret_bottom10)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explain**:\n", "\n", "- Step 1: We select the top/bottom 10% stocks by trading volume. Note that we use the same technique used in the previous problem: Frist create a boolean vector with `prcvol>=quantile(prcvol, 0.9)` and then use it to select the returns with `retx[prcvol>=quantile(prcvol, 0.9)]`. We also select the market capital `cap` for the top/bottom 10% stocks because we'll use them as the weight in Step 2.\n", "\n", "- Step 2: Compute the value-weighted return for each day.\n", "\n", "- Step 3: Compute the correlation coefficient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(avoid-dup-rows)=\n", "## ⭐️ Find out the industries whose component stocks have a larger average dollar trading volume than the market average" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make sure we're on the same page, my understanding of this problem is that it requires us to:\n", "\n", "- Calculate the average trading volume of stocks on the market on each day (i.e., we aggregate the trading volume of all stocks on the market and then divide by the number of stocks). This is the \"market average.\"\n", "\n", "- Similarly, calculate the average trading volume for stocks within each industry. So every industry will have its own value. This is the \"industry average.\"\n", "\n", "- Select all industries whose \"industry average\" is larger than the \"market average.\"" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol_ind</th><th scope=col>prcvol_mkt</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Information </td><td>2849298873</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Manufacturing</td><td>2940308173</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Retail Trade </td><td>2219859287</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-04</td><td>Information </td><td>4682660169</td><td>2103339254</td></tr>\n", "\t<tr><td>2023-01-04</td><td>Manufacturing</td><td>2634129565</td><td>2103339254</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 4\n", "\\begin{tabular}{llll}\n", " date & industry & prcvol\\_ind & prcvol\\_mkt\\\\\n", " <date> & <chr> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Information & 2849298873 & 1998086920\\\\\n", "\t 2023-01-03 & Manufacturing & 2940308173 & 1998086920\\\\\n", "\t 2023-01-03 & Retail Trade & 2219859287 & 1998086920\\\\\n", "\t 2023-01-04 & Information & 4682660169 & 2103339254\\\\\n", "\t 2023-01-04 & Manufacturing & 2634129565 & 2103339254\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 4\n", "\n", "| date <date> | industry <chr> | prcvol_ind <dbl> | prcvol_mkt <dbl> |\n", "|---|---|---|---|\n", "| 2023-01-03 | Information | 2849298873 | 1998086920 |\n", "| 2023-01-03 | Manufacturing | 2940308173 | 1998086920 |\n", "| 2023-01-03 | Retail Trade | 2219859287 | 1998086920 |\n", "| 2023-01-04 | Information | 4682660169 | 2103339254 |\n", "| 2023-01-04 | Manufacturing | 2634129565 | 2103339254 |\n", "\n" ], "text/plain": [ " date industry prcvol_ind prcvol_mkt\n", "1 2023-01-03 Information 2849298873 1998086920\n", "2 2023-01-03 Manufacturing 2940308173 1998086920\n", "3 2023-01-03 Retail Trade 2219859287 1998086920\n", "4 2023-01-04 Information 4682660169 2103339254\n", "5 2023-01-04 Manufacturing 2634129565 2103339254" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: compute the \"market average\"\n", " .(prcvol_mkt=weighted.mean(prcvol, cap, na.rm=T), industry, prcvol, cap), \n", " keyby=.(date)\n", " # Step 2: compute the \"industry average\"\n", " ][, .(prcvol_ind=weighted.mean(prcvol, cap, na.rm=T), prcvol_mkt=prcvol_mkt[1]), \n", " keyby=.(date, industry)\n", " # Step 3: only select industries whose average trading volume is larger than the market average\n", " ][prcvol_ind>prcvol_mkt\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- Take \"2023-01-03\" for example. This date has three rows (1-3), each representing an industry that meet our condition. `prcvol_ind` is the industry average dollar volume and `prcvol_mkt` is the market average dollar volume.\n", "\n", "**Execution Explained:**\n", "\n", "- Step 1: We first group by `date`, because we want to include all stocks on each day to compute the market average. `.(prcvol_mkt=weighted.mean(prcvol, cap, na.rm=T), industry, prcvol, cap)` does two things:\n", "\n", " - It includes `industry`, `prcvol` and `cap` in the output. These three columns are presented in the original data. We include them because we'll need them in Step 2.\n", " \n", " - It also creates a new column `procvol_mkt`, which is the weighted average dollar trading volume of the market.\n", "\n", "- Step 2: We next group by `date` and `industry`, because we want to calculate the industry average. The tricky thing is `prcvol_mkt=prcvol_mkt[1]`, why we need to explicitly select the first element (`[1]`)? Let's look at the full line of code:\n", "\n", " - `.(prcvol_ind=weighted.mean(prcvol, cap, na.rm=T), prcvol_mkt=prcvol_mkt[1])`. This line created two columns: `prcvol_ind` and `prcvol_mkt`. For `prcvol_ind`, the result is a **scalar** (a single number). However, `prcvol_mkt` is a **constant vector**, meaning its elements are identical. This is understandable, for each industry, the \"market average\" should be the same. The problem is, if you put a scaler and a constant together in the output, the result will be \"broadcasted,\" meaning the scaler will be repeated until it has the same length as the vector. This is not desirable, and that's why we also need to reduce `prcvol_mkt` to a scaler by `prcvol_mkt=prcvol_mkt[1]`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} Avoid Unnecessary Broadcasting \n", "When using `data.table`, you need to pay special attention to the size of the output. Techniques like `prcvol_mkt=prcvol_mkt[1]` are very useful to avoid unnecessary broadcasting. \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we don't reduce `prcvol_mkt` to a scaler, the code and output will become like this (note that many duplicates are introduced):" ] }, { "cell_type": "code", "execution_count": 164, "metadata": { "tags": [], "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 5 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>prcvol_ind</th><th scope=col>prcvol_mkt</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services</td><td>407673816</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services</td><td>407673816</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services</td><td>407673816</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services</td><td>407673816</td><td>1998086920</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services</td><td>407673816</td><td>1998086920</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 5 × 4\n", "\\begin{tabular}{llll}\n", " date & industry & prcvol\\_ind & prcvol\\_mkt\\\\\n", " <date> & <chr> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 407673816 & 1998086920\\\\\n", "\t 2023-01-03 & Accommodation and Food Services & 407673816 & 1998086920\\\\\n", "\t 2023-01-03 & Accommodation and Food Services & 407673816 & 1998086920\\\\\n", "\t 2023-01-03 & Accommodation and Food Services & 407673816 & 1998086920\\\\\n", "\t 2023-01-03 & Accommodation and Food Services & 407673816 & 1998086920\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 5 × 4\n", "\n", "| date <date> | industry <chr> | prcvol_ind <dbl> | prcvol_mkt <dbl> |\n", "|---|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 407673816 | 1998086920 |\n", "| 2023-01-03 | Accommodation and Food Services | 407673816 | 1998086920 |\n", "| 2023-01-03 | Accommodation and Food Services | 407673816 | 1998086920 |\n", "| 2023-01-03 | Accommodation and Food Services | 407673816 | 1998086920 |\n", "| 2023-01-03 | Accommodation and Food Services | 407673816 | 1998086920 |\n", "\n" ], "text/plain": [ " date industry prcvol_ind prcvol_mkt\n", "1 2023-01-03 Accommodation and Food Services 407673816 1998086920\n", "2 2023-01-03 Accommodation and Food Services 407673816 1998086920\n", "3 2023-01-03 Accommodation and Food Services 407673816 1998086920\n", "4 2023-01-03 Accommodation and Food Services 407673816 1998086920\n", "5 2023-01-03 Accommodation and Food Services 407673816 1998086920" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: compute the \"market average\"\n", " .(prcvol_mkt=weighted.mean(prcvol, cap, na.rm=T), industry, prcvol, cap), \n", " keyby=.(date)\n", " # Step 2: compute the \"industry average\"\n", " ][, .(prcvol_ind=weighted.mean(prcvol, cap, na.rm=T), prcvol_mkt=prcvol_mkt), \n", " keyby=.(date, industry)\n", " ][1:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(lm-in-j)=\n", "## ⭐️ What's the daily \"abnormal return\" of each stock?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In finance, the daily stock return is conceptually viewed as the sum of two parts: a \"baseline return,\" which is the part that can be explained by some known factors (e.g., firm characteristics), and an \"abnormal return\" that cannot. As you can see, abnormal return is defined against how the baseline return is computed. In this example, we use the \"market model\" to calculate the baseline return, and the abnormal return is calculated as the raw stock return less the baseline return:\n", "\n", "Assume $r_{it}$ is the raw daily return for stock $i$ on day $t$; $r_t^{mkt}$ is the market return on day $t$. We first run the following regression:\n", "\n", "$$\n", "r_{it} = \\alpha_i + \\beta_i r_{t}^{mkt} + \\varepsilon_{it}\n", "$$\n", "\n", "The $\\alpha_i + \\beta_i r_{t}^{mkt}$ part is called the baseline return, because it's something that can be explained by the coefficients of the regression. Abnormal return $AR_{it}$ is hence defined as:\n", "\n", "$$\n", "AR_{it} = r_{it} - (\\alpha_i + \\beta_i r_{t}^{mkt})\n", "$$\n", "\n", "which is exactly the $\\varepsilon$ of the regression." ] }, { "cell_type": "code", "execution_count": 165, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 5</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>date</th><th scope=col>ar</th><th scope=col>alpha</th><th scope=col>beta</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>2023-01-03</td><td> 0.012975042</td><td>7.201256e-05</td><td>0.2987578</td></tr>\n", "\t<tr><td>10026</td><td>2023-01-04</td><td>-0.004386641</td><td>7.201256e-05</td><td>0.2987578</td></tr>\n", "\t<tr><td>10026</td><td>2023-01-05</td><td>-0.007474872</td><td>7.201256e-05</td><td>0.2987578</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 5\n", "\\begin{tabular}{lllll}\n", " permno & date & ar & alpha & beta\\\\\n", " <dbl> & <date> & <dbl> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 2023-01-03 & 0.012975042 & 7.201256e-05 & 0.2987578\\\\\n", "\t 10026 & 2023-01-04 & -0.004386641 & 7.201256e-05 & 0.2987578\\\\\n", "\t 10026 & 2023-01-05 & -0.007474872 & 7.201256e-05 & 0.2987578\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 5\n", "\n", "| permno <dbl> | date <date> | ar <dbl> | alpha <dbl> | beta <dbl> |\n", "|---|---|---|---|---|\n", "| 10026 | 2023-01-03 | 0.012975042 | 7.201256e-05 | 0.2987578 |\n", "| 10026 | 2023-01-04 | -0.004386641 | 7.201256e-05 | 0.2987578 |\n", "| 10026 | 2023-01-05 | -0.007474872 | 7.201256e-05 | 0.2987578 |\n", "\n" ], "text/plain": [ " permno date ar alpha beta \n", "1 10026 2023-01-03 0.012975042 7.201256e-05 0.2987578\n", "2 10026 2023-01-04 -0.004386641 7.201256e-05 0.2987578\n", "3 10026 2023-01-05 -0.007474872 7.201256e-05 0.2987578" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: construct the market return\n", " ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date)\n", " ][, \n", " # Step 2: only keep stocks with at least 50 observations\n", " .SD[.N>=50], keyby=.(permno)\n", " ][\n", " # (optional) remove missing values\n", " !is.na(retx)\n", " ][,\n", " # Step 3: get alpha, beta, and AR\n", " {\n", " lm_out = lm(retx ~ ret_mkt)\n", " alpha = coef(lm_out)[1]\n", " beta = coef(lm_out)[2]\n", " ar = resid(lm_out)\n", " list(date, ar, alpha, beta)\n", " },\n", " keyby=.(permno)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: We construct a new column `ret_mkt` to capture the daily value-weighted return of the whole market.\n", "\n", "- Step 2: We remove any stock that has less than 50 observations (days) in the sample. (If there're too few observations, the regression results in Step 3 will not be robust.)\n", "\n", " - We first group by `permno`, because the operation is for individual stocks.\n", "\n", " - You should be familiar with `.SD` now, which represents the current group, i.e., the whole data for the current `permno`.\n", "\n", " - `.N>=50`: `.N` is the number of rows in the current group, we require it to be no less than 50.\n", "\n", "- Step 3: The whole operations are wrapped in curly brackets, because we want to reuse what we've created in the `j` part (we also used this technique in [this problem](q-9)).\n", "\n", " - We first group by `permno`, because the regression is for individual stocks.\n", "\n", " - We then regress the daily return on the market return, `lm(retx ~ ret_mkt)`, and save the result to `lm_out`.\n", "\n", " - We use `coef(lm_out)` to extract the coefficients of the regression results. The output is a list, with the first element being the intercept and the second element being the beta.\n", "\n", " - We also use `resid(lm_out)` to extract the residuals. That's exactly the abnormal return we're looking for.\n", "\n", " - At last, we specify what variables we want to put in the final output: `list(date, ar, alpha, beta)`\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ What's the daily \"self-excluding\" industry return?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let me explain the requirements of the problem. Suppose on day t, there're three industries: A, B, and C, and their daily returns are \"ret_A,\" \"ret_B\" and \"ret_A.\" The problem asks us to calculate three versions of daily industry return: 1) the average of ret_A and ret_B, 2) the average of ret_B and ret_C, and 3) the average of ret_A and ret_C.\n", "\n", "Specifically, for industry $i$ on day $t$, its daily self-excluding return $ret_{it}$ is defined as:\n", "\n", "$$\n", "ret_{it} = \\sum_{j=1,j\\ne i}^N \\Biggl\\{ \\frac {ind\\_cap_{jt} \\times ind\\_ret_{jt}}{\\sum_{j=1,j\\ne i}^{N_j} ind\\_cap_j} \\Biggr\\}\n", "$$\n", "where \n", "- $N$: total number of industries\n", "- $N_j$: total number of stocks in industry $j$.\n", "- $ind\\_cap_{jt}$: daily market capital of industry $j$.\n", "- $ind\\_ret_{jt}$: daily return of industry $j$.\n", "\n", "\n", "We're going to approach this problem as follows:\n", "\n", "- First, compute the daily average return for each industry. \n", "\n", " - We define industry average return as the value-weighed return of its component stocks. We've done this in [this problem](industry-volatility).\n", "\n", "- Second, calculate the self-excluding return." ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>ind_ret</th><th scope=col>ind_cap</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>0.002703254</td><td> 722345679</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>0.001455542</td><td>1470427710</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.002476840</td><td> 48613819</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 4\n", "\\begin{tabular}{llll}\n", " date & industry & ind\\_ret & ind\\_cap\\\\\n", " <date> & <chr> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & 0.002703254 & 722345679\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & 0.001455542 & 1470427710\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & 0.002476840 & 48613819\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 4\n", "\n", "| date <date> | industry <chr> | ind_ret <dbl> | ind_cap <dbl> |\n", "|---|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | 0.002703254 | 722345679 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | 0.001455542 | 1470427710 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | 0.002476840 | 48613819 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " ind_ret ind_cap \n", "1 0.002703254 722345679\n", "2 0.001455542 1470427710\n", "3 0.002476840 48613819" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# First, compute the daily average return for each industry\n", "# For the definition of `ind_ret` and `ind_cap`, see the above explanation\n", "ind_rets = data[, \n", " .(ind_ret=weighted.mean(retx, cap, na.rm=T),\n", " ind_cap=sum(cap, na.rm=T)),\n", " keyby=.(date, industry)]\n", "\n", "ind_rets[1:3]" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>industry</th><th scope=col>self_ex_ret</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>Accommodation and Food Services </td><td>-0.004230309</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Administrative and Support and Waste Management and Remediation Services</td><td>-0.004322716</td></tr>\n", "\t<tr><td>2023-01-03</td><td>Agriculture, Forestry, Fishing and Hunting </td><td>-0.004106102</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & industry & self\\_ex\\_ret\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-03 & Accommodation and Food Services & -0.004230309\\\\\n", "\t 2023-01-03 & Administrative and Support and Waste Management and Remediation Services & -0.004322716\\\\\n", "\t 2023-01-03 & Agriculture, Forestry, Fishing and Hunting & -0.004106102\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | industry <chr> | self_ex_ret <dbl> |\n", "|---|---|---|\n", "| 2023-01-03 | Accommodation and Food Services | -0.004230309 |\n", "| 2023-01-03 | Administrative and Support and Waste Management and Remediation Services | -0.004322716 |\n", "| 2023-01-03 | Agriculture, Forestry, Fishing and Hunting | -0.004106102 |\n", "\n" ], "text/plain": [ " date \n", "1 2023-01-03\n", "2 2023-01-03\n", "3 2023-01-03\n", " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " self_ex_ret \n", "1 -0.004230309\n", "2 -0.004322716\n", "3 -0.004106102" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Then, calculate the self-excluding return\n", "ind_rets[,\n", " {\n", " # Step 1: create an empty vector to store the results\n", " self_ex_ret = vector()\n", "\n", " # Step 2: for each industry, compute the self-excluding return\n", " for (i in 1:.N) {\n", " self_ex_ret[i] = weighted.mean(ind_ret[-i], ind_cap[-i], na.rm=T)\n", " }\n", "\n", " # Step 3: return the results\n", " list(industry, self_ex_ret)\n", " },\n", " keyby=.(date)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "Note that we wrap the whole `j` part in curly brackets again.\n", "\n", "- Step 1: We initialize the self-excluding return `self_ex_ret` as an empty vector (this helps to speed up the loop execution in Step 2).\n", "\n", "- Step 2: We loop over each industry (from `1` to `.N`). Note that we've already grouped by `date`, so `.N` is exactly the number of industries in each group.\n", "\n", " - `ind_ret[-i]` represents the return vector `ind_ret` with the i-th element removed.\n", "\n", " - `ind_cap[-i]` represents the market capital vector `ind_cap` with the i-th element removed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ How many industries exceed the market average return on each day?" ] }, { "cell_type": "code", "execution_count": 168, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 2 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk</th><th scope=col>industry</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th><th scope=col><list></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-03</td><td>14</td><td>Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Finance and Insurance , Health Care and Social Assistance , Information , Other Services (except Public Administration) , Real Estate and Rental and Leasing , Retail Trade , Transportation and Warehousing , Wholesale Trade </td></tr>\n", "\t<tr><td>2023-01-04</td><td>15</td><td>Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Health Care and Social Assistance , Management of Companies and Enterprises , Manufacturing , Mining, Quarrying, and Oil and Gas Extraction , Other Services (except Public Administration) , Professional, Scientific, and Technical Services , Real Estate and Rental and Leasing , Transportation and Warehousing , Utilities </td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 2 × 3\n", "\\begin{tabular}{lll}\n", " date & num\\_stk & industry\\\\\n", " <date> & <int> & <list>\\\\\n", "\\hline\n", "\t 2023-01-03 & 14 & Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Finance and Insurance , Health Care and Social Assistance , Information , Other Services (except Public Administration) , Real Estate and Rental and Leasing , Retail Trade , Transportation and Warehousing , Wholesale Trade \\\\\n", "\t 2023-01-04 & 15 & Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Health Care and Social Assistance , Management of Companies and Enterprises , Manufacturing , Mining, Quarrying, and Oil and Gas Extraction , Other Services (except Public Administration) , Professional, Scientific, and Technical Services , Real Estate and Rental and Leasing , Transportation and Warehousing , Utilities \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 2 × 3\n", "\n", "| date <date> | num_stk <int> | industry <list> |\n", "|---|---|---|\n", "| 2023-01-03 | 14 | Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Finance and Insurance , Health Care and Social Assistance , Information , Other Services (except Public Administration) , Real Estate and Rental and Leasing , Retail Trade , Transportation and Warehousing , Wholesale Trade |\n", "| 2023-01-04 | 15 | Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Health Care and Social Assistance , Management of Companies and Enterprises , Manufacturing , Mining, Quarrying, and Oil and Gas Extraction , Other Services (except Public Administration) , Professional, Scientific, and Technical Services , Real Estate and Rental and Leasing , Transportation and Warehousing , Utilities |\n", "\n" ], "text/plain": [ " date num_stk\n", "1 2023-01-03 14 \n", "2 2023-01-04 15 \n", " industry \n", "1 Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Finance and Insurance , Health Care and Social Assistance , Information , Other Services (except Public Administration) , Real Estate and Rental and Leasing , Retail Trade , Transportation and Warehousing , Wholesale Trade \n", "2 Accommodation and Food Services , Administrative and Support and Waste Management and Remediation Services, Agriculture, Forestry, Fishing and Hunting , Arts, Entertainment, and Recreation , Construction , Educational Services , Health Care and Social Assistance , Management of Companies and Enterprises , Manufacturing , Mining, Quarrying, and Oil and Gas Extraction , Other Services (except Public Administration) , Professional, Scientific, and Technical Services , Real Estate and Rental and Leasing , Transportation and Warehousing , Utilities " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: compute the market return\n", " ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date)\n", " ][,\n", " # Step 2: compute the industry return\n", " .(ind_ret=weighted.mean(retx, cap, na.rm=T), ret_mkt=ret_mkt[1]),\n", " keyby=.(date, industry)\n", " ][\n", " # Step 3: calculate the number of stocks exceeding the market average\n", " ind_ret>ret_mkt,\n", " .(num_stk=.N, industry=list(industry)),\n", " keyby=.(date)\n", " ][1:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1 and Step 2: Calculate the industry- and market-level return. We've done this many times in previous problems.\n", "\n", "- Step 3: Calculate the number of stocks exceeding the market average\n", "\n", " - `ind_ret>ret_mkt` (the `i` part) only selects industries that exceed the market average.\n", "\n", " - `num_ind=.N` counts the number of industry.\n", "\n", " - `industry=list(industry)`. This part is interesting. It basically \"packs\" the multiple elements in a vector into a single element of a list, avoiding duplicated lines in the output. To understand that, recall that `industry` is a vector that contains all the industries that meet our criteria in the given day. To `data.table`, each element of `industry` will be expanded to a separate row. However, we only want to keep one row for each day in the output. By wrapping `industry` using `list`, we collect all the industries into a single cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{tip}\n", "`var = list(var)` or `var = unique(list(var))` is a frequently used technique to pack the otherwise-occupying-many-lines output into a single row. This is especially useful when you want your output to be a **set**.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(q-9)=\n", "## ⭐️ How many stocks have experienced at least one daily price increase of 5%+ in the past three days? (Count the number on each day.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To be clear, we define what's \"past three days\" as follows: on day `t`, the previous three days are `[t-3, t-2, t-1]`. Of course you can have different definition as mine, but the solution is the same.\n", "\n", "The challenge in Q9 is that we need to do **rolling** count. On day `t`, we need to query the values from `(t-3):(t-1)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{warning}\n", "Q9 is difficult and it requires some advanced techniques of `data.table`. However, as you can see below, the core of the code is only about five lines. I can image that the code could easily exceed dozens of lines if you use `dplyr` or `pandas.`\n", "\n", "I hope that after reading Q9, you'll get an appreciation of how amazing and efficient the syntax of `data.table` is.\n", "```" ] }, { "cell_type": "code", "execution_count": 169, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk_3d</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-06</td><td>738</td></tr>\n", "\t<tr><td>2023-01-09</td><td>867</td></tr>\n", "\t<tr><td>2023-01-10</td><td>747</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " date & num\\_stk\\_3d\\\\\n", " <date> & <int>\\\\\n", "\\hline\n", "\t 2023-01-06 & 738\\\\\n", "\t 2023-01-09 & 867\\\\\n", "\t 2023-01-10 & 747\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| date <date> | num_stk_3d <int> |\n", "|---|---|\n", "| 2023-01-06 | 738 |\n", "| 2023-01-09 | 867 |\n", "| 2023-01-10 | 747 |\n", "\n" ], "text/plain": [ " date num_stk_3d\n", "1 2023-01-06 738 \n", "2 2023-01-09 867 \n", "3 2023-01-10 747 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: convert the permno into a list\n", " retx>=0.05, .(permno_list=list(permno)), keyby=.(date)\n", " ][, {\n", " # Step 2: compute the number of stocks that open up 5% or more\n", " num_stk_3d = vector()\n", " for (i in 4:.N) {\n", " num_stk_3d[i] = permno_list[(i-3):(i-1)] %>% \n", " unlist() %>% unique() %>% length()\n", " }\n", " list(date, num_stk_3d)\n", " }\n", " ][!is.na(num_stk_3d)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: We first select stocks with daily return larger than 5% (`retx>=0.05`), then we create a new column `permno_list` which represents all `permno` in a given day as a `list` (see the output below). \n", "\n", " - Converting to a list is important because it allows us to collect all the stocks across different days and run deduplication in Step 2.\n", "\n", "\n", "- Step 2: We compute the number of stocks that open up 5% or more in the previous three days.\n", " \n", " - `num_stk_3d = vector()`: We first create an empty vector, it will store all the `permno`s (stock IDs) that open up 5% or more.\n", "\n", " - `for (i in 4:.N)...`: This is the key part of the code. Let's take `i` to be 4 and see what it does. \n", "\n", " - `permno_list[(i-3):(i-1)]` becomes `permno_list[1:3]`, that is, the stock IDs in the first three days. Note that `permno_list[1:3]` is a list of **three** elements, where each element represents a day.\n", "\n", " - `%>% unlist()`: The `unlist` function \"unpacks (flatterns)\" `permno_list[1:3]` so the result becomes a non-nested list of stock IDs.\n", "\n", " - `%>% unique()`: Remove any duplicated stock IDs.\n", "\n", " - `%>% length()`: Count the number of stocks.\n", "\n", " - As you can see, when `i` equals 4, we count from day 1 to 3. Similarly when `i` equals 5, we count from day 2 to 4.\n", "\n", " - `list(date, num_stk_3d)`: Collect the final output columns and pack them into a list. This is required by `data.table` if the `j` part is wrapped by curly brackets `{...}` (if you don't know what's the `j` part please see the introduction of `data.table`). You can think of this line as a `return` command but with the `return` keyword hidden." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(past-three-days)=\n", "## ⭐️ How many stocks outperform the market average return on *all* of the past three days?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem is very much like the one we've just solved. The difference is that in the previous problem, stocks that meet the criteria in *any* of the past three days will be included, but this time we require that the stocks have to meet the criteria on *all** of the past three days.\n", "\n", "Try to do it yourself before continuing." ] }, { "cell_type": "code", "execution_count": 170, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk_3d</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-06</td><td>676</td></tr>\n", "\t<tr><td>2023-01-09</td><td>519</td></tr>\n", "\t<tr><td>2023-01-10</td><td>456</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " date & num\\_stk\\_3d\\\\\n", " <date> & <int>\\\\\n", "\\hline\n", "\t 2023-01-06 & 676\\\\\n", "\t 2023-01-09 & 519\\\\\n", "\t 2023-01-10 & 456\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| date <date> | num_stk_3d <int> |\n", "|---|---|\n", "| 2023-01-06 | 676 |\n", "| 2023-01-09 | 519 |\n", "| 2023-01-10 | 456 |\n", "\n" ], "text/plain": [ " date num_stk_3d\n", "1 2023-01-06 676 \n", "2 2023-01-09 519 \n", "3 2023-01-10 456 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: Compute the market average return\n", " , ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][\n", " # Step 2: Convert the stocks meeting the criteria into a list\n", " retx>=ret_mkt, .(permno_list=list(permno)), keyby=.(date)\n", " ][, {\n", " # Step 3: compute the number of stocks that meet the criteria\n", " num_stk_3d = vector()\n", " for (i in 4:.N) {\n", " num_stk_3d[i] = Reduce(\n", " intersect, \n", " permno_list[(i-3):(i-1)]\n", " ) %>% length()\n", " }\n", " list(date, num_stk_3d)\n", " }\n", " ][!is.na(num_stk_3d)\n", " ][1:3]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ How many stocks outperform the market at least for *two out of the past three* days?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's make the problem harder!" ] }, { "cell_type": "code", "execution_count": 171, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk_3d</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-06</td><td>2268</td></tr>\n", "\t<tr><td>2023-01-09</td><td>1951</td></tr>\n", "\t<tr><td>2023-01-10</td><td>1922</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " date & num\\_stk\\_3d\\\\\n", " <date> & <int>\\\\\n", "\\hline\n", "\t 2023-01-06 & 2268\\\\\n", "\t 2023-01-09 & 1951\\\\\n", "\t 2023-01-10 & 1922\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| date <date> | num_stk_3d <int> |\n", "|---|---|\n", "| 2023-01-06 | 2268 |\n", "| 2023-01-09 | 1951 |\n", "| 2023-01-10 | 1922 |\n", "\n" ], "text/plain": [ " date num_stk_3d\n", "1 2023-01-06 2268 \n", "2 2023-01-09 1951 \n", "3 2023-01-10 1922 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 1: Compute the market average return\n", " , ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][\n", " # Step 2: Convert the stocks meeting the criteria into a list\n", " retx>=ret_mkt, .(permno_list=list(permno)), keyby=.(date)\n", " ][, {\n", " # Step 3: compute the number of stocks that meet the criteria\n", " num_stk_3d = vector()\n", " for (i in 4:.N) {\n", " num_stk_3d[i] = permno_list[(i-3):(i-1)] %>% unlist() %>% \n", " table() %>% .[.>=2] %>% \n", " length()\n", " }\n", " list(date, num_stk_3d)\n", " }\n", " ][!is.na(num_stk_3d)\n", " ][1:3]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the key change is in Step 3, where we use\n", "\n", "```r\n", "permno_list[(i-3):(i-1)] %>% unlist() %>% \n", " table() %>% .[.>=2]\n", "```\n", "\n", "The above lines of code find out stocks that at least appear two times in the previous three days. To better understand how it works, let's look at a simpler example:" ] }, { "cell_type": "code", "execution_count": 172, "metadata": { "tags": [ "remove-output" ], "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<style>\n", ".list-inline {list-style: none; margin:0; padding: 0}\n", ".list-inline>li {display: inline-block}\n", ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", "</style>\n", "<ol class=list-inline><li>'a'</li><li>'b'</li></ol>\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'a'\n", "\\item 'b'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'a'\n", "2. 'b'\n", "\n", "\n" ], "text/plain": [ "[1] \"a\" \"b\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "<style>\n", ".list-inline {list-style: none; margin:0; padding: 0}\n", ".list-inline>li {display: inline-block}\n", ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", "</style>\n", "<ol class=list-inline><li>'a'</li><li>'b'</li><li>'c'</li><li>'a'</li><li>'b'</li><li>'f'</li><li>'a'</li><li>'h'</li><li>'i'</li></ol>\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'a'\n", "\\item 'b'\n", "\\item 'c'\n", "\\item 'a'\n", "\\item 'b'\n", "\\item 'f'\n", "\\item 'a'\n", "\\item 'h'\n", "\\item 'i'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'a'\n", "2. 'b'\n", "3. 'c'\n", "4. 'a'\n", "5. 'b'\n", "6. 'f'\n", "7. 'a'\n", "8. 'h'\n", "9. 'i'\n", "\n", "\n" ], "text/plain": [ "[1] \"a\" \"b\" \"c\" \"a\" \"b\" \"f\" \"a\" \"h\" \"i\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ ".\n", "a b c f h i \n", "3 2 1 1 1 1 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ ".\n", "a b \n", "3 2 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "<style>\n", ".list-inline {list-style: none; margin:0; padding: 0}\n", ".list-inline>li {display: inline-block}\n", ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", "</style>\n", "<ol class=list-inline><li>'a'</li><li>'b'</li></ol>\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'a'\n", "\\item 'b'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'a'\n", "2. 'b'\n", "\n", "\n" ], "text/plain": [ "[1] \"a\" \"b\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# suppose we have a list with three elements, each elements is a vector containing letters\n", "l = list(c('a', 'b', 'c'), c('a', 'b', 'f'), c('a', 'h', 'i'))\n", "\n", "# Now we want to find out the letters that appear at least two times in the list\n", "l %>% unlist() %>% table() %>% .[.>=2] %>% names()\n", "#> [1] \"a\" \"b\"\n", "\n", "# To understand the above line, let's break it down:\n", "\n", "l %>% unlist() # unpack\n", "#> [1] \"a\" \"b\" \"c\" \"a\" \"b\" \"f\" \"a\" \"h\" \"i\"\n", "\n", "l %>% unlist() %>% table() # count frequency of each letter\n", "#> .\n", "#> a b c f h i \n", "#> 3 2 1 1 1 1\n", "\n", "l %>% unlist() %>% table() %>% .[.>=2] # only keep frequencies >= 2\n", "#> .\n", "#> a b\n", "#> 3 2\n", "\n", "l %>% unlist() %>% table() %>% .[.>=2] %>% names()\n", "#> [1] \"a\" \"b\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you understand the above example, you won't have any problem in understanding the answer key." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## In each *month*, how many stocks outperform the market?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make sure our understanding of this problem is on the same page: This problem requires us to compare the *monthly* return of the stocks and the market, not daily.\n", "\n", "In the following solution, I put the calculation of monthly \"stock\" and \"market\" returns in two separate cells. It's because the computation requires us to aggregate the data on different levels: to compute market return, we only aggregate on year-month, but for stock return, we need to aggregate on year-month as well as on the individual stock level. So it's better to process them separately." ] }, { "cell_type": "code", "execution_count": 173, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>yearmon</th><th scope=col>mon_ret_mkt</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-01</td><td> 0.07608082</td></tr>\n", "\t<tr><td>2023-02-01</td><td>-0.01650494</td></tr>\n", "\t<tr><td>2023-03-01</td><td> 0.03510999</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " yearmon & mon\\_ret\\_mkt\\\\\n", " <date> & <dbl>\\\\\n", "\\hline\n", "\t 2023-01-01 & 0.07608082\\\\\n", "\t 2023-02-01 & -0.01650494\\\\\n", "\t 2023-03-01 & 0.03510999\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| yearmon <date> | mon_ret_mkt <dbl> |\n", "|---|---|\n", "| 2023-01-01 | 0.07608082 |\n", "| 2023-02-01 | -0.01650494 |\n", "| 2023-03-01 | 0.03510999 |\n", "\n" ], "text/plain": [ " yearmon mon_ret_mkt\n", "1 2023-01-01 0.07608082\n", "2 2023-02-01 -0.01650494\n", "3 2023-03-01 0.03510999" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#--- Calculate the monthly \"market\" return ---#\n", "\n", "mon_ret_mkt = data[\n", " # Step 0: sort by permno and date\n", " order(permno, date), .(permno, date, retx, cap)\n", " ][,\n", " # Step 1: create the daily market return\n", " .(ret_mkt=weighted.mean(retx, cap, na.rm=T),\n", " yearmon=floor_date(date, 'month')), \n", " keyby=.(date)\n", " ][,\n", " # Step 2: create the monthly market return\n", " .(mon_ret_mkt=prod(1+ret_mkt)-1),\n", " keyby=.(yearmon)\n", " ]\n", "\n", "mon_ret_mkt[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "Most of the above code shouldn't be unfamiliar to you by now. There're two places worth noting:\n", "\n", "- In Step 1, we create a month indicator by using the `floor_date` function from the `lubridate` package. `floor_date(date, 'month')` will floor every date to the beginning of the month (note here we specify the rounding unit to \"month\"), e.g., \"2023-05-23\" will be floored to \"2023-05-01.\" This quickly creates a month indicator.\n", "\n", "- In Step 2, we use `prod(1+ret_mkt)-1` to compute the monthly market return. `prod` is essentially $\\prod$, which multiply all elements in the given vector. Because `ret_mkt` is the daily market return, the result of `prod(1+ret_mkt)` is the net value at the end of the month. We take `1` from the result, yielding the market return over the past month." ] }, { "cell_type": "code", "execution_count": 174, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>yearmon</th><th scope=col>num_stk</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-01</td><td>2140</td></tr>\n", "\t<tr><td>2023-02-01</td><td>1897</td></tr>\n", "\t<tr><td>2023-03-01</td><td> 843</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " yearmon & num\\_stk\\\\\n", " <date> & <int>\\\\\n", "\\hline\n", "\t 2023-01-01 & 2140\\\\\n", "\t 2023-02-01 & 1897\\\\\n", "\t 2023-03-01 & 843\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| yearmon <date> | num_stk <int> |\n", "|---|---|\n", "| 2023-01-01 | 2140 |\n", "| 2023-02-01 | 1897 |\n", "| 2023-03-01 | 843 |\n", "\n" ], "text/plain": [ " yearmon num_stk\n", "1 2023-01-01 2140 \n", "2 2023-02-01 1897 \n", "3 2023-03-01 843 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#--- Calculate the monthly \"stock\" return and count the required stocks ---#\n", "\n", "data[\n", " # Step 0: sort by permno and date\n", " order(permno, date), .(permno, date, retx, cap)\n", " ][,\n", " # Step 1: create a month indicator\n", " ':='(yearmon=floor_date(date, 'month'))\n", " ][,\n", " # Step 2: calculate the monthly return for stocks\n", " .(mon_ret_stk=prod(1+retx)-1),\n", " keyby=.(yearmon, permno)\n", " ][\n", " # Step 3: merge the market return to the stock return\n", " mon_ret_mkt, on=.(yearmon), nomatch=NULL\n", " ][,\n", " # Step 4: count the stocks that beat the market\n", " .(num_stk=sum(mon_ret_stk>mon_ret_mkt, na.rm=T)),\n", " keyby=.(yearmon)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1 to 2 calculate the monthly stock return. It's very similar to how we calculate the monthly market return previously.\n", "\n", "- Step 3: We merge the market return (which we just computed) to the stock return. The key used to join is `yearmon`.\n", "\n", "- Step 4: We count the stocks that beat the market." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{tip}\n", "There're other methods to create a month indicator. For example, we can create a string by extracting the year and month part of a date, something like\n", "\n", "```r\n", "x = as.Date('2023-03-12')\n", "\n", "strftime(x, '%Y-%m')\n", "#> [1] \"2023-03\"\n", "```\n", "\n", "However, I personally didn't recommend this method for the following reason:\n", "\n", "- Representing a date using a string is slow, because CPU is much faster processing numbers (the `Date` object is represented as a number in R) than strings.\n", "\n", "- It creates many troubles when you need to visualize your data (i.e., plotting). Visualization packages treat strings as discrete, unrelated observations, so \"2023-01-01,\" \"2023-02-01,\" \"2023-06-01\" will all be *evenly spaced* on the axis, while actually their gaps are one month and four month, respectively. \n", "\n", "````" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Find out the three stocks that're mostly correlated with the market" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first compute, for each stock, the correlation coefficient between the stock return and the market return. Then we select the top 10." ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>cor_coef</th><th scope=col>ticker</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><chr></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>15407</td><td>1</td><td>SPNE</td></tr>\n", "\t<tr><td>15542</td><td>1</td><td>VTVT</td></tr>\n", "\t<tr><td>24009</td><td>1</td><td>GENK</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " permno & cor\\_coef & ticker\\\\\n", " <dbl> & <dbl> & <chr>\\\\\n", "\\hline\n", "\t 15407 & 1 & SPNE\\\\\n", "\t 15542 & 1 & VTVT\\\\\n", "\t 24009 & 1 & GENK\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| permno <dbl> | cor_coef <dbl> | ticker <chr> |\n", "|---|---|---|\n", "| 15407 | 1 | SPNE |\n", "| 15542 | 1 | VTVT |\n", "| 24009 | 1 | GENK |\n", "\n" ], "text/plain": [ " permno cor_coef ticker\n", "1 15407 1 SPNE \n", "2 15542 1 VTVT \n", "3 24009 1 GENK " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: compute the market return\n", " ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][,\n", " # Step 2: calculate the correlation coefficient\n", " .(cor_coef=cor(retx, ret_mkt), ticker=ticker[1]), keyby=.(permno)\n", " ][\n", " # Step 3: sort and select the top 10\n", " order(-cor_coef)][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "No new techniques are introduced in this problem. The only thing you need to pay attention is `ticker=ticker[1]` in Step 1. We only keep the first element of `ticker` to avoid outputting duplicated rows. Refer to [this problem](avoid-dup-rows) for details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(create-new-column-in-by)=\n", "## Find out the stock that has the largest monthly decrease in trading volume" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "My understanding of the problem: at the end of every month, we compute the percentage change in monthly trading volume for each stock, than we select the one with the largest decrease." ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>yearmon</th><th scope=col>industry</th><th scope=col>mon_prcvol_change</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-02-01</td><td>Agriculture, Forestry, Fishing and Hunting</td><td> 0.3305681</td></tr>\n", "\t<tr><td>2023-03-01</td><td>Finance and Insurance </td><td> 0.7379635</td></tr>\n", "\t<tr><td>2023-04-01</td><td>Arts, Entertainment, and Recreation </td><td>-0.1556650</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " yearmon & industry & mon\\_prcvol\\_change\\\\\n", " <date> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 2023-02-01 & Agriculture, Forestry, Fishing and Hunting & 0.3305681\\\\\n", "\t 2023-03-01 & Finance and Insurance & 0.7379635\\\\\n", "\t 2023-04-01 & Arts, Entertainment, and Recreation & -0.1556650\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| yearmon <date> | industry <chr> | mon_prcvol_change <dbl> |\n", "|---|---|---|\n", "| 2023-02-01 | Agriculture, Forestry, Fishing and Hunting | 0.3305681 |\n", "| 2023-03-01 | Finance and Insurance | 0.7379635 |\n", "| 2023-04-01 | Arts, Entertainment, and Recreation | -0.1556650 |\n", "\n" ], "text/plain": [ " yearmon industry mon_prcvol_change\n", "1 2023-02-01 Agriculture, Forestry, Fishing and Hunting 0.3305681 \n", "2 2023-03-01 Finance and Insurance 0.7379635 \n", "3 2023-04-01 Arts, Entertainment, and Recreation -0.1556650 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: calculate the montly trading volume\n", " # (also create the month indicator)\n", " .(mon_prcvol=sum(prcvol)),\n", " keyby=.(industry, yearmon=floor_date(date, 'month'))\n", " ][,\n", " # Step 2: calculate the percentage change in trading volume\n", " .(yearmon, mon_prcvol_change=mon_prcvol/shift(mon_prcvol)-1),\n", " keyby=.(industry), \n", " ][, \n", " # Step 3: remove NA\n", " na.omit(.SD)\n", " ][\n", " # Step 3: pick the largest decrease\n", " order(yearmon, -mon_prcvol_change),\n", " .SD[1],\n", " keyby=.(yearmon)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: Calculate monthly trading volume by aggregating the daily trading volume in the month. Note how we create the monthly indicator `yearmon` in the `by` part! (Yes, you can create new columns in the `by` part on the fly!) Of course, you can first create `yearmon` and then group by it, but isn't it cool that `data.table` can do two things in one shot?\n", "\n", "- Step 2: Calculate monthly change. Note how we use `data.table::shift` to create a lag term of `mon_prcvol`.\n", "\n", "- Step 3: Pick the largest decrease. Step 3 is executed in the following order:\n", "\n", " - `order(yearmon, -mon_prcvol)`: Sort by `yearmon` (ascendingly) and `mon_prcvol` (descendingly).\n", "\n", " - `keyby=.(yearmon)`: Then group by `yearmon`. Because we've already sorted the data, now the first row in each group has the largest decrease. Similar technique is also used in [this problem](.SD[1]).\n", "\n", " - `.SD[1]`: Select the first row in each group." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ What's the maximum drawdown of each stock?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> \"A drawdown is a peak-to-trough decline during a specific period for an investment, trading account, or fund. A drawdown measures the historical risk of different investments, compares fund performance, or monitors personal trading performance.\"\n", ">\n", "> -- [Invespedia](https://www.investopedia.com/terms/d/drawdown.asp#:~:text=Key%20Takeaways%201%20A%20drawdown%20refers%20to%20how,also%20be%20considered%20when%20assessing%20drawdowns.%20More%20items)\n", "\n", "The maxinum drawdown of a stock is the largest decline of all the drawdowns in during the period. We'll use the [`table.Drawdowns`](https://rdocumentation.org/packages/PerformanceAnalytics/versions/2.0.4/topics/table.Drawdowns) function from the `PerformanceAnalytics` package to compute the maximum drawdowns. " ] }, { "cell_type": "code", "execution_count": 233, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 5</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>start</th><th scope=col>trough</th><th scope=col>end</th><th scope=col>depth</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><date></th><th scope=col><date></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>2023-01-17</td><td>2023-02-01</td><td>2023-04-28</td><td>-0.1009774</td></tr>\n", "\t<tr><td>10028</td><td>2023-03-13</td><td>2023-05-01</td><td>2023-06-30</td><td>-0.2337500</td></tr>\n", "\t<tr><td>10032</td><td>2023-01-25</td><td>2023-05-04</td><td>2023-06-30</td><td>-0.2609265</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 5\n", "\\begin{tabular}{lllll}\n", " permno & start & trough & end & depth\\\\\n", " <dbl> & <date> & <date> & <date> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 2023-01-17 & 2023-02-01 & 2023-04-28 & -0.1009774\\\\\n", "\t 10028 & 2023-03-13 & 2023-05-01 & 2023-06-30 & -0.2337500\\\\\n", "\t 10032 & 2023-01-25 & 2023-05-04 & 2023-06-30 & -0.2609265\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 5\n", "\n", "| permno <dbl> | start <date> | trough <date> | end <date> | depth <dbl> |\n", "|---|---|---|---|---|\n", "| 10026 | 2023-01-17 | 2023-02-01 | 2023-04-28 | -0.1009774 |\n", "| 10028 | 2023-03-13 | 2023-05-01 | 2023-06-30 | -0.2337500 |\n", "| 10032 | 2023-01-25 | 2023-05-04 | 2023-06-30 | -0.2609265 |\n", "\n" ], "text/plain": [ " permno start trough end depth \n", "1 10026 2023-01-17 2023-02-01 2023-04-28 -0.1009774\n", "2 10028 2023-03-13 2023-05-01 2023-06-30 -0.2337500\n", "3 10032 2023-01-25 2023-05-04 2023-06-30 -0.2609265" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "drawdowns <- function(ret, date, top=5) {\n", " # This function calculates the drawdowns for a given return series\n", " # Input:\n", " # ret: a vector of returns\n", " # date: a vector of dates\n", " # top: the number of largest drawdowns to be returned\n", "\n", " input = data.table(date, ret)[order(date)]\n", "\n", " # a drawdown is defined whenever the cumulative return is below the previous peak\n", " drawdowns_raw = input[, {\n", " ret_cum=cumprod(1 + ret)\n", " ret_cummax=cummax(c(1, ret_cum))[-1]\n", " drawdowns=ret_cum/ret_cummax - 1\n", " list(date, ret, ret_cum, ret_cummax, drawdowns)\n", " }]\n", "\n", " # find the largest drawdowns\n", " drawdowns = drawdowns_raw[, ':='(grp = rleid(drawdowns<0))\n", " ][drawdowns<0, \n", " .(start=date[1], trough=date[which.min(drawdowns)],\n", " end=date[.N], depth=drawdowns[which.min(drawdowns)]),\n", " keyby=.(grp)\n", " ][order(depth)\n", " ][1:top\n", " ][, ':='(grp=NULL)]\n", "}\n", "\n", "data[\n", " # Step 1: sort by permno, date\n", " order(permno, date)\n", " ][, \n", " # Step 2: calculate drawdowns for each stock\n", " {drawdowns(retx, date, top=1)},\n", " keyby=.(permno)\n", " ][1:3]" ] }, { "cell_type": "code", "execution_count": 235, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>V1</th><th scope=col>V2</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>0.001109345</td><td>0.05221605</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 2\n", "\\begin{tabular}{ll}\n", " V1 & V2\\\\\n", " <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 0.001109345 & 0.05221605\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 2\n", "\n", "| V1 <dbl> | V2 <dbl> |\n", "|---|---|\n", "| 0.001109345 | 0.05221605 |\n", "\n" ], "text/plain": [ " V1 V2 \n", "1 0.001109345 0.05221605" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, .(mean(retx), sd(retx))]" ] }, { "cell_type": "code", "execution_count": 234, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "0.8875" ], "text/latex": [ "0.8875" ], "text/markdown": [ "0.8875" ], "text/plain": [ "[1] 0.8875" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(1/8)/(1/15.1)-1" ] }, { "cell_type": "code", "execution_count": 223, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>From</th><th scope=col>Trough</th><th scope=col>Depth</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><date></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>2023-01-17</td><td>2023-02-01</td><td>-0.1010</td></tr>\n", "\t<tr><td>10028</td><td>2023-03-13</td><td>2023-05-01</td><td>-0.2337</td></tr>\n", "\t<tr><td>10032</td><td>2023-01-25</td><td>2023-05-04</td><td>-0.2609</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 4\n", "\\begin{tabular}{llll}\n", " permno & From & Trough & Depth\\\\\n", " <dbl> & <date> & <date> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 2023-01-17 & 2023-02-01 & -0.1010\\\\\n", "\t 10028 & 2023-03-13 & 2023-05-01 & -0.2337\\\\\n", "\t 10032 & 2023-01-25 & 2023-05-04 & -0.2609\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 4\n", "\n", "| permno <dbl> | From <date> | Trough <date> | Depth <dbl> |\n", "|---|---|---|---|\n", "| 10026 | 2023-01-17 | 2023-02-01 | -0.1010 |\n", "| 10028 | 2023-03-13 | 2023-05-01 | -0.2337 |\n", "| 10032 | 2023-01-25 | 2023-05-04 | -0.2609 |\n", "\n" ], "text/plain": [ " permno From Trough Depth \n", "1 10026 2023-01-17 2023-02-01 -0.1010\n", "2 10028 2023-03-13 2023-05-01 -0.2337\n", "3 10032 2023-01-25 2023-05-04 -0.2609" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "suppressWarnings({\n", " data[\n", " # Step 1: sort by permno, date\n", " order(permno, date)\n", " ][, \n", " # Step 2: calculate drawdowns for each stock\n", " {\n", " ret_zoo = zoo(retx, order.by=date)\n", " PerformanceAnalytics::table.Drawdowns(ret_zoo, top=1)[, c(\"From\", \"Trough\", \"Depth\")]\n", " },\n", " keyby=.(permno)\n", " ][1:3]\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "- `From`: the start date of the maximum drawdown.\n", "- `Trough`: the date of the trough of the maximum drawdown.\n", "- `Depth`: the percentage decline, i.e., the magnitude of drawdown.\n", "\n", "For more details about the `From`, `Trough`, `Depth` and perhaps other possible outputs, please refer to the [documentation](https://rdocumentation.org/packages/PerformanceAnalytics/versions/2.0.4/topics/table.Drawdowns) of the `table.Drawdowns`.\n", "\n", "\n", "**Execution Explained:**\n", "\n", "- Step 1: Sort by `permno` and `date`.\n", "\n", "- Step 2: Calculate drawdowns for each stock.\n", "\n", " - We group by `permno`, because the drawdown is calculated for each stock.\n", "\n", " - `table.Drawdowns` requires the input to be a `zoo` or `xts` object, so we create a `zoo` object with `ret_zoo = zoo(retx, order.by=date)`. The resulting `ret_zoo` object contains the daily stock return `retx`, and its row index represents the date `date`.\n", "\n", " - `PerformanceAnalytics::table.Drawdowns()`: We call the `table.Drawdowns()` function. Note that we explicitly tell R that the functdion is from the `PerformanceAnalytics` package by adding the `PerformanceAnalytics::` prefix. This is sometimes convenient especially when we only want to use one function without `library`ing the whole package.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the win (success) ratio of each stock?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The win/loss, or success ratio, is a trader's number of winning trades divided by the number of losing trades. When it comes to the stock like in this problem, it's the ratio of positive-return days during the period." ] }, { "cell_type": "code", "execution_count": 178, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>win_ratio</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>0.5322581</td></tr>\n", "\t<tr><td>10028</td><td>0.5483871</td></tr>\n", "\t<tr><td>10032</td><td>0.5080645</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " permno & win\\_ratio\\\\\n", " <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 0.5322581\\\\\n", "\t 10028 & 0.5483871\\\\\n", "\t 10032 & 0.5080645\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| permno <dbl> | win_ratio <dbl> |\n", "|---|---|\n", "| 10026 | 0.5322581 |\n", "| 10028 | 0.5483871 |\n", "| 10032 | 0.5080645 |\n", "\n" ], "text/plain": [ " permno win_ratio\n", "1 10026 0.5322581\n", "2 10028 0.5483871\n", "3 10032 0.5080645" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[\n", " # Step 0: sort by permno, date\n", " order(permno, date),\n", " # Step 1: calculate the win ratio\n", " .(win_ratio=sum(retx>0)/.N),\n", " keyby=.(permno)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: Calculate the win ratio. `sum(rex>0)` gives us the number of days with positive returns. We then divide it by the total number of days (`.N`), which gives us the win ratio." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the win ratio of the market?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem is very similar to the previous one." ] }, { "cell_type": "code", "execution_count": 179, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 1</caption>\n", "<thead>\n", "\t<tr><th scope=col>win_ratio</th></tr>\n", "\t<tr><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>0.5806452</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 1\n", "\\begin{tabular}{l}\n", " win\\_ratio\\\\\n", " <dbl>\\\\\n", "\\hline\n", "\t 0.5806452\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 1\n", "\n", "| win_ratio <dbl> |\n", "|---|\n", "| 0.5806452 |\n", "\n" ], "text/plain": [ " win_ratio\n", "1 0.5806452" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: Calculate the market daily return\n", " .(ret_mkt=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date)\n", " ][,\n", " # Step 2: Calculat the win ratio\n", " .(win_ratio=sum(ret_mkt>0)/.N)\n", " ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "Hmm... It seems there're more gaining days than losing days." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the win ratio of each industry?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try it yourself before continuing to read!" ] }, { "cell_type": "code", "execution_count": 180, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>industry</th><th scope=col>win_ratio</th></tr>\n", "\t<tr><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>Accommodation and Food Services </td><td>0.5806452</td></tr>\n", "\t<tr><td>Administrative and Support and Waste Management and Remediation Services</td><td>0.5161290</td></tr>\n", "\t<tr><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.5403226</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " industry & win\\_ratio\\\\\n", " <chr> & <dbl>\\\\\n", "\\hline\n", "\t Accommodation and Food Services & 0.5806452\\\\\n", "\t Administrative and Support and Waste Management and Remediation Services & 0.5161290\\\\\n", "\t Agriculture, Forestry, Fishing and Hunting & 0.5403226\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| industry <chr> | win_ratio <dbl> |\n", "|---|---|\n", "| Accommodation and Food Services | 0.5806452 |\n", "| Administrative and Support and Waste Management and Remediation Services | 0.5161290 |\n", "| Agriculture, Forestry, Fishing and Hunting | 0.5403226 |\n", "\n" ], "text/plain": [ " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " win_ratio\n", "1 0.5806452\n", "2 0.5161290\n", "3 0.5403226" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: calculate the industry return\n", " .(ind_ret=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date, industry)\n", " ][,\n", " # Step 2: calculate the win ratio\n", " .(win_ratio=sum(ind_ret>0)/.N),\n", " keyby=.(industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the profit/loss ratio of each stock?\n", "\n", "In this problem, the profit/loss ratio is defined as the average return on winning days (i.e., days with positive returns) divided by the average loss on losing days (i.e., days with negative returns):\n", "\n", "$$\n", "ratio = \\Bigl(\\frac{1}{\\#\\ winning\\ days} \\sum_{t \\in winning\\ days} ret_t \\Bigl) \\Big/ \\Bigl( \\frac{1}{\\#\\ losing\\ days} \\sum_{t \\in losing\\ days} ret_t \\Bigl)\n", "$$\n", "\n", "Profit/loss ratio can also be used to evaluate the performance of a trader, see [details here](https://www.investopedia.com/terms/p/profit_loss_ratio.asp)." ] }, { "cell_type": "code", "execution_count": 181, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>avg_win</th><th scope=col>avg_loss</th><th scope=col>ratio</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>0.009876289</td><td>-0.01007681</td><td>0.9801012</td></tr>\n", "\t<tr><td>10028</td><td>0.020682302</td><td>-0.02011190</td><td>1.0283615</td></tr>\n", "\t<tr><td>10032</td><td>0.012812238</td><td>-0.01370729</td><td>0.9347023</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 4\n", "\\begin{tabular}{llll}\n", " permno & avg\\_win & avg\\_loss & ratio\\\\\n", " <dbl> & <dbl> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 0.009876289 & -0.01007681 & 0.9801012\\\\\n", "\t 10028 & 0.020682302 & -0.02011190 & 1.0283615\\\\\n", "\t 10032 & 0.012812238 & -0.01370729 & 0.9347023\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 4\n", "\n", "| permno <dbl> | avg_win <dbl> | avg_loss <dbl> | ratio <dbl> |\n", "|---|---|---|---|\n", "| 10026 | 0.009876289 | -0.01007681 | 0.9801012 |\n", "| 10028 | 0.020682302 | -0.02011190 | 1.0283615 |\n", "| 10032 | 0.012812238 | -0.01370729 | 0.9347023 |\n", "\n" ], "text/plain": [ " permno avg_win avg_loss ratio \n", "1 10026 0.009876289 -0.01007681 0.9801012\n", "2 10028 0.020682302 -0.02011190 1.0283615\n", "3 10032 0.012812238 -0.01370729 0.9347023" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " {\n", " # Step 1: compute the variables\n", " avg_win=mean(retx[retx>0])\n", " avg_loss=mean(retx[retx<0])\n", " ratio=abs(avg_win/avg_loss)\n", "\n", " # Step 2: collect the outputs\n", " list(avg_win=avg_win, avg_loss=avg_loss, ratio=ratio)\n", " },\n", " keyby=.(permno)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: Calculate the average win/loss and the profit/loss ratio. Note that we use `retx[retx>0]` to extract the positive elements in `retx`.\n", "\n", "- Step 2: We collect the outputs into a list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the profit/loss ratio of the market?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Very similar to the previous one." ] }, { "cell_type": "code", "execution_count": 182, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>avg_win</th><th scope=col>avg_loss</th><th scope=col>ratio</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>0.007913403</td><td>-0.007194593</td><td>1.09991</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 3\n", "\\begin{tabular}{lll}\n", " avg\\_win & avg\\_loss & ratio\\\\\n", " <dbl> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 0.007913403 & -0.007194593 & 1.09991\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 3\n", "\n", "| avg_win <dbl> | avg_loss <dbl> | ratio <dbl> |\n", "|---|---|---|\n", "| 0.007913403 | -0.007194593 | 1.09991 |\n", "\n" ], "text/plain": [ " avg_win avg_loss ratio \n", "1 0.007913403 -0.007194593 1.09991" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[, \n", " # Step 1: calculate the market return\n", " .(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][,\n", " # Step 2: calculate the win ratio\n", " {\n", " avg_win=mean(ret_mkt[ret_mkt>0])\n", " avg_loss=mean(ret_mkt[ret_mkt<0])\n", " ratio=abs(avg_win/avg_loss)\n", " list(avg_win, avg_loss, ratio)\n", " }\n", " ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Output Explained:**\n", "\n", "Hmm... At least in the sample period, the market is going up, as shown by the `ratio` being larger than one." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Is there a situation where the monthly trading volume of a stock exceeds the total daily trading volume of its industry in the same month?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While in real world it's rare that we're asked to answer such a question, but this problem is a good test of your data wrangling skills anyway.\n", "\n", "This problem is a mouthful, let's make sure we're on the same page: To solve this problem, we first compute the monthly trading volume of each stock. We also calculate the minimum daily trading volume of the industry that this stock belongs to in the same month. At last, we compare the stock's monthly trading volume with the industry's minimum daily trading volume." ] }, { "cell_type": "code", "execution_count": 183, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 5</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>yearmon</th><th scope=col>stk_mon_prcvol</th><th scope=col>industry</th><th scope=col>ind_min_daily_prcvol</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><dbl></th><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>14338</td><td>2023-01-01</td><td> 4220384337</td><td>Accommodation and Food Services</td><td>4181358836</td></tr>\n", "\t<tr><td>43449</td><td>2023-01-01</td><td>14546642335</td><td>Accommodation and Food Services</td><td>4181358836</td></tr>\n", "\t<tr><td>77702</td><td>2023-01-01</td><td>13580162865</td><td>Accommodation and Food Services</td><td>4181358836</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 5\n", "\\begin{tabular}{lllll}\n", " permno & yearmon & stk\\_mon\\_prcvol & industry & ind\\_min\\_daily\\_prcvol\\\\\n", " <dbl> & <date> & <dbl> & <chr> & <dbl>\\\\\n", "\\hline\n", "\t 14338 & 2023-01-01 & 4220384337 & Accommodation and Food Services & 4181358836\\\\\n", "\t 43449 & 2023-01-01 & 14546642335 & Accommodation and Food Services & 4181358836\\\\\n", "\t 77702 & 2023-01-01 & 13580162865 & Accommodation and Food Services & 4181358836\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 5\n", "\n", "| permno <dbl> | yearmon <date> | stk_mon_prcvol <dbl> | industry <chr> | ind_min_daily_prcvol <dbl> |\n", "|---|---|---|---|---|\n", "| 14338 | 2023-01-01 | 4220384337 | Accommodation and Food Services | 4181358836 |\n", "| 43449 | 2023-01-01 | 14546642335 | Accommodation and Food Services | 4181358836 |\n", "| 77702 | 2023-01-01 | 13580162865 | Accommodation and Food Services | 4181358836 |\n", "\n" ], "text/plain": [ " permno yearmon stk_mon_prcvol industry \n", "1 14338 2023-01-01 4220384337 Accommodation and Food Services\n", "2 43449 2023-01-01 14546642335 Accommodation and Food Services\n", "3 77702 2023-01-01 13580162865 Accommodation and Food Services\n", " ind_min_daily_prcvol\n", "1 4181358836 \n", "2 4181358836 \n", "3 4181358836 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "stk_mon_prcvol = data[,\n", " # Step 1: calculate the stocks' monthly trading volume\n", " .(stk_mon_prcvol=sum(prcvol), industry=industry[1]),\n", " keyby=.(permno, yearmon=floor_date(date, 'month'))]\n", "\n", "\n", "ind_mon_prcvol = data[,\n", " # Step 2: calculate the industry's daily trading volume\n", " .(ind_daily_prcvol=sum(prcvol)),\n", " keyby=.(industry, date)\n", " ][,\n", " # Step 3: find the mininum daily trading volume for each month\n", " .(ind_min_daily_prcvol=min(ind_daily_prcvol)),\n", " keyby=.(industry, yearmon=floor_date(date, 'month'))\n", " ]\n", "\n", "stk_mon_prcvol[\n", " # Step 4: join the two tables\n", " ind_mon_prcvol, on=.(industry, yearmon), nomatch=NULL\n", " ][\n", " # Step 4: find out the stocks meeting our criteria\n", " stk_mon_prcvol>ind_min_daily_prcvol\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: calculate the stocks' monthly trading volume. Note how we create a year-month indicator (`yearmon`) in the `by` part. The same technique is also used in, for example, [this problem](create-new-column-in-by).\n", "\n", "- Step 2: calculate the industry's daily trading volume.\n", "\n", "- Step 3: find the minimum daily trading volume of each industry in a month.\n", "\n", "- Step 4: join the two tables: \"monthly stock trading volume\" and \"minimum industry daily trading volume in each month\".\n", "\n", "- Step 5: select the stocks that meet our criteria." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's the standard deviation of returns for stocks in each industry?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem Explained:**\n", "\n", "This problems requires us to:\n", "\n", "- First, calculate the standard deviation of returns for each stocks.\n", "\n", "- Second, calculate the industry-level standard deviation, which is defined as the value-weighed average of the standard deviations of all its component stocks." ] }, { "cell_type": "code", "execution_count": 184, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 2</caption>\n", "<thead>\n", "\t<tr><th scope=col>industry</th><th scope=col>ret_sd</th></tr>\n", "\t<tr><th scope=col><chr></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>Accommodation and Food Services </td><td>0.01466278</td></tr>\n", "\t<tr><td>Administrative and Support and Waste Management and Remediation Services</td><td>0.01664406</td></tr>\n", "\t<tr><td>Agriculture, Forestry, Fishing and Hunting </td><td>0.01639743</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 2\n", "\\begin{tabular}{ll}\n", " industry & ret\\_sd\\\\\n", " <chr> & <dbl>\\\\\n", "\\hline\n", "\t Accommodation and Food Services & 0.01466278\\\\\n", "\t Administrative and Support and Waste Management and Remediation Services & 0.01664406\\\\\n", "\t Agriculture, Forestry, Fishing and Hunting & 0.01639743\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 2\n", "\n", "| industry <chr> | ret_sd <dbl> |\n", "|---|---|\n", "| Accommodation and Food Services | 0.01466278 |\n", "| Administrative and Support and Waste Management and Remediation Services | 0.01664406 |\n", "| Agriculture, Forestry, Fishing and Hunting | 0.01639743 |\n", "\n" ], "text/plain": [ " industry \n", "1 Accommodation and Food Services \n", "2 Administrative and Support and Waste Management and Remediation Services\n", "3 Agriculture, Forestry, Fishing and Hunting \n", " ret_sd \n", "1 0.01466278\n", "2 0.01664406\n", "3 0.01639743" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data[,\n", " # Step 1: calculate the return sd for each stock\n", " .(ret_sd=sd(retx), industry=industry[1], cap=mean(cap)),\n", " keyby=.(permno)\n", " ][,\n", " # Step 2: calculate the industry-level average sd.\n", " .(ret_sd=weighted.mean(ret_sd, cap, na.rm=T)),\n", " keyby=.(industry)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: calculate the return sd for each stock. We keep `industry` because we'll group by it in Step 2. We also calculate the average daily market capital `cap`, which will be used as the weight in Step 2.\n", "\n", "- Step 2: calculate the industry-level average sd." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ For each stock, we can regress its daily returns on the market returns, which gives us one intercept term and one beta. What's the distribution of the intercept term and the beta?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem Explained:**\n", "\n", "We'll use many techniques introduced previously to solve this problem.\n", "\n", "Let $stk\\_ret_it$ be the daily return for stock $i$ on day $t$ and $mkt\\_ret_{it}$ be the market daily return. This problem first requires us to run the following regression:\n", "\n", "$$\n", "stk\\_ret_{it} = \\alpha_i + \\beta_{1i} \\cdot mkt\\_ret_{it} + \\varepsilon_i\n", "$$\n", "\n", "Then, the problems asks about the distribution of $\\alpha_i$ and $\\beta_{1i}$. There're many ways to describe the distribution of a variable. In this problem, we focus on the following three things:\n", "\n", "- What's the mean and variance of $\\alpha_i$ and $\\beta_{1i}$?\n", "\n", "- Plot the density of $\\alpha_i$ and $\\beta_{1i}$." ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "tags": [ "hide-output" ], "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>alpha</th><th scope=col>beta</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td> 7.201256e-05</td><td>0.2987578</td></tr>\n", "\t<tr><td>10028</td><td> 2.093678e-03</td><td>0.6188396</td></tr>\n", "\t<tr><td>10032</td><td>-1.450841e-03</td><td>0.8415072</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " permno & alpha & beta\\\\\n", " <dbl> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 7.201256e-05 & 0.2987578\\\\\n", "\t 10028 & 2.093678e-03 & 0.6188396\\\\\n", "\t 10032 & -1.450841e-03 & 0.8415072\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| permno <dbl> | alpha <dbl> | beta <dbl> |\n", "|---|---|---|\n", "| 10026 | 7.201256e-05 | 0.2987578 |\n", "| 10028 | 2.093678e-03 | 0.6188396 |\n", "| 10032 | -1.450841e-03 | 0.8415072 |\n", "\n" ], "text/plain": [ " permno alpha beta \n", "1 10026 7.201256e-05 0.2987578\n", "2 10028 2.093678e-03 0.6188396\n", "3 10032 -1.450841e-03 0.8415072" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 1 × 4</caption>\n", "<thead>\n", "\t<tr><th scope=col>alpha</th><th scope=col>beta</th><th scope=col>sd_alpha</th><th scope=col>sd_beta</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>-0.0005043781</td><td>1.056732</td><td>0.003975075</td><td>0.857834</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 1 × 4\n", "\\begin{tabular}{llll}\n", " alpha & beta & sd\\_alpha & sd\\_beta\\\\\n", " <dbl> & <dbl> & <dbl> & <dbl>\\\\\n", "\\hline\n", "\t -0.0005043781 & 1.056732 & 0.003975075 & 0.857834\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 1 × 4\n", "\n", "| alpha <dbl> | beta <dbl> | sd_alpha <dbl> | sd_beta <dbl> |\n", "|---|---|---|---|\n", "| -0.0005043781 | 1.056732 | 0.003975075 | 0.857834 |\n", "\n" ], "text/plain": [ " alpha beta sd_alpha sd_beta \n", "1 -0.0005043781 1.056732 0.003975075 0.857834" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAABLAAAAMgCAMAAAAEPmswAAADAFBMVEUAAAABAQECAgIDAwMEBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDA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xShPBYoxVmggWY6zSRLAYY5UmgsUYqzQRLMZYpYlgMcYqTQSLMVZpIliMsUoTwWKMVZoIFmOs0kSwGGOVJoLFGKs0/T/+XlS3lpoTcAAAAABJRU5ErkJggg==", "text/plain": [ "plot without title" ] }, "metadata": { "image/png": { "height": 400, "width": 600 } }, "output_type": "display_data" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAABLAAAAMgCAMAAAAEPmswAAADAFBMVEUAAAABAQECAgIDAwMEBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJycoKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6Ojo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tMTExNTU1OTk5PT09QUFBRUVFSUlJTU1NUVFRVVVVWVlZXV1dYWFhZWVlaWlpbW1tcXFxdXV1eXl5fX19gYGBhYWFiYmJjY2NkZGRlZWVmZmZnZ2doaGhpaWlqampra2tsbGxtbW1ubm5vb29wcHBxcXFycnJzc3N0dHR1dXV2dnZ3d3d4eHh5eXl6enp7e3t8fHx9fX1+fn5/f3+AgICBgYGCgoKDg4OEhISFhYWGhoaHh4eIiIiJiYmKioqLi4uMjIyNjY2Ojo6Pj4+QkJCRkZGSkpKTk5OUlJSVlZWWlpaXl5eYmJiZmZmampqbm5ucnJydnZ2enp6fn5+goKChoaGioqKjo6OkpKSlpaWmpqanp6eoqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLS1tbW2tra3t7e4uLi5ubm6urq7u7u8vLy9vb2+vr6/v7/AwMDBwcHCwsLDw8PExMTFxcXGxsbHx8fIyMjJycnKysrLy8vMzMzNzc3Ozs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gGN/i/suZVlCSwVRHGwcMjP8YX1VwO7r40vDAMGrYnfrr0U6JzYpOR7WEeiwZzkPtcAU8OaV1mawFJBFAer7k+pxdOBB2MxdwscvzF5f+PRwKJYGVg7b3lmasN5wP0hjKpsTmCpIIqD1bd48RvgrFjsIeDN4hXPA0/E0vyU8ANgZJAzqggksFQQjUuhlKwx/hGL9UStRf9J9SFwSawSWCu/ePGGHQWWqpzAUkEUB2tGyfKR2DoWOxHli3/JVQbW0huO3a54vcBSFRNYKojiYL1XstwKO8dirSuAlfiuewlYjzcACvZpN3C0XhKqKgksFURxsMaULG+PNrHYudiy0ibFYM0uwI6PrUys+FFgqcoJLBVEcbD6FS8uBC6Mxe4BVhSvWPfbb4nzHIrB6g/MSq3/XmCpygksFURxsOr9klrsAkyJxT4Eri5+bBBqJ87QKgarAzbZkFr/mMBSlRNYKogSJ44etiS+sOHGArROrOmAWrcmT8R6tg6cxG0crAWx5Omiqd/Imbg5cE9I4ypbE1gqiBJgYbtz7h10MFBndmLNj5sBhwx89I5/Ak0WJlbEv6AaOOOD2Mza2PKm16bcfSy2KcChH+mXc1T5BJYKonFo9ORWqZ8INno1terrQ4t/RLjnZ8n7X9RK/fLz0ILU6sN+PCj+/8+GOLSyL4GlgmgctowtvXS/zTff/+qS77XH1t177Hb19jz1sfXF98fuW3/bs+O3H3fcu17jU57ZGPv4oLp/028TqvIJLKVUZBJYSqnIJLCUUpFJYCmlIpPAUkpFJoGllIpMAkspFZkEllIqMgkspVRkElhKqcgksJRSkUlgKaUik8BSSkUmgaWUikwCSykVmQSWUioyCSylVGQSWEqpyCSwlFKRSWAppSKTwFJKRSaBpZSKTAJLKRWZBJZSKjL9P0ke4S7e5nCQAAAAAElFTkSuQmCC", "text/plain": [ "plot without title" ] }, "metadata": { "image/png": { "height": 400, "width": 600 } }, "output_type": "display_data" } ], "source": [ "alpha_beta = data[,\n", " # Step 1: calculate the daily market return\n", " ':='(mkt_ret=weighted.mean(retx, cap, na.rm=T)),\n", " keyby=.(date)\n", " ][,\n", " # Step 2: get the \\alpha and \\beta\n", " {\n", " lm_out = lm(retx ~ mkt_ret)\n", " alpha = coef(lm_out)[1]\n", " beta = coef(lm_out)[2]\n", " list(alpha=alpha, beta=beta)\n", " },\n", " keyby=.(permno)\n", " ][\n", " # step 3: remove extreme values\n", " alpha %between% c(quantile(alpha, 0.02), quantile(alpha, 0.98))]\n", "\n", "alpha_beta[1:3]\n", "\n", "alpha_beta[, \n", " # Step 4: calculate the statistics of alpha and beta\n", " .(alpha=mean(alpha, na.rm=T), beta=mean(beta, na.rm=T),\n", " sd_alpha=sd(alpha, na.rm=T), sd_beta=sd(beta, na.rm=T))\n", " ]\n", "\n", "library(ggplot2)\n", "\n", "# Step 5: plot the distribution of alpha and beta\n", "alpha_beta %>% \n", " ggplot(aes(x=alpha)) +\n", " geom_density()\n", "\n", "alpha_beta %>% \n", " ggplot(aes(x=beta)) +\n", " geom_density()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "- Step 1: calculate the daily market return.\n", "\n", "- Step 2: calculate the alphas and betas. The technique is introduced in [this problem](lm-in-j).\n", "\n", "- Step 3: we remove extreme values beyond the 2% and 98% percentiles. We do this to make the following visualization more clear. In real world you may not want to this.\n", "\n", "- Step 4: calculate the statistics of alpha and beta.\n", "\n", "- Step 5: plot the density of alphas and betas using `ggplot2`. This is the first time we use visualization in this chapter. A quick start of `ggplot2` can be found [here](http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ Find out the stocks whose abnormal returns rank top 100 for all of the previous three days" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem Explained:**\n", "\n", "This problem is a combinition of [problem \"calculate the daily abnormal return\"](lm-in-j) and [prolbem \"find out stocks that outperform the market in the past three days\"](past-three-days). It requires us to:\n", "\n", "- First, compute the daily abnormal return for each stock.\n", "\n", "- Second, find out stocks whose abnormal return rank top 100 in each of the past three days.\n", "\n", "Since you've already learned all the necessary techniques. Try it before continuing to read!" ] }, { "cell_type": "code", "execution_count": 186, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>permno</th><th scope=col>date</th><th scope=col>ar</th></tr>\n", "\t<tr><th scope=col><dbl></th><th scope=col><date></th><th scope=col><dbl></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>10026</td><td>2023-01-03</td><td> 0.012975042</td></tr>\n", "\t<tr><td>10026</td><td>2023-01-04</td><td>-0.004386641</td></tr>\n", "\t<tr><td>10026</td><td>2023-01-05</td><td>-0.007474872</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " permno & date & ar\\\\\n", " <dbl> & <date> & <dbl>\\\\\n", "\\hline\n", "\t 10026 & 2023-01-03 & 0.012975042\\\\\n", "\t 10026 & 2023-01-04 & -0.004386641\\\\\n", "\t 10026 & 2023-01-05 & -0.007474872\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| permno <dbl> | date <date> | ar <dbl> |\n", "|---|---|---|\n", "| 10026 | 2023-01-03 | 0.012975042 |\n", "| 10026 | 2023-01-04 | -0.004386641 |\n", "| 10026 | 2023-01-05 | -0.007474872 |\n", "\n" ], "text/plain": [ " permno date ar \n", "1 10026 2023-01-03 0.012975042\n", "2 10026 2023-01-04 -0.004386641\n", "3 10026 2023-01-05 -0.007474872" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Section 1: calculate daily abnormal return\n", "\n", "daily_abnormal_return = data[,\n", " ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][, .SD[.N>=50], keyby=.(permno)\n", " ][!is.na(retx)\n", " ][, {\n", " lm_out = lm(retx ~ ret_mkt)\n", " ar = resid(lm_out)\n", " list(date, ar)\n", " },\n", " keyby=.(permno)]\n", "\n", "daily_abnormal_return[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "Calculate the daily abnormal return for each stock. See [this problem](lm-in-j) for details." ] }, { "cell_type": "code", "execution_count": 187, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk_3d</th><th scope=col>stk_list_3d</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th><th scope=col><list></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-06</td><td>6</td><td>19969, 89411, 16178, 16303, 22797, 15059</td></tr>\n", "\t<tr><td>2023-01-09</td><td>2</td><td>88626, 17906</td></tr>\n", "\t<tr><td>2023-01-10</td><td>3</td><td>21089, 15779, 17906</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & num\\_stk\\_3d & stk\\_list\\_3d\\\\\n", " <date> & <int> & <list>\\\\\n", "\\hline\n", "\t 2023-01-06 & 6 & 19969, 89411, 16178, 16303, 22797, 15059\\\\\n", "\t 2023-01-09 & 2 & 88626, 17906\\\\\n", "\t 2023-01-10 & 3 & 21089, 15779, 17906\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | num_stk_3d <int> | stk_list_3d <list> |\n", "|---|---|---|\n", "| 2023-01-06 | 6 | 19969, 89411, 16178, 16303, 22797, 15059 |\n", "| 2023-01-09 | 2 | 88626, 17906 |\n", "| 2023-01-10 | 3 | 21089, 15779, 17906 |\n", "\n" ], "text/plain": [ " date num_stk_3d stk_list_3d \n", "1 2023-01-06 6 19969, 89411, 16178, 16303, 22797, 15059\n", "2 2023-01-09 2 88626, 17906 \n", "3 2023-01-10 3 21089, 15779, 17906 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Section 2: find out stocks whose abnormal return rank top 100 for all of the past three days\n", "\n", "daily_abnormal_return[\n", " # Step 1: find out the stocks whose abnormal return ranks top 100 on each day\n", " order(date, -ar), .(permno_list=list(permno[1:100])), keyby=.(date)\n", " ][, \n", " # Step 2: compute the number of stocks that meet the criteria\n", " {\n", " num_stk_3d = vector() # store the number of stocks on each day\n", " stk_list_3d = list() # store the list of stocks on each day\n", "\n", " for (i in 4:.N) {\n", " # `stks` is the list of stocks that meet the criteria on each day\n", " stks = Reduce(intersect, permno_list[(i-3):(i-1)])\n", "\n", " stk_list_3d[[i]] = stks # don't use \"[i]\", you must use \"[[i]]\"\n", " num_stk_3d[i] = length(stks)\n", " }\n", " list(date, num_stk_3d, stk_list_3d)\n", " }\n", " ][!is.na(num_stk_3d)\n", " ][1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Execution Explained:**\n", "\n", "The main part of the code is very similar to [this problem](past-three-days). I only highlight some differences:\n", "\n", "- Step 1: We first sort by `permno` and `date`, then we group by `permno`. As a result, the first 100 rows in each group will have the largest abnormal return (`list(permno[1:100])`).\n", "\n", "- Step 2: In this problem, I not only want to count the number of stocks that meet our criteria, but also want to return the `permno` of these stocks. To this end, I created two variables, `num_stk_3d` and `stk_list_3d`, to store them separately." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⭐️ Final Exam: Find out the stocks whose abnormal returns rank top 30% in the industry in all of the previous three days" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem Explained:**\n", "\n", "This is the final exam, try to solve it as a test of your learning outcome!\n", "\n", "This problem requires us to:\n", "\n", "- First, calculate the daily abnormal return of each stock.\n", "\n", "- Second, find out the stocks whose abnormal return rank top 30% in its industry in all of the previous three days." ] }, { "cell_type": "code", "execution_count": 188, "metadata": { "tags": [ "hide-output" ], "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "<table class=\"dataframe\">\n", "<caption>A data.table: 3 × 3</caption>\n", "<thead>\n", "\t<tr><th scope=col>date</th><th scope=col>num_stk_3d</th><th scope=col>stk_list_3d</th></tr>\n", "\t<tr><th scope=col><date></th><th scope=col><int></th><th scope=col><list></th></tr>\n", "</thead>\n", "<tbody>\n", "\t<tr><td>2023-01-06</td><td>172</td><td>16981, 85726, 16019, 77902, 23297, 65752, 24459, 93423, 14045, 20848, 77300, 13168, 19813, 89167, 17875, 89002, 93434, 13860, 16802, 14317, 14716, 76279, 79909, 92089, 19920, 90164, 20338, 21286, 12448, 20115, 19859, 21598, 80960, 66157, 15580, 17369, 91413, 16880, 20178, 18086, 64995, 22618, 71563, 21277, 58246, 15064, 23473, 14574, 59408, 79851, 16964, 18043, 79323, 21361, 81593, 18802, 18838, 18783, 88284, 34666, 82281, 88372, 16049, 63773, 14490, 16975, 75320, 22092, 18134, 16697, 26403, 15087, 81696, 78664, 20447, 14889, 19969, 89411, 19559, 16178, 16303, 19621, 22797, 80759, 92594, 18089, 15059, 77202, 14543, 16773, 92486, 91416, 72304, 78908, 77437, 18664, 13410, 19993, 22312, 77496, 91471, 14185, 17906, 88626, 22240, 85072, 17977, 20207, 13328, 83651, 93004, 88332, 14442, 90101, 15177, 19561, 91359, 20057, 22351, 91615, 19176, 84348, 19476, 27909, 22293, 15371, 25419, 14459, 92648, 93345, 90558, 13947, 92570, 43553, 88319, 89952, 82800, 22897, 22899, 81774, 76711, 21866, 18203, 17825, 19994, 21538, 13537, 15642, 22772, 23267, 18677, 89941, 78988, 21120, 91547, 18915, 20637, 20823, 89908, 67467, 12880, 14182, 20682, 18051, 15506, 16008, 19568, 82702, 20578, 31668, 78690, 13577</td></tr>\n", "\t<tr><td>2023-01-09</td><td>111</td><td>16981, 85726, 77902, 20545, 22255, 15273, 18514, 21608, 93434, 13860, 20338, 19002, 20115, 14229, 19920, 76136, 85860, 20464, 23473, 14574, 10933, 34746, 17357, 23477, 82775, 17880, 52936, 58246, 91413, 18043, 91176, 14127, 34666, 18415, 81761, 92528, 88372, 16049, 22092, 16697, 65875, 13757, 14811, 88626, 16924, 14937, 16773, 17906, 23269, 14823, 89411, 19621, 19559, 88332, 11654, 15177, 20288, 20207, 83462, 17358, 21750, 90520, 52396, 78908, 13936, 14536, 19993, 90558, 86778, 66384, 17139, 88597, 19561, 79586, 34817, 21072, 17372, 80759, 11275, 91606, 14543, 17281, 20581, 41188, 22897, 16689, 89952, 81774, 82800, 23273, 13537, 19313, 17885, 20292, 21538, 15057, 23267, 21866, 91547, 19555, 77515, 19494, 67467, 20682, 20823, 16008, 23056, 78690, 17105, 19142, 16084</td></tr>\n", "\t<tr><td>2023-01-10</td><td>100</td><td>85726, 16981, 77902, 20545, 75034, 21608, 19880, 18058, 22255, 15273, 21685, 17361, 91498, 20452, 90454, 14574, 20115, 76136, 69649, 91413, 89443, 16548, 17151, 82486, 19002, 14229, 23477, 20451, 23014, 20464, 15072, 16697, 87356, 16049, 34948, 23505, 22280, 17358, 22244, 21750, 15779, 17906, 88626, 83244, 23269, 88417, 15177, 21875, 19621, 16145, 19993, 23381, 14543, 83462, 20207, 78915, 84032, 33849, 87426, 85502, 86047, 85424, 28564, 51086, 16773, 21072, 20670, 16276, 19081, 83149, 11275, 76081, 28629, 11154, 91283, 22897, 82800, 47723, 87321, 21089, 16532, 21538, 16108, 93083, 92557, 23267, 91547, 19555, 19433, 67467, 19494, 20682, 15860, 90175, 50606, 91611, 23056, 66683, 19022, 84275</td></tr>\n", "</tbody>\n", "</table>\n" ], "text/latex": [ "A data.table: 3 × 3\n", "\\begin{tabular}{lll}\n", " date & num\\_stk\\_3d & stk\\_list\\_3d\\\\\n", " <date> & <int> & <list>\\\\\n", "\\hline\n", "\t 2023-01-06 & 172 & 16981, 85726, 16019, 77902, 23297, 65752, 24459, 93423, 14045, 20848, 77300, 13168, 19813, 89167, 17875, 89002, 93434, 13860, 16802, 14317, 14716, 76279, 79909, 92089, 19920, 90164, 20338, 21286, 12448, 20115, 19859, 21598, 80960, 66157, 15580, 17369, 91413, 16880, 20178, 18086, 64995, 22618, 71563, 21277, 58246, 15064, 23473, 14574, 59408, 79851, 16964, 18043, 79323, 21361, 81593, 18802, 18838, 18783, 88284, 34666, 82281, 88372, 16049, 63773, 14490, 16975, 75320, 22092, 18134, 16697, 26403, 15087, 81696, 78664, 20447, 14889, 19969, 89411, 19559, 16178, 16303, 19621, 22797, 80759, 92594, 18089, 15059, 77202, 14543, 16773, 92486, 91416, 72304, 78908, 77437, 18664, 13410, 19993, 22312, 77496, 91471, 14185, 17906, 88626, 22240, 85072, 17977, 20207, 13328, 83651, 93004, 88332, 14442, 90101, 15177, 19561, 91359, 20057, 22351, 91615, 19176, 84348, 19476, 27909, 22293, 15371, 25419, 14459, 92648, 93345, 90558, 13947, 92570, 43553, 88319, 89952, 82800, 22897, 22899, 81774, 76711, 21866, 18203, 17825, 19994, 21538, 13537, 15642, 22772, 23267, 18677, 89941, 78988, 21120, 91547, 18915, 20637, 20823, 89908, 67467, 12880, 14182, 20682, 18051, 15506, 16008, 19568, 82702, 20578, 31668, 78690, 13577\\\\\n", "\t 2023-01-09 & 111 & 16981, 85726, 77902, 20545, 22255, 15273, 18514, 21608, 93434, 13860, 20338, 19002, 20115, 14229, 19920, 76136, 85860, 20464, 23473, 14574, 10933, 34746, 17357, 23477, 82775, 17880, 52936, 58246, 91413, 18043, 91176, 14127, 34666, 18415, 81761, 92528, 88372, 16049, 22092, 16697, 65875, 13757, 14811, 88626, 16924, 14937, 16773, 17906, 23269, 14823, 89411, 19621, 19559, 88332, 11654, 15177, 20288, 20207, 83462, 17358, 21750, 90520, 52396, 78908, 13936, 14536, 19993, 90558, 86778, 66384, 17139, 88597, 19561, 79586, 34817, 21072, 17372, 80759, 11275, 91606, 14543, 17281, 20581, 41188, 22897, 16689, 89952, 81774, 82800, 23273, 13537, 19313, 17885, 20292, 21538, 15057, 23267, 21866, 91547, 19555, 77515, 19494, 67467, 20682, 20823, 16008, 23056, 78690, 17105, 19142, 16084\\\\\n", "\t 2023-01-10 & 100 & 85726, 16981, 77902, 20545, 75034, 21608, 19880, 18058, 22255, 15273, 21685, 17361, 91498, 20452, 90454, 14574, 20115, 76136, 69649, 91413, 89443, 16548, 17151, 82486, 19002, 14229, 23477, 20451, 23014, 20464, 15072, 16697, 87356, 16049, 34948, 23505, 22280, 17358, 22244, 21750, 15779, 17906, 88626, 83244, 23269, 88417, 15177, 21875, 19621, 16145, 19993, 23381, 14543, 83462, 20207, 78915, 84032, 33849, 87426, 85502, 86047, 85424, 28564, 51086, 16773, 21072, 20670, 16276, 19081, 83149, 11275, 76081, 28629, 11154, 91283, 22897, 82800, 47723, 87321, 21089, 16532, 21538, 16108, 93083, 92557, 23267, 91547, 19555, 19433, 67467, 19494, 20682, 15860, 90175, 50606, 91611, 23056, 66683, 19022, 84275\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.table: 3 × 3\n", "\n", "| date <date> | num_stk_3d <int> | stk_list_3d <list> |\n", "|---|---|---|\n", "| 2023-01-06 | 172 | 16981, 85726, 16019, 77902, 23297, 65752, 24459, 93423, 14045, 20848, 77300, 13168, 19813, 89167, 17875, 89002, 93434, 13860, 16802, 14317, 14716, 76279, 79909, 92089, 19920, 90164, 20338, 21286, 12448, 20115, 19859, 21598, 80960, 66157, 15580, 17369, 91413, 16880, 20178, 18086, 64995, 22618, 71563, 21277, 58246, 15064, 23473, 14574, 59408, 79851, 16964, 18043, 79323, 21361, 81593, 18802, 18838, 18783, 88284, 34666, 82281, 88372, 16049, 63773, 14490, 16975, 75320, 22092, 18134, 16697, 26403, 15087, 81696, 78664, 20447, 14889, 19969, 89411, 19559, 16178, 16303, 19621, 22797, 80759, 92594, 18089, 15059, 77202, 14543, 16773, 92486, 91416, 72304, 78908, 77437, 18664, 13410, 19993, 22312, 77496, 91471, 14185, 17906, 88626, 22240, 85072, 17977, 20207, 13328, 83651, 93004, 88332, 14442, 90101, 15177, 19561, 91359, 20057, 22351, 91615, 19176, 84348, 19476, 27909, 22293, 15371, 25419, 14459, 92648, 93345, 90558, 13947, 92570, 43553, 88319, 89952, 82800, 22897, 22899, 81774, 76711, 21866, 18203, 17825, 19994, 21538, 13537, 15642, 22772, 23267, 18677, 89941, 78988, 21120, 91547, 18915, 20637, 20823, 89908, 67467, 12880, 14182, 20682, 18051, 15506, 16008, 19568, 82702, 20578, 31668, 78690, 13577 |\n", "| 2023-01-09 | 111 | 16981, 85726, 77902, 20545, 22255, 15273, 18514, 21608, 93434, 13860, 20338, 19002, 20115, 14229, 19920, 76136, 85860, 20464, 23473, 14574, 10933, 34746, 17357, 23477, 82775, 17880, 52936, 58246, 91413, 18043, 91176, 14127, 34666, 18415, 81761, 92528, 88372, 16049, 22092, 16697, 65875, 13757, 14811, 88626, 16924, 14937, 16773, 17906, 23269, 14823, 89411, 19621, 19559, 88332, 11654, 15177, 20288, 20207, 83462, 17358, 21750, 90520, 52396, 78908, 13936, 14536, 19993, 90558, 86778, 66384, 17139, 88597, 19561, 79586, 34817, 21072, 17372, 80759, 11275, 91606, 14543, 17281, 20581, 41188, 22897, 16689, 89952, 81774, 82800, 23273, 13537, 19313, 17885, 20292, 21538, 15057, 23267, 21866, 91547, 19555, 77515, 19494, 67467, 20682, 20823, 16008, 23056, 78690, 17105, 19142, 16084 |\n", "| 2023-01-10 | 100 | 85726, 16981, 77902, 20545, 75034, 21608, 19880, 18058, 22255, 15273, 21685, 17361, 91498, 20452, 90454, 14574, 20115, 76136, 69649, 91413, 89443, 16548, 17151, 82486, 19002, 14229, 23477, 20451, 23014, 20464, 15072, 16697, 87356, 16049, 34948, 23505, 22280, 17358, 22244, 21750, 15779, 17906, 88626, 83244, 23269, 88417, 15177, 21875, 19621, 16145, 19993, 23381, 14543, 83462, 20207, 78915, 84032, 33849, 87426, 85502, 86047, 85424, 28564, 51086, 16773, 21072, 20670, 16276, 19081, 83149, 11275, 76081, 28629, 11154, 91283, 22897, 82800, 47723, 87321, 21089, 16532, 21538, 16108, 93083, 92557, 23267, 91547, 19555, 19433, 67467, 19494, 20682, 15860, 90175, 50606, 91611, 23056, 66683, 19022, 84275 |\n", "\n" ], "text/plain": [ " date num_stk_3d\n", "1 2023-01-06 172 \n", "2 2023-01-09 111 \n", "3 2023-01-10 100 \n", " stk_list_3d \n", "1 16981, 85726, 16019, 77902, 23297, 65752, 24459, 93423, 14045, 20848, 77300, 13168, 19813, 89167, 17875, 89002, 93434, 13860, 16802, 14317, 14716, 76279, 79909, 92089, 19920, 90164, 20338, 21286, 12448, 20115, 19859, 21598, 80960, 66157, 15580, 17369, 91413, 16880, 20178, 18086, 64995, 22618, 71563, 21277, 58246, 15064, 23473, 14574, 59408, 79851, 16964, 18043, 79323, 21361, 81593, 18802, 18838, 18783, 88284, 34666, 82281, 88372, 16049, 63773, 14490, 16975, 75320, 22092, 18134, 16697, 26403, 15087, 81696, 78664, 20447, 14889, 19969, 89411, 19559, 16178, 16303, 19621, 22797, 80759, 92594, 18089, 15059, 77202, 14543, 16773, 92486, 91416, 72304, 78908, 77437, 18664, 13410, 19993, 22312, 77496, 91471, 14185, 17906, 88626, 22240, 85072, 17977, 20207, 13328, 83651, 93004, 88332, 14442, 90101, 15177, 19561, 91359, 20057, 22351, 91615, 19176, 84348, 19476, 27909, 22293, 15371, 25419, 14459, 92648, 93345, 90558, 13947, 92570, 43553, 88319, 89952, 82800, 22897, 22899, 81774, 76711, 21866, 18203, 17825, 19994, 21538, 13537, 15642, 22772, 23267, 18677, 89941, 78988, 21120, 91547, 18915, 20637, 20823, 89908, 67467, 12880, 14182, 20682, 18051, 15506, 16008, 19568, 82702, 20578, 31668, 78690, 13577\n", "2 16981, 85726, 77902, 20545, 22255, 15273, 18514, 21608, 93434, 13860, 20338, 19002, 20115, 14229, 19920, 76136, 85860, 20464, 23473, 14574, 10933, 34746, 17357, 23477, 82775, 17880, 52936, 58246, 91413, 18043, 91176, 14127, 34666, 18415, 81761, 92528, 88372, 16049, 22092, 16697, 65875, 13757, 14811, 88626, 16924, 14937, 16773, 17906, 23269, 14823, 89411, 19621, 19559, 88332, 11654, 15177, 20288, 20207, 83462, 17358, 21750, 90520, 52396, 78908, 13936, 14536, 19993, 90558, 86778, 66384, 17139, 88597, 19561, 79586, 34817, 21072, 17372, 80759, 11275, 91606, 14543, 17281, 20581, 41188, 22897, 16689, 89952, 81774, 82800, 23273, 13537, 19313, 17885, 20292, 21538, 15057, 23267, 21866, 91547, 19555, 77515, 19494, 67467, 20682, 20823, 16008, 23056, 78690, 17105, 19142, 16084 \n", "3 85726, 16981, 77902, 20545, 75034, 21608, 19880, 18058, 22255, 15273, 21685, 17361, 91498, 20452, 90454, 14574, 20115, 76136, 69649, 91413, 89443, 16548, 17151, 82486, 19002, 14229, 23477, 20451, 23014, 20464, 15072, 16697, 87356, 16049, 34948, 23505, 22280, 17358, 22244, 21750, 15779, 17906, 88626, 83244, 23269, 88417, 15177, 21875, 19621, 16145, 19993, 23381, 14543, 83462, 20207, 78915, 84032, 33849, 87426, 85502, 86047, 85424, 28564, 51086, 16773, 21072, 20670, 16276, 19081, 83149, 11275, 76081, 28629, 11154, 91283, 22897, 82800, 47723, 87321, 21089, 16532, 21538, 16108, 93083, 92557, 23267, 91547, 19555, 19433, 67467, 19494, 20682, 15860, 90175, 50606, 91611, 23056, 66683, 19022, 84275 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Section 1: calculate daily abnormal return\n", "\n", "daily_abnormal_return = data[,\n", " ':='(ret_mkt=weighted.mean(retx, cap, na.rm=T)), keyby=.(date)\n", " ][, .SD[.N>=50], keyby=.(permno)\n", " ][!is.na(retx)\n", " ][, {\n", " lm_out = lm(retx ~ ret_mkt)\n", " ar = resid(lm_out)\n", " list(date, ar, industry=industry[1])\n", " },\n", " keyby=.(permno)]\n", "\n", "\n", "# Section 2: find out stocks whose abnormal return rank top 100 for all of the past three days\n", "\n", "daily_abnormal_return[\n", " # Step 1: find out the stocks whose abnormal return ranks top 30% in the industry on each day\n", " order(date, industry, -ar), .(permno_list=list(permno[1:(0.3*.N)])), keyby=.(date, industry)\n", " ][,\n", " # Step 2: (important!) unpack and repack the permno list by date\n", " .(permno_list=list(unlist(permno_list))), keyby=.(date)\n", " ][, \n", " # Step 3: compute the number of stocks that meet the criteria\n", " {\n", " num_stk_3d = vector() # store the number of stocks on each day\n", " stk_list_3d = list() # store the list of stocks on each day\n", "\n", " for (i in 4:.N) {\n", " # `stks` is the list of stocks that meet the criteria on each day\n", " stks = Reduce(intersect, permno_list[(i-3):(i-1)])\n", "\n", " stk_list_3d[[i]] = stks # don't use \"[i]\", you must use \"[[i]]\"\n", " num_stk_3d[i] = length(stks)\n", " }\n", " list(date, num_stk_3d, stk_list_3d)\n", " }\n", " ][!is.na(num_stk_3d)\n", " ][1:3]" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.3.2" } }, "nbformat": 4, "nbformat_minor": 2 }