Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python scipy.odrpack.odr example (with sample input / output)?

Tags:

python

scipy

I am a satisfied user of scipy.optimize.leastsq.

I now have -- really have always had -- x,y data with variable error bars, and it looks like scipy.odrpack.odr is what I need to use to respect the greater uncertainty in some of the data.

Unfortunately, I cannot find an online tutorial which includes sample code with sample input and output. (I am trying to make this as easy as possible.)

I would appreciate it if somebody could post sample code with sample I/O. This would be easy for somebody who uses the routine a lot.

Thanks! Bill

like image 263
user1579230 Avatar asked Apr 21 '13 00:04

user1579230


1 Answers

This is a fleshed-out version of the example in the docs:

import numpy as np
import scipy.odr.odrpack as odrpack
np.random.seed(1)

N = 100
x = np.linspace(0,10,N)
y = 3*x - 1 + np.random.random(N)
sx = np.random.random(N)
sy = np.random.random(N)

def f(B, x):
    return B[0]*x + B[1]
linear = odrpack.Model(f)
# mydata = odrpack.Data(x, y, wd=1./np.power(sx,2), we=1./np.power(sy,2))
mydata = odrpack.RealData(x, y, sx=sx, sy=sy)

myodr = odrpack.ODR(mydata, linear, beta0=[1., 2.])
myoutput = myodr.run()
myoutput.pprint()
# Beta: [ 3.02012862 -0.63168734]
# Beta Std Error: [ 0.01188347  0.05616458]
# Beta Covariance: [[ 0.00067276 -0.00267082]
#  [-0.00267082  0.01502792]]
# Residual Variance: 0.209906660703
# Inverse Condition #: 0.105981202542
# Reason(s) for Halting:
#   Sum of squares convergence
like image 91
unutbu Avatar answered Oct 29 '22 12:10

unutbu