Evaluating type-B uncertainty
The shorter name tb
has been defined as an alias for type_b
, to resolve the
names of objects in this module.
Least-squares regression
line_fit()
implements an ordinary least-squares
straight-line regression calculation that accepts uncertain
real numbers for the independent and dependent variables.
line_fit_wls()
implements a weighted least-squares
straight-line regression calculation. It accepts uncertain
real numbers for the independent and dependent variables.
It is also possible to specify weights for the regression.
line_fit_wtls()
implements a total least-squares
algorithm for a straight-line fitting that can perform a
weighted least-squares regression when both y and x data
are uncertain real numbers, it also handles correlation
between (x,y) data pairs.
Probability distributions: real-valued problems
Functions that convert the half-width of a one-dimensional distribution to a standard uncertainty:
Probability distributions: complex-valued problems
Functions that convert information about two-dimensional distributions into standard uncertainties:
A table of distributions
The mapping
distribution
is provided so that the functions above can be selected by name. For example,>>> a = 1.5 >>> ureal( 1, type_b.distribution['gaussian'](a) ) ureal(1.0,1.5,inf) >>> ureal( 1, type_b.distribution['uniform'](a) ) ureal(1.0,0.8660254037844387,inf) >>> ureal( 1, type_b.distribution['arcsine'](a) ) ureal(1.0,1.0606601717798212,inf)Keys to
distribution
are (case-sensitive):
gaussian
uniform
triangular
arcsine
u_shaped
uniform_ring
uniform_disk
Module contents
- mean(seq, *args, **kwargs)
Return the arithmetic mean of data in
seq
- Parameters:
An uncertain number is returned if
seq
contains uncertain numbers.Example
>>> seq = [ ureal(1,1), ureal(2,1), ureal(3,1) ] >>> function.mean(seq) ureal(2.0,0.5773502691896257,inf)
Note
When
seq
is an emptyndarray
or andarray
containing anyNaN
elementsNaN
is returned.In other cases, a
ZeroDivisionError
is raised when there are no elements inseq
.
- line_fit(x, y)
Least-squares fit intercept and slope
New in version 1.2.
- Parameters:
x – sequence of independent variable data
y – sequence of dependent variable data
- Return type:
y
must be a sequence of uncertain real numbers.Performs an ordinary least-squares regression.
Note
Uncertainty in the parameter estimates is found by propagation through the regression formulae. This does not take residuals into account.
The function
type_a.line_fit()
performs a regression analysis that evaluates uncertainty in the parameter estimates using the residuals.If appropriate, the results from both type-A and type-B analyses can be merged (see
type_a.merge()
).Example:
>>> a0 =10 >>> b0 = -3 >>> u0 = .2 >>> x = [ float(x_i) for x_i in xrange(10) ] >>> y = [ ureal(b0*x_i + a0,u0) for x_i in x ] >>> a,b = tb.line_fit(x,y).a_b >>> a ureal(10.0,0.1175507627290...,inf) >>> b ureal(-3.0,0.02201927530252...,inf)
- line_fit_wls(x, y, u_y=None)
Weighted least-squares linear regression
New in version 1.2.
- Parameters:
x – sequence of independent variable data
y – sequence of dependent variable data
u_y – sequence of uncertainties in
y
- Return type:
y
must be a sequence of uncertain real numbers.Performs a weighted least-squares regression.
Weights are calculated from the uncertainty of the
y
elements unless the sequenceu_y
is provided.Note
The uncertainty in the parameter estimates is found by propagation of uncertainty through the regression formulae. This does not take account of the residuals.
The function
type_a.line_fit_wls()
can be used to carry out a regression analysis that obtains uncertainty in the parameter estimates due to the residuals.If necessary, the results of both type-A and type-B analyses can be merged (see
type_a.merge()
).Example:
>>> x = [1,2,3,4,5,6] >>> y = [3.2, 4.3, 7.6, 8.6, 11.7, 12.8] >>> u_y = [0.5,0.5,0.5,1.0,1.0,1.0] >>> y = [ ureal(y_i,u_y_i) for y_i, u_y_i in zip(y,u_y) ] >>> fit = type_b.line_fit_wls(x,y) >>> a, b = fit.a_b >>> a ureal(0.8852320675105...,0.5297081435088...,inf) >>> b ureal(2.0569620253164...,0.1778920167412...,inf)
- line_fit_wtls(x, y, u_x=None, u_y=None, a_b=None, r_xy=None)
Perform straight-line regression with uncertainty in
x
andy
New in version 1.2.
- Parameters:
x – list of uncertain real numbers for the independent variable
y – list of uncertain real numbers for the dependent variable
u_x – a sequence of uncertainties for the
x
datau_y – a sequence of uncertainties for the
y
dataa_b – a pair of initial estimates for the intercept and slope
r_xy – correlation between x-y pairs [default: 0]
Returns a
LineFitWTLS
objectThe elements of
x
andy
must be uncertain numbers with non-zero uncertainties. If specified, the optional argumentsu_x
andu_y
will be used uncertainties to weight the data for the regression, otherwise the uncertainties of the uncertain numbers in the sequences are used.The optional argument
a_b
can be used to provide a pair of initial estimates for the intercept and slope. Otherwise, initial estimates will be obtained by calling line_fit_wls.Implements a Weighted Total Least Squares algorithm that allows for correlation between x-y pairs. See reference:
M Krystek and M Anton, Meas. Sci. Technol. 22 (2011) 035101 (9pp)
Example:
# Pearson-York test data # see, e.g., Lybanon, M. in Am. J. Phys 52 (1), January 1984 >>> xin=[0.0,0.9,1.8,2.6,3.3,4.4,5.2,6.1,6.5,7.4] >>> wx=[1000.0,1000.0,500.0,800.0,200.0,80.0,60.0,20.0,1.8,1.0] >>> yin=[5.9,5.4,4.4,4.6,3.5,3.7,2.8,2.8,2.4,1.5] >>> wy=[1.0,1.8,4.0,8.0,20.0,20.0,70.0,70.0,100.0,500.0] # Convert weights to standard uncertainties >>> uxin=[1./math.sqrt(wx_i) for wx_i in wx ] >>> uyin=[1./math.sqrt(wy_i) for wy_i in wy ] # Define uncertain numbers >>> x = [ ureal(xin_i,uxin_i) for xin_i,uxin_i in zip(xin,uxin) ] >>> y = [ ureal(yin_i,uyin_i) for yin_i,uyin_i in zip(yin,uyin) ] # TLS returns uncertain numbers >>> a,b = type_b.line_fit_wtls(x,y).a_b >>> a ureal(5.47991018...,0.29193349...,inf) >>> b ureal(-0.48053339...,0.057616740...,inf)
- class LineFitOLS(a, b, ssr, N)
Class to hold results from an ordinary linear regression to data.
New in version 1.2.
- property N
The number of points in the sample
- property a_b
Return the intercept
a
and slopeb
as a tuple of uncertain numbers
- property intercept
Return the intercept as an uncertain number.
- property slope
Return the slope as an uncertain number.
- property ssr
Sum of the squared residuals
The sum of the squared deviations between values predicted by the model and the actual data.
If weights are used during the fit, the squares of weighted deviations are summed.
- x_from_y(yseq, x_label=None)
Estimate the stimulus
x
corresponding to the responses inyseq
- Parameters:
yseq – a sequence of further observations of
y
x_label – a label for the return uncertain number x
The items in
yseq
must be uncertain real numbers.Note
When
x_label
is defined, the uncertain number returned will be declared an intermediate result (usingresult()
)
- y_from_x(x, y_label=None)
Return an uncertain number
y
that predicts the response tox
- Parameters:
x – an uncertain real number
y_label – a label for the return uncertain number y
This is a prediction of a single future response
y
to a stimulusx
Note
When
y_label
is defined, the uncertain number returned will be declared an intermediate result (usingresult()
)
- class LineFitWLS(a, b, ssr, N)
This object holds results from a weighted LS linear regression to data.
New in version 1.2.
- property N
The number of points in the sample
- property a_b
Return the intercept
a
and slopeb
as a tuple of uncertain numbers
- property intercept
Return the intercept as an uncertain number.
- property slope
Return the slope as an uncertain number.
- property ssr
Sum of the squared residuals
The sum of the squared deviations between values predicted by the model and the actual data.
If weights are used during the fit, the squares of weighted deviations are summed.
- x_from_y(yseq, x_label=None)
Estimate the stimulus
x
corresponding to the responses inyseq
- Parameters:
yseq – a sequence of further observations of
y
x_label – a label for the return uncertain number x
The items in
yseq
must be uncertain real numbers.Note
When
x_label
is defined, the uncertain number returned will be declared an intermediate result (usingresult()
)
- y_from_x(x, y_label=None)
Return an uncertain number
y
that predicts the response tox
- Parameters:
x – an uncertain real number
y_label – a label for the return uncertain number y
This is a prediction of a single future response
y
to a stimulusx
Note
When
y_label
is defined, the uncertain number returned will be declared an intermediate result (usingresult()
)
- class LineFitWTLS(a, b, ssr, N)
This object holds results from a TLS linear regression to data.
New in version 1.2.
- property N
The number of points in the sample
- property a_b
Return the intercept
a
and slopeb
as a tuple of uncertain numbers
- property intercept
Return the intercept as an uncertain number.
- property slope
Return the slope as an uncertain number.
- property ssr
Sum of the squared residuals
The sum of the squared deviations between values predicted by the model and the actual data.
If weights are used during the fit, the squares of weighted deviations are summed.
- uniform(a)
Return the standard uncertainty for a uniform distribution.
- Parameters:
a (float) – the half-width
Example:
>>> x = ureal(1,type_b.uniform(1)) >>> x ureal(1.0,0.5773502691896258,inf)
- triangular(a)
Return the standard uncertainty for a triangular distribution.
- Parameters:
a (float) – the half-width
Example:
>>> x = ureal(1,type_b.triangular(1)) >>> x ureal(1.0,0.4082482904638631,inf)
- u_shaped(a)
Return the standard uncertainty for an arcsine distribution.
- Parameters:
a (float) – the half-width
Example:
>>> x = ureal(1,type_b.arcsine(1)) >>> x ureal(1.0,0.7071067811865475,inf)
- arcsine(a)
Return the standard uncertainty for an arcsine distribution.
- Parameters:
a (float) – the half-width
Example:
>>> x = ureal(1,type_b.arcsine(1)) >>> x ureal(1.0,0.7071067811865475,inf)
- uniform_ring(a)
Return the standard uncertainty for a uniform ring
- Parameters:
a (float) – the radius
Convert the radius of a uniform ring distribution
a
to a standard uncertaintySee reference: B D Hall, Metrologia 48 (2011) 324-332
Example:
>>> z = ucomplex( 0, type_b.uniform_ring(1) ) >>> z ucomplex((0+0j), u=[0.7071067811865475,0.7071067811865475], r=0.0, df=inf)
- uniform_disk(a)
Return the standard uncertainty for a uniform disk
- Parameters:
a (float) – the radius
Convert the radius of a uniform disk distribution
a
to a standard uncertainty.See reference: B D Hall, Metrologia 48 (2011) 324-332
Example:
>>> z = ucomplex( 0, type_b.uniform_disk(1) ) >>> z ucomplex((0+0j), u=[0.5,0.5], r=0.0, df=inf)
- unknown_phase_product(u1, u2)
Return the standard uncertainty for a product when phases are unknown
- Parameters:
u1 – the standard uncertainty of the first multiplicand
u2 – the standard uncertainty of the second multiplicand
Obtains the standard uncertainty associated with a complex product when estimates have unknown phase.
The arguments
u1
andu2
are the standard uncertainties associated with each multiplicand.See reference: B D Hall, Metrologia 48 (2011) 324-332
Example:
# X = Gamma1 * Gamma2 >>> X = ucomplex( 0, type_b.unknown_phase_product(.1,.1) ) >>> X ucomplex((0+0j), u=[0.014142135623730954,0.014142135623730954], r=0.0, df=inf)
- distribution
Maps
distribution[name]
\(\rightarrow\) function for standard uncertaintyExample:
>>> type_b.distribution['arcsine'](1.5) 1.06066017177982...