title: How to use SGD for time series analysis
body: <p>Is it possible to use <a href="http://en.wikipedia.org/wiki/Stochastic_gradient_descent" rel="nofollow">stochastic gradient descent</a> for time-series analysis?</p>
  
  <p>My initial idea, given a series of (t, v) pairs where I want an SGD regressor to predict the v associated with t+1, would be to convert the date/time into an integer value, and train the regressor on this list using the hinge loss function. Is this feasible?</p>
  
  <p>Edit: This is example code using the <a href="http://scikit-learn.org/dev/modules/generated/sklearn.linear_model.SGDRegressor.html" rel="nofollow">SGD implementation</a> in scikit-learn. However, it fails to properly predict a simple linear time series model. All it seems to do is calculate the average of the training Y-values, and use that as its prediction of the test Y-values. Is SGD just unsuitable for time-series-analysis or am I formulating this incorrectly?</p>
  
  <pre><code>from datetime import date
  from sklearn.linear_model import SGDRegressor
  
  # Build data.
  s = date(2010,1,1)
  i = 0
  training = []
  for _ in xrange(12):
      i += 1
      training.append([[date(2012,1,i).toordinal()], i])
  testing = []
  for _ in xrange(12):
      i += 1
      testing.append([[date(2012,1,i).toordinal()], i])
  
  clf = SGDRegressor(loss='huber')
  
  print 'Training...'
  for _ in xrange(20):
      try:
          print _
          clf.partial_fit(X=[X for X,_ in training], y=[y for _,y in training])
      except ValueError:
          break
  
  print 'Testing...'
  for X,y in testing:
      p = clf.predict(X)
      print y,p,abs(p-y)
  </code></pre>