title: Croston's method on intermittent demand in Python
body: <p>I am struggling with Croston's method which I am applying on an intermittent demand dataset.
  The library I use is the following: <a href="https://pypi.org/project/croston//" rel="nofollow noreferrer">https://pypi.org/project/croston//</a>.</p>
  <p>The dataset that I use consists of 6 years of intermittent demand data: <a href="https://i.stack.imgur.com/cZzKd.png" rel="nofollow noreferrer">Erratic and intermittent demand datasets</a>. The intermittent demand pattern is displayed in red. As can be seen from the picture, intervals of no demand switch of demand periods. The average length of an interval is 15, and the avg length of demand is 3, and thus the ADI is 5 which makes it an intermittent demand series according to Syntetos, Boylan, and Croston (2005). The code that I used is this:</p>
  <pre><code>Crost1 = croston.fit_croston(intermittent_time_series['D'],1,'original')
  Croston = pd.DataFrame(np.concatenate([Crost1['croston_fittedvalues'],Crost1['croston_forecast']]))
  Croston.index = pd.to_datetime(Croston.index, unit='D') 
  intermittent_time_series['Croston'] = Croston
  </code></pre>
  <p>The output I get looks weird though. It fits the first few (zero demand) observations correctly, but after these it all gives values of around 1. I wonder what I am doing wrong. Ofcourse I would like better estimates of the intermittent demand.</p>
  <p>To improve the fit, I investigated fora for solutions, but I could not find any. I also tried to switch up the methods ('original'/'sba'/'sbj'/'tsb'), and looked at the source code. Unfortunately, I did not manage to come up with a solution.</p>
  <p>Thanks in advance,
  Daan.</p>
  <p><strong>EDIT</strong></p>
  <p>I do not know how to share a dataframe over the interwebz, but I can share the code I used to create the demand:</p>
  <pre><code>def intermittent_demand(demand_period,interval,stop):  
  Dt = []
  while stop &lt; years*days:
      temp = round(interval + np.random.normal(0,5,size=None))
      if stop + temp &gt; years*days:
          Dt = Dt + (years*days - stop) * [0]
          stop = years*days
      else:
          Dt = Dt + temp * [0]
          stop = stop + temp
          
      temp = round(demand_period + np.random.normal(0,1.2,size=None))
      if stop + temp &gt; years*days:
          Dt = Dt + (years*days - stop) * [0] 
          stop = years*days
      else: 
          Dt = Dt + temp * [1]
          stop = stop + temp
  
  for day in range(years*days):
      if Dt[day] == 1:
          Dt[day] = math.ceil(a_intermittent + error_inter[day]) 
  
  return Dt
  for day in range (days*years):
      intermittent_time_series = intermittent_demand(demand_period,interval,stop)
  </code></pre>
  <p>Where days = 365, years = 6, demand_period = 3, interval = 15, stop = 0, a_intermittent = 4, and error_inter = 'np.random.normal(avg, 2<em>dev, days</em>years)'</p>