SDDSlib
Loading...
Searching...
No Matches
linfit.c File Reference

Provides routines for performing simple linear fits. More...

#include "mdb.h"

Go to the source code of this file.

Functions

long unweightedLinearFit (double *xData, double *yData, long nData, double *slope, double *intercept, double *variance)
 Performs an unweighted linear fit on the provided data.
 
long unweightedLinearFitSelect (double *xData, double *yData, short *select, long nData, double *slope, double *intercept, double *variance)
 Performs an unweighted linear fit on the provided data with optional data point selection.
 

Detailed Description

Provides routines for performing simple linear fits.

This file contains functions to perform unweighted linear regression on datasets. It includes functions to compute the slope, intercept, and variance of the best-fit line.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file linfit.c.

Function Documentation

◆ unweightedLinearFit()

long unweightedLinearFit ( double * xData,
double * yData,
long nData,
double * slope,
double * intercept,
double * variance )

Performs an unweighted linear fit on the provided data.

This function computes the slope and intercept of the best-fit line for the given x and y data by invoking the select-based linear fit function without selecting specific data points.

Parameters
xDataPointer to the array of x-values.
yDataPointer to the array of y-values.
nDataNumber of data points in the arrays.
slopePointer to store the computed slope of the best-fit line.
interceptPointer to store the computed intercept of the best-fit line.
variancePointer to store the computed variance of the fit.
Returns
1 on successful fit,
0 if the fit could not be performed (e.g., insufficient data).

Definition at line 37 of file linfit.c.

38 {
39 return unweightedLinearFitSelect(xData, yData, NULL, nData, slope, intercept, variance);
40}
long unweightedLinearFitSelect(double *xData, double *yData, short *select, long nData, double *slope, double *intercept, double *variance)
Performs an unweighted linear fit on the provided data with optional data point selection.
Definition linfit.c:60

◆ unweightedLinearFitSelect()

long unweightedLinearFitSelect ( double * xData,
double * yData,
short * select,
long nData,
double * slope,
double * intercept,
double * variance )

Performs an unweighted linear fit on the provided data with optional data point selection.

This function computes the slope and intercept of the best-fit line for the given x and y data, optionally considering only selected data points as indicated by the select array. It also calculates the variance of the residuals from the fit.

Parameters
xDataPointer to the array of x-values.
yDataPointer to the array of y-values.
selectPointer to an array of selection flags (non-zero to include the data point). Can be NULL to include all points.
nDataNumber of data points in the arrays.
slopePointer to store the computed slope of the best-fit line.
interceptPointer to store the computed intercept of the best-fit line.
variancePointer to store the computed variance of the fit.
Returns
1 on successful fit,
0 if the fit could not be performed (e.g., insufficient data or no variation in x-data).

Definition at line 60 of file linfit.c.

61 {
62 long i;
63 double x, y, sum_x2, sum_x, sum_xy, sum_y, D, residual1, nUsed;
64
65 if (nData < 2)
66 return 0;
67
68 /* compute linear fit and associated parameters */
69 /* linear fit to y = a + bx:
70 a = (S x^2 Sy - S x S xy)/D
71 b = (N S xy - Sx Sy)/D
72 D = N S x^2 - (S x)^2
73 */
74 sum_x = sum_x2 = sum_y = sum_xy = 0;
75 nUsed = 0;
76 for (i = 0; i < nData; i++) {
77 if (!select || select[i]) {
78 nUsed++;
79 sum_x += (x = xData[i]);
80 sum_x2 += x * x;
81 sum_y += (y = yData[i]);
82 sum_xy += x * y;
83 }
84 }
85 if (nUsed < 2)
86 return 0;
87
88 if ((D = nUsed * sum_x2 - sum_x * sum_x)) {
89 *slope = (nUsed * sum_xy - sum_x * sum_y) / D;
90 *intercept = (sum_x2 * sum_y - sum_x * sum_xy) / D;
91 *variance = 0;
92 for (i = 0; i < nData; i++) {
93 if (!select || select[i]) {
94 residual1 = (yData[i] - (xData[i] * (*slope) + (*intercept)));
95 *variance += residual1 * residual1;
96 }
97 }
98 if (nUsed > 2)
99 *variance /= (nUsed - 2);
100 else
101 *variance = DBL_MAX;
102 return 1;
103 } else
104 return 0;
105}