Description
Using python to do the requirements in word
Unformatted Attachment Preview
Convince me whether “correlations go to one” during crises. Also,
convince me if there is a similar pattern for capital asset pricing model
(CAPM) betas. You should explain your findings to convince me.
Here are implementation details:
1.
Use daily returns for the current stocks in the S&P 500 from 1980
through 2023.
1.
The code below calculates these returns and saves them to
sp500.csv.
2.
sp500.csv also includes daily closes for the S&P 500 (ticker
^GSPC) from 1927 through 2023.
3.
Your code should download any other data you need with
the pandas-datareader or yfinance packages.
2.
You may define a crisis as follows:
1.
Begins with a bear market, which is a 20% decline in the
S&P 500 from its recent peak
2.
Ends with the next bull market, which is a 20% rise in the
S&P 500 from its recent trough
Here are tips:
1.
You may want to consider monthly and quarterly windows
2.
You may want to consider summary statistics (e.g., mean and
median) for correlations and CAPM betas
3.
You may want to consider the following formulas to help explain
your findings:
( , )
1.
Correlation: , = ( ) ( )
2.
CAPM beta: , = , + ( , − , ) + , , where = ( )
( , )
4.
Highlight any weaknesses of your analysis and possible solutions
5.
Although you will submit your report as a Word document
instead of PowerPoint slides, you should still focus on clear
plots and bulleted explanations instead of lengthy prose.
Criteria
Topic
Clarity, correctness, and completeness of calculations
Clarity, correctness, and completeness of visualizations
Clarity, correctness, and completeness of discussions
Submission guidelines
Artificial Intelligence (AI)
You may use AI (e.g., ChatGPT or Copilot) to help you write code and
prepare your report. However:
1.
AI will not do very well on this project without significant input
from your team
2.
AI will not be a defense against plagiarism because AI should not
write your code and slides1
1
If you plagiarize an AI that plagiarizes other sources, you plagiarized
that AI and its sources.
Data
The following code creates sp500.csv, which contains daily returns for
the current stocks in the S&P 500 and the daily close of the S&P 500
(ticker ^GSPC).
import pandas as pd
import yfinance as yf
url = ‘https://en.wikipedia.org/wiki/List_of_S%26P_500_companies’
tickers = pd.read_html(io=url)[0][‘Symbol’].to_list()
# Wikipedia uses “.” in tickers, but Yahoo! Finance uses “-” in ticke
rs
tickers = [t.replace(‘.’, ‘-‘) for t in tickers]
tickers[:5]
[‘MMM’, ‘AOS’, ‘ABT’, ‘ABBV’, ‘ACN’]
def get_data(tickers, index, start, end, file):
(
yf.download(tickers=tickers, start=start, end=end)
[‘Adj Close’]
.pct_change()
.join(
other=(
# get all prior index data to identify peaks and trough
s (and bear and bull markets)
yf.download(tickers=index, start=’1900-01-01′, end=en
d)
[[‘Close’]]
.rename(columns={‘Close’: index})
),
how=’outer’
)
.to_csv(file)
)
get_data(tickers=tickers, index=’^GSPC’, start=’1980-01-01′, end=’20
23-12-31′, file=’sp500.csv’)
sp500 = (
pd.read_csv(
filepath_or_buffer=’sp500.csv’,
parse_dates=[‘Date’],
index_col=[‘Date’]
)
.rename_axis(columns=’Ticker’)
)
sp500.iloc[-5:, -5:].pipe(display)
Ticker
ZBH
ZBRA
ZION
ZTS
^GSPC
0.007605
0.015454
0.008712
0.001644 4754.629883
0.009142
0.022605
0.020000
0.002667 4774.750000
2023-12-
–
0.001053
–
0.007161 4781.580078
27
0.002388
2023-12-
0.004128
Date
2023-1222
2023-1226
28
2023-1229
0.008244
–
0.003370
0.001320 4783.350098
–
–
0.001065 4769.830078
0.007336
0.017689
0.001595
0.000576
Purchase answer to see full
attachment