import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import yfinance as yfMcKinney Chapter 10 - Practice - Blank
FINA 6333 for Spring 2025
%precision 4
pd.options.display.float_format = '{:.4f}'.format
# %config InlineBackend.figure_format = 'retina'Announcements
Five-Minute Review
Practice
Replicate the following .pivot_table() output with .groupby()
ind = (
yf.download(
tickers='^GSPC ^DJI ^IXIC ^FTSE ^N225 ^HSI',
auto_adjust=False,
progress=False
)
.rename_axis(columns=['Variable', 'Index'])
.stack(future_stack=True)
)[**************** 33% ] 2 of 6 completed[**********************50% ] 3 of 6 completed[**********************67%******* ] 4 of 6 completed[**********************83%*************** ] 5 of 6 completed[*********************100%***********************] 6 of 6 completed
a = (
ind
.loc['2015':]
.reset_index()
.pivot_table(
values='Close',
index=pd.Grouper(key='Date', freq='YE'),
columns='Index',
aggfunc=['min', 'max']
)
)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.
matana = (
yf.download(
tickers='MSFT AAPL TSLA AMZN NVDA GOOG',
auto_adjust=False,
progress=False
)
.rename_axis(columns=['Variable', 'Ticker'])
)[**************** 33% ] 2 of 6 completed[**********************50% ] 3 of 6 completed[**********************67%******* ] 4 of 6 completed[**********************83%*************** ] 5 of 6 completed[*********************100%***********************] 6 of 6 completed
Calculate the mean and standard deviation of returns and the maximum of closing prices by ticker for the MATANA stocks
Calculate monthly means and volatilities for SPY and GOOG returns
Plot the monthly means and volatilities from the previous exercise
Assign the Dow Jones stocks to five portfolios based on the preceding month’s volatility
Plot the time-series volatilities of these five portfolios
Calculate the mean monthly correlation between the Dow Jones stocks
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