DGtal  1.1.0
OrderedLinearRegression.h
1
17 #pragma once
18
34 #if defined(OrderedLinearRegression_RECURSES)
35 #error Recursive header files inclusion detected in OrderedLinearRegression.h
36 #else // defined(OrderedLinearRegression_RECURSES)
37
38 #define OrderedLinearRegression_RECURSES
39
40 #if !defined OrderedLinearRegression_h
41
42 #define OrderedLinearRegression_h
43
44
46 #include <iostream>
47 #include <vector>
48 #include "DGtal/math/SimpleLinearRegression.h"
50 namespace DGtal
51 {
53  // class OrderedLinearRegression
55
66  {
67  // ----------------------- Standard services ------------------------------
68  public:
69
74  {}
75
83  OrderedLinearRegression( double eps_zero = 1e-8 ):
84  myEpsilonZero(eps_zero),
85  myN(0)
86  {}
87
91  void clear()
92  {
93  myX.clear();
94  myY.clear();
95  myN = 0;
96  }
97
110  template <class XIterator, class YIterator>
111  void addSamples( XIterator begin_x, XIterator end_x, YIterator begin_y )
112  {
113  for ( ; begin_x != end_x; ++begin_x, ++begin_y )
114  {
116  }
117  }
118
119
131  void addSample( const double x, const double y )
132  {
133  myX.push_back( x );
134  myY.push_back( y );
135  ++myN;
136  }
137
138
139  // ----------------------- Interface --------------------------------------
158  const unsigned int n = 4,
159  const double alpha = 0.01) const
160  {
161  linearModel.setEpsilonZero(myEpsilonZero);
162  linearModel.clear();
163  std::vector<double>::const_iterator itx = myX.begin();
164  std::vector<double>::const_iterator itxe = myX.end();
165  std::vector<double>::const_iterator ity = myY.begin();
166  linearModel.addSamples( itx, itx + n, ity );
167  linearModel.computeRegression();
168  itx += n;
169  ity += n;
170  unsigned int l = myX.size() - n + 1;
171  for ( ; itx != itxe; ++itx, ++ity, --l )
172  {
173  std::pair<double,double> ic;
174  ic = linearModel.trustIntervalForY( *itx, alpha );
175  if ( ( *ity < ic.first ) || ( *ity > ic.second ) )
176  break;
178  linearModel.computeRegression();
179  }
180  }
181
182
201  const unsigned int n = 4,
202  const double alpha = 0.01 ) const
203  {
204  linearModel.setEpsilonZero(myEpsilonZero);
205  linearModel.clear();
206  std::vector<double>::const_reverse_iterator itx = myX.rbegin();
207  std::vector<double>::const_reverse_iterator itxe = myX.rend();
208  std::vector<double>::const_reverse_iterator ity = myY.rbegin();
209  linearModel.addSamples( itx, itx + n, ity );
210  linearModel.computeRegression();
211  itx += n;
212  ity += n;
213  unsigned int l = myX.size() - n + 1;
214  for ( ; itx != itxe; ++itx, ++ity, --l )
215  {
216  std::pair<double,double> ic;
217  ic = linearModel.trustIntervalForY( *itx, alpha );
218  if ( ( *ity < ic.first ) || ( *ity > ic.second ) )
219  break;
221  linearModel.computeRegression();
222  }
223  }
224
225  // ----------------------- Interface --------------------------------------
226  public:
227
232  void selfDisplay( std::ostream & that_stream ) const
233  {
234  that_stream << "[OrderedLinearRegression] Number of samples="<< myN;
235  }
236
241  bool isValid() const
242  {
243  return true;
244  }
245
246
247  // ------------------------- Datas ----------------------------------------
248
249  private:
250
253
255  unsigned int myN;
256
258  std::vector<double> myY;
259
261  std::vector<double> myX;
262
263
264  // ------------------------- Hidden services ------------------------------
265  protected:
266
267  private:
268
275
283
284  // ------------------------- Internals ------------------------------------
285  private:
286
287
288  };
289
296  std::ostream&
297  operator<<( std::ostream & that_stream,
298  const OrderedLinearRegression & that_object_to_display );
299
300
301 } // namespace DGtal
302
303 // //
305 #endif // !defined OrderedLinearRegression_h
306
307 #undef OrderedLinearRegression_RECURSES
308 #endif // else defined(OrderedLinearRegression_RECURSES)
DGtal::SimpleLinearRegression::setEpsilonZero
void setEpsilonZero(const double aEpsilonZero)
Definition: SimpleLinearRegression.h:181
DGtal::OrderedLinearRegression::myY
std::vector< double > myY
Ordinate values of sample points.
Definition: OrderedLinearRegression.h:258
DGtal::OrderedLinearRegression::isValid
bool isValid() const
Definition: OrderedLinearRegression.h:241
DGtal::OrderedLinearRegression::myX
std::vector< double > myX
Abscissa values of sample points.
Definition: OrderedLinearRegression.h:261
DGtal::SimpleLinearRegression::trustIntervalForY
std::pair< double, double > trustIntervalForY(const double x, const double a) const
DGtal::OrderedLinearRegression::myN
unsigned int myN
Number of samples.
Definition: OrderedLinearRegression.h:255
void addSample(const double x, const double y)
DGtal::SimpleLinearRegression::computeRegression
bool computeRegression()
DGtal::OrderedLinearRegression::forwardSLR
void forwardSLR(SimpleLinearRegression &linearModel, const unsigned int n=4, const double alpha=0.01) const
Definition: OrderedLinearRegression.h:157
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)
Definition: OrderedLinearRegression.h:111
DGtal::OrderedLinearRegression::~OrderedLinearRegression
~OrderedLinearRegression()
Definition: OrderedLinearRegression.h:73
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::OrderedLinearRegression::clear
void clear()
Definition: OrderedLinearRegression.h:91
DGtal::OrderedLinearRegression::myEpsilonZero
double myEpsilonZero
Epsilon zero value.
Definition: OrderedLinearRegression.h:252
DGtal::OrderedLinearRegression::OrderedLinearRegression
OrderedLinearRegression(const OrderedLinearRegression &other)
DGtal::OrderedLinearRegression::selfDisplay
void selfDisplay(std::ostream &that_stream) const
Definition: OrderedLinearRegression.h:232
DGtal::SimpleLinearRegression
Description of class 'SimpleLinearRegression'.
Definition: SimpleLinearRegression.h:74
void addSample(const double x, const double y)
Definition: OrderedLinearRegression.h:131
DGtal::SimpleLinearRegression::clear
void clear()
DGtal::OrderedLinearRegression::OrderedLinearRegression
OrderedLinearRegression(double eps_zero=1e-8)
Definition: OrderedLinearRegression.h:83
DGtal::OrderedLinearRegression
Description of class 'OrderedLinearRegression'.
Definition: OrderedLinearRegression.h:66
DGtal::OrderedLinearRegression::backwardSLR
void backwardSLR(SimpleLinearRegression &linearModel, const unsigned int n=4, const double alpha=0.01) const
Definition: OrderedLinearRegression.h:200
DGtal::OrderedLinearRegression::operator=
OrderedLinearRegression & operator=(const OrderedLinearRegression &other)