SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
linfit.c
Go to the documentation of this file.
1/**
2 * @file linfit.c
3 * @brief Provides routines for performing simple linear fits.
4 *
5 * This file contains functions to perform unweighted linear regression on datasets.
6 * It includes functions to compute the slope, intercept, and variance of the best-fit line.
7 *
8 * @copyright
9 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
10 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
11 *
12 * @license
13 * This file is distributed under the terms of the Software License Agreement
14 * found in the file LICENSE included with this distribution.
15 *
16 * @author M. Borland, C. Saunders, R. Soliday
17 */
18
19#include "mdb.h"
20
21/**
22 * @brief Performs an unweighted linear fit on the provided data.
23 *
24 * This function computes the slope and intercept of the best-fit line for the given x and y data
25 * by invoking the select-based linear fit function without selecting specific data points.
26 *
27 * @param xData Pointer to the array of x-values.
28 * @param yData Pointer to the array of y-values.
29 * @param nData Number of data points in the arrays.
30 * @param slope Pointer to store the computed slope of the best-fit line.
31 * @param intercept Pointer to store the computed intercept of the best-fit line.
32 * @param variance Pointer to store the computed variance of the fit.
33 * @return
34 * @return 1 on successful fit,
35 * @return 0 if the fit could not be performed (e.g., insufficient data).
36 */
37long unweightedLinearFit(double *xData, double *yData, long nData,
38 double *slope, double *intercept, double *variance) {
39 return unweightedLinearFitSelect(xData, yData, NULL, nData, slope, intercept, variance);
40}
41
42/**
43 * @brief Performs an unweighted linear fit on the provided data with optional data point selection.
44 *
45 * This function computes the slope and intercept of the best-fit line for the given x and y data,
46 * optionally considering only selected data points as indicated by the select array.
47 * It also calculates the variance of the residuals from the fit.
48 *
49 * @param xData Pointer to the array of x-values.
50 * @param yData Pointer to the array of y-values.
51 * @param select Pointer to an array of selection flags (non-zero to include the data point). Can be NULL to include all points.
52 * @param nData Number of data points in the arrays.
53 * @param slope Pointer to store the computed slope of the best-fit line.
54 * @param intercept Pointer to store the computed intercept of the best-fit line.
55 * @param variance Pointer to store the computed variance of the fit.
56 * @return
57 * @return 1 on successful fit,
58 * @return 0 if the fit could not be performed (e.g., insufficient data or no variation in x-data).
59 */
60long unweightedLinearFitSelect(double *xData, double *yData, short *select, long nData,
61 double *slope, double *intercept, double *variance) {
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}
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
long unweightedLinearFit(double *xData, double *yData, long nData, double *slope, double *intercept, double *variance)
Performs an unweighted linear fit on the provided data.
Definition linfit.c:37