Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _GMM_H__
00011 #define _GMM_H__
00012
00013 #include <shogun/lib/config.h>
00014
00015 #ifdef HAVE_LAPACK
00016
00017 #include <shogun/distributions/Distribution.h>
00018 #include <shogun/distributions/Gaussian.h>
00019 #include <shogun/lib/common.h>
00020
00021 namespace shogun
00022 {
00036 class CGMM : public CDistribution
00037 {
00038 public:
00040 CGMM();
00046 CGMM(int32_t n, ECovType cov_type=FULL);
00053 CGMM(SGVector<CGaussian*> components, SGVector<float64_t> coefficients, bool copy=false);
00054 virtual ~CGMM();
00055
00057 void cleanup();
00058
00065 virtual bool train(CFeatures* data=NULL);
00066
00075 float64_t train_em(float64_t min_cov=1e-9, int32_t max_iter=1000, float64_t min_change=1e-9);
00076
00087 float64_t train_smem(int32_t max_iter=100, int32_t max_cand=5, float64_t min_cov=1e-9, int32_t max_em_iter=1000, float64_t min_change=1e-9);
00088
00094 void max_likelihood(SGMatrix<float64_t> alpha, float64_t min_cov);
00095
00100 virtual int32_t get_num_model_parameters();
00101
00107 virtual float64_t get_log_model_parameter(int32_t num_param);
00108
00115 virtual float64_t get_log_derivative(
00116 int32_t num_param, int32_t num_example);
00117
00125 virtual float64_t get_log_likelihood_example(int32_t num_example);
00126
00134 virtual float64_t get_likelihood_example(int32_t num_example)
00135 {
00136 return CMath::exp(get_log_likelihood_example(num_example));
00137 }
00138
00145 virtual inline SGVector<float64_t> get_nth_mean(int32_t num)
00146 {
00147 ASSERT(num<m_components.vlen);
00148 return m_components.vector[num]->get_mean();
00149 }
00150
00156 virtual inline void set_nth_mean(SGVector<float64_t> mean, int32_t num)
00157 {
00158 ASSERT(num<m_components.vlen);
00159 m_components.vector[num]->set_mean(mean);
00160 }
00161
00168 virtual inline SGMatrix<float64_t> get_nth_cov(int32_t num)
00169 {
00170 ASSERT(num<m_components.vlen);
00171 return m_components.vector[num]->get_cov();
00172 }
00173
00179 virtual inline void set_nth_cov(SGMatrix<float64_t> cov, int32_t num)
00180 {
00181 ASSERT(num<m_components.vlen);
00182 m_components.vector[num]->set_cov(cov);
00183 }
00184
00189 virtual inline SGVector<float64_t> get_coef()
00190 {
00191 return m_coefficients;
00192 }
00193
00198 virtual inline void set_coef(SGVector<float64_t> coefficients)
00199 {
00200 m_coefficients.destroy_vector();
00201 m_coefficients=coefficients;
00202 }
00203
00208 virtual inline SGVector<CGaussian*> get_comp()
00209 {
00210 return m_components;
00211 }
00212
00217 virtual inline void set_comp(SGVector<CGaussian*> components)
00218 {
00219 for (int32_t i=0; i<m_components.vlen; i++)
00220 {
00221 SG_UNREF(m_components.vector[i]);
00222 }
00223
00224 m_components.destroy_vector();
00225 m_components=components;
00226
00227 for (int32_t i=0; i<m_components.vlen; i++)
00228 {
00229 SG_REF(m_components.vector[i]);
00230 }
00231 }
00232
00237 SGVector<float64_t> sample();
00238
00244 SGVector<float64_t> cluster(SGVector<float64_t> point);
00245
00247 inline virtual const char* get_name() const { return "GMM"; }
00248
00249 private:
00256 SGMatrix<float64_t> alpha_init(SGMatrix<float64_t> init_means);
00257
00259 void register_params();
00260
00270 void partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min_cov, int32_t max_em_iter, float64_t min_change);
00271
00272 protected:
00274 SGVector<CGaussian*> m_components;
00276 SGVector<float64_t> m_coefficients;
00277 };
00278 }
00279 #endif //HAVE_LAPACK
00280 #endif //_GMM_H__