Data Module

The data module provides a set of helper functions which allow to process experimental data, e.g. calculate weighted average, statistical deviation or linear regressions.

maabara.data.general_fit(f, xdata, ydata, p0=None, sigma=None, **kw)[source]

Use non-linear least squares to fit a function, f, to data.

Assumes ydata = f(xdata, *params) + eps

f : callable
The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
xdata : An N-length sequence or an (k,N)-shaped array
for functions with k predictors. The independent variable where the data is measured.
ydata : N-length sequence
The dependent data — nominally f(xdata, …)
p0 : None, scalar, or M-length sequence
Initial guess for the parameters. If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).
sigma : None or N-length sequence
If not None, it represents the standard-deviation of ydata. This vector, if given, will be used as weights in the least-squares problem.
out : Nx2 array
Parameter value and its deviation

The algorithm uses the Levenburg-Marquardt algorithm through leastsq. Additional keyword arguments are passed directly to that algorithm.

Based on http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/

Copyright http://redlands.edu

>>> def func(x, a, b, c):
...     return a*np.exp(-b*x) + c
>>> x = np.linspace(0,4,50)
>>> y = func(x, 2.5, 1.3, 0.5)
>>> yn = y + 0.2*np.random.normal(size=len(x))
>>> r = general_fit(func, x, yn)
maabara.data.linear_fit(xdata, ydata, ysigma=None, name='r')[source]

Performs a linear fit to data.

xdata : array like ydata : array like ysigma : None or array like

If provided it will be used as standard-deviation of ydata and weights in the fit.
name : string, optional
Latex name
out : m, b, tex
m – gradient ufloat b – ordinate ufloat tex – Latex markup of linear polynom

Based on http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/

Copyright http://redlands.edu

Basic matplotlib figure

>>> a4width = 448.13095 / 72.27
... A4 = (a4width, a4width/1.41421356237)
... pl = figure(figsize=A4)

Set labels

>>> ticklabel_format(style='sci', axis='both', scilimits=(-3,3))
... xlabel('x [1]')
... ylabel('y [1]')

Perform linear fit

>>> x = [0.0, 2.0, 4.0, 6.0, 8.0]
... y = [1.1, 1.9, 3.2, 4.0, 5.9]
... y_err = [0.1, 0.2, 0.1, 0.3, 0.3]
... m, b, tex = ma.data.linear_fit(x,y,y_err)

Plot results

>>> errorbar(x, y, y_err, label='')
... x = linspace(min(x),max(x))
... plot(x, m.n*x+b.n, "r-", label ="$" + tex + "$")

Save plot to pdf

>>> pl.savefig("pl.pdf", transparent=True, format="pdf", bbox_inches='tight')
maabara.data.literature_value(lit, value, dev=0, mode='default')[source]

Comparision with literature value

lit : float
Literature value
value : float
Nominal value
dev : float, optional
Deviation
mode : {‘default’, ‘ufloat’, ‘tex’, ‘tex!’, ‘print’, or ‘print:Latex name’}
Set return mode
out : mixed
Relative deviation
maabara.data.statistic_values(x)[source]

Returns mean including deviation out of statistical data set (see definiton below)

x : numpy array
Statistical values (differnt shapes possible, see Returns)
out : mixed
if x is a linear array:
(float) mean, (float) deviation, (float) deviation for a single value in set
if x is two-dimensional:
numpy array with two columns for mean and deviation corrosponding to input data

Definition:

\[ \begin{align}\begin{aligned}\bar{x} &= \frac{1}{N} \sum_{i=1}^{N} x_i\\\sigma_{\bar{x}} &= \sqrt {\frac1{N(N-1)} \sum_{i=1}^N (x_i-\overline{x})^2}\end{aligned}\end{align} \]
maabara.data.student_t(x)[source]

Returns mean with deviation of statistical data set multiplied by student t-factor

See statistic_values()

maabara.data.weighted_average(data, mode='default')[source]

Weighted average (see definition below)

data : Nx2 numpy array
Two column array, first value, second deviation
mode : {‘default’, ‘ufloat’, ‘print’, ‘print:Latex name’}
Set return mode
out : mixed
ufloat or value and deviation
>>> data = [[ 1. ,  6. ],
...         [ 8. ,  1.2]]
>>> ma.data.weighted_average(data)
(7.7307692307692308, 1.1766968108291043)

Definition:

\[ \begin{align}\begin{aligned}\bar{x} &= \frac{ \sum_{i=1}^n \left( x_i \sigma_i^{-2} \right)}{\sum_{i=1}^n \sigma_i^{-2}}\\\sigma_{\bar{x}}^2 &= \frac{ 1 }{\sum_{i=1}^n \sigma_i^{-2}}\end{aligned}\end{align} \]