2 *------------------------------------------------------------------
3 * Copyright (c) 2006-2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* see "Numerical Recipies in C, 2nd ed." p 665 */
24 * Linear regression of (xi, yi), returns parameters for least-squares
25 * fit y = a + bx. Also, compute Pearson's R.
27 void linreg (double *x, double *y, int nitems, double *a, double *b,
28 double *minp, double *maxp, double *meanp, double *r)
35 double ss, meanx, meany, t;
36 double errx, erry, prodsum, sqerrx, sqerry;
41 for (i = 0; i < nitems; i++) {
51 meany = *meanp = sy / ss;
55 for (i = 0; i < nitems; i++) {
68 /* Compute numerator of Pearson's R */
69 for (i = 0; i < nitems; i++) {
72 prodsum += errx * erry;
77 *r = prodsum / (sqrt(sqerrx)*sqrt(sqerry));