Skip to content

Instantly share code, notes, and snippets.

@kgullikson88
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kgullikson88/147f6beb6256307d1360 to your computer and use it in GitHub Desktop.
Save kgullikson88/147f6beb6256307d1360 to your computer and use it in GitHub Desktop.
Interpolation with error propagation
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline as spline
class ErrorPropagationSpline(object):
"""
Does a spline fit, but returns both the spline value and associated uncertainty.
"""
def __init__(self, x, y, yerr, N=1000, *args, **kwargs):
"""
See docstring for InterpolatedUnivariateSpline
"""
yy = np.vstack([y + np.random.normal(loc=0, scale=yerr) for i in range(N)]).T
self._splines = [spline(x, yy[:, i], *args, **kwargs) for i in range(N)]
def __call__(self, x, *args, **kwargs):
"""
Get the spline value and uncertainty at point(s) x. args and kwargs are passed to spline.__call__
:param x:
:return: a tuple with the mean value at x and the standard deviation
"""
x = np.atleast_1d(x)
s = np.vstack([curve(x, *args, **kwargs) for curve in self._splines])
return (np.mean(s, axis=0), np.std(s, axis=0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment