Special functions#
The sdt.funcs
module contains classes for construction of certain
kinds of functions,
StepFunction
for step functions,ECDF
for empirical cumulative distribution functions.
Additionally, 1D and 2D Gaussians can be evaluated using gaussian_1d()
and gaussian_2d()
, respectively. Sums of exponentials are implemented
by exp_sum()
and exp_sum_lmfit()
.
Examples
Create a step function:
>>> xs = numpy.arange(10)
>>> sfun = StepFunction(xs, xs) # left-sided by default
Evaluate the function for some x:
>>> x = numpy.linspace(-1, 10, 23)
>>> sfun(x)
array([0., 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 5., 5., 6., 6., 7., 7.,
8., 8., 9., 9., 9., 9.])
Create an eCDF from generated data:
>>> obs = numpy.random.rand(100) # Uniformly distributed observations
>>> e = ECDF(obs)
Evaluate the eCDF:
>>> x = numpy.linspace(0, 1, 21)
>>> e(x)
array([0. , 0.05, 0.12, 0.19, 0.22, 0.27, 0.31, 0.36, 0.44, 0.48, 0.52,
0.53, 0.6 , 0.64, 0.71, 0.73, 0.79, 0.87, 0.89, 0.96, 1. ])
Programming reference#
- class sdt.funcs.StepFunction(x, y, fill_value='extrapolate', side='left', sort=True)[source]#
A step function
Given the sites of jumps, x and the function values y at those sites, construct a function that is constant inbetween.
Examples
>>> xs = numpy.arange(10) >>> sfun = StepFunction(xs, xs) # left-sided by default >>> x = numpy.linspace(-1, 10, 23) >>> sfun(x) array([0., 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 5., 5., 6., 6., 7., 7., 8., 8., 9., 9., 9., 9.]) >>> sfun2 = StepFunction(xs, xs, side="right") >>> sfun2(x) array([0., 0., 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 5., 5., 6., 6., 7., 7., 8., 8., 9., 9., 9.])
- Parameters:
x (array-like) – Function graph
y (array-like) – Function graph
fill_value (float or (float, float) or "extrapolate", optional) – Values to use for
x < x[0]
andx > x[-1]
. If this is “extrapolate”, usey[0]
andy[-1]
, respectively. If a float is given, use it in both cases. If a pair of floats is given, the first entry is used on the lower end, the second on the upper end. Defaults to “extrapolate”.sort (bool, optional) – Whether to sort data such that x is in ascending order. Set to False if x is already sorted to avoid re-sorting. Defaults to True.
side ({"left", "right"}, optional) – The step function will yield
y[i+1]
for any x in the interval (x[i]
,x[i+1]
] ifside="left"
(thus making the step function continuos from the left). It will yieldy[i]
and for x in the interval [x[i]
,x[i+1]
) ifside="right"
(continuous from the right). Defaults to “left”.
- property x#
Abscissa values of the steps; sorted
- property y#
Ordinate values of the steps
- class sdt.funcs.ECDF(obs, interp=None, sort=True)[source]#
Empirical cumulative distribution function (eCDF)
Examples
>>> obs = numpy.random.rand(100) # Uniformly distributed observations >>> e = ECDF(obs) >>> x = numpy.linspace(0, 1, 21) >>> e(x) array([0. , 0.05, 0.12, 0.19, 0.22, 0.27, 0.31, 0.36, 0.44, 0.48, 0.52, 0.53, 0.6 , 0.64, 0.71, 0.73, 0.79, 0.87, 0.89, 0.96, 1. ])
- Parameters:
obs (array-like) – List of observations
interp (None or int, optional) – Which interpolation to use. None will create a right-continuous step function. Using an integer, the interpolating spline order can be specified. Defaults to None.
sort (bool, optional) – If obs is already sorted in ascending order, set to False to avoid re-sorting. Defaults to True.
- __call__(x)[source]#
Get eCDF value(s) at x
- Parameters:
x (number or array-like) – Where to evaluate the step function
- Returns:
eCDF value(s) at x
- Return type:
number or numpy.ndarray
- property observations#
Array of observations
- sdt.funcs.gaussian_1d(x, amplitude=1.0, center=0.0, sigma=1.0, offset=0.0)[source]#
1D Gaussian
\[A e^\frac{(x - c)^2}{2\sigma^2} + b\]- Parameters:
x (ndarray) – Independent variable
amplitude (float) – A in the formula above.
center (float) – c in the formula above.
sigma (float) – \(\sigma\) in the formula above.
offset (float) – b in the formula above.
- Return type:
Function values
- sdt.funcs.gaussian_2d(x, y, amplitude=1.0, center=(0.0, 0.0), sigma=(1.0, 1.0), offset=0.0, rotation=0.0)[source]#
2D Gaussian
\[A \exp(\frac{(R(x - c_x))^2}{2\sigma_x^2} + \frac{(R(y - c_y))^2}{2\sigma_y^2}) + b,\]where \(R\) rotates the vector (x, y) by rotation radiants.
- Parameters:
x (ndarray) – Independent variables
y (ndarray) – Independent variables
amplitude (float) – A in the formula above.
center (Tuple[float, float]) – \(c_x\), \(c_y\) in the formula above.
sigma (Tuple[float, float]) – \(\sigma_x\), \(\sigma_y\) in the formula above.
offset (float) – b in the formula above.
rotation (float) – Rotate the Gaussian by that many radiants.
- Return type:
Function values
- sdt.funcs.exp_sum(x, offset, mant, exp)[source]#
Sum of exponentials
Return
offset + mant[0] * exponential(exp[0] * x) + mant[1] * exponential(exp[1] * x) + …
- Parameters:
x (ndarray) – Independent variable
offset (float) – Additive parameter
mant (ndarray) – Mantissa coefficients
exp (ndarray) – Coefficients in the exponent
- Return type:
Function values
Examples
>>> exp_sum(np.arange(10), offset=1, mant=[-0.2, -0.8], exp=[-0.1, -0.01]) array([ 0. , 0.02699265, 0.05209491, 0.07547993, 0.09730444, 0.11771033, 0.13682605, 0.15476788, 0.17164113, 0.18754112])
- sdt.funcs.exp_sum_lmfit(x, offset=1.0, **params)[source]#
Sum of exponentials, usable for
lmfit.Model
Return
offset + mant0 * exponential(exp0 * x) + mant1 * exponential(exp1 * x) + …
. This is more suitable for fitting usinglmfit.Model
thanexp_sum()
. See the example below.- Parameters:
x (ndarray) – Independent variable
offset (float) – Additive parameter
**params (float) – To get the sum of n+1 exponentials, one needs to supply floats mant0, mant1, …, mantn as mantissa coefficients and exp0, …, expn as coefficients in the exponent.
- Return type:
Function values
Examples
>>> x = numpy.array(...) # x values >>> y = numpy.array(...) # y values
>>> mant_names = ["mant{}".format(i) for i in num_exp] >>> exp_names = ["exp{}".format(i) for i in num_exp] >>> m = lmfit.Model(funcs.exp_sum_lmfit) >>> m.set_param_hint("offset", ...) >>> for m in mant_names: ... m.set_param_hint(m, ...) >>> for e in exp_names: ... m.set_param_hint(e, ...) >>> p = m.make_params() >>> f = m.fit(y, params=p, t=x)