Mixture.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
3  * All rights reserved. *
4  ************************************************************************
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "problib/pdfs/Mixture.h"
38 
39 using namespace pbl;
40 
41 Mixture::Mixture() : PDF(-1, PDF::MIXTURE), ptr_(0) {
42 }
43 
44 Mixture::Mixture(const Mixture& orig) : PDF(orig), ptr_(orig.ptr_){
45  if (ptr_) {
46  ++ptr_->n_ptrs_;
47  }
48 }
49 
51  if (ptr_) {
52  --ptr_->n_ptrs_;
53 
54  if (ptr_->n_ptrs_ == 0) {
55  delete ptr_;
56  }
57  }
58 }
59 
61  if (this != &other) {
62  if (ptr_) {
63  --ptr_->n_ptrs_;
64  if (ptr_->n_ptrs_ == 0) {
65  delete ptr_;
66  }
67  }
68  ptr_ = other.ptr_;
69  ++ptr_->n_ptrs_;
70 
71  dimensions_ = other.dimensions_;
72  }
73  return *this;
74 }
75 
77  return new Mixture(*this);
78 }
79 
81  if (ptr_->n_ptrs_ > 1) {
82  --ptr_->n_ptrs_;
83  ptr_ = new MixtureStruct(*ptr_);
84  }
85 }
86 
87 double Mixture::getLikelihood(const PDF& pdf) const {
88  assert_msg(ptr_, "Mixture does not contain components.");
89  assert(ptr_->num_components_ > 0);
90  assert(ptr_->weights_total_ == 1);
91 
92  double likelihood = 0;
93  std::vector<double>::const_iterator it_w =ptr_-> weights_.begin();
94  for (std::vector<PDF*>::const_iterator it_pdf = ptr_->components_.begin(); it_pdf != ptr_->components_.end(); ++it_pdf) {
95  likelihood += (*it_w) * (*it_pdf)->getLikelihood(pdf);
96  ++it_w;
97  }
98  return likelihood;
99 }
100 
102  if (ptr_) {
103  --ptr_->n_ptrs_;
104  if (ptr_->n_ptrs_ == 0) {
105  delete ptr_;
106  }
107  ptr_ = 0;
108  }
109 }
110 
111 double Mixture::getMaxDensity() const {
112  assert_msg(false, "Mixture does not contain components.");
113  return 0;
114 }
115 
116 int Mixture::components() const {
117  return ptr_->num_components_;
118 }
119 
120 void Mixture::addComponent(const PDF& pdf, double w) {
121  if (dimensions_ < 0) {
122  dimensions_ = pdf.dimensions();
123  } else {
124  assert(dimensions_ == pdf.dimensions());
125  }
126 
127  if (!ptr_) {
128  ptr_ = new MixtureStruct();
129  } else {
130  cloneStruct();
131  }
132 
133  ptr_->components_.push_back(pdf.clone());
134  ptr_->weights_.push_back(w);
135  ptr_->weights_total_ += w;
137 }
138 
139 const PDF& Mixture::getComponent(int i) const {
140  assert_msg(ptr_, "Mixture does not contain components.");
141  return *(ptr_->components_[i]);
142 }
143 
144 double Mixture::getWeight(int i) const {
145  assert_msg(ptr_, "Mixture does not contain components.");
146  return ptr_->weights_[i];
147 }
148 
150  assert_msg(ptr_, "Mixture does not contain components.");
151 
152  if (ptr_->weights_total_ == 1) return;
153 
154  assert(ptr_->weights_total_ > 0);
155 
156  for (std::vector<double>::iterator it_w = ptr_->weights_.begin(); it_w != ptr_->weights_.end(); ++it_w) {
157  (*it_w) /= ptr_->weights_total_;
158  }
159  ptr_->weights_total_ = 1;
160 }
161 
162 std::string Mixture::toString(const std::string& indent) const {
163  if (!ptr_) {
164  return "MIX(-)";
165  }
166 
167  std::string new_indent = indent + " ";
168 
169  std::stringstream ss;
170  ss << "MIX{\n";
172  for (std::vector<PDF*>::const_iterator it_pdf = ptr_->components_.begin(); it_pdf != ptr_->components_.end(); ++it_pdf) {
173  ss << new_indent << (*it_w) << " : " << (*it_pdf)->toString(new_indent) << "\n";
174  ++it_w;
175  }
176  ss << indent << "}";
177  return ss.str();
178 }
int dimensions() const
Definition: PDF.cpp:52
Mixture * clone() const
Creates a clone of the object. The clone method is cheap since it only copies a pointer. A deep clone will only be created if the original object is modified.
Definition: Mixture.cpp:76
This class represents the weighted sum of a finite set of probability density functions.
Definition: Mixture.h:54
int dimensions_
Definition: PDF.h:87
#define assert_msg(_Expression, _Msg)
Definition: globals.h:53
std::vector< double > weights_
Definition: Mixture.h:150
void cloneStruct()
Definition: Mixture.cpp:80
void normalizeWeights()
Normalizes the weights of all components.
Definition: Mixture.cpp:149
int components() const
Returns the number of components.
Definition: Mixture.cpp:116
void clear()
Removes all components.
Definition: Mixture.cpp:101
virtual PDF * clone() const =0
MixtureStruct * ptr_
Definition: Mixture.h:173
iterator(field< oT > &in_M, const bool at_end=false)
Mixture & operator=(const Mixture &other)
Assignment operator. The operation is cheap since it only copies a pointer. A deep clone will only be...
Definition: Mixture.cpp:60
double getMaxDensity() const
NOT IMPLEMENTED FOR MIXTURE.
Definition: Mixture.cpp:111
void addComponent(const PDF &pdf, double w)
Adds a component pdf with given weight.
Definition: Mixture.cpp:120
const PDF & getComponent(int i) const
Returns a reference to the i&#39;th component.
Definition: Mixture.cpp:139
const_iterator(const field< oT > &in_M, const bool at_end=false)
double getLikelihood(const PDF &pdf) const
Definition: Mixture.cpp:87
virtual ~Mixture()
Destructor.
Definition: Mixture.cpp:50
std::vector< PDF * > components_
Definition: Mixture.h:148
Definition: PDF.h:47
Mixture()
Constructs a mixture pdf with no initial components.
Definition: Mixture.cpp:41
double getWeight(int i) const
Returns the weight of the i&#39;th component.
Definition: Mixture.cpp:144
std::string toString(const std::string &indent="") const
Represents the Mixture as a string for easier console output.
Definition: Mixture.cpp:162


problib
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:19