MIRA
IncrementalMoments.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_INCREMENTALMOMENTS_H_
49 #define _MIRA_INCREMENTALMOMENTS_H_
50 
51 #include <math/Eigen.h>
52 
53 namespace mira {
54 
56 
61 template <typename T, int D, int Order>
63 {
64  static_assert(sizeof(T)==0, "There is no implementation for the moment of the given dimension D and Order");
65 };
66 
68 
69 // 1D cases
70 template <typename T>
71 class IncrementalMoments<T,1,1>
72 {
73 public:
74 
76  reset();
77  }
78 
79  void reset()
80  {
81  k = T(0);
82  m1 = T(0);
83  }
84 
86  void insert(const T& x, T weight = T(1))
87  {
88  Ctx ctx;
89  _insert(x, weight, ctx);
90  }
91 
92 public:
93 
98  T sumWeight() const { return k; }
99 
101  T mean() const { return m1; }
102 
103 protected:
104 
105  struct Ctx {
106  T k;
107  T nk;
108  T alpha;
109  T beta;
110  };
111 
113  T k;
115  T m1;
116 
117 protected:
118 
119  void _insert(const T& x, T weight, Ctx& ctx)
120  {
121  ctx.k = k;
122  ctx.nk = k+weight;
123  ctx.alpha = k/ctx.nk; // = k / (k+weight);
124  ctx.beta = weight/ctx.nk; // = weight / (k+weight);
125 
126  // update mean
127  m1 = ctx.alpha*m1 + ctx.beta*x;
128  k = ctx.nk;
129  }
130 };
131 
132 template <typename T>
133 class IncrementalMoments<T,1,2> : public IncrementalMoments<T,1,1>
134 {
136  typedef typename Base::Ctx Ctx;
137 
138 public:
139 
141  reset();
142  }
143 
144  void reset()
145  {
146  Base::reset();
147  m2 = T(0);
148  }
149 
151  void insert(const T& x, T weight = T(1))
152  {
153  Ctx ctx;
154  _insert(x, weight, ctx);
155  }
156 
158  T var() const { return m2; }
159 
160 protected:
161 
163  T m2;
164 
165 protected:
166 
167  void _insert(const T& x, T weight, Ctx& ctx)
168  {
169  Base::_insert(x, weight, ctx);
170  float alpha_beta = ctx.alpha*ctx.beta; // = (k*weight) / (k+weight);
171  // update covariance
172  T d = this->m1 - x;
173  m2 = (ctx.alpha*m2) + alpha_beta * (d*d);
174  }
175 };
176 
178 
179 // n-D cases
180 template <typename T, int D>
181 class IncrementalMoments<T,D,1>
182 {
183 public:
186 
187 public:
188 
190  reset();
191  }
192 
193  void reset()
194  {
195  k = T(0);
196  m1.setZero();
197  }
198 
200  void insert(const Vector& x, T weight = T(1))
201  {
202  Ctx ctx;
203  _insert(x, weight, ctx);
204  }
205 
206 public:
207 
212  T sumWeight() const { return k; }
213 
215  const Vector& mean() const { return m1; }
216 
217 protected:
218 
219  struct Ctx {
220  T k;
221  T nk;
222  T alpha;
223  T beta;
224  };
225 
227  T k;
230 
231 protected:
232 
233  void _insert(const Vector& x, T weight, Ctx& ctx)
234  {
235  ctx.k = k;
236  ctx.nk = k+weight;
237  ctx.alpha = k/ctx.nk; // = k / (k+weight);
238  ctx.beta = weight/ctx.nk; // = weight / (k+weight);
239 
240  // update mean
241  m1 = ctx.alpha*m1 + ctx.beta*x;
242  k = ctx.nk;
243  }
244 };
245 
246 template <typename T, int D>
247 class IncrementalMoments<T,D,2> : public IncrementalMoments<T,D,1>
248 {
250  typedef typename Base::Vector Vector;
251  typedef typename Base::Matrix Matrix;
252  typedef typename Base::Ctx Ctx;
253 
254 public:
255 
257  reset();
258  }
259 
260  void reset()
261  {
262  Base::reset();
263  m2.setZero();
264  }
265 
267  void insert(const Vector& x, T weight = T(1))
268  {
269  Ctx ctx;
270  _insert(x, weight, ctx);
271  }
272 
274  Matrix cov() const { return m2; }
275 
276 protected:
277 
280 
281 protected:
282 
283  void _insert(const Vector& x, T weight, Ctx& ctx)
284  {
285  Base::_insert(x, weight, ctx);
286  float alpha_beta = ctx.alpha*ctx.beta; // = (k*weight) / (k+weight);
287  // update covariance
288  Vector d = this->m1 - x;
289  m2 = (ctx.alpha*m2) + alpha_beta * (d*d.transpose());
290  }
291 };
292 
293 
294 
296 
297 } // namespace
298 
299 #endif
T k
Definition: IncrementalMoments.h:106
Include file for all eigen related things.
T m1
the computed mean
Definition: IncrementalMoments.h:115
void reset()
Definition: IncrementalMoments.h:260
Eigen::Matrix< T, D, D > Matrix
Definition: IncrementalMoments.h:185
Incremental computation of one- and higher-dimensional statistical moments of first and higher orders...
Definition: IncrementalMoments.h:62
void reset()
Definition: IncrementalMoments.h:193
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
T nk
Definition: IncrementalMoments.h:107
T nk
Definition: IncrementalMoments.h:221
T sumWeight() const
Returns the sum of all sample weights, if all samples have the weight 1.0 this corresponds to the num...
Definition: IncrementalMoments.h:98
Matrix cov() const
Returns the computed covariance.
Definition: IncrementalMoments.h:274
T sumWeight() const
Returns the sum of all sample weights, if all samples have the weight 1.0 this corresponds to the num...
Definition: IncrementalMoments.h:212
void _insert(const Vector &x, T weight, Ctx &ctx)
Definition: IncrementalMoments.h:233
void _insert(const Vector &x, T weight, Ctx &ctx)
Definition: IncrementalMoments.h:283
Vector m1
the computed mean
Definition: IncrementalMoments.h:229
Eigen::Matrix< T, D, 1 > Vector
Definition: IncrementalMoments.h:184
T beta
Definition: IncrementalMoments.h:109
Definition: IncrementalMoments.h:181
T beta
Definition: IncrementalMoments.h:223
T alpha
Definition: IncrementalMoments.h:108
T var() const
Returns the computed variance.
Definition: IncrementalMoments.h:158
void insert(const T &x, T weight=T(1))
inserts a new sample
Definition: IncrementalMoments.h:151
void insert(const T &x, T weight=T(1))
inserts a new sample
Definition: IncrementalMoments.h:86
T k
the cumulated weights
Definition: IncrementalMoments.h:227
T mean() const
Returns the computed mean.
Definition: IncrementalMoments.h:101
IncrementalMoments()
Definition: IncrementalMoments.h:75
T k
Definition: IncrementalMoments.h:220
IncrementalMoments()
Definition: IncrementalMoments.h:140
IncrementalMoments()
Definition: IncrementalMoments.h:189
T m2
the computed variance
Definition: IncrementalMoments.h:163
IncrementalMoments()
Definition: IncrementalMoments.h:256
void reset()
Definition: IncrementalMoments.h:144
void reset()
Definition: IncrementalMoments.h:79
void _insert(const T &x, T weight, Ctx &ctx)
Definition: IncrementalMoments.h:119
void insert(const Vector &x, T weight=T(1))
inserts a new sample
Definition: IncrementalMoments.h:200
const Vector & mean() const
Returns the computed mean.
Definition: IncrementalMoments.h:215
Matrix m2
the computed covariance
Definition: IncrementalMoments.h:279
void _insert(const T &x, T weight, Ctx &ctx)
Definition: IncrementalMoments.h:167
T k
the cumulated weights
Definition: IncrementalMoments.h:113
void insert(const Vector &x, T weight=T(1))
inserts a new sample
Definition: IncrementalMoments.h:267
T alpha
Definition: IncrementalMoments.h:222
Definition: IncrementalMoments.h:71