편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.11.01

    파이썬 rolling regression 질문드립니다


    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 가

    문제입니다.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.11.01

    파이썬 질문드립니다


    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 가

    문제입니다 ㅠ