contrib/mul/mbl/mbl_stats_1d.h
Go to the documentation of this file.
00001 #ifndef mbl_stats_1d_h_
00002 #define mbl_stats_1d_h_
00003 
00004 //:
00005 // \file
00006 // \brief Simple statistics on a 1D variable.
00007 // \author Tim Cootes
00008 
00009 #include <vcl_iosfwd.h>
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vcl_vector.h>
00012 
00013 // windows thinks min and max are macros in this file, but they
00014 // are not, don't know where they are defined (somewhere in vxl!)
00015 #undef min
00016 #undef max
00017 
00018 //: Simple statistics on a 1D variable
00019 // \code
00020 //   // A rather trivial example
00021 //   mbl_stats_1d stats,odd_stats,even_stats,sum_stats;
00022 //
00023 //   const int n = 10;
00024 //   for (unsigned i=0;i<n;i++)
00025 //   {
00026 //     stats.obs(i);
00027 //     if (i%2) even_stats.obs(i);
00028 //     else     odd_stats.obs(i);
00029 //   }
00030 //
00031 //   vcl_cout << stats << "\nStats of odd numbers :\n" << odd_stats;
00032 //
00033 //   sum_stats = odd_stats + even_stats;
00034 //
00035 //   vcl_cout << "Sum of odd and even stats\n" << sum_stats;
00036 // \endcode
00037 class mbl_stats_1d
00038 {
00039   double sum_;
00040   double sum_sq_;
00041   double min_v_;
00042   double max_v_;
00043   unsigned n_obs_;
00044   double w_obs_;
00045 
00046 
00047 public:
00048 
00049   //: Default constructor
00050   mbl_stats_1d();
00051 
00052   //: Construct with a set of observations
00053   mbl_stats_1d(const vcl_vector<double>& observations);
00054 
00055     //: Remove all data
00056   void clear();
00057 
00058     //: Add given observation
00059   void obs(double v);
00060 
00061     //: Add given weighted observation
00062     // \p weight doesn't affect the max or min records.
00063   void obs(double v, double weight);
00064 
00065     //: Number of discrete observations.
00066   int nObs() const { return n_obs_; }
00067 
00068     //: Weight of all observations
00069   double wObs() const { return w_obs_; }
00070 
00071     //: Mean of current observations
00072   double mean() const ;
00073 
00074 
00075     //: Standard deviation of current observations
00076   double sd() const;
00077     //: Standard error (sd of estimate of mean) of current observations
00078   double stdError() const;
00079     //: Variance of current observations
00080   double variance() const;
00081     //: Min of current observations
00082   double min() const;
00083     //: Max of current observations
00084   double max() const;
00085     //: Sum of current observations
00086   double sum() const;
00087     //: Sum of squares of current observations
00088   double sumSq() const;
00089 
00090   //: RMS of current observations;
00091   // \note If nobs==0, returns -1.0
00092   double rms() const;
00093 
00094     //: Add statistics together
00095   mbl_stats_1d& operator+=(const mbl_stats_1d& s1);
00096   void print_summary(vcl_ostream& os) const;
00097     //: Version number for I/O
00098   void b_write(vsl_b_ostream& bfs) const;
00099   void b_read(vsl_b_istream& bfs);
00100 
00101     //: Test for equality
00102   bool operator==(const mbl_stats_1d& s) const;
00103 
00104   friend
00105   mbl_stats_1d operator+(const mbl_stats_1d& s1, const mbl_stats_1d& s2);
00106 };
00107 
00108 //: Binary file stream output operator for class reference
00109 void vsl_b_write(vsl_b_ostream& bfs, const mbl_stats_1d& b);
00110 
00111 //: Binary file stream input operator for class reference
00112 void vsl_b_read(vsl_b_istream& bfs, mbl_stats_1d& b);
00113 
00114 //: Stream output operator for class reference
00115 vcl_ostream& operator<<(vcl_ostream& os,const mbl_stats_1d& stats);
00116 
00117 //: Stream output operator for class reference
00118 void vsl_print_summary(vcl_ostream& os,const mbl_stats_1d& stats);
00119 
00120 #endif