Skip to content

Instantly share code, notes, and snippets.

@syrte
Created May 16, 2023 08:40
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 syrte/04fe67fc8ebf795b83d8ac37771a83e4 to your computer and use it in GitHub Desktop.
Save syrte/04fe67fc8ebf795b83d8ac37771a83e4 to your computer and use it in GitHub Desktop.
import np
def kern_smooth(x, xp, yp, bw=1, return_std=False):
w = np.exp(-0.5 * ((xp - x.reshape(-1, 1)) / bw)**2)
w = w / w.sum(1, keepdims=True)
y = (yp * w).sum(1)
if not return_std:
return y
else:
# x should cover the range of xp to validate interp of dy
assert x[0] <= xp[0] and x[-1] >= xp[-1]
dy = yp - np.interp(xp, x, y) # needed if y varies fast
ystd = ((dy**2 * w).sum(1) - (dy * w).sum(1)**2)**0.5
return y, ystd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment