diff --git a/example/13-2-1-calculate-beta.py b/example/13-2-1-calculate-beta.py new file mode 100644 index 0000000..2892fbc --- /dev/null +++ b/example/13-2-1-calculate-beta.py @@ -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) \ No newline at end of file