title: Outlier detection for time series
body: <p>I wanted to generate a very simple example of anomaly detection for time series. So I created sample data with one very obvious outlier but I didn't get any method to detect the outlier reliably so far. I tried local outlier factor, isolation forests and k nearest neighbors. From what I read, at least one of those methods should be suitable. I also tried tweaking the parameters but that didn't really help.</p>
  
  <p>What mistake do I make here? Are the methods not appropriate?</p>
  
  <p>Below is a code example.</p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  np.random.seed(1)
  
  t=np.linspace(0,10,101).reshape(-1,1)
  y_test=0.5+t+t**2+2*np.random.randn(len(t),1)
  
  y_test[10]=y_test[10]*7
  
  plt.figure(1)
  plt.plot(t,y_test)
  plt.show;
  
  from sklearn.neighbors import LocalOutlierFactor
  
  clf=LocalOutlierFactor(contamination='auto')
  pred=clf.fit_predict(y_test)
  
  plt.figure(2)
  plt.plot(t[pred==1],y_test[pred==1],'bx')
  plt.plot(t[pred==-1],y_test[pred==-1],'ro')
  plt.show
  
  from sklearn.ensemble import IsolationForest
  
  clf=IsolationForest(behaviour='new',contamination='auto')
  pred=clf.fit_predict(y_test)
  
  plt.figure(3)
  plt.plot(t[pred==1],y_test[pred==1],'bx')
  plt.plot(t[pred==-1],y_test[pred==-1],'ro')
  plt.show
  
  from pyod.models.knn import KNN
  
  clf = KNN()
  clf.fit(y_test)
  pred=clf.predict(y_test)
  
  plt.figure(4)
  plt.plot(t[pred==0],y_test[pred==0],'bx')
  plt.plot(t[pred==1],y_test[pred==1],'ro')
  plt.show
  </code></pre>