Skip to content

Instantly share code, notes, and snippets.

@dtanham
Created November 8, 2015 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtanham/1f2b06c48a975c4354d7 to your computer and use it in GitHub Desktop.
Save dtanham/1f2b06c48a975c4354d7 to your computer and use it in GitHub Desktop.
LOESS implementation in Python - courtesy of Phillip K Janert's O'Reilly book Data Analysis with Open Source Tools (aka the bible)
from pylab import *
# # x: location; h: bandwidth; xp, yp: data points (vectors)
def loess( x, h, xp, yp):
w = exp( -0.5*( ((x-xp)/h)**2)/sqrt(2*pi*h**2) )
b = sum(w*xp)*sum(w*yp) - sum(w)*sum(w*xp*yp)
b /= sum(w*xp)**2 - sum(w)*sum(w*xp**2)
a = ( sum(w*yp) - b*sum(w*xp) )/sum(w)
return a + b*x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment