-
Notifications
You must be signed in to change notification settings - Fork 0
/
20200917FDA.py
356 lines (177 loc) · 6.02 KB
/
20200917FDA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python
# coding: utf-8
# In[66]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
import pandas_datareader as web
# ***Return Distribution with Amazon ***
# In[3]:
amzn = web.get_data_yahoo('AMZN', start = '2000-01-01', end = '2020-09-01')
amzn.head()
# In[4]:
#수익률 계산
amzn_ret = amzn['Adj Close'].pct_change()
# In[5]:
amzn_ret.head()
# In[6]:
#NA를 제거하고 update해서 저장
amzn_ret.dropna(inplace = True)
amzn_ret.head()
# In[7]:
amzn_ret.describe()
# *** Plot returns along with average ***
# In[10]:
plt.figure(figsize = (15,5))
plt.plot(amzn_ret.index, amzn_ret, label = 'Return', zorder = 0)
#평균으로 수평선 그리기 zorder = 1 : 빨간선을 파란선 위에 그리겟다는 뜻인듯?
plt.hlines(amzn_ret.mean(), amzn_ret.index[0], amzn_ret.index[-1], label = 'Mean', color = 'r', zorder = 1)
plt.legend(loc = 'best')
plt.grid()
# In[12]:
#Plot historgram using Seaborn
import seaborn as sns
import scipy as sp
# In[14]:
#kde =false : 밀도 그래프를 그리지 않는다.
#fit = sp.stats.norm : 정규분포와 비교함
sns.distplot(amzn_ret, kde = False, fit = sp.stats.norm)
# In[17]:
#Plot histogram with including rugplot
#rug plot을 포함시켜서 값들이 어디에 분포하는지 알 수 있다.
sns.distplot(amzn_ret, rug = True, kde = False, fit = sp.stats.norm)
# In[18]:
#Check Skewnewss and Kurtosis using Scipy
sp.stats.skew(amzn_ret), sp.stats.kurtosis(amzn_ret)
# *** Using Q-Q plot to compare with normal distribution ***
# In[22]:
#subplot() : 여러개의 그래프를 그리기 위함
ax = plt.subplot()
sp.stats.probplot(amzn_ret, plot = ax)
plt.show()
# In[24]:
#S&P500으로 Q-Q plot 비교
sp500 = web.get_data_yahoo('^GSPC', start = '2000-01-01', end = '2020-09-01')
ax = plt.subplot()
sp.stats.probplot(sp500['Adj Close'].pct_change().dropna(), plot = ax)
plt.show()
# *** Normality Test using K-S and J-B tests ***
# In[25]:
#K - S Test
#다른 dist와 비교하는 것이기에 비교 dist가 input으로 들어가야함
sp.stats.kstest(amzn_ret, 'norm')
# In[26]:
sp500_ret = sp500['Adj Close'].pct_change().dropna()
sp.stats.kstest(sp500_ret, 'norm')
# In[27]:
#J-B Test
#normality 만 검증하기 때문에 input이 더 필요없고 return만 넣어주면됨
sp.stats.jarque_bera(amzn_ret)
# In[28]:
sp.stats.jarque_bera(sp500_ret)
# *** Correlation of Assets ***
# In[29]:
#Use 5 industry portfolios(devides the stock market into 5 industries)
ind5 = web.DataReader('5_Industry_Portfolios', 'famafrench', start = '1990-01-01', end = '2020-09-01')
# In[33]:
#fama french에서 불러오는건 dict형태라서 pct_change함수를 사용할 수 없음!!
ind5_ret = ind5[0] / 100
ind5_ret.head()
# In[34]:
ind5_ret.corr()
# In[35]:
#Correlation Visualization using a heatmap( using matplotlib pyplot)
plt.matshow(ind5_ret.corr())
plt.colorbar()
# In[36]:
#Correlation Visualization heatmap using seaborn
#annot = True : 값 표시
sns.heatmap(ind5_ret.corr(), annot = True)
# In[37]:
# 금융위기 상황에서의 correlation
#Global finance crisis 2008
crisis1 = ind5_ret.loc['2007-01':'2008-12']
sns.heatmap(crisis1.corr(), annot = True)
#=> 전반적으로 산업군들의 상관관계가 높아진다!
# In[38]:
#Global finance crisis with Covid-19
crisis2 = ind5_ret.loc['2020-01' : '2020-9']
sns.heatmap(crisis2.corr(), annot = True)
# In[39]:
#많은 산업군으로 비교할수록 차이는 더 극명해진다.
#10 Industry Porfolios
ind10 = web.DataReader('10_Industry_Portfolios', 'famafrench', start = '1990-01-01', end = '2020-09-01')
ind10_ret = ind10[0]/100
ind10_ret.head()
# In[41]:
sns.heatmap(ind10_ret.corr(), annot = True)
# *** High Volatility ***
# 변동성이 큰 시기에 자산, 산업들의 상관관계가 높아진다
# 그렇다면 변동성은 어떻게 측정할 것인가?
# => 변동성만 나타내는 지수가 있다 : 'VIX'
# In[45]:
vix = web.get_data_yahoo('^VIX', start = '2000-01-01', end = '2020-09-01')
vix.head()
# In[46]:
plt.figure(figsize = (10,5))
plt.plot(vix.index, vix.Close)
plt.grid()
# *** Exercise ***
# 주식의 수익률이 Skewed되어있을 떄 trimmed average를 계산하고 싶다.
# trimmed average : outlier 때문에 평균이 영향을 받을 떄 outlier 영향을 받지 않는 평균
# => outlier 빼고 나머지 값으로 계산한다.
# In[47]:
sp500 = web.get_data_yahoo('^GSPC', start = '1990-01-01', end = '2020-09-01')['Adj Close'].pct_change().dropna()
# In[48]:
sp500.head()
# In[51]:
# sort 하기
sp500_sort = sp500.sort_values()
sp500_sort.head()
# In[53]:
sp500_sort.shape
# In[54]:
n = sp500_sort.shape[0]
# In[56]:
#Check the histogram and mean returns
sns.distplot(sp500_sort, rug = True, kde = False, fit = sp.stats.norm)
# In[59]:
print('Daily mean : %.3f%%' % (sp500_sort.mean()*100))
print('Annualized mean : %.3f%%' % (sp500_sort.mean()*252*100))
# In[61]:
means = pd.Series(sp500_sort.mean(), index = [0])
means
# In[62]:
for i in range(100):
means[i + 1] = sp500_sort[i + 1 : -(i + 1)].mean()
means.tail()
# In[63]:
plt.plot(means)
# In[64]:
plt.plot(means * 252)
# In[67]:
#Numpy arrays를 사용하여 구하는 방식
#size 100이고 nan값으로 구성된 배열생성
means_np = np.full(100, np.nan)
for i in range(1,101):
means_np[i-1] = sp500_sort[i : -i].mean()
means_np
# In[70]:
#Scipy trim_mean을 사용
#prop1 : [0/n, 1/n... 100 / n] n = 앞에서 정의한 변수(sp500_sort의 총 행 갯수)
#비율 변환해준거임
prop1 = np.arange(0,101) / n
trim1 = np.full(100, np.nan)
for i in range(100):
trim1[i] = sp.stats.trim_mean(sp500_sort, prop1[i])
plt.plot(trim1)
# In[71]:
#trimming using proportion
#linspace : 시작과 끝을 넣고 그 사이를 동일한 구간으로 쪼개줌(stat, end, stepsize)
prop2 = np.linspace(0,100/n, 100)
trim2 = np.full(prop2.shape[0], np.nan)
for i in range(prop2.shape[0]):
trim2[i] = sp.stats.trim_mean(sp500_sort, prop2[i])
plt.plot(trim2)