contrib/rpl/rrel/rrel_kernel_density_obj.h
Go to the documentation of this file.
00001 #ifndef rrel_kernel_density_obj_h_
00002 #define rrel_kernel_density_obj_h_
00003 //:
00004 //  \file
00005 //  \brief Kernel Density objective function
00006 //  \author Ying-Lin Bess Lee (leey@cs.rpi.edu)
00007 //  \date Aug 2002
00008 
00009 #include <rrel/rrel_objective.h>
00010 
00011 enum rrel_kernel_scale_type { RREL_KERNEL_MAD, RREL_KERNEL_PRIOR, RREL_KERNEL_MUSE };
00012 
00013 
00014 //: Kernel Density objective function.
00015 //  Implements the Kernel Density Estimation as presented in the
00016 //  paper "Robust Computer Vision through Kernel Density" by Chen
00017 //  and Meer, 2002.
00018 //  Given residuals ri, i = 1,...,n, the cost function is the estimated
00019 //  density f(x) based on a kernel function K(u) and a bandwidth h as
00020 //  f(x) = -1 / (nh) * sum( K(u) )
00021 //  where
00022 //  u = (ri-x)/h
00023 //  K(u) = 1.09375 * (1 - u^2)^3
00024 //  h = [243 * R(K) / 35 / Mu(K)^2 / n]^0.2 * scale
00025 //  The scale can be provided as a prior scale, or computed by MAD or MUSE.
00026 
00027 class rrel_kernel_density_obj : public rrel_objective
00028 {
00029  public:
00030   //: Constructor.
00031   rrel_kernel_density_obj(rrel_kernel_scale_type scale_type=RREL_KERNEL_MAD);
00032 
00033   //: Destructor.
00034   virtual ~rrel_kernel_density_obj() {}
00035 
00036   //: Evaluate the objective function on heteroscedastic residuals.
00037   //  Not implemented.
00038   //  \sa rrel_objective::fcn.
00039   virtual double fcn( vect_const_iter res_begin, vect_const_iter res_end,
00040                       vect_const_iter scale_begin,
00041                       vnl_vector<double>* param_vector=0 ) const;
00042 
00043   //: Evaluate the objective function on homoscedastic residuals.
00044   //  prior_scale is needed if the type RREL_KERNEL_PRIOR is used.
00045   //  \sa rrel_objective::fcn.
00046   virtual double fcn( vect_const_iter res_begin, vect_const_iter res_end,
00047                       double prior_scale = 0,
00048                       vnl_vector<double>* = 0) const;
00049 
00050   //: Set the type of the scale.
00051   //  RREL_KERNEL_MAD uses median absolute deviations to estimate the scale.
00052   //  RREL_KERNEL_PRIOR uses the prior scale provided.
00053   //  RREL_KERNEL_MUSE uses MUSE to estimate the scale.
00054   virtual void set_scale_type( rrel_kernel_scale_type t = RREL_KERNEL_MAD )
00055   { scale_type_ = t; }
00056 
00057   //: Depends on the scale type used.
00058   //  \sa rrel_objective::requires_prior_scale.
00059   virtual bool requires_prior_scale() const
00060   { return scale_type_ == RREL_KERNEL_PRIOR; }
00061 
00062   //: x is set to 0;
00063   void fix_x() { fix_x_ = true; }
00064 
00065   //: The mode of the density estimate which maximizes the estimated kernel density.
00066   //  The value can be used to shift the estimated parameters.
00067   double best_x( vect_const_iter res_begin, vect_const_iter res_end,
00068                  double scale = 0 ) const;
00069  private:
00070 
00071   //: Calculate the bandwidth.
00072   double bandwidth(vect_const_iter res_begin, vect_const_iter res_end,
00073                    double prior_scale) const;
00074 
00075   //: Given a kernel and the bandwidth, the estimated density of residuals.
00076   double kernel_density(vect_const_iter res_begin, vect_const_iter res_end,
00077                         double x, double h) const;
00078 
00079   //: Kernel function K(u).
00080   double kernel_function(double u) const;
00081 
00082   rrel_kernel_scale_type scale_type_;
00083 
00084   bool fix_x_;
00085 };
00086 
00087 #endif