File: lin_reg.c

package info (click to toggle)
bing 1.3.5-5
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 456 kB
  • sloc: ansic: 3,774; makefile: 51
file content (161 lines) | stat: -rw-r--r-- 4,208 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *		Unofficial release 1.3.0
 *	        	B I N G
 *
 */

/* $Id: lin_reg.c,v 1.4 1999/10/16 07:07:58 fgouget Exp $ */

#include <math.h>
#include <float.h>

#include "lin_reg.h"

/*
 * The linear regression stuff.
 * The basic formula used to do the linear regression are given below but
 * for more details you should see a maths book.
 *
 * We have a set of n samples (xi,yi). Then if we write
 *
 *    y = a * x + b
 *
 * the a and b that best approximate the samples are given by:
 *
 *    a = Covariance ( xi , yi ) / Variance ( xi )
 *
 *    b = Covariance ( xi , yi ) / Variance (xi) * Mean ( xi ) - Mean ( yi )
 *
 * Note that in bing we only care about a.
 *
 * To compute a we maintain the variables below:
 *
 *    sum_x =  Sum ( xi )
 *    sum_x2 = Sum ( xi * xi )
 *    sum_y =  Sum ( yi )
 *    sum_xy = Sum ( xi * yi )
 *
 * And then the covariance and variance can be computed as:
 *
 *    Covariance ( xi , yi ) = ( sum_xy - sum_x * sum_y / n ) / ( n - 1 )
 *
 *    Variance ( xi ) = ( sum_x2 - sum_x * sum_x / n ) / ( n - 1 )
 *
 * Thus:
 *
 *      n * sum_xy - sum_x * sum_y
 * a = ----------------------------
 *      n * sum_x2 - sum_x * sum_x
 *
 *      sum_x2 * sum_y - sum_x * sum_xy
 * b = ---------------------------------
 *         n * sum_x2 - sum_x * sum_x
 *
 *
 * We also compute the correlation value so that we know whether the stats 
 * indeed look like a line or not. The correlation is computed from the 
 * following formula:
 *
 * Corr (xi , yi ) = Covariance ( xi , yi ) / Sqrt ( Variance ( xi ) / Variance ( yi ) )
 *
 * i.e.
 *
 *                                          n * sum_xy - sum_x * sum_y
 * Corr (xi , yi ) = -------------------------------------------------------------------------
 *                    Sqrt( ( n * sum_x2 - sum_x * sum_x ) * ( n * sum_y2 - sum_y * sum_y ) )
 *
 */

void linreg_update(linreg_t* linreg)
{
    if (linreg->nb_samples>1) {
        /*
         * This function computes the end result from the computed 
         * intermediate statistics.
         */
        linreg->a=(linreg->sum_xy-linreg->sum_x*linreg->sum_y/linreg->nb_samples)/
                  (linreg->sum_x2-linreg->sum_x*linreg->sum_x/linreg->nb_samples);
        linreg->b=(linreg->sum_x2*linreg->sum_y-linreg->sum_x*linreg->sum_xy)/
                  (linreg->nb_samples*linreg->sum_x2-linreg->sum_x*linreg->sum_x);
        linreg->corr=(linreg->sum_xy-linreg->sum_x*linreg->sum_y/linreg->nb_samples)/
                     sqrt((linreg->sum_x2-linreg->sum_x*linreg->sum_x/linreg->nb_samples) *
                          (linreg->sum_y2-linreg->sum_y*linreg->sum_y/linreg->nb_samples)
                         );
    } else {
        linreg->a=0;
        linreg->b=0;
        linreg->corr=0;
    }
}

int linreg_init(linreg_t *linreg)
{
    linreg->nb_samples=0;
    linreg->sum_x=0.0;
    linreg->sum_y=0.0;
    linreg->sum_x2=0.0;
    linreg->sum_y2=0.0;
    linreg->sum_xy=0.0;
    linreg->a=0.0;
    linreg->b=0.0;
    linreg->corr=0.0;

    return 0;
}

int linreg_add_all(linreg_t *linreg,int  nb_samples, int* x, double* y, int update)
{
    int i;
    
    /* Initialise the results data structure */
    for (i=0;i<nb_samples;i++) {
        if (y[i]==DBL_MAX)
            continue;
        linreg->sum_x+=x[i];
        linreg->sum_y+=y[i];
        linreg->sum_x2+=x[i]*x[i];
        linreg->sum_y2+=y[i]*y[i];
        linreg->sum_xy+=x[i]*y[i];
        linreg->nb_samples++;
    }
    
    /* Update the regression results */
    if (update!=0)
        linreg_update(linreg);

    return 0;
}


int linreg_del_sample(linreg_t *linreg, int x, double y, int update)
{
    linreg->nb_samples--;
    linreg->sum_x-=x;
    linreg->sum_y-=y;
    linreg->sum_x2-=x*x;
    linreg->sum_y2-=y*y;
    linreg->sum_xy-=x*y;
    
    /* Update the regression results */
    if (update!=0)
        linreg_update(linreg);

    return 0;
}

int linreg_add_sample(linreg_t *linreg, int x, double y, int update)
{
    linreg->nb_samples++;
    linreg->sum_x+=x;
    linreg->sum_y+=y;
    linreg->sum_x2+=x*x;
    linreg->sum_y2+=y*y;
    linreg->sum_xy+=x*y;

    /* Update the regression results */
    if (update!=0)
        linreg_update(linreg);

    return 0;
}