Package reflectometry :: Package reduction :: Module wsolve

Module wsolve

source code

Solve a potentially over-determined system with uncertainty in the values.

Given: A x = y +/- dy Use: s = wsolve(A,y,dy)

wsolve uses the singular value decomposition for increased accuracy. Estimates the uncertainty for the solution from the scatter in the data.

The returned model object s provides:

s.x solution s.std uncertainty estimate assuming no correlation s.rnorm residual norm s.DoF degrees of freedom s.cov covariance matrix s.ci(p) confidence intervals at point p s.pi(p) prediction intervals at point p s(p) predicted value at point p

Example

Weighted system:

import numpy,wsolve
A = numpy.matrix("1,2,3;2,1,3;1,1,1",'d').A
xin = numpy.array([1,2,3],'d')
dy = numpy.array([0.2,0.01,0.1])
y = numpy.random.normal(numpy.dot(A,xin),dy)
print A,y,dy
s = wsolve.wsolve(A,y,dy)
print "xin,x,dx", xin, s.x, s.std

Note there is a counter-intuitive result that scaling the estimated uncertainty in the data does not affect the computed uncertainty in the fit. This is the correct result --- if the data were indeed selected from a process with ten times the uncertainty, you would expect the scatter in the data to increase by a factor of ten as well. When this new data set is fitted, it will show a computed uncertainty increased by the same factor. Monte carlo simulations bear this out. The conclusion is that the dataset carries its own information about the variance in the data, and the weight vector serves only to provide relative weighting between the points.

Classes
  LinearModel
Model evaluator for linear solution to Ax = y.
  PolynomialModel
Model evaluator for best fit polynomial p(x) = y.
Functions
 
wsolve(A, y, dy=1, rcond=1e-12)
Given a linear system y = A*x + e(dy), estimates x,dx
source code
 
wpolyfit(x, y, dy=1, degree=None, origin=False)
Return the polynomial of degree n that minimizes sum( (p(x_i) - y_i)**2/dy_i**2).
source code
 
demo() source code
 
test()
smoke test...make sure the function continues to return the same result for a particular system.
source code
Function Details

wsolve(A, y, dy=1, rcond=1e-12)

source code 

Given a linear system y = A*x + e(dy), estimates x,dx

A is an n x m array y is an n x k array or vector of length n dy is a scalar or an n x 1 array x is a m x k array

wpolyfit(x, y, dy=1, degree=None, origin=False)

source code 

Return the polynomial of degree n that minimizes sum( (p(x_i) - y_i)**2/dy_i**2).

if origin is True, the fit should go through the origin.