TPCCLIB
|
Linear least-squares fit with errors in both coordinates. More...
#include "libtpcmodel.h"
Go to the source code of this file.
Functions | |
int | llsqwt (double *x, double *y, int n, double *wx, double *wy, double tol, double *w, double *ic, double *slope, double *nwss, double *sic, double *sslope, double *cx, double *cy) |
int | best_llsqwt (double *x, double *y, double *wx, double *wy, int nr, int min_nr, int mode, double *slope, double *ic, double *nwss, double *sslope, double *sic, double *cx, double *cy, int *bnr) |
int | llsqperp (double *x, double *y, int nr, double *slope, double *ic, double *ssd) |
int | llsqperp3 (double *x, double *y, int nr, double *slope, double *ic, double *ssd) |
int | quadratic (double a, double b, double c, double *m1, double *m2) |
int | medianline (double *x, double *y, int nr, double *slope, double *ic) |
Linear least-squares fit with errors in both coordinates.
Definition in file llsqwt.c.
int best_llsqwt | ( | double * | x, |
double * | y, | ||
double * | wx, | ||
double * | wy, | ||
int | nr, | ||
int | min_nr, | ||
int | mode, | ||
double * | slope, | ||
double * | ic, | ||
double * | nwss, | ||
double * | sslope, | ||
double * | sic, | ||
double * | cx, | ||
double * | cy, | ||
int * | bnr ) |
Finds the best least-squares line to (x,y)-data, leaving points out either from the beginning (mode=0) or from the end (mode=1).
x | Plot x axis values. |
y | Plot y axis values. |
wx | Weighting factors for x. |
wy | Weighting factors for y. |
nr | Nr of plot data points. |
min_nr | Min nr of points to use in the fit; must be >=4. |
mode | Leave out points from beginning (0) or from end (1). |
slope | Slope is returned in here. |
ic | Y axis intercept is returned in here. |
nwss | sqrt(WSS)/wsum is returned here. |
sslope | Expected sd of slope at calculated points. |
sic | Expected sd of intercept at calculated points. |
cx | Calculated x data points. |
cy | Calculated y data points. |
bnr | Number of points in the best fit (incl data with w=0). |
Definition at line 294 of file llsqwt.c.
int llsqperp | ( | double * | x, |
double * | y, | ||
int | nr, | ||
double * | slope, | ||
double * | ic, | ||
double * | ssd ) |
Simple non-iterative perpendicular line fitting. This function is fully based on the article [1].
References:
x | Coordinates of data points (dimension nr). |
y | Coordinates of data points (dimension nr). |
nr | Number of data points. |
slope | Estimated slope. |
ic | Estimated intercept. |
ssd | Sum of squared distances / nr. |
Definition at line 382 of file llsqwt.c.
Referenced by img_logan(), img_patlak(), llsqperp3(), and mtga_best_perp().
int llsqperp3 | ( | double * | x, |
double * | y, | ||
int | nr, | ||
double * | slope, | ||
double * | ic, | ||
double * | ssd ) |
Simple non-iterative perpendicular line fitting. This version of the function accepts data that contains NaN's.
x | Coordinates of data points (dimension nr). |
y | Coordinates of data points (dimension nr). |
nr | Number of data points. |
slope | Estimated slope. |
ic | Estimated intercept. |
ssd | Sum of squared distances / nr. |
Definition at line 453 of file llsqwt.c.
int llsqwt | ( | double * | x, |
double * | y, | ||
int | n, | ||
double * | wx, | ||
double * | wy, | ||
double | tol, | ||
double * | w, | ||
double * | ic, | ||
double * | slope, | ||
double * | nwss, | ||
double * | sic, | ||
double * | sslope, | ||
double * | cx, | ||
double * | cy ) |
Iterative method for linear least-squares fit with errors in both coordinates.
This function is fully based on article [3].
For n data-point pairs (x[i], y[i]) each point has its own weighting factors in (wx[i], wy[i]). This routine finds the values of the parameters m (slope) and c (intercept, ic) that yield the "best-fit" of the model equation Y = mX + c to the data, where X and Y are the predicted or calculated values of the data points.
Weighting factors wx and wy must be assigned as the inverses of the variances or squares of the measurement uncertainties (SDs), i.e. w[i]=1/(sd[i])^2
If true weights are unknown but yet the relative weights are correct, the slope, intercept and residuals (WSS) will be correct. The applied term S/(N-2) makes also the estimate of sigma (sd) of slope less dependent on the scaling of weights. The sigmas are not exact, since only the lowest-order terms in Taylor-series expansion are incorporated; anyhow sigmas are more accurate than the ones based on York algorithm.
One or more data points can be excluded from the fit by setting either x or y weight to 0.
References:
x | coordinates of data points (of dimension n). |
y | coordinates of data points (of dimension n). |
n | number of data points. |
wx | weighting factors in x. |
wy | weighting factors in y. |
tol | allowed tolerance in slope estimation. |
w | work vector (of dimension n); effective weights w[i] are returned in it. |
ic | Estimated intercept. |
slope | Estimated slope. |
nwss | sqrt(WSS)/wsum of the residuals. |
sic | expected sd of intercept at calculated points; If NULL, then not calculated and variable is left unchanged. |
sslope | Expected sd of slope at calculated points; If NULL, then not calculated and variable is left unchanged. |
cx | Estimated data points (X,y); If NULL, then not calculated and variable is left unchanged. |
cy | Estimated data points (x,Y); If NULL, then not calculated and variable is left unchanged. |
Definition at line 48 of file llsqwt.c.
Referenced by best_llsqwt(), and best_logan_reed().
int medianline | ( | double * | x, |
double * | y, | ||
int | nr, | ||
double * | slope, | ||
double * | ic ) |
Median-based distribution-free estimation of slope and intercept. This method has no need for weighting and is insensitive to outliers. Note that this is not LMS !
Reference (containing reference to the original idea):
x | Coordinates of data points (dimension nr). |
y | Coordinates of data points (dimension nr). |
nr | Number of data points. |
slope | Estimated slope. |
ic | Estimated intercept. |
Definition at line 533 of file llsqwt.c.
int quadratic | ( | double | a, |
double | b, | ||
double | c, | ||
double * | m1, | ||
double * | m2 ) |
Finds the real roots of a*x^2 + b*x + c = 0
a | Input A |
b | Input B |
c | Input C |
m1 | Output: Root 1 |
m2 | Output: Root 2 |
Definition at line 490 of file llsqwt.c.
Referenced by llsqperp().