2017-04-11 67 views
1

我有一个数据框如下。我想将一个已定义的函数(hurst())应用于每列(将时间序列作为参数),然后将结果输出到另一个数据帧。将函数应用于数据帧列

def hurst(ts): 
    #returns the Hurst Exponent of the time series vector ts 

    # Create the range of lag values 
    lags = range (1,5) 

    # Calculate the array of the variances of the lagged differences 
    tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags] 

    # Use a linear fit to estimate the Hurst Exponent 
    poly = polyfit(log(lags), log(tau), 1) 

    # Return the Hurst exponent from the polyfit output 

    return poly[0]*2.0 

如何从这个

enter image description here

得到

enter image description here

完整的数据帧都有一个1Y申历史,并继续E,F等

+0

'df.sum()'有什么问题?这将有助于准确定义你的问题,因为这个简单的例子已经有了答案 – EdChum

+0

是的。看看'apply' df.apply(yourFunction),无论是按列还是按行(不推荐)。 –

+0

我的功能在下面,但使用df.apply(hurst,axis = 1)我得到a你不能改变数组错误的一部分。 DEF赫斯特(TS): #returns的时间序列矢量的Hurst指数TS #创建滞后的范围内的值 滞后=范围(2,50) #计算滞后的方差的阵列差异 #使用线性拟合来估计Hurst指数 poly = polyfit(log(t(t)(std(减去(ts [lag:],ts [: - lag])))对于滞后滞后] # 1) #从polyfit输出中返回Hurst指数 return poly [0] * 2.0 –

回答

0

让我们试试:

def hurst(ts): 
    #returns the Hurst Exponent of the time series vector ts 

    # Create the range of lag values 
    lags = range (1,5) 

    # Calculate the array of the variances of the lagged differences 
    tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags] 

    # Use a linear fit to estimate the Hurst Exponent 
    poly = polyfit(log(lags), log(tau), 1) 

    # Return the Hurst exponent from the polyfit output 

    return poly[0]*2.0 

df[list('ABCD')].apply(hurst) 
相关问题