程序代写 GR5260 Class7 – Jupyter Notebook

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
import pandas as pd
import numpy as np
import pandas_datareader.data as web

Copyright By PowCoder代写 加微信 powcoder

d = web.DataReader(name=’GOOGL’, data_source=’yahoo’, start=’2021-06-04′,
end=’2022-03-04′)
High Low Open
2022-03-02 2705.250000 2661.429932 2692.560059 2691.429932 1176000 2691.429932 2022-03-03 2728.790039 2660.510010 2720.199951 2677.989990 1225900 2677.989990 2022-03-04 2677.030029 2606.750000 2658.550049 2638.129883 1522532 2638.129883
d.Close.plot()
Out[4]:
Out[5]: Date
2021-06-04 2393.570068
2021-06-07 2402.300049
2021-06-08 2398.439941
2021-06-09 2407.939941
2021-06-10 2435.129883
2022-02-28 2701.139893
2022-03-01 2681.229980
2022-03-02 2691.429932
2022-03-03 2677.989990
2022-03-04 2638.129883
Name: Adj Close, Length: 190, dtype: float64
d[‘Adj Close’]
# log price return = ln(P(t)) – ln(P(t-1))
localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
Out[6]: Date
2021-06-04 NaN
d[‘Adj Close’].shift(1)
2021-06-07 2393.570068
2021-06-08 2402.300049
2021-06-09 2398.439941
2021-06-10 2407.939941
2022-02-28 2689.189941
2022-03-01 2701.139893
2022-03-02 2681.229980
2022-03-03 2691.429932
2022-03-04 2677.989990
Name: Adj Close, Length: 190, dtype: float64
r = np.log(d[‘Adj Close’]) – np.log(d[‘Adj Close’].shift(1))
Out[14]: Date
2021-06-04 NaN
2021-06-07 0.003641
2021-06-08 -0.001608
2021-06-09 0.003953
2021-06-10 0.011229
2022-02-28 0.004434
2022-03-01 -0.007398
2022-03-02 0.003797
2022-03-03 -0.005006
2022-03-04 -0.014996
Name: Adj Close, Length: 190, dtype: float64
# SMA over 3 days
r_sma = r.rolling(window=3) # returns a Rolling object (used to compute mean and std type(r_sma)
Out[12]: pandas.core.window.rolling.Rolling In [13]:
Out[13]: Date
2021-06-04 NaN
r_sma.mean() # SMA mean of log price return r
2021-06-07 NaN
2021-06-08 NaN
2021-06-09 0.001995
2021-06-10 0.004524
2022-02-28 0.018964
2022-03-01 0.003425
2022-03-02 0.000278
2022-03-03 -0.002869
2022-03-04 -0.005402
Name: Adj Close, Length: 190, dtype: float64
r_ewm = r.ewm(alpha=0.06, # alpha = 1 – lambda, lamda=0.94 # returns a ewm object
adjust=True, # True: weighted sum formula, False: recursive formula min_periods=3)
localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
pandas.core.window.ewm.ExponentialMovingWindow
type(r_ewm)
r_ewm.mean()
sma_r = r_sma.mean()
ewm_r = r_ewm.mean() sma_r.plot(label=’SMA’, legend=True) ewm_r.plot(label=’EWM’, legend=True)
2021-06-04 NaN
2021-06-07 NaN
2021-06-08 NaN
2021-06-09 0.002004
2021-06-10 0.004528
2022-02-28 -0.000467
2022-03-01 -0.000883
2022-03-02 -0.000602
2022-03-03 -0.000866
2022-03-04 -0.001714
Name: Adj Close, Length: 190, dtype: float64

localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
sma_vol = r_sma.std()
ewm_vol = r_ewm.std() sma_vol.plot(label=’SMA’, legend=True) ewm_vol.plot(label=’EWM’, legend=True)
Out[22]:
# class exercise: download the price series for FB and AAPL
# and compare their EWMA vol estimates (past half year or longer period)
d = web.DataReader(name=’GOOGL’, data_source=’yahoo’, start=’2021-03-04′,
end=’2022-03-04′)
d1 = web.DataReader(name=’FB’, data_source=’yahoo’, start=’2021-03-04′,
end=’2022-03-04′)
d2 = web.DataReader(name=’AAPL’, data_source=’yahoo’, start=’2021-03-04′,
end=’2022-03-04′)
r = np.log(d[‘Adj Close’]) – np.log(d[‘Adj Close’].shift(1))
r1 = np.log(d1[‘Adj Close’]) – np.log(d1[‘Adj Close’].shift(1)) r2 = np.log(d2[‘Adj Close’]) – np.log(d2[‘Adj Close’].shift(1))
localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
r_ewm = r.ewm(alpha=0.06, adjust=True, min_periods=3) r1_ewm = r1.ewm(alpha=0.06, adjust=True, min_periods=3) r2_ewm = r2.ewm(alpha=0.06, adjust=True, min_periods=3)
ewm_voL = r_ewm.std()
ewm_vol1 = r1_ewm.std()
ewm_vol2 = r2_ewm.std() ewm_vol.plot(label=’GOOGL’, legend=True) ewm_vol1.plot(label=’FB’, legend=True) ewm_vol2.plot(label=’AAPL’, legend=True)
Out[31]:
ewm_vol[-25:-10].plot(label=’GOOGL’, legend=True) ewm_vol1[-25:-10].plot(label=’FB’, legend=True) ewm_vol2[-25:-10].plot(label=’AAPL’, legend=True)
Out[41]:
localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

3/4/22, 10:09 PM GR5260 Class7 – Jupyter Notebook
#d1[‘Adj Close’].tail()
r1[-25:-15]
Out[44]: Date
2022-01-28 0.023712
2022-01-31 0.037567
2022-02-01 0.018158
2022-02-02 0.012461
2022-02-03 -0.306391
2022-02-04 -0.002822
2022-02-07 -0.052739
2022-02-08 -0.021255
2022-02-09 0.052292
2022-02-10 -0.017085
Name: Adj Close, dtype: float64
localhost:8889/notebooks/2022_GR5260/GR5260 Class7.ipynb

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com