DGtal 1.4.0
Loading...
Searching...
No Matches
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)
38#define OrderedLinearRegression_RECURSES
39
40#if !defined OrderedLinearRegression_h
42#define OrderedLinearRegression_h
43
44
46#include <iostream>
47#include <vector>
48#include "DGtal/math/SimpleLinearRegression.h"
50namespace DGtal
51{
53 // class OrderedLinearRegression
55
66 {
67 // ----------------------- Standard services ------------------------------
68 public:
69
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 {
115 addSample( *begin_x, *begin_y );
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 = (unsigned int)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;
177 linearModel.addSample( *itx, *ity );
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 = static_cast<unsigned int>(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;
220 linearModel.addSample( *itx, *ity );
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)
Description of class 'OrderedLinearRegression'.
std::vector< double > myX
Abscissa values of sample points.
double myEpsilonZero
Epsilon zero value.
void backwardSLR(SimpleLinearRegression &linearModel, const unsigned int n=4, const double alpha=0.01) const
void selfDisplay(std::ostream &that_stream) const
std::vector< double > myY
Ordinate values of sample points.
void addSample(const double x, const double y)
unsigned int myN
Number of samples.
OrderedLinearRegression(double eps_zero=1e-8)
OrderedLinearRegression & operator=(const OrderedLinearRegression &other)
void addSamples(XIterator begin_x, XIterator end_x, YIterator begin_y)
OrderedLinearRegression(const OrderedLinearRegression &other)
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)
std::pair< double, double > trustIntervalForY(const double x, const double a) const
void setEpsilonZero(const double aEpsilonZero)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)