-
-
Save thriveth/4680e3d3cd2cfe561a57 to your computer and use it in GitHub Desktop.
Interpolation with error propagation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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