# Fits¶

## Details¶

Collection of fitting functions

fits.demo_ransac()[source]

Find the best-fit circle in an image, using the RANSAC algorithm

fits.fit_circle(x, y)[source]

Determine the best-fit circle to given datapoints.

Parameters: x : array (N,) x-values. y : array (N,) corresponding y-values. center : array (2,) x/y coordinates of center of the circle radius : float Circle radius.

Examples

>>> r = 2
>>> center = np.r_[5,5]
>>> theta = np.r_[0:2*np.pi:10j]
>>> x = r*np.cos(theta)+center[0]
>>> y = r*np.sin(theta)+center[1]
>>> cFit,rFit = thLib.fits.fit_circle(x,y)

fits.fit_ellipse(x, y)[source]

Ellipse fit by Taubin’s Method

Parameters: x : array x-coordinates of the ellipse points y : array y-coordinates of the ellipse points A : array Ellipse parameters A = [a b c d e f] is the vector of algebraic parameters of thefitting ellipse: ax^2 + bxy + cy^2 +dx + ey + f = 0 The vector A is normed, so that ||A||=1.

Notes

Among fast non-iterative ellipse fitting methods, this is perhaps the most accurate and robust.

This method fits a quadratic curve (conic) to a set of points; if points are better approximated by a hyperbola, this fit will return a hyperbola. To fit ellipses only, use “Direct Ellipse Fit”.

Published in G. Taubin, “Estimation Of Planar Curves, Surfaces And Nonplanar Space Curves Defined By Implicit Equations, With Applications To Edge And Range Image Segmentation”, IEEE Trans. PAMI, Vol. 13, pages 1115-1138, (1991)

fits.fit_exp(tFit, yFit, plotFlag=False)[source]

Calculates best-fit parameters for the exponential decay to an offset. This can serve as an example for a general non-linear fit.

Parameters: tFit : array (N,) Time values. yFit : array (N,) Function values offset : float Function offset/bias. amp : float Amplitude of exponential function tau : float Decay time.

Examples

>>> t = np.arange(10)
>>> tau = 2.
>>> amp = 1.
>>> offset = 2.
>>> x = offset + amp*np.exp(-t/tau)
>>> fitted =  thLib.fits.fit_exp(t,x)

fits.fit_line(x, y, alpha=0.05, newx=[], plotFlag=False)[source]

Linear regression fit.

Parameters: x : ndarray Input / Predictor. y : ndarray Input / Estimator. alpha : float Confidence limit [default=0.05] newx : float or ndarray Values for which the fit and the prediction limits are calculated (optional) plotFlag: int, optional 1 = plot, 0 = no_plot [default] a : float Intercept b : float Slope ci : ndarray Lower and upper confidence interval for the slope info : dictionary contains return information on - residuals - var_res - sd_res - alpha - tval - df newy : list(ndarray) Predictions for (newx, newx-ciPrediction, newx+ciPrediction)

Notes

Example data and formulas are taken from D. Altman, “Practical Statistics for Medicine”

Examples

>>> x = np.r_[0:10:11j]
>>> y = x**2
>>> (a,b,(ci_a, ci_b),_) = thLib.fits.fit_line(x,y)
Summary: a=-15.0000+/-12.4590, b=10.0000+/-2.1060
Confidence intervals: ci_a=(-27.4590 - -2.5410), ci_b=(7.8940 - 12.1060)
Residuals: variance = 95.3333, standard deviation = 9.7639
alpha = 0.050, tval = 2.2622, df=9

fits.fit_sin(tList, yList, freq)[source]

Fit a sine wave with a known frequency to a given set of data.

y = amplitude * sin(2*pi*freq * tList + phase*pi/180) + bias

Parameters: yList : array datapoints tList : float time base, in sec freq : float in Hz phase : float in degrees amplitude : float bias : float

Examples

>>> np.random.seed(1234)
>>> t = np.arange(0,10,0.1)
>>> x = 3 + 4*np.sin(2*np.pi*t + 5*np.pi/180) + np.random.randn(len(t))
>>> (phase, amp, offset) = thLib.fits.fit_sin(t, x, 1)

fits.regress(y, X, alpha=0.05, intercept=True)[source]

Multilinear regression and confidence intervals. Note that by default, an intercept is added to the design matrix!

Parameters: X : ndarray (N,) or (N,p) predictors at each of N observations y : ndarray(N,) observed responses alpha : float, optional Defines the 100*(1-alpha)% confidence level in ci. Default alpha=0.05 intercept : boolean If ‘True’, an intercept is automatically added to the design matrix. If ‘False’, the behavior is like the Matlab “regress” function, and no intercept is added. fit : float best fit intercept and regression parameters ci : ndarray (2,) confidence intervals for the coefficient estimates at the given alpha level (default: 95%-level)

Examples

>>> x = np.array([0, 0, 10, 10])
>>> y = np.array([1, 3, 11, 13])
>>> (fit,ci) = thLib.fits.regress(y,x)

>>> X = np.random.randn(10,2)
>>> mat = np.hstack( (np.ones( (len(X),1) ), X) )
>>> pars = np.c_[[2, 4, 5]]
>>> y = mat.dot(pars)
>>> y += 0.1*np.random.randn(*y.shape)
>>> (fit,ci) = thLib.fits.regress(y, mat)