DGtal 1.3.0
Searching...
No Matches
testSimpleRegression.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include "DGtal/base/Common.h"
33#include "ConfigTest.h"
34#include "DGtal/helpers/StdDefs.h"
35#include "DGtal/math/SimpleLinearRegression.h"
36#include "DGtal/math/OrderedLinearRegression.h"
38
39using namespace std;
40using namespace DGtal;
41
43// Functions for testing class SimpleRegression.
45
50{
51 unsigned int nbok = 0;
52 unsigned int nb = 0;
53
54 trace.beginBlock ( "Testing SimpleLinearRegression ..." );
55
57
62
63 nbok += SLR.computeRegression() ? 1 : 0;
64 nb++;
65 trace.info() << "(" << nbok << "/" << nb << ") "
66 << "Regression == true" << std::endl;
67
68 trace.info() << "Got slope= "<< SLR.slope()<<std::endl;
69 trace.info() << "Got Intercept= "<< SLR.intercept()<<std::endl;
70
71
72 nbok += ( std::abs(SLR.slope() - 1) < 0.01) ? 1 : 0;
73 nb++;
74 trace.info() << "(" << nbok << "/" << nb << ") "
75 << "slope == 1" << std::endl;
76 nbok += ( std::abs(SLR.intercept() - 0.0) < 0.01) ? 1 : 0;
77 nb++;
78 trace.info() << "(" << nbok << "/" << nb << ") "
79 << "intercept == 0" << std::endl;
81
82 return nbok == nb;
83}
84
85
91{
92 unsigned int nbok = 0;
93 unsigned int nb = 0;
94
95 trace.beginBlock ( "Testing SimpleLinearRegression2..." );
96
98
99 std::vector<double> x;
100 x.push_back(0);
101 x.push_back(2);
102 x.push_back(34);
103 x.push_back(3);
104 std::vector<double> y;
105 y.push_back(0);
106 y.push_back(2);
107 y.push_back(33);
108 y.push_back(2.7);
109
111
112 nbok += SLR.computeRegression() ? 1 : 0;
113 nb++;
114 trace.info() << "(" << nbok << "/" << nb << ") "
115 << "Regression == true" << std::endl;
116
117 trace.info() << "Got slope= "<< SLR.slope()<<std::endl;
118 trace.info() << "Got Intercept= "<< SLR.intercept()<<std::endl;
119
120
121 nbok += ( std::abs(SLR.slope() - 1) < 0.1) ? 1 : 0;
122 nb++;
123 trace.info() << "(" << nbok << "/" << nb << ") "
124 << "|slope| =~= 1" << std::endl;
125 nbok += ( std::abs(SLR.intercept() ) < 0.1) ? 1 : 0;
126 nb++;
127 trace.info() << "(" << nbok << "/" << nb << ") "
128 << "|intercept| < (10^-1)" << std::endl;
129 trace.endBlock();
130
131 return nbok == nb;
132}
133
139{
140 unsigned int nbok = 0;
141 unsigned int nb = 0;
142
143 trace.beginBlock ( "Testing SimpleLinearRegression3..." );
144
146
147 double x[] = {5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20};
148
149 double y[] = {0.00366568,0.0376311,0.0173014,0.0205614,0.00139069,0.0108356,0.000985302,0.00360493,0.00808965,0.014353,0.00497899,0.0115723,0.00651013,0.00758458,0.00392271,0.00752993,0.00597875,0.00841424,0.00704232,0.00848176,0.00676336,0.00564121,0.00584509,0.00702953,0.00591087,0.00745775,0.00618557,0.00727818,0.00534952,0.0053612,0.00426009};
150
151 std::vector<double> xx(31),yy(31);
152 for(unsigned int i=0; i < 31; ++i)
153 {
154 xx[i] = std::log(x[i]);
155 yy[i] = std::log(y[i]);
156 }
157
159
160 nbok += SLR.computeRegression() ? 1 : 0;
161 nb++;
162 trace.info() << "(" << nbok << "/" << nb << ") "
163 << "Regression == true" << std::endl;
164
165 trace.info() << "Got slope= "<< SLR.slope()<<std::endl;
166 trace.info() << "Got Intercept= "<< SLR.intercept()<<std::endl;
167
168
169 nbok += ( std::abs(SLR.slope() + 0.25) < 0.1) ? 1 : 0;
170 nb++;
171 trace.info() << "(" << nbok << "/" << nb << ") "
172 << "|slope| =~= 1" << std::endl;
173 trace.endBlock();
174
175 return nbok == nb;
176}
177
182{
183 unsigned int nbok = 0;
184 unsigned int nb = 0;
185
186 trace.beginBlock ( "Testing OrderedLinearRegression..." );
187
188 double x[] = {1, 2, 2.5, 3, 4 ,5 , 6};
189 double y[] = {1, 2, 2.5, 2.9, 4.1, 15, 25.9};
190
192 OLR.addSamples( &x[0] , &x[7], &y[0]);
193
195 SLR.addSamples( &x[0] , &x[7], &y[0]);
196
197 SimpleLinearRegression forward;
198 SimpleLinearRegression backward;
199
200 OLR.forwardSLR(forward, 4);
201 OLR.backwardSLR(backward, 3);
202
203 SLR.computeRegression();
204
205 trace.info() << "SLR slope = " << SLR.slope() <<std::endl;
206 nbok += ( SLR.slope() < 5 ) ? 1 : 0;
207 nb++;
208 trace.info() << "Forward slope = " << forward.slope() << " " << forward.size() << std::endl;
209 nbok += (( forward.size() == 5 ) && ( forward.slope() < 1.05 )) ? 1 : 0;
210 nb++;
211 trace.info() << "Backward slope = " << backward.slope() << " " << backward.size() << std::endl;
212 nbok += (( backward.slope() < 11 ) && ( backward.slope() > 10 )) ? 1 : 0;
213 nb++;
214
215 trace.endBlock();
216
217 return nbok == nb;
218}
219
220
222// Standard services - public :
223
224int main( int argc, char** argv )
225{
226 trace.beginBlock ( "Testing class SimpleRegression" );
227 trace.info() << "Args:";
228 for ( int i = 0; i < argc; ++i )
229 trace.info() << " " << argv[ i ];
230 trace.info() << endl;
231
232 bool res = testSimpleRegression()
235 && testSimpleRegressionOrdered(); // && ... other tests
236 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
237 trace.endBlock();
238 return res ? 0 : 1;
239}
240// //
Description of class 'OrderedLinearRegression'.
void backwardSLR(SimpleLinearRegression &linearModel, const unsigned int n=4, const double alpha=0.01) const
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)
void forwardSLR(SimpleLinearRegression &linearModel, const unsigned int n=4, const double alpha=0.01) const
Description of class 'SimpleLinearRegression'.
void addSample(const double x, const double y)
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
int main()
Definition: testBits.cpp:56
bool testSimpleRegression()
bool testSimpleRegression2()
bool testSimpleRegression3()
bool testSimpleRegressionOrdered()