MISR Toolkit  1.5.1
MtkLinearRegressionCalc.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkLinearRegressionCalc =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2008, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrRegression.h"
18 #include "MisrUtil.h"
19 #include <stdlib.h>
20 #include <math.h>
21 
40  int Size,
41  const double *X,
42  const double *Y,
43  const double *Y_Sigma,
44  double *A,
45  double *B,
46  double *Correlation
47 )
48 {
49  MTKt_status status_code; /* Return status of this function */
50  double s, sx, sxx, sxy, sy, syy, delta;
51  double pop_sd_x, pop_sd_y;
52  int i;
53 
54  /* -------------------------------------------------------------- */
55  /* Argument check: Size < 1 */
56  /* -------------------------------------------------------------- */
57 
58  if (Size < 1) {
60  }
61 
62  /* -------------------------------------------------------------- */
63  /* Argument check: X = NULL */
64  /* -------------------------------------------------------------- */
65 
66  if (X == NULL) {
68  }
69 
70  /* -------------------------------------------------------------- */
71  /* Argument check: Y = NULL */
72  /* -------------------------------------------------------------- */
73 
74  if (Y == NULL) {
76  }
77 
78  /* -------------------------------------------------------------- */
79  /* Argument check: Y_Sigma = NULL */
80  /* -------------------------------------------------------------- */
81 
82  if (Y_Sigma == NULL) {
84  }
85 
86  /* -------------------------------------------------------------- */
87  /* Argument check: A = NULL */
88  /* -------------------------------------------------------------- */
89 
90  if (A == NULL) {
92  }
93 
94  /* -------------------------------------------------------------- */
95  /* Argument check: B = NULL */
96  /* -------------------------------------------------------------- */
97 
98  if (B == NULL) {
100  }
101 
102  /* -------------------------------------------------------------- */
103  /* Argument check: Correlation = NULL */
104  /* -------------------------------------------------------------- */
105 
106  if (Correlation == NULL)
108 
109  /* -------------------------------------------------------------- */
110  /* Calculate linear regression */
111  /* Argument check: Y_Sigma[x] <= 0.0 */
112  /* -------------------------------------------------------------- */
113 
114  s = 0;
115  sx = 0;
116  sy = 0;
117  sxx = 0;
118  sxy = 0;
119  syy = 0;
120  for (i = 0 ; i < Size ; i++) {
121  double t;
122 
123  if (Y_Sigma[i] <= 0.0) {
125  }
126 
127  t = 1.0 / (Y_Sigma[i] * Y_Sigma[i]);
128  s += 1.0 * t;
129  sx += X[i] * t;
130  sy += Y[i] * t;
131  sxx += (X[i] * X[i]) * t;
132  sxy += (X[i] * Y[i]) * t;
133  syy += (Y[i] * Y[i]) * t;
134  }
135 
136  delta = s * sxx - sx * sx;
137 
138  if (delta == 0.0) {
140  }
141 
142  pop_sd_x = sqrt(s*sxx-sx*sx);
143  pop_sd_y = sqrt(s*syy-sy*sy);
144 
145  if (pop_sd_x == 0.0 || pop_sd_y == 0.0) {
147  }
148 
149  *A = (sxx * sy - sx * sxy) / delta;
150  *B = (s * sxy - sx * sy) / delta;
151 
152  *Correlation = ( ((s * sxy) - (sx * sy)) /
153  ( pop_sd_x * pop_sd_y )
154  );
155 
156  return MTK_SUCCESS;
157 
158 ERROR_HANDLE:
159  return status_code;
160 }
MTKt_status MtkLinearRegressionCalc(int Size, const double *X, const double *Y, const double *Y_Sigma, double *A, double *B, double *Correlation)
Use linear regression to fit a set of observations (x,y) to the model: y(x) = a + b * x...
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
double sx
Definition: misr_init.c:11
MTKt_status
Definition: MisrError.h:11
double sy
Definition: misr_init.c:12

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51