feat: 13-2-1 베타 계산 추가

This commit is contained in:
Ayuriel 2025-03-10 17:53:06 +09:00
parent 4f559443c0
commit 4459164fd6

View File

@ -0,0 +1,28 @@
import yfinance as yf
import pandas as pd
import statsmodels.api as sm
KOSPI_CODE = '^KS11'
KIIUM = '039490.KS'
# KOSPI 코드(^KS11), 전통적인 고베타주인 증권주 중 키움증권(039490.KS)
tickers = [KOSPI_CODE, KIIUM]
all_data = {}
for ticker in tickers:
all_data[ticker] = yf.download(ticker, start="2020-01-01", end="2024-12-31")
# print(all_data)
# for tic, data in all_data.items():
# print(f"{tic}: {type(data)} -> {type(data['Close'])}")
# print(data['Close'])
# 종가(Close)에 해당하는 열만 선택해서 데이터프레임으로 가공
prices = pd.DataFrame({tic: data['Close'].squeeze() for tic, data in all_data.items()})
# 수익률 계산(pct_change), NA 데이터 삭제(dropna)
ret = prices.pct_change().dropna()
ret['intercept'] = 1
reg = sm.OLS(ret[[KIIUM]], ret[[KOSPI_CODE, 'intercept']]).fit()
print(reg.summary())
print(reg.params)