import matplotlib.pyplot as plt
import numpy as np
import pandas as pdMcKinney Chapter 10 - Practice Blank
FINA 6333 for Spring 2024
1 Announcements
2 Practice
%precision 4
pd.options.display.float_format = '{:.4f}'.format
%config InlineBackend.figure_format = 'retina'import yfinance as yf
import pandas_datareader as pdr2.1 Replicate the following .pivot_table() output with .groupby()
ind = (
yf.download(tickers='^GSPC ^DJI ^IXIC ^FTSE ^N225 ^HSI')
.rename_axis(columns=['Variable', 'Index'])
.stack()
)[*********************100%%**********************] 6 of 6 completed
(
ind
.loc['2015':]
.reset_index()
.pivot_table(
values='Close',
index=pd.Grouper(key='Date', freq='A'),
columns='Index',
aggfunc=['min', 'max']
)
)| min | max | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Index | ^DJI | ^FTSE | ^GSPC | ^HSI | ^IXIC | ^N225 | ^DJI | ^FTSE | ^GSPC | ^HSI | ^IXIC | ^N225 |
| Date | ||||||||||||
| 2015-12-31 | 15666.4404 | 5874.1001 | 1867.6100 | 20556.5996 | 4506.4902 | 16795.9609 | 18312.3906 | 7104.0000 | 2130.8201 | 28442.7500 | 5218.8599 | 20868.0293 |
| 2016-12-31 | 15660.1797 | 5537.0000 | 1829.0800 | 18319.5801 | 4266.8398 | 14952.0195 | 19974.6191 | 7142.7998 | 2271.7200 | 24099.6992 | 5487.4399 | 19494.5293 |
| 2017-12-31 | 19732.4004 | 7099.2002 | 2257.8301 | 22134.4707 | 5429.0801 | 18335.6309 | 24837.5098 | 7687.7998 | 2690.1599 | 30003.4902 | 6994.7598 | 22939.1797 |
| 2018-12-31 | 21792.1992 | 6584.7002 | 2351.1001 | 24585.5293 | 6192.9199 | 19155.7402 | 26828.3906 | 7877.5000 | 2930.7500 | 33154.1211 | 8109.6899 | 24270.6191 |
| 2019-12-31 | 22686.2207 | 6692.7002 | 2447.8899 | 25064.3594 | 6463.5000 | 19561.9609 | 28645.2598 | 7686.6001 | 3240.0200 | 30157.4902 | 9022.3896 | 24066.1191 |
| 2020-12-31 | 18591.9297 | 4993.8999 | 2237.3999 | 21696.1309 | 6860.6699 | 16552.8301 | 30606.4805 | 7674.6001 | 3756.0701 | 29056.4199 | 12899.4199 | 27568.1504 |
| 2021-12-31 | 29982.6191 | 6407.5000 | 3700.6499 | 22744.8594 | 12609.1602 | 27013.2500 | 36488.6289 | 7420.7002 | 4793.0601 | 31084.9395 | 16057.4404 | 30670.0996 |
| 2022-12-31 | 28725.5098 | 6826.2002 | 3577.0300 | 14687.0195 | 10213.2900 | 24717.5293 | 36799.6484 | 7672.3999 | 4796.5601 | 24965.5508 | 15832.7998 | 29332.1602 |
| 2023-12-31 | 31819.1406 | 7256.8999 | 3808.1001 | 16201.4902 | 10305.2402 | 25716.8594 | 37557.9219 | 8014.2998 | 4768.3701 | 22688.9004 | 15044.7031 | 33753.3281 |
2.2 Calulate the mean and standard deviation of returns by ticker for the MATANA (MSFT, AAPL, TSLA, AMZN, NVDA, and GOOG) stocks
Consider only dates with complete returns data. Try this calculation with wide and long data frames, and confirm your results are the same.
2.3 Calculate the mean and standard deviation of returns and the maximum of closing prices by ticker for the MATANA stocks
Again, consider only dates with complete returns data. Try this calculation with wide and long data frames, and confirm your results are the same.
2.4 Calculate monthly means and volatilities for SPY and GOOG returns
2.5 Plot the monthly means and volatilities from the previous exercise
2.6 Assign the Dow Jones stocks to five portfolios based on their monthly volatility
First, we need to download Dow Jones stock data and calculate daily returns. Use data from 2019 through today.
2.7 Plot the time-series volatilities of these five portfolios
How do these portfolio volatilies compare to (1) each other and (2) the mean volatility of their constituent stocks?
2.8 Calculate the mean monthly correlation between the Dow Jones stocks
Drop duplicate correlations and self correlations (i.e., correlation between AAPL and AAPL), which are 1, by definition.
2.9 Is market volatility higher during wars?
Here is some guidance:
- Download the daily factor data from Ken French’s website
- Calculate daily market returns by summing the market risk premium and risk-free rates (
Mkt-RFandRF, respectively) - Calculate the volatility (standard deviation) of daily returns every month by combining
pd.Grouper()and.groupby()) - Multiply by \(\sqrt{252}\) to annualize these volatilities of daily returns
- Plot these annualized volatilities
Is market volatility higher during wars? Consider the following dates:
- WWII: December 1941 to September 1945
- Korean War: 1950 to 1953
- Viet Nam War: 1959 to 1975
- Gulf War: 1990 to 1991
- War in Afghanistan: 2001 to 2021