【问题标题】:Calculate functions with multiple input in a moving window在移动窗口中计算具有多个输入的函数
【发布时间】:2021-06-09 17:05:12
【问题描述】:

您好,我有一个具有多个输入的函数,我想通过滚动窗口计算它。

我的功能是:

def tracking_error(r_a, r_b):
    '''
    Returns the tracking error between two return series. 
    This method is used in Sharpe Analysis minimization problem.
    See STYLE_ANALYSIS method.
    '''
    return ( ((r_a - r_b)**2).sum() )**(0.5)

def style_analysis_tracking_error(weights, ref_r, bb_r):
    '''
    Sharpe style analysis objective function.
    Returns the tracking error between the reference returns
    and a portfolio of building block returns held with given weights. 
    '''
    return tracking_error(ref_r, (weights*bb_r).sum(axis=1))

def style_analysis(dep_var, exp_vars):
    '''
    Sharpe style analysis optimization problem.
    Returns the optimal weights that minimizes the tracking error between a portfolio 
    of the explanatory (return) variables and the dependent (return) variable.
    '''
    # dep_var is expected to be a pd.Series
    if isinstance(dep_var,pd.DataFrame):
        dep_var = dep_var[dep_var.columns[0]]
    
    n = exp_vars.shape[1]
    init_guess = np.repeat(1/n, n)
    weights_const = {
        'type': 'eq',
        'fun': lambda weights: 1 - np.sum(weights)
    }
    solution = minimize(style_analysis_tracking_error, 
                        init_guess,
                        method='SLSQP',
                        options={'disp': False},
                        args=(dep_var, exp_vars),
                        constraints=(weights_const,),
                        bounds=((0.0, 1.0),)*n)
    weights = pd.Series(solution.x, index=exp_vars.columns)
    return weights

据此,我有一个包含我的数据的数据框,并计算长度为windowie 的时间段内每个日期的输出,例如: weights = rolling(252).apply(style_analysis, y, x)

有什么想法吗?

【问题讨论】:

  • 一目了然,您所做的优化将破坏您使用内置rolling 功能可能获得的任何效率。既然如此,我只会自己“滚动”它。 for i in range(252, len(df)): df_ = df.iloc[i - 252: i]df_ 做你想做的事。这有意义吗?
  • 不清楚!我的函数返回:AGG 0.030380 IWP 0.357634 IWX 0.526391 IWY 0.085595 dtype: float64,我想将结果存储在 df 中,日期为索引。
  • 我建议提供minimal reproducible example 以便我可以查看和使用数据。否则,我们会围着对方兜圈子,一无所获。
  • 我的数据来自:import yfinance as yf etf = yf.download("IWX IWY IWS IWP ACWX AGG", start="2010-01-01")['Adj Close'] 然后:df = etf.pct_change().dropna() 最后我想将所有输出权重存储在 df 中:weight = style_analysis(ret['ACWX'], ret[['AGG', 'IWP', 'IWX', 'IWY']])
  • 这对我来说工作量太大了。我想查看可以复制和粘贴的数据子集。使用比 252... 更短的值...比如 10 来表达观点。然后显示结果数据应该是什么样子。我知道这看起来像是工作,但我更愿意你做而不是我做。

标签: python pandas apply rolling-computation


【解决方案1】:

这是我的数据的一个子集:

    ACWX    AGG IWP IWS
Date                
2010-01-01  0.000000    0.000000    0.000000    0.000000
2010-01-04  0.028111    0.001163    0.014115    0.015968
2010-01-05  0.003566    0.004550    0.003262    0.008258
2010-01-06  0.003554    -0.000578   0.003685    0.003699
2010-01-07  -0.007082   -0.001156   0.002160    0.007107
2010-01-08  0.007608    0.000578    0.003017    0.004705
2010-01-11  0.008730    -0.000772   0.000430    0.000780
2010-01-12  -0.011228   0.006372    -0.012028   -0.013517
2010-01-13  0.006151    -0.004221   0.010435    0.013439
2010-01-14  0.002821    0.002601    0.000645    0.003640
2010-01-15  -0.014068   0.001921    -0.011395   -0.015803
2010-01-18  0.000000    0.000000    0.000000    0.000000
2010-01-19  0.012604    -0.001630   0.009352    0.016846
2010-01-20  -0.026303   0.002593    -0.009265   -0.010095
2010-01-21  -0.023397   0.000288    -0.013484   -0.015691
2010-01-22  -0.021487   0.000575    -0.020061   -0.021785
2010-01-25  0.013629    -0.000096   0.002250    0.004346
2010-01-26  -0.008964   0.001244    -0.001347   -0.005949
2010-01-27  -0.004020   -0.000765   0.002922    0.001632
2010-01-28  -0.016397   -0.000861   -0.013671   -0.012493

【讨论】:

    【解决方案2】:

    这是我的功能:

    from scipy.optimize import minimize
    
    def tracking_error(r_a, r_b):
        '''
        Returns the tracking error between two return series. 
        This method is used in Sharpe Analysis minimization problem.
        See STYLE_ANALYSIS method.
        '''
        return ( ((r_a - r_b)**2).sum() )**(0.5)
    
    def style_analysis_tracking_error(weights, ref_r, bb_r):
        '''
        Sharpe style analysis objective function.
        Returns the tracking error between the reference returns
        and a portfolio of building block returns held with given weights. 
        '''
        return tracking_error(ref_r, (weights*bb_r).sum(axis=1))
    
    def style_analysis(dep_var, exp_vars):
        '''
        Sharpe style analysis optimization problem.
        Returns the optimal weights that minimizes the tracking error between a portfolio 
        of the explanatory (return) variables and the dependent (return) variable.
        '''
        # dep_var is expected to be a pd.Series
        if isinstance(dep_var,pd.DataFrame):
            dep_var = dep_var[dep_var.columns[0]]
        
        n = exp_vars.shape[1]
        init_guess = np.repeat(1/n, n)
        weights_const = {
            'type': 'eq',
            'fun': lambda weights: 1 - np.sum(weights)
        }
        solution = minimize(style_analysis_tracking_error, 
                            init_guess,
                            method='SLSQP',
                            options={'disp': False},
                            args=(dep_var, exp_vars),
                            constraints=(weights_const,),
                            bounds=((0.0, 1.0),)*n)
        weights = pd.Series(solution.x, index=exp_vars.columns)
        return weights
    

    【讨论】:

      【解决方案3】:

      我终于做到了:

      weights = pd.DataFrame()
      
      for tps in ret.index: 
          end = tps+pd.Timedelta(days=365)    
          beg = end-pd.Timedelta(days=365)  
          weights.loc[end, ['AGG', 'IWP', 'IWX', 'IWY'] ] = 100*style_analysis(ret.loc[beg:end, 'ACWX'], ret.loc[beg:end, ['AGG', 'IWP', 'IWX', 'IWY']]).values
      

      它似乎可以工作,但速度很慢(1 分 36 秒!!!)

      无论如何要加速?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-14
        • 1970-01-01
        • 1970-01-01
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        相关资源
        最近更新 更多