DGtal  1.5.beta
testSimpleRegression.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "ConfigTest.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/math/SimpleLinearRegression.h"
#include "DGtal/math/OrderedLinearRegression.h"
Include dependency graph for testSimpleRegression.cpp:

Go to the source code of this file.

Functions

bool testSimpleRegression ()
 
bool testSimpleRegression2 ()
 
bool testSimpleRegression3 ()
 
bool testSimpleRegressionOrdered ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2014/03/26

Functions for testing class SimpleRegression.

This file is part of the DGtal library.

Definition in file testSimpleRegression.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 224 of file testSimpleRegression.cpp.

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 }
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Trace trace
Definition: Common.h:153
bool testSimpleRegression()
bool testSimpleRegression2()
bool testSimpleRegression3()
bool testSimpleRegressionOrdered()

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testSimpleRegression(), testSimpleRegression2(), testSimpleRegression3(), testSimpleRegressionOrdered(), and DGtal::trace.

◆ testSimpleRegression()

bool testSimpleRegression ( )

Example of a test. To be completed.

Definition at line 49 of file testSimpleRegression.cpp.

50 {
51  unsigned int nbok = 0;
52  unsigned int nb = 0;
53 
54  trace.beginBlock ( "Testing SimpleLinearRegression ..." );
55 
57 
58  SLR.addSample(0,0);
59  SLR.addSample(1,1);
60  SLR.addSample(34,34);
61  SLR.addSample(3,3);
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;
80  trace.endBlock();
81 
82  return nbok == nb;
83 }
Description of class 'SimpleLinearRegression'.
void addSample(const double x, const double y)

References DGtal::SimpleLinearRegression::addSample(), DGtal::Trace::beginBlock(), DGtal::SimpleLinearRegression::computeRegression(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleLinearRegression::intercept(), DGtal::SimpleLinearRegression::slope(), and DGtal::trace.

Referenced by main().

◆ testSimpleRegression2()

bool testSimpleRegression2 ( )

Example of a test. To be completed.

Definition at line 90 of file testSimpleRegression.cpp.

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 
110  SLR.addSamples( x.begin(), x.end(), y.begin());
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 }
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)

References DGtal::SimpleLinearRegression::addSamples(), DGtal::Trace::beginBlock(), DGtal::SimpleLinearRegression::computeRegression(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleLinearRegression::intercept(), DGtal::SimpleLinearRegression::slope(), and DGtal::trace.

Referenced by main().

◆ testSimpleRegression3()

bool testSimpleRegression3 ( )

Example of a test. To be completed.

Definition at line 138 of file testSimpleRegression.cpp.

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 
158  SLR.addSamples( xx.begin(), xx.end(), yy.begin());
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 }

References DGtal::SimpleLinearRegression::addSamples(), DGtal::Trace::beginBlock(), DGtal::SimpleLinearRegression::computeRegression(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleLinearRegression::intercept(), DGtal::SimpleLinearRegression::slope(), and DGtal::trace.

Referenced by main().

◆ testSimpleRegressionOrdered()

bool testSimpleRegressionOrdered ( )

Definition at line 181 of file testSimpleRegression.cpp.

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 }
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

References DGtal::OrderedLinearRegression::addSamples(), DGtal::SimpleLinearRegression::addSamples(), DGtal::OrderedLinearRegression::backwardSLR(), DGtal::Trace::beginBlock(), DGtal::SimpleLinearRegression::computeRegression(), DGtal::Trace::endBlock(), DGtal::OrderedLinearRegression::forwardSLR(), DGtal::Trace::info(), DGtal::SimpleLinearRegression::size(), DGtal::SimpleLinearRegression::slope(), and DGtal::trace.

Referenced by main().