contrib/gel/vifa/vifa_incr_var.h
Go to the documentation of this file.
00001 // This is gel/vifa/vifa_incr_var.h
00002 #ifndef _VIFA_INCR_VAR_H_
00003 #define _VIFA_INCR_VAR_H_
00004 //-----------------------------------------------------------------------------
00005 //:
00006 // \file
00007 // \brief Incremental variance measures.
00008 //
00009 // The vifa_incr_var class is used to accumulate the mean & variance of a data
00010 // set incrementally, one data sample at a time.
00011 //
00012 // \author Anthony Hoogs, from DDB in TargetJr
00013 //
00014 // \verbatim
00015 //  Modifications:
00016 //   MPP Mar 2003, Ported to VXL
00017 // \endverbatim
00018 //-----------------------------------------------------------------------------
00019 
00020 #include <vcl_vector.h>
00021 #include <vbl/vbl_ref_count.h>
00022 #include <vbl/vbl_bounding_box.h>
00023 #include <vbl/vbl_smart_ptr.h>
00024 #include <vul/vul_timestamp.h>
00025 
00026 //: Compute the mean & variance measures of a data set.
00027 //
00028 // The vifa_incr_var class is used to accumulate the mean & variance of a data
00029 // set incrementally, one data sample at a time.
00030 
00031 class vifa_incr_var : public vul_timestamp, public vbl_ref_count
00032 {
00033  protected:
00034   //: The current mean of the data set
00035   double data_mean_;
00036 
00037   //: The current variance of the data set
00038   double data_var_;
00039 
00040   //: The number of samples in the data set
00041   int    n_;
00042 
00043   //: The minimum-value and maximum-value sample of the data set
00044   vbl_bounding_box<double,1> min_max_;
00045 
00046  public:
00047   //: Default constructor
00048   vifa_incr_var() : data_mean_(0.0), data_var_(0.0), n_(0) {}
00049 
00050   // copy constructor - compiler-provided one sets ref_count to nonzero which is wrong -PVr
00051   vifa_incr_var(vifa_incr_var const& v)
00052     : vul_timestamp(), vbl_ref_count(),
00053       data_mean_(v.data_mean_), data_var_(v.data_var_),
00054       n_(v.n_), min_max_(v.min_max_) {}
00055 
00056   //: Fetch the current mean
00057   double get_mean() const { return data_mean_; }
00058 
00059   //: Fetch the current variance
00060   double get_var() const { return data_var_; }
00061 
00062   //: Fetch the number of samples received so far
00063   int    get_n() const { return n_; }
00064 
00065   //: Fetch the minimum-value sample of the data set
00066   double get_min() const { return min_max_.min()[0]; }
00067 
00068   //: Fetch the maximum-value sample of the data set
00069   double get_max() const { return min_max_.max()[0]; }
00070 
00071   //: Update the mean & variance measures with a new sample
00072   // This method is a convenience front-end for add_sample() (see below)
00073   void   add_sample(double data_point); // !< The new data sample
00074 
00075   //: Update the mean & variance measures with a new sample
00076   void   add_sample(double data_point,  // !< The new data sample
00077                     double prev_factor, // !< Ratio of current n_ & new n_
00078                     double curr_factor);// !< Inverse of new n_
00079 };
00080 
00081 typedef vbl_smart_ptr<vifa_incr_var>   vifa_incr_var_sptr;
00082 typedef vcl_vector<vifa_incr_var_sptr> incr_var_list;
00083 
00084 #endif // _VIFA_INCR_VAR_H_