libtpcmodel
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Functions
llsqwt.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "include/llsqwt.h"
Include dependency graph for llsqwt.c:

Go to the source code of this file.

Functions

int _medianline_cmp (const void *e1, const void *e2)
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)

Function Documentation

int _medianline_cmp ( const void *  e1,
const void *  e2 
)

Definition at line 588 of file llsqwt.c.

Referenced by medianline().

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).

Returns:
Returns 0, if ok.
Parameters:
xPlot x axis values
yPlot y axis values
wxWeighting factors for x
wyWeighting factors for y
nrNr of plot data points
min_nrMin nr of points to use in the fit; must be >=4
modeLeave out points from beginning (0) or from end (1)
slopeSlope is returned in here
icY axis intercept is returned in here
nwsssqrt(WSS)/wsum is returned here
sslopeExpected sd of slope at calculated points
sicExpected sd of intercept at calculated points
cxCalculated x data points
cyCalculated y data points
bnrNumber of points in the best fit (incl data with w=0)

Definition at line 331 of file llsqwt.c.

References llsqwt(), and LLSQWT_TEST.

Here is the call graph for this function:

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:

  1. Varga J & Szabo Z. Modified regression model for the Logan plot. J Cereb Blood Flow Metab 2002; 22:240-244.
Returns:
If successful, function returns value 0.
Parameters:
xCoordinates of data points (dimension nr)
yCoordinates of data points (dimension nr)
nrNumber of data points
slopeEstimated slope
icEstimated intercept
ssdSum of squared distances / nr

Definition at line 426 of file llsqwt.c.

References LLSQWT_TEST, and quadratic().

Referenced by llsqperp3(), and mtga_best_perp().

Here is the call graph for this function:

int llsqperp3 ( double *  x,
double *  y,
int  nr,
double *  slope,
double *  ic,
double *  ssd 
)

See llsqperp(). This function accepts data that contains NaN's.

Definition at line 490 of file llsqwt.c.

References llsqperp().

Here is the call graph for this function:

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:

  1. York, D. Linear-squares fitting of a straight line. Can. J. Phys. 1966;44:1079-1086.
  2. Lybanon, M. A better least squares method when both variables have uncertainties. Am. J. Phys. 1984;52:22-26 and 276-278.
  3. Reed BC. Linear least-squares fits with errors in both coordinates. II: Comments on parameter variances. Am. J. Phys. 1992;60:59-62.
Returns:
If successful, function returns value 0.
Parameters:
xcoordinates of data points (of dimension n)
ycoordinates of data points (of dimension n)
nnumber of data points
wxweighting factors in x
wyweighting factors in y
tolallowed tolerance in slope estimation
wwork vector (of dimension n); effective weights w[i] are returned in it.
icEstimated intercept
slopeEstimated slope
nwsssqrt(WSS)/wsum of the residuals
sicexpected sd of intercept at calculated points; If NULL, then not calculated and variable is left unchanged
sslopeExpected sd of slope at calculated points; If NULL, then not calculated and variable is left unchanged
cxEstimated data points (X,y); If NULL, then not calculated and variable is left unchanged
cyEstimated data points (x,Y); If NULL, then not calculated and variable is left unchanged

Definition at line 84 of file llsqwt.c.

References LLSQWT_TEST.

Referenced by best_llsqwt().

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):

  1. Siegel AF. Robust regression using repeated medians. Biometrika 1982; 69(1):242-244.
Returns:
If successful, function returns value 0.
Parameters:
xCoordinates of data points (dimension nr)
yCoordinates of data points (dimension nr)
nrNumber of data points
slopeEstimated slope
icEstimated intercept

Definition at line 546 of file llsqwt.c.

References _medianline_cmp(), and LLSQWT_TEST.

Here is the call graph for this function:

int quadratic ( double  a,
double  b,
double  c,
double *  m1,
double *  m2 
)

Finds the real roots of a*x^2 + b*x + c = 0

Returns:
Returns the nr of roots, and the roots in m1 and m2.

Definition at line 513 of file llsqwt.c.

Referenced by llsqperp().