#5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices (Python Financial Analysis)

wsh
Python Financial Analysis
7 min readAug 11, 2021

#5.2 COVID-19 Impacts by Market Caps(Compare weighted average prices)

Python Financial Analysis | Home

Introduction

In the last story, we saw how much the COVID19 pandemic affected the stock market for each sector. We found that while some sectors like “Computer and Technology” and “Medical” were keep growing at the earlier stage of the pandemic, sectors like “Oils Energy” and “Transportation” got a sever damage. The last story told us that, by dividing the phenomenon into pieces (sectors), we can figure out what’s going on the field.

Thus, in this story, we want to divide the phenomenon from another point of view again. That is market capitalizations. You may think the damage of the pandemic is the same for any the market cap scales. But actually it’s not!

You can download the data set from this link
https://drive.google.com/drive/folders/1Ux2u1s5mctYiywS08sv7_3_PbnWd8v0G?usp=sharing

List of articles

1. Python Financial Analysis

1 Read fundamental data from a CSV in Python
2 Handling table like data in Python with DataFrame
3 Make graphs of stock price in Python
4.1 Make custom market index — prerequisites
4.2 Make custom market index — make your own index
4.3 Make custom market index — market cap based index
5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices
5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices
5.3 Find companies that lost or gained from the COVID19 pandemic

2. Python Data Analysis Basics (easiest ways)

Python “datetime” in the easiest way (how to handle dates in data science with Python)
Python DataFrame slicing in the easiest way (How to find a company from 5000 companies)

Performance Analysis

1. Earlier stage of pandemic (Jan — May)

The chart below shows how much the effect of the COVID19 pandemic changes by the market caps. We target companies whose market caps are greater than $1B. There are about 2627 companies listed on the US market. Then, we classify these companies into 11 according to their market caps. The largest 250 companies, for example, is labeled as “0–255”.

The performance of each market cap class is calculated as weighted sum of prices, where the weights are taken from their market caps. This means, Apple (AAPL) has the largest effect in the class “0–255”. Then we set the weighted averages to 1 at 2020–01–01, in order to see their relative change from the beginning of the pandemic.

The result is clear. It shows that the larger a company is, the smaller the effect of the pandemic is. While most valuable 250 companies lost 26% of their market caps by March 20, the smallest companies lost almost 53% (gains smaller than 1 means loss of capitalization).

You can see the same phenomenon from the line chart below. While largest companies were still growing at the earlier stage of the pandemic, smaller companies already started to lose (Jan — Feb). After the market impact, all lines were starting to drop sharply, the amount of loss were not the same between market caps.

This may indicate that investors shifted money from smaller (high risk) companies to larger companies (low risk) companies for safety. Another perspective is because larger companies are mainly composed of high-tech and the sector “Computer and Technology” got a huge gain during the pandemic.

2. Performance after the impact (Jan — Next May)

But things are not the same after that. The long-term performance until May 2021 doesn’t have any unevenness between the market cap classes. While all the market cap classes have grown after the pandemic impact, the growth of largest companies was getting slower. On the other hand, smaller companies started to grow with acceleration since Nov 2020. This leads to a removal of unevenness of growth until May 2021.

Python script

You can download the code from the link above. If you feel it’s hard, please read the previous stories.

1. Import packages

We first import packages for financial analysis. If you haven’t installed them, you can use the “pip” command on Windows, and “pip3” on Mac.

2. Read datasets

Then we read the datasets. The CSV files are available from the link above. The returned variables are called DataFrame, which are table like object.

3. Make the x-axis of graph

As in the previous stories, we have to convert date strings into datetime objects, because we can’t feed them directly into Matplotlib. The converted results are saved on a new column “datetime”.

4. Handling missing values

Because not all companies have price data for over this 5 years, we have to handle missing values. For example, newer companies don’t have price data of 5 years ago. These missing spaces are filled with NaN objects (Not a Number). Before calculating weighted average of prices, we need to replace them with zeros, because any results of calculation which include NaN values become NaNs.

5. Select range of price

Then we select range of price. Because we evaluate the effect of the pandemic in two periods (beginning of the pandemic and after that), the end date of the crop is either “2020–03–20” or “2021–04–20”. You can select the period by commenting out either of them. The beginning of the period is always “2020–01–01”.

The string “CROP_LABEL” is necessary as suffix of image names and file names of CSV.

6. Prepare for calculation

After selecting the period to evaluate, we define how to classify companies by market caps. Remember, we classified them such that each class has just 250 companies. This is defined by “RANK_STEPS”. Them, at the next line, we pick up companies whose market caps are at least over $1B. This is because smaller companies are too noisy for calculation. Then we sort them in descending order by the market caps.

The variable “RANK_BINS” defines the border points to split the DataFrame “meta_over1B”. Of course, the first entry is 0, and 250 comes next, and so on. In addition to that, we append “NUM_COMPANIES” to the generated list “RANK_BINS”, because the total number of companies is not a multiple of 250.

After defining the split order, we make three variables “total_marketcaps”, “gains”, and “rank_labels” for saving the results of analysis. We initialize them with zeros.

7. Calculate the performance

The next is a little bit hard. We calculate the performance of each class of companies one by one with a “for” loop. The iteration is done over the “RANK_BINS”. Because “RANK_BINS” defines where we split the table “meta_over1B”, we ignore the last iterations (at the last iteration, we don’t have the end point of crop).

Then we crop the table with the start point RANK_BINS[i] and RANK_BINS[i+1]. At the next line, we calculate the total market caps of this market cap class. This is the sum of market caps of 250 companies. This is necessary because the weights of the weighted average is their market caps. “rank_labels[i]” is just for making names of images and CSV files.

Then we iterate over the 250 tickers. “w” is the weight and “p” is the price of a specific company. Because some companies have too low price for calculation, we ignore companies whose price is less than $1B. At the line 74, we divide the result with it’s oldest values (the value at “2020–01–01”), for making it relative to the beginning of the pandemic. Remember, because the table of “price_crop” has the oldest entry at first row, the index “0” means “beginning”, while the index “-1” means “the latest”.

After evaluating the performance, we add a graph of the “price_crop[COLNAME]”. Then we save the figure as an image.

8. Save results as table

When we make a CSV table, we first define the data as a DataFrame object, and then save it as a CSV with the function “to_csv()”. Here, “summary” is an empty DataFrame. We then save the three lists “rank_labels”, “total_marketcaps”, and “gains” as new columns.

9. Make the bar chart

There’s not so much to explain here. The comments on the code is everything.

10. Run the code!

Type “python pandemic_impact_by_marketcap.py” on the console!

Full Python code

Other Links

Python Financial Analysis | Home
Python Data Analysis | Home

--

--