vpdl_kernel_base.h
Go to the documentation of this file.
1 // This is core/vpdl/vpdl_kernel_base.h
2 #ifndef vpdl_kernel_base_h_
3 #define vpdl_kernel_base_h_
4 //:
5 // \file
6 // \author Matthew Leotta
7 // \date February 24, 2009
8 // \brief Base classes for kernel (aka Parzen window) distributions
9 //
10 // \verbatim
11 // Modifications
12 // None
13 // \endverbatim
14 
15 #include <vector>
17 #include <vpdl/vpdt/vpdt_access.h> // function vpdt_size(v)
18 #include <cassert>
19 #ifdef _MSC_VER
20 # include <vcl_msvc_warnings.h>
21 #endif
22 
23 //: A base class for kernel (aka Parzen window) distributions
24 // A kernel distribution is restricted form of a mixture where each component
25 // has the same weight and takes the same form. Essentially, a copy of a single
26 // distribution is translated (and possibly scaled) to each point in a collection
27 // of samples
28 template<class T, unsigned int n=0>
30 {
31  public:
32  //: the data type used for vectors
34  //: the data type used for matrices
36 
37  // Default Constructor
39 
40  // Constructor from sample points
41  vpdl_kernel_base(const std::vector<vector>& samplez)
42  : samples_(samplez) {}
43 
44  //: Return the number of components in the mixture
45  unsigned int num_components() const { return samples_.size(); }
46 
47  //: Return the run time dimension, which does not equal \c n when \c n==0
48  virtual unsigned int dimension() const
49  {
50  if (n > 0 || num_components() == 0)
51  return n;
52  return vpdt_size(samples_[0]);
53  }
54 
55  //: Add a new sample point
56  virtual void add_sample(const vector& s)
57  {
58  // set variable dimension from the first inserted component
59  assert(vpdt_size(s) == this->dimension() || num_components() == 0);
60  samples_.push_back(s);
61  }
62 
63  //: Remove all sample points
64  virtual void clear_samples()
65  {
66  samples_.clear();
67  }
68 
69  //: Set the collection of sample points
70  virtual void set_samples(const std::vector<vector>& samplez)
71  {
72  samples_ = samplez;
73  }
74 
75  //: Access the sample points
76  const std::vector<vector>& samples() const
77  {
78  return samples_;
79  }
80 
81  //: Compute the mean of the distribution.
82  // Assume that each kernel has its mean at the sample point
83  virtual void compute_mean(vector& mean) const
84  {
85  const unsigned int d = this->dimension();
86  vpdt_set_size(mean,d);
87  vpdt_fill(mean,T(0));
88  if (samples_.empty())
89  return;
90  typedef typename std::vector<vector>::const_iterator samp_itr;
91  for (samp_itr s = samples_.begin(); s != samples_.end(); ++s) {
92  mean += *s;
93  }
94  mean /= T(samples_.size());
95  }
96 
97  private:
98  //: The sample points around which the kernels are centered
99  std::vector<vector> samples_;
100 };
101 
102 
103 //: A base class for fixed bandwidth kernel distributions
104 // This class assumes that the bandwidth is fixed for all kernels
105 template<class T, unsigned int n=0>
107 {
108  public:
109  //: the data type used for vectors
111  //: the data type used for matrices
113 
114  // Default Constructor
116  : bandwidth_(T(1)) {}
117 
118  // Constructor from sample points and a bandwidth
119  vpdl_kernel_fbw_base(const std::vector<vector>& samplez, T bandwid = T(1))
120  : vpdl_kernel_base<T,n>(samplez), bandwidth_(bandwid) {}
121 
122  //: Access the bandwidth
123  T bandwidth() const { return bandwidth_; }
124 
125  //: Set the kernel bandwidth
126  void set_bandwidth(T b) { bandwidth_ = b; }
127 
128  //: The normalization constant for the kernel
129  virtual T kernel_norm_const() const = 0;
130 
131  //: The normalization constant for the density
132  // When density() is multiplied by this value it becomes prob_density
133  // norm_const() is reciprocal of the integral of density over the entire field
134  virtual T norm_const() const
135  {
136  return kernel_norm_const()/this->num_components();
137  }
138 
139  private:
140  //: the fixed bandwidth for all kernels
142 };
143 
144 
145 //: A base class for variable bandwidth kernel distributions
146 // This class assumes that each sample has its own bandwidth
147 template<class T, unsigned int n=0>
149 {
150  public:
151  //: the data type used for vectors
153  //: the data type used for matrices
155 
156  // Default Constructor
157  vpdl_kernel_vbw_base(unsigned int var_dim = n)
158  : vpdl_kernel_base<T,n>(var_dim) {}
159 
160  // Constructor from sample points and bandwidths
161  vpdl_kernel_vbw_base(const std::vector<vector>& samplez,
162  const std::vector<T>& bandwidthz)
163  : vpdl_kernel_base<T,n>(samplez), bandwidths_(bandwidthz) {}
164 
165  //: Add a new sample point
166  virtual void add_sample(const vector& s)
167  {
169  bandwidths_.push_back(T(1));
170  }
171 
172  //: Add a new sample point with bandwidth
173  virtual void add_sample(const vector& s, T bw)
174  {
176  bandwidths_.push_back(bw);
177  }
178 
179  //: Remove all sample points
180  virtual void clear_samples()
181  {
183  bandwidths_.clear();
184  }
185 
186  //: Set the collection of sample points
187  virtual void set_samples(const std::vector<vector>& samplez)
188  {
190  bandwidths_.clear();
191  bandwidths_.resize(samplez.size(),T(1));
192  }
193 
194  //: Set the collection of sample points and bandwidths
195  virtual void set_samples(const std::vector<vector>& samplez,
196  const std::vector<T>& bandwidthz)
197  {
198  assert(samplez.size() == bandwidthz.size());
200  bandwidths_ = bandwidthz;
201  }
202 
203  //: Access the bandwidths
204  const std::vector<T>& bandwidths() const { return bandwidths_; }
205 
206  private:
207  //: the bandwidths for each kernel
208  std::vector<T> bandwidths_;
209 };
210 
211 
212 #endif // vpdl_kernel_base_h_
virtual unsigned int dimension() const
Return the run time dimension, which does not equal n when n==0.
T bandwidth() const
Access the bandwidth.
virtual void clear_samples()
Remove all sample points.
virtual void add_sample(const vector &s)
Add a new sample point.
virtual void compute_mean(vector &mean) const
Compute the mean of the distribution.
base class for multiple component distributions
vpdl_kernel_base(const std::vector< vector > &samplez)
virtual void clear_samples()
Remove all sample points.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
virtual T kernel_norm_const() const =0
The normalization constant for the kernel.
vpdt_field_traits< vector >::matrix matrix
the data type used for matrices.
virtual void set_samples(const std::vector< vector > &samplez)
Set the collection of sample points.
vpdt_field_traits< vector >::matrix_type matrix
the data type used for matrices.
virtual void add_sample(const vector &s)
Add a new sample point.
unsigned int num_components() const
Return the number of components in the mixture.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
std::vector< T > bandwidths_
the bandwidths for each kernel.
vpdl_kernel_vbw_base(unsigned int var_dim=n)
virtual void set_samples(const std::vector< vector > &samplez)
Set the collection of sample points.
A base class for kernel (aka Parzen window) distributions.
virtual void add_sample(const vector &s, T bw)
Add a new sample point with bandwidth.
void set_bandwidth(T b)
Set the kernel bandwidth.
T bandwidth_
the fixed bandwidth for all kernels.
vpdt_field_default< T, n >::type vector
the data type used for vectors.
void vpdt_set_size(vnl_vector< T > &v, unsigned int s)
Set the size of a vnl_vector.
Definition: vpdt_access.h:63
The field traits class (scalar).
virtual void set_samples(const std::vector< vector > &samplez, const std::vector< T > &bandwidthz)
Set the collection of sample points and bandwidths.
vpdl_kernel_fbw_base(const std::vector< vector > &samplez, T bandwid=T(1))
The base class for all multiple component probability distributions.
Overloaded functions to allow uniform API access to various field types.
const std::vector< vector > & samples() const
Access the sample points.
vpdl_kernel_vbw_base(const std::vector< vector > &samplez, const std::vector< T > &bandwidthz)
virtual T norm_const() const
The normalization constant for the density.
vpdt_field_traits< vector >::matrix_type matrix
the data type used for matrices.
unsigned int vpdt_size(const vnl_vector< T > &v)
Access the size of a vnl_vector.
Definition: vpdt_access.h:36
void vpdt_fill(vnl_vector< T > &v, const T &val)
Fill a vnl_vector.
Definition: vpdt_access.h:78
A base class for variable bandwidth kernel distributions.
const std::vector< T > & bandwidths() const
Access the bandwidths.
std::vector< vector > samples_
The sample points around which the kernels are centered.
A base class for fixed bandwidth kernel distributions.