Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

llsqwt.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "include/llsqwt.h"

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
 

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

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

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

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

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

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

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.


Generated on Fri Jan 21 16:17:41 2005 for libPET by  doxygen 1.4.1