input data:-
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
here i am using simple exponential smoothing for calculating the q_ses and a_ses values.
calculations:-
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
syntax:-
#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)
by using the above code i am only getting the fitted values for q and a values only i.e, for 11 observations but i want fitted values for all the observations in x.
I got the the logic behind how croston method produces the fitted values but i am unable to get the desired code for that.
logic:- every non_zero fitted value is assigned as fitted values for next zero_elements+next one non_zero element.
i mentioned fitted values in the calculation part.
desired output for fitted values:-
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
I want a python code for getting the above output,
i tried some approaches like,finding zero elements positions replacing the values but that doesn't work in this case.
please can anyone help me to tackle this issue.
thanks in advance.