1 #ifndef __NUMERIC_STATS_HPP__ 2 #define __NUMERIC_STATS_HPP__ 5 #include <base/Eigen.hpp> 14 static inline Type
value( Type a ) {
return a * a; }
15 static inline Type
value( Type a, Type b ) {
return a * b; }
21 static inline T
value() {
return 0; }
25 template <
class _Scalar,
int _Rows,
int _Options>
26 struct Square< Eigen::Matrix<_Scalar, _Rows, 1, _Options> >
28 typedef typename Eigen::Matrix<_Scalar, _Rows, 1, _Options>
T;
29 typedef typename Eigen::Matrix<_Scalar, _Rows, _Rows, _Options>
Type;
30 static inline Type
value(
const T& a ) {
return a * a.transpose(); }
31 static inline Type
value(
const T& a,
const T& b ) {
return a * b.transpose(); }
34 template <
class _Scalar,
int _Rows,
int _Cols,
int _Options>
35 struct Zero< Eigen::Matrix<_Scalar, _Rows, _Cols, _Options> >
37 typedef Eigen::Matrix<_Scalar, _Rows, _Cols, _Options>
T;
38 static inline T
value() {
return T::Zero(); }
41 template <
class T> T
min_el( T a, T b ) {
return std::min( a, b ); }
42 template <
class T> T
max_el( T a, T b ) {
return std::max( a, b ); }
44 template <
class _Scalar,
int _Rows,
int _Options>
45 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
min_el(
46 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
const& a,
47 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
const& b )
49 return a.array().min( b.array() ).matrix();
52 template <
class _Scalar,
int _Rows,
int _Options>
53 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
max_el(
54 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
const& a,
55 Eigen::Matrix<_Scalar, _Rows, 1, _Options>
const& b )
57 return a.array().max( b.array() ).matrix();
80 void init( T
const& data);
84 void update( T
const& data,
double weight = 1.0 );
90 SquareType var()
const;
92 double sumWeights()
const;
101 void setDDof(
double new_ddof) { ddof_ = new_ddof; }
129 int rows = data.rows();
134 M2_ = base::MatrixXd::Zero(rows,rows);
135 mean_ = base::VectorXd::Zero(rows);
158 double temp = weight + sum_weight_;
159 T delta = data - mean_;
160 T R = delta * weight / temp;
189 return ( sum_weight_ > 0.0 ) ? SquareType(M2_ / (sum_weight_ - ddof_)) :
190 base::MatrixXd::Zero(M2_.rows(),M2_.cols());
196 return sqrt( var() );
202 return var().diagonal().array().sqrt();
236 Eigen::VectorXd min_;
237 Eigen::VectorXd max_;
238 Eigen::VectorXd mean_;
239 Eigen::VectorXd stdev_;
241 Eigen::MatrixXd var_;
247 template<
typename Derived>
248 Eigen::VectorXd
weightOnes(
const Eigen::MatrixBase<Derived>& data) {
249 return Eigen::VectorXd::Ones(data.cols());
252 template <
typename Derived>
253 void compute (
const Eigen::MatrixBase<Derived>& data) {
254 compute(data, weightOnes(data), 0.0);
257 template <
typename Derived>
258 void compute (
const Eigen::MatrixBase<Derived>& data,
double ddof) {
259 compute(data, weightOnes(data), ddof);
262 template <
typename Derived1,
typename Derived2>
263 void compute (
const Eigen::MatrixBase<Derived1>& data,
264 const Eigen::MatrixBase<Derived2>& weights) {
266 compute(data, weights, 0.0);
269 template <
typename Derived1,
typename Derived2>
270 void compute (
const Eigen::MatrixBase<Derived1>& data,
271 const Eigen::MatrixBase<Derived2>& weights,
double ddof) {
273 min_ = data.array().rowwise().minCoeff();
274 max_ = data.array().rowwise().maxCoeff();
276 double w_sum = weights.sum();
277 Eigen::MatrixXd weighted_data = data * (weights / w_sum * n_).asDiagonal();
278 mean_ = weighted_data.rowwise().mean();
279 Eigen::MatrixXd centered_data = weighted_data.colwise() - mean_;
280 var_ = centered_data * centered_data.adjoint() / ( n_ - ddof );
281 stdev_ = var_.diagonal().array().sqrt();
286 template <
typename Derived>
287 SeriesStats(
const Eigen::MatrixBase<Derived>& data) { compute(data); }
289 template <
typename Derived>
290 SeriesStats(
const Eigen::MatrixBase<Derived>& data,
double ddof) {
294 template <
typename Derived1,
typename Derived2>
296 const Eigen::MatrixBase<Derived2>& weights) {
297 compute(data, weights);
308 template <
typename Derived1,
typename Derived2>
310 const Eigen::MatrixBase<Derived2>& weights,
double ddof) {
311 compute(data, weights, ddof);
314 const Eigen::VectorXd&
min()
const {
return min_; }
315 const Eigen::VectorXd&
max()
const {
return max_; }
316 const Eigen::VectorXd&
mean()
const {
return mean_; }
317 const Eigen::MatrixXd&
var()
const {
return var_; }
318 const Eigen::VectorXd&
stdev()
const {
return stdev_; }
319 size_t n()
const {
return n_; }
void compute(const Eigen::MatrixBase< Derived > &data, double ddof)
Definition: Stats.hpp:258
double sumWeights() const
Definition: Stats.hpp:169
const Eigen::VectorXd & stdev() const
Definition: Stats.hpp:318
const Eigen::MatrixXd & var() const
Definition: Stats.hpp:317
static T value()
Definition: Stats.hpp:21
static Type value(const T &a, const T &b)
Definition: Stats.hpp:31
static Type value(Type a, Type b)
Definition: Stats.hpp:15
SeriesStats(const Eigen::MatrixBase< Derived1 > &data, const Eigen::MatrixBase< Derived2 > &weights, double ddof)
Definition: Stats.hpp:309
const Eigen::VectorXd & max() const
Definition: Stats.hpp:315
SeriesStats(const Eigen::MatrixBase< Derived > &data, double ddof)
Definition: Stats.hpp:290
void compute(const Eigen::MatrixBase< Derived > &data)
Definition: Stats.hpp:253
size_t n() const
Definition: Stats.hpp:319
void setDDof(double new_ddof)
Definition: Stats.hpp:101
SeriesStats(const Eigen::MatrixBase< Derived > &data)
Definition: Stats.hpp:287
SeriesStats(const Eigen::MatrixBase< Derived1 > &data, const Eigen::MatrixBase< Derived2 > &weights)
Definition: Stats.hpp:295
const Eigen::VectorXd & mean() const
Definition: Stats.hpp:316
T min_el(T a, T b)
Definition: Stats.hpp:41
size_t n() const
Definition: Stats.hpp:218
SquareType var() const
Definition: Stats.hpp:181
Eigen::VectorXd weightOnes(const Eigen::MatrixBase< Derived > &data)
Definition: Stats.hpp:248
Eigen::Matrix< _Scalar, _Rows, _Cols, _Options > T
Definition: Stats.hpp:37
Eigen::Matrix< _Scalar, _Rows, 1, _Options > T
Definition: Stats.hpp:28
Definition: Stats.hpp:234
Stats()
Definition: Stats.hpp:105
static Type value(Type a)
Definition: Stats.hpp:14
T min() const
Definition: Stats.hpp:206
T Type
Definition: Stats.hpp:13
void clear()
Definition: Stats.hpp:112
T mean() const
Definition: Stats.hpp:175
T max_el(T a, T b)
Definition: Stats.hpp:42
T stdev() const
Definition: Stats.hpp:194
T max() const
Definition: Stats.hpp:212
const Eigen::VectorXd & min() const
Definition: Stats.hpp:314
static T value()
Definition: Stats.hpp:38
Eigen::Matrix< _Scalar, _Rows, _Rows, _Options > Type
Definition: Stats.hpp:29
void compute(const Eigen::MatrixBase< Derived1 > &data, const Eigen::MatrixBase< Derived2 > &weights, double ddof)
Definition: Stats.hpp:270
void compute(const Eigen::MatrixBase< Derived1 > &data, const Eigen::MatrixBase< Derived2 > &weights)
Definition: Stats.hpp:263
static Type value(const T &a)
Definition: Stats.hpp:30
void update(T const &data, double weight=1.0)
Definition: Stats.hpp:139