用户定义的函数,用于计算python中间歇性croston方法的拟合值

时间:2018-09-11 11:16:27

标签: python intermittent

输入数据:-

x  0    2   0   1   0   11  0   0   0   0   2   0 6 3   0   0   0   0   0    
   7    0   0   0   0   0   0   0   3   1   0   0   1   0   1   0   0

在这里,我使用简单的指数平滑来计算q_ses和a_ses值。

计算:-

q     2   1    11    2    6    3    7    3    1    1    1 
a     2   2     2    5    2    1    6    8    1    3    2 
q_ses     2  1.9   2.81  2.73 3.07 3.05 3.44 3.4  3.16 2.94
a_ses     2    2      2  2.3  2.27 2.14 2.52 3.08 2.86 2.88
fitted    1  0.95  1.41  1.19 1.35 1.42 1.36 1.11 1.10 1.02

q:- non-zero elements
a:- inter-arrival time
q_ses:-alpha*at+(1-alpha)*ft
a_ses:-alpha*at+(1-alpha)*ft
fitted:-q_ses/a_ses

语法:-

#x=data series
#h=numer of periods to forecast
#alpha=simple exponential smoothing parameter

def croston(x,h,alpha=np.nan):
    if isnull(alpha):
       alpha=0.1
    y= x[x!=0].reset_index()
    y['a'] = y['index'] - y['index'].shift(1)
    y.drop('index', axis=1, inplace=True)
    y= y.bfill()

    #fitteing simple exponential smoothing 

    fit_non_zero=SimpleExpSmoothing(y.iloc[:,0]).
          fit(smoothing_level=alpha,optimized=False)

    fit_inter_arrival=SimpleExpSmoothing(y.iloc[:,1]).
           fit(smoothing_level=alpha,optimized=False)
    #fitted values for non-zero and interarrival time.

    non_zero_fitted=fit_non_zero.fittedvalues
    inter_arrival_fitted=fit_inter_arrival.fittedvalues
    final_predict=non_zero_fitted/inter_arrival_fitted

    # forecast values for h periods

    forecast_q=fit_non_zero.forecast(h)
    forecast_a=fit_inter_arrival.forecast(h)
    final_forecast=forecast_q/forecast_a
    return(final_forecast,final_predict)

通过使用上面的代码,我仅获得q的拟合值和仅一个值,即11个观测值的拟合值,但我希望获得x中所有观测值的拟合值。

我了解了croston方法如何产生拟合值的逻辑,但我无法为此获得所需的代码。

逻辑:- 每个非零拟合值都被分配为下一个zero_elements +下一个non_zero元素的拟合值。

我在计算部分提到了拟合值。

所需的拟合值输出:-

x: 0    2   0   1   0   11  0   0   0   0   2   0   6   3   0   0   0    
   0    0   7   0   0   0   0   0   0   0   3   1   0   0   1   0   1    0
fitted: NA  0   1   1   0.95    0.95    1.41    1.41    1.41    1.41    1.41     
        1.19    1.19    1.35    1.42    1.42    1.42    1.42    1.42    1.42     
        1.36    1.36    1.36    1.36    1.36    1.36    1.36    1.36    1.11     
        1.1     1.1     1.1     1.02    1.02    0.985   0.985

我想要获取以上输出的python代码

我尝试了一些方法,例如找到零个元素位置来替换值,但这在这种情况下不起作用。

任何人都可以帮助我解决这个问题。

先谢谢。

0 个答案:

没有答案