contrib/mul/vpdfl/vpdfl_pc_gaussian.h
Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_pc_gaussian.h
00002 #ifndef vpdfl_pc_gaussian_h
00003 #define vpdfl_pc_gaussian_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Interface for Multi-variate Principle Component Gaussian PDF.
00010 // \author Ian Scott
00011 // \date 21-Jul-2000
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   23 April 2001 IMS - Ported to VXL
00016 // \endverbatim
00017 //=======================================================================
00018 
00019 #include <vpdfl/vpdfl_gaussian.h>
00020 #include <vcl_iosfwd.h>
00021 
00022 //=======================================================================
00023 
00024 class vpdfl_pc_gaussian_builder;
00025 
00026 //: Multi-variate principal Component Gaussian PDF.
00027 // The principal components can have general covariance.
00028 // The complementary components (least significant eigenvalues) have a
00029 // spherical distribution.
00030 class vpdfl_pc_gaussian : public vpdfl_gaussian
00031 {
00032   //: The number of dimensions to have full Covariance (i.e. elliptical shape)
00033   // The rest are assumed spherical
00034   unsigned partition_;
00035 
00036   //: The constant when calculating the Mahalanobis dist in the principal space.
00037   double log_k_principal_;
00038 
00039   //: Pre-calculate log_k_principal_.
00040   // Should only be called after setting all other data values.
00041   void calcPartLogK();
00042 
00043   //: How the inherited set() function chooses the partition.
00044   // If NULL, set(const vnl_vector<double>&,const vnl_matrix<double>& evecs,const vnl_vector<double>&)
00045   // will fail.
00046   vpdfl_pc_gaussian_builder * partition_chooser_;
00047 
00048  public:
00049   //: Dflt ctor
00050   vpdfl_pc_gaussian();
00051 
00052   //: Destructor
00053   virtual ~vpdfl_pc_gaussian();
00054 
00055   //: Number of principal components
00056   // i.e. the number of dimensions to have full Covariance (i.e. elliptical shape)
00057   unsigned n_principal_components() const {return partition_;}
00058 
00059   //: Return sampler object for this PDF
00060   //  Object is created on heap. Caller responsible for deletion.
00061   // The sampler object is a vpdfl_gaussian_sampler.
00062   // There is no vpdfl_pc_gaussian_sampler.
00063   virtual vpdfl_sampler_base* sampler() const;
00064 
00065   //: how the inherited set() function chooses the partition
00066   const vpdfl_pc_gaussian_builder * partition_chooser() const;
00067 
00068   //: Set the how the inherited set() function chooses the partition
00069   // If not NULL, a local copy of the builder will be taken.
00070   void set_partition_chooser(const vpdfl_pc_gaussian_builder *);
00071 
00072   //: Initialise safely
00073   // The partition between principal components space and complementary space is
00074   // defined by the length of the Eigenvalues vector (evals.)
00075   // Calculates the variance, and checks that
00076   // the Eigenvalues are ordered and the Eigenvectors are unit normal
00077   // Turn off assertions to remove error checking.
00078   void set(const vnl_vector<double>& mean,
00079            const vnl_matrix<double>& evecs,
00080            const vnl_vector<double>& evals,
00081            double complementEVal);
00082 
00083   //: Initialise safely as you would a vpdfl_gaussian
00084   // Calculates the variance, and checks that
00085   // the Eigenvalues are ordered and the Eigenvectors are unit normal
00086   // Turn off assertions to remove error checking.
00087   // This function takes a fully specified set of Eigenvectors and Eigenvalues, and truncates
00088   // the principal space as defined in partition_chooser() which must not be null.
00089   virtual void set(const vnl_vector<double>& mean,
00090                    const vnl_matrix<double>& evecs,
00091                    const vnl_vector<double>& evals);
00092 
00093   //: log of normalisation constant for Gaussian
00094   double log_k_principal() const { return log_k_principal_; }
00095 
00096   //: Return Mahalanobis and Euclidean distances from centroid to input.
00097   // Strictly it is the normalised Mahalanobis distance (-log_p()) from the input projected into the
00098   // principal space to the centroid, and the Euclidean distance from the input
00099   // to the input projected into the principal space.
00100   // Also, the two values are the squares of the distances.
00101   void get_distances(double &mahalDIFS, double &euclidDFFS, const vnl_vector<double>& x) const;
00102 
00103   //: Calculate the log probability density at position x.
00104   // You could use vpdfl_gaussian::log_p() which would give the same answer,
00105   // but this method, only rotates into the principal components, not the entire rotated space,
00106   // so saving considerable time.
00107   double log_p(const vnl_vector<double>& x) const;
00108 
00109 
00110   //: Version number for I/O
00111   short version_no() const;
00112 
00113   //: Name of the class
00114   virtual vcl_string is_a() const;
00115 
00116   //: Does the name of the class match the argument?
00117   virtual bool is_class(vcl_string const& s) const;
00118 
00119   //: Create a copy on the heap and return base class pointer
00120   virtual vpdfl_pdf_base* clone() const;
00121 
00122   //: Print class to os
00123   virtual void print_summary(vcl_ostream& os) const;
00124 
00125   //: Save class to binary file stream
00126   virtual void b_write(vsl_b_ostream& bfs) const;
00127 
00128 
00129   //: Load class from binary file stream
00130   virtual void b_read(vsl_b_istream& bfs);
00131 };
00132 //=======================================================================
00133 
00134 #endif // vpdfl_pc_gaussian_h