Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnSuffStats.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnFeatureWhitener.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 #include <limits>
17 
18 #include "Eigen/Core"
19 
20 #include "drwnBase.h"
21 
22 using namespace std;
23 using namespace Eigen;
24 
25 // drwnPairSuffStatsType -----------------------------------------------------
27 
28 typedef enum _drwnPairSuffStatsType {
29  DRWN_PSS_FULL,
30  DRWN_PSS_DIAG,
31  DRWN_PSS_NONE,
32 } drwnPairSuffStatsType;
33 
34 // drwnSuffStats class -------------------------------------------------------
42 
44  private:
45  int _n;
46  drwnPairSuffStatsType _pairStats;
47 
48  double _count;
49  VectorXd _sum;
50  MatrixXd _sum2;
51 
52  public:
54  drwnSuffStats(int n = 1, drwnPairSuffStatsType pairStats = DRWN_PSS_FULL);
56  drwnSuffStats(double count, VectorXd& sum, MatrixXd& sum2);
58  drwnSuffStats(const drwnSuffStats& stats);
59  ~drwnSuffStats();
60 
62  void clear();
65  void clear(int n, drwnPairSuffStatsType pairStats = DRWN_PSS_FULL);
67  void diagonalize();
68 
70  inline int size() const { return _n; }
72  inline bool isDiagonal() const { return (_pairStats == DRWN_PSS_DIAG); }
74  inline bool hasPairs() const { return (_pairStats != DRWN_PSS_NONE); }
76  inline double count() const { return _count; }
78  inline double sum(int i = 0) const { return _sum(i); }
80  inline double sum2(int i = 0, int j = 0) const {
81  switch (_pairStats) {
82  case DRWN_PSS_FULL: return _sum2(i, j);
83  case DRWN_PSS_DIAG: return (i == j) ? _sum2(i, 0) : 0.0;
84  case DRWN_PSS_NONE: return 0.0;
85  }
86  return 0.0;
87  }
88 
91  inline const VectorXd& firstMoments() const { return _sum; }
93  inline MatrixXd secondMoments() const {
94  switch (_pairStats) {
95  case DRWN_PSS_FULL: return _sum2;
96  case DRWN_PSS_DIAG: return _sum2.col(0).asDiagonal();
97  case DRWN_PSS_NONE: return MatrixXd::Zero(_n, _n);
98  }
99  return _sum2;
100  }
101 
102  // i/o
103  const char *type() const { return "drwnSuffStats"; }
104  drwnSuffStats *clone() const { return new drwnSuffStats(*this); }
105  bool save(drwnXMLNode& xml) const;
106  bool load(drwnXMLNode& xml);
107 
108  // modification
110  void accumulate(const vector<double>& x);
112  void accumulate(const vector<double>& x, double w);
114  void accumulate(const vector<vector<double> >& x, double w = 1.0);
116  void accumulate(const drwnSuffStats& stats, double w = 1.0);
118  void subtract(const vector<double>& x);
120  void subtract(const vector<double>& x, double w);
122  void subtract(const vector<vector<double> >& x, double w = 1.0);
124  void subtract(const drwnSuffStats& stats, double w = 1.0);
125 
126  // standard operators
128  drwnSuffStats& operator=(const drwnSuffStats& stats);
129 };
130 
131 // drwnCondSuffStats class --------------------------------------------------
149 
151  private:
152  int _n;
153  int _k;
154 
155  vector<drwnSuffStats> _stats;
156 
157  public:
160  drwnCondSuffStats(int n = 1, int k = 2, drwnPairSuffStatsType pairStats = DRWN_PSS_FULL);
162  drwnCondSuffStats(const drwnCondSuffStats& condStats);
164 
166  void clear();
169  void clear(int n, int k, drwnPairSuffStatsType pairStats = DRWN_PSS_FULL);
170 
172  inline int size() const { return _n; }
174  inline int states() const { return _k; }
176  inline double count() const;
178  inline double count(int k) const { return _stats[k].count(); }
179 
181  inline drwnSuffStats const& suffStats(int k) const {
182  return _stats[k];
183  }
184 
185  // i/o
186  const char *type() const { return "drwnCondSuffStats"; }
187  drwnCondSuffStats *clone() const { return new drwnCondSuffStats(*this); }
188  bool save(drwnXMLNode& xml) const;
189  bool load(drwnXMLNode& xml);
190 
191  // modification
193  inline void accumulate(const vector<double>& x, int y);
195  inline void accumulate(const vector<double>& x, int y, double w);
197  void accumulate(const vector<vector<double> >& x, int y, double w = 1.0);
199  void accumulate(const vector<vector<double> >& x, const vector<int>& y);
201  void accumulate(const vector<vector<double> >& x, const vector<int>& y, const vector<double>& w);
203  void accumulate(const drwnSuffStats& stats, int y, double w = 1.0);
205  inline void subtract(const vector<double>& x, int y);
207  inline void subtract(const vector<double>& x, int y, double w);
209  void subtract(const vector<vector<double> >& x, int y, double w = 1.0);
211  void subtract(const vector<vector<double> >& x, const vector<int>& y);
213  void subtract(const vector<vector<double> >& x, const vector<int>& y, const vector<double>& w);
215  void subtract(const drwnSuffStats& stats, int y, double w = 1.0);
217  void redistribute(int y, int k);
218 
219  // operators
221  inline const drwnSuffStats& operator[](unsigned k) const { return _stats[k]; }
222 };
223 
224 // inline functions ---------------------------------------------------------
225 
226 inline double drwnCondSuffStats::count() const {
227  double c = 0;
228  for (int y = 0; y < _k; y++) {
229  c += _stats[y].count();
230  }
231 
232  return c;
233 }
234 
235 inline void drwnCondSuffStats::accumulate(const vector<double>& x, int y) {
236  DRWN_ASSERT_MSG((y >= 0) && (y < _k), y);
237  _stats[y].accumulate(x);
238 }
239 
240 inline void drwnCondSuffStats::accumulate(const vector<double>& x, int y, double w) {
241  DRWN_ASSERT_MSG((y >= 0) && (y < _k), y);
242  _stats[y].accumulate(x, w);
243 }
244 
245 inline void drwnCondSuffStats::subtract(const vector<double>& x, int y) {
246  DRWN_ASSERT_MSG((y >= 0) && (y < _k), y);
247  _stats[y].subtract(x);
248 }
249 
250 inline void drwnCondSuffStats::subtract(const vector<double>& x, int y, double w) {
251  DRWN_ASSERT_MSG((y >= 0) && (y < _k), y);
252  _stats[y].subtract(x, w);
253 }
drwnCondSuffStats * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnSuffStats.h:187
const VectorXd & firstMoments() const
return the first moment return the weighted sum of samples accumulated
Definition: drwnSuffStats.h:91
Implements a class for accumulating conditional first- and second-order sufficient statistics...
Definition: drwnSuffStats.h:150
double count() const
return the weighted count of samples accumulated
Definition: drwnSuffStats.h:76
int size() const
return the number of dimensions
Definition: drwnSuffStats.h:172
void subtract(const vector< double > &x, int y)
remove a single sample from the sufficient statistics for state y
Definition: drwnSuffStats.h:245
const drwnSuffStats & operator[](unsigned k) const
copy constructor
Definition: drwnSuffStats.h:221
double sum2(int i=0, int j=0) const
return the weighted sum-of-squares of samples accumulated a given dimension
Definition: drwnSuffStats.h:80
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnSuffStats.h:186
bool isDiagonal() const
return true if only accumulating diagonalized statistics (i.e., no cross terms)
Definition: drwnSuffStats.h:72
int states() const
return the number of conditional states
Definition: drwnSuffStats.h:174
double count() const
return the total weight of samples accumulated
Definition: drwnSuffStats.h:226
Implements a class for accumulating first- and second-order sufficient statistics (moments)...
Definition: drwnSuffStats.h:43
MatrixXd secondMoments() const
return the weighted sum-of-squares of samples accumulated
Definition: drwnSuffStats.h:93
int size() const
return the dimensionality of the sufficient statistics
Definition: drwnSuffStats.h:70
bool hasPairs() const
return true if accumulating both first- and second-order statistics
Definition: drwnSuffStats.h:74
void accumulate(const vector< double > &x, int y)
add a single sample to the sufficient statistics for state y
Definition: drwnSuffStats.h:235
double sum(int i=0) const
return the weighted sum of samples accumulated for a given dimension
Definition: drwnSuffStats.h:78
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnSuffStats.h:103
double count(int k) const
return the weight of samples accumulated for the k-th state
Definition: drwnSuffStats.h:178
drwnSuffStats const & suffStats(int k) const
return the sufficient statistics for the k-th state
Definition: drwnSuffStats.h:181
drwnSuffStats * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnSuffStats.h:104
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72