Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/regressionline.cc
[mareframe] / trunk / gadget / regressionline.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/regressionline.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "regressionline.h"
2 :     #include "mathfunc.h"
3 :     #include "errorhandler.h"
4 :     #include "gadget.h"
5 :     #include "global.h"
6 :    
7 :     // ********************************************************
8 :     // Functions for Regression
9 :     // ********************************************************
10 :     Regression::Regression() {
11 :     linetype = FREE;
12 :     error = 1;
13 :     useweights = 0;
14 :     sse = a = b = 0.0;
15 :     }
16 :    
17 :     Regression::Regression(LineType ltype) {
18 :     linetype = ltype;
19 :     error = 0;
20 :     useweights = 0;
21 :     sse = a = b = 0.0;
22 :     }
23 :    
24 :     void Regression::setWeights(const DoubleVector& weights) {
25 :     if (!useweights) {
26 :     handle.logMessage(LOGWARN, "Warning in regression - unexpected use of weights");
27 :     error = 1;
28 :     return;
29 :     }
30 :     w = weights;
31 :     }
32 :    
33 :     double Regression::getSSE() {
34 :     if (error)
35 :     return verybig;
36 :     return sse;
37 :     }
38 :    
39 :     void Regression::calcFit() {
40 :     if (error)
41 :     return;
42 :    
43 :     switch (linetype) {
44 :     case FREE:
45 :     //need to calculate both the slope and the intercept
46 :     this->calcSlopeIntercept();
47 :     break;
48 :     case FIXEDSLOPE:
49 :     //only need to calculate the intercept
50 :     this->calcIntercept();
51 :     break;
52 :     case FIXEDINTERCEPT:
53 :     //only need to calculate the slope
54 :     this->calcSlope();
55 :     break;
56 :     case FIXED:
57 :     //nothing to be done here
58 :     break;
59 :     default:
60 :     handle.logMessage(LOGWARN, "Warning in regression - unrecognised linetype", linetype);
61 :     break;
62 :     }
63 :    
64 :     //finally calculate the SSE value
65 :     if (useweights)
66 :     this->calcSSEWeights();
67 :     else
68 :     this->calcSSE();
69 :     }
70 :    
71 :     void Regression::calcSSE() {
72 :     if (error)
73 :     return;
74 :    
75 :     int i;
76 :     double tmp;
77 :     sse = 0.0;
78 :     for (i = 0; i < x.Size(); i++) {
79 :     tmp = y[i] - (a + b * x[i]);
80 :     sse += tmp * tmp;
81 :     }
82 :     }
83 :    
84 :     void Regression::calcSSEWeights() {
85 :     if (error)
86 :     return;
87 :    
88 :     int i;
89 :     double tmp;
90 :     sse = 0.0;
91 :     for (i = 0; i < x.Size(); i++) {
92 :     tmp = y[i] - (a + b * x[i]);
93 :     sse += w[i] * tmp * tmp;
94 :     }
95 :     }
96 :    
97 :     void Regression::calcSlope() {
98 :     if (error)
99 :     return;
100 :    
101 :     int i;
102 :     double sumX, sumY;
103 :     sumX = sumY = 0.0;
104 :     for (i = 0; i < x.Size(); i++) {
105 :     sumX += x[i];
106 :     sumY += y[i];
107 :     }
108 :     if (isZero(sumX))
109 :     b = 0.0;
110 :     else
111 :     b = (sumY - (a * x.Size())) / sumX;
112 :    
113 :     //JMB - if there is a negative slope for the regression then things are going wrong
114 :     if (b < 0.0) {
115 :     handle.logMessage(LOGWARN, "Warning in regression - negative slope for regression line", b);
116 :     error = 1;
117 :     }
118 :     }
119 :    
120 :     void Regression::calcIntercept() {
121 :     if (error)
122 :     return;
123 :    
124 :     int i;
125 :     double sumX, sumY;
126 :     sumX = sumY = 0.0;
127 :     for (i = 0; i < x.Size(); i++) {
128 :     sumX += x[i];
129 :     sumY += y[i];
130 :     }
131 :     a = (sumY - (b * sumX)) / x.Size();
132 :     }
133 :    
134 :     void Regression::calcSlopeIntercept() {
135 :     if (error)
136 :     return;
137 :    
138 :     int i;
139 :     double sumX, sumY, nom, denom;
140 :     sumX = sumY = nom = denom = 0.0;
141 :     for (i = 0; i < x.Size(); i++) {
142 :     sumX += x[i];
143 :     sumY += y[i];
144 :     }
145 :     sumX /= x.Size();
146 :     sumY /= y.Size();
147 :    
148 :     for (i = 0; i < x.Size(); i++) {
149 :     nom += (x[i] - sumX) * (y[i] - sumY);
150 :     denom += (x[i] - sumX) * (x[i] - sumX);
151 :     }
152 :    
153 :     if (isZero(denom)) {
154 :     b = 0.0;
155 :     a = sumY;
156 :     } else {
157 :     b = nom / denom;
158 :     a = sumY - (b * sumX);
159 :     }
160 :    
161 :     //JMB - if there is a negative slope for the regression then things are going wrong
162 :     if (b < 0.0) {
163 :     handle.logMessage(LOGWARN, "Warning in regression - negative slope for regression line", b);
164 :     error = 1;
165 :     }
166 :     }
167 :    
168 :     // ********************************************************
169 :     // Functions for LinearRegression
170 :     // ********************************************************
171 :     LinearRegression::LinearRegression(LineType ltype) : Regression(ltype) {
172 :     }
173 :    
174 :     void LinearRegression::storeVectors(const DoubleVector& modData, const DoubleVector& obsData) {
175 :     error = 0; //begin by cleaning up error status
176 :     if ((modData.Size() != obsData.Size()) || (modData.Size() < 2)) {
177 :     handle.logMessage(LOGWARN, "Warning in linear regression - invalid vector sizes");
178 :     error = 1;
179 :     return;
180 :     }
181 :     x = modData;
182 :     y = obsData;
183 :     }
184 :    
185 :     // ********************************************************
186 :     // Functions for LogLinearRegression
187 :     // ********************************************************
188 :     LogLinearRegression::LogLinearRegression(LineType ltype) : Regression(ltype) {
189 :     }
190 :    
191 :     void LogLinearRegression::storeVectors(const DoubleVector& modData, const DoubleVector& obsData) {
192 :     error = 0; //begin by cleaning up error status
193 :     if ((modData.Size() != obsData.Size()) || (modData.Size() < 2)) {
194 :     handle.logMessage(LOGWARN, "Warning in log linear regression - invalid vector sizes");
195 :     error = 1;
196 :     return;
197 :     }
198 :    
199 :     x.Reset();
200 :     x.resize(modData.Size(), 0.0);
201 :     y.Reset();
202 :     y.resize(obsData.Size(), 0.0);
203 :    
204 :     int i, l = 0;
205 :     for (i = 0; i < x.Size(); i++, l++) {
206 :     if (isZero(modData[i]) && isZero(obsData[i])) {
207 :     //omit the point (0.0, 0.0)
208 :     x.Delete(l);
209 :     y.Delete(l);
210 :     l--;
211 :     } else if ((modData[i] < verysmall) || (obsData[i] < verysmall)) {
212 :     handle.logMessage(LOGWARN, "Warning in log linear regession - received invalid values");
213 :     error = 1;
214 :     return;
215 :     } else {
216 :     x[l] = log(modData[i]);
217 :     y[l] = log(obsData[i]);
218 :     }
219 :     }
220 :     }
221 :    
222 :     // ********************************************************
223 :     // Functions for WeightRegression
224 :     // ********************************************************
225 :     WeightRegression::WeightRegression(LineType ltype) : LinearRegression(ltype) {
226 :     useweights = 1;
227 :     }
228 :    
229 :     void WeightRegression::storeVectors(const DoubleVector& modData, const DoubleVector& obsData) {
230 :     LinearRegression::storeVectors(modData, obsData);
231 :     if (x.Size() != w.Size()) {
232 :     handle.logMessage(LOGWARN, "Warning in weight regression - invalid vector sizes");
233 :     error = 1;
234 :     }
235 :     }
236 :    
237 :     // ********************************************************
238 :     // Functions for LogWeightRegression
239 :     // ********************************************************
240 :     LogWeightRegression::LogWeightRegression(LineType ltype) : LogLinearRegression(ltype) {
241 :     useweights = 1;
242 :     }
243 :    
244 :     void LogWeightRegression::storeVectors(const DoubleVector& modData, const DoubleVector& obsData) {
245 :     LogLinearRegression::storeVectors(modData, obsData);
246 :     if (x.Size() != w.Size()) {
247 :     handle.logMessage(LOGWARN, "Warning in log weight regression - invalid vector sizes");
248 :     error = 1;
249 :     }
250 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge