파이썬 변형 질문

파이썬 변형 질문

QA

파이썬 변형 질문

본문

KOSPI 월간 수익률 = a + b * S&P500 월간 수익률 + c * 상해종합주가지수 월간 수익률에대한

회귀분석까진 하였습니다.

 

 

회귀분석 코드:

import statsmodels.formula.api as sm

model = sm.ols(formula = 'M_kospi ~ F_sp500  + FF_상하이',
               data =monthly_asset_rre_df).fit()
print(model.summary())

 

이에 대한 rolling regression 코드 여쭤봅니다 ㅠㅠ

변수가 두개(sp500,kospi) 일때 코드는 밑에 처럼 알고 있는데 3개일땐 어떻게 하는지 모르겠네요 ㅠ 

# Rolling Regression

start = 0
end = 120
timeseries = {"period":[],
              "alpha": [], 
              "beta":[],
              "r_sqrd":[]} #딕셔너리

while end <= len(monthly_asset_rre_df):
    df = monthly_asset_rre_df.iloc[start:end]
    period = str(df.index[0].to_timestamp().to_period('M')) + " to " + str(df.index[-1].to_timestamp().to_period('M'))
    x = df[['F_sp500'] ] 
    y = df['M_kospi']
    x = sm.add_constant(x) #adds a constant term to the linear equation it is fitting
    model_Simple = sm.OLS(df[y], df[x]).fit()
    model = sm.OLS(df[y], sm.add_constant(df[x])).fit()
    model_result = model.summary()
    
    timeseries["period"].append(period)
    timeseries["r_sqrd"].append(model.rsquared)
    results_as_html = model_result.tables[1].as_html()
    timeseries["alpha"].append(pd.read_html(results_as_html, header=0, index_col=0)[0]["coef"][0]) #intercept
    timeseries["beta"].append(pd.read_html(results_as_html, header=0, index_col=0)[0]["coef"][1]) #coef
    start+=1  #start = start + 1
    end+=1    #end = end + 1
    
timeseries_result = pd.DataFrame.from_dict(timeseries)

------------------------------------

여기서 추가로

a, b, c, R2를 저장하여 다음의 그림을 그리기
1) b, c의 time-series plot
2) a, R2의 time-series plot 가

 

문제입니다 ㅠ

이 질문에 댓글 쓰기 :

답변 1

Rolling Regression 용어 자체도 어렵고.  답변을 위해서 거의 코드 분석을 해야 되니.  답변이 어려운 것 같습니다.

 

질문을 좀더 간단하게 만들었으면 합니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 88
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT