Bayesian Filtering Library  Generated from SVN r
pdf.h
1 // $Id$
2 // Copyright (C) 2002 Klaas Gadeyne <first dot last at gmail dot com>
3  /***************************************************************************
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public *
6  * License as published by the Free Software Foundation; *
7  * version 2 of the License. *
8  * *
9  * As a special exception, you may use this file as part of a free *
10  * software library without restriction. Specifically, if other files *
11  * instantiate templates or use macros or inline functions from this *
12  * file, or you compile this file and link it with other files to *
13  * produce an executable, this file does not by itself cause the *
14  * resulting executable to be covered by the GNU General Public *
15  * License. This exception does not however invalidate any other *
16  * reasons why the executable file might be covered by the GNU General *
17  * Public License. *
18  * *
19  * This library is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22  * Lesser General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public *
25  * License along with this library; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 #ifndef PDF_H
31 #define PDF_H
32 
33 #include <vector>
34 #include <iostream>
35 #include "../wrappers/matrix/vector_wrapper.h"
36 #include "../wrappers/matrix/matrix_wrapper.h"
37 #include "../sample/sample.h"
38 #include "../bfl_err.h"
39 #include "../bfl_constants.h"
40 
41 
42 namespace BFL
43 {
44  using namespace std;
45 
46  // Enum for different sampling methods
47  enum class SampleMthd { DEFAULT, BOXMULLER, CHOLESKY, RIPLEY };
48 
50  template <typename T> class Pdf
51  {
52 
53  public:
55 
57  Pdf(unsigned int dimension=0);
58 
59  // Default Copy Constructor will do the job
60 
62  virtual ~Pdf();
63 
65  virtual Pdf<T>* Clone() const = 0;
66 
68 
84  virtual bool SampleFrom (vector<Sample<T> >& list_samples,
85  const unsigned int num_samples,
86  const SampleMthd method = SampleMthd::DEFAULT,
87  void * args = NULL) const;
88 
90 
100  virtual bool SampleFrom (Sample<T>& one_sample,
101  const SampleMthd method = SampleMthd::DEFAULT,
102  void * args = NULL) const;
103 
105 
108  virtual Probability ProbabilityGet(const T& input) const;
109 
111 
113  unsigned int DimensionGet() const;
114 
116 
118  virtual void DimensionSet(unsigned int dim);
119 
121 
129  virtual T ExpectedValueGet() const;
130 
132 
138  virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const;
139 
140  private:
142  unsigned int _dimension;
143 
144  };
145 
146 template<typename T>
147 Pdf<T>::Pdf(unsigned int dim)
148 {
149  assert((int)dim >= 0);
150 
151  _dimension = dim;
152 #ifdef __CONSTRUCTOR__
153  cout << "Pdf constructor" << endl;
154 #endif
155 }
156 
157 template<typename T>
158 Pdf<T>::~Pdf()
159 {
160 #ifdef __DESTRUCTOR__
161  cout << "Pdf destructor" << endl;
162 #endif
163 }
164 
165 template<typename T> inline unsigned int
166 Pdf<T>::DimensionGet () const
167 {
168  return _dimension;
169 }
170 
171 template<typename T> void
172 Pdf<T>::DimensionSet ( unsigned int dim )
173 {
174  assert((int)dim >= 0);
175  _dimension = dim;
176 }
177 
178 template<typename T> bool
179 Pdf<T>::SampleFrom (vector<Sample<T> >& list_samples,
180  const unsigned int num_samples,
181  const SampleMthd method,
182  void * args) const
183 {
184  list_samples.resize(num_samples);
185  typename vector<Sample<T> >::iterator sample_it;
186  for (sample_it = list_samples.begin(); sample_it != list_samples.end() ; sample_it++)
187  if (!this->SampleFrom(*sample_it, method,args))
188  return false;
189 
190  return true;
191 }
192 
193 template<typename T> bool
194 Pdf<T>::SampleFrom (Sample<T>& one_sample,
195  const SampleMthd method,
196  void * args) const
197 {
198  cerr << "Error Pdf<T>: The SampleFrom function was called, but you didn't implement it!\n";
199  exit(-BFL_ERRMISUSE);
200  return false;
201 }
202 
203 template<typename T> Probability
204 Pdf<T>::ProbabilityGet (const T& input) const
205 {
206  cerr << "Error Pdf<T>: The ProbabilityGet function was called, but you didn't implement it!\n";
207  exit(-BFL_ERRMISUSE);
208  return 1;
209 }
210 
211 template<typename T> T
213 {
214  cerr << "Error Pdf<T>: The ExpectedValueGet function was called, but you didn't implement it!\n";
215  exit(-BFL_ERRMISUSE);
216  T t;
217  return t;
218 }
219 
220 
221 template<typename T> MatrixWrapper::SymmetricMatrix
222 Pdf<T>::CovarianceGet () const
223 {
224  cerr << "Error Pdf<T>: The CovarianceGet function was called, but you didn't implement it!\n";
225  exit(-BFL_ERRMISUSE);
227  return m;
228 }
229 
230 
231 
232 } // End namespace
233 #endif
virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
Class PDF: Virtual Base class representing Probability Density Functions.
Definition: pdf.h:50
virtual bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, const SampleMthd method=SampleMthd::DEFAULT, void *args=NULL) const
Draw multiple samples from the Pdf (overloaded)
unsigned int DimensionGet() const
Get the dimension of the argument.
virtual Probability ProbabilityGet(const T &input) const
Get the probability of a certain argument.
Pdf(unsigned int dimension=0)
Constructor.
virtual T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
virtual ~Pdf()
Destructor.
Class representing a probability (a double between 0 and 1)
Definition: bfl_constants.h:37
virtual void DimensionSet(unsigned int dim)
Set the dimension of the argument.