Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnGaussianMixture.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: drwnGaussianMixture.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 #include "Eigen/Cholesky"
20 #include "Eigen/LU"
21 
22 #include "drwnBase.h"
23 #include "drwnSuffStats.h"
24 
25 using namespace std;
26 using namespace Eigen;
27 
28 // drwnGaussianMixture ------------------------------------------------------
56 
58 public:
59  static int MAX_ITERATIONS;
60 
61 protected:
62  unsigned _n;
63  vector<drwnGaussian> _g;
64  VectorXd _logLambda;
65 
66 public:
69  drwnGaussianMixture(unsigned n = 1, unsigned k = 1);
70  virtual ~drwnGaussianMixture();
71 
72  // initialization
75  void initialize(unsigned n, unsigned k);
76 
77  // i/o
78  const char *type() const { return "drwnGaussianMixture"; }
79  drwnGaussianMixture *clone() const { return new drwnGaussianMixture(*this); }
80  bool save(drwnXMLNode& xml) const;
81  bool load(drwnXMLNode& xml);
82 
83  // evaluate (log-likelihood)
85  void evaluate(const MatrixXd& x, VectorXd& p) const;
87  void evaluate(const vector<vector<double> >& x, vector<double>& p) const;
89  double evaluateSingle(const VectorXd& x) const;
91  double evaluateSingle(const vector<double>& x) const;
92 
93  // sampling
95  void sample(VectorXd& x) const;
97  void sample(vector<double>& x) const;
98 
99  // learn parameters
104  void train(const MatrixXd& x, double lambda = 1.0e-3);
106  void train(const vector<vector<double> >& x, double lambda = 1.0e-3);
107 
108  // access
110  unsigned mixtures() const { return _g.size(); }
112  unsigned dimension() const { return _n; }
114  double weight(int k) const { return exp(_logLambda[k]); }
116  const VectorXd& mean(int k) const { return _g[k].mean(); }
118  const MatrixXd& covariance(int k) const { return _g[k].covariance(); }
120  const drwnGaussian& component(int k) const { return _g[k]; }
121 
122  // standard operators
124  drwnGaussianMixture& operator=(const drwnGaussianMixture& model);
125 };
126 
const MatrixXd & covariance(int k) const
returns the covariance matrix for the k-th component
Definition: drwnGaussianMixture.h:118
vector< drwnGaussian > _g
gaussian components
Definition: drwnGaussianMixture.h:63
drwnGaussianMixture * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnGaussianMixture.h:79
unsigned dimension() const
returns the dimensionality of the features space
Definition: drwnGaussianMixture.h:112
VectorXd _logLambda
mixture weights (log space)
Definition: drwnGaussianMixture.h:64
Implements a multi-variant Gaussian mixture model.
Definition: drwnGaussianMixture.h:57
const VectorXd & mean(int k) const
returns the mean of the k-th component
Definition: drwnGaussianMixture.h:116
Implements a multi-variate gaussian distribution.
Definition: drwnGaussian.h:38
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnGaussianMixture.h:78
double weight(int k) const
returns the mixture weight of the k-th component
Definition: drwnGaussianMixture.h:114
const drwnGaussian & component(int k) const
returns the k-th component as a gaussian distribution
Definition: drwnGaussianMixture.h:120
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72
unsigned _n
feature dimension
Definition: drwnGaussianMixture.h:62
unsigned mixtures() const
returns the number of mixture components
Definition: drwnGaussianMixture.h:110