피어슨 상관계수 | 산점도¶
1단계 : 데이터 로드하기¶
In [ ]:
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
2단계: 데이터 확인하기¶
In [ ]:
SP500_SECTORS_CSV = '/content/drive/MyDrive/통계공부/PSDS/data/sp500_sectors.csv'
SP500_DATA_CSV='/content/drive/MyDrive/통계공부/PSDS/data/sp500_data.csv.gz'
In [ ]:
import pandas as pd
sp500_sym = pd.read_csv(SP500_SECTORS_CSV)
sp500_px = pd.read_csv(SP500_DATA_CSV, index_col=0)
3단계: 계산하기¶
In [ ]:
telecomSymbols = sp500_sym[sp500_sym['sector'] == 'telecommunications_services']['symbol']
In [ ]:
#상관행렬
telecom = sp500_px.loc[sp500_px.index >= '2012-07-01', telecomSymbols]
telecom.corr()
Out[ ]:
| T | CTL | FTR | VZ | LVLT | |
|---|---|---|---|---|---|
| T | 1.000000 | 0.474683 | 0.327767 | 0.677612 | 0.278626 |
| CTL | 0.474683 | 1.000000 | 0.419757 | 0.416604 | 0.286665 |
| FTR | 0.327767 | 0.419757 | 1.000000 | 0.287386 | 0.260068 |
| VZ | 0.677612 | 0.416604 | 0.287386 | 1.000000 | 0.242199 |
| LVLT | 0.278626 | 0.286665 | 0.260068 | 0.242199 | 1.000000 |
In [ ]:
#상관관계 시각화
import seaborn as sns
import matplotlib.pylab as plt
etfs = sp500_px.loc[sp500_px.index > '2012-07-01', sp500_sym[sp500_sym['sector'] == 'etf']['symbol']]
fig, ax = plt.subplots(figsize=(5, 4))
ax = sns.heatmap(etfs.corr(), vmin=-1, vmax=1,
cmap=sns.diverging_palette(20, 220, as_cmap=True),
ax=ax)
plt.tight_layout()
plt.show()
- ETF는 S&P 500과 다우 존스 지수와 높은 상관성
- 첨단 기술 회사들인 QQQ와 XLK 양의 상관관계
- 금값, 유가, 시장 변동성 등과 관려된 더 안정적인 ETF상품은 다른 ETF와 약한 혹은 음의 상관관계
In [ ]:
#산점도
ax = telecom.plot.scatter(x='T', y='VZ', figsize=(4, 4), marker='$\u25EF$')
ax.set_xlabel('ATT (T)')
ax.set_ylabel('Verizon (VZ)')
ax.axhline(0, color='grey', lw=1)
ax.axvline(0, color='grey', lw=1)
plt.tight_layout()
plt.show()
- 두 수익은 0 주변에 모여 있지만 강한 양의 상관성
- 두 주식이 함께 오르거나 떨어짐, 서로 반대되는 경우는 드물었음.
'수학 및 통계 > PSDS' 카테고리의 다른 글
| [PSDS] 두 개 이상의 변수 탐색하기 실습 (0) | 2023.09.28 |
|---|---|
| [PSDS] 두 개 이상의 변수 탐색하기 (1) | 2023.09.27 |
| [PSDS] 상관관계 (0) | 2023.09.25 |
| [PSDS] 이진 데이터와 범주 데이터 탐색하기 실습 (0) | 2023.09.24 |
| [PSDS] 이진 데이터와 범주 데이터 탐색하기 (0) | 2023.09.23 |