contrib/mul/mbl/mbl_sample_stats_1d.h
Go to the documentation of this file.
00001 #ifndef mbl_sample_stats_1d_h_
00002 #define mbl_sample_stats_1d_h_
00003 //:
00004 // \file
00005 // \brief Provides simple statistics on a 1D variable and stores the samples
00006 // \author Graham Vincent
00007 
00008 #include <vcl_stdexcept.h>
00009 #include <vcl_iostream.h>
00010 #include <vcl_vector.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vsl/vsl_fwd.h>
00013 #include <mbl/mbl_stats_1d.h>
00014 
00015 //: Provides simple statistics on a 1D variable and stores the samples
00016 class mbl_sample_stats_1d
00017 {
00018   vcl_vector<double> samples_;
00019   mbl_stats_1d stats_1d_;
00020   bool use_mvue_;
00021 
00022  public:
00023 
00024   //! Default ctor
00025   mbl_sample_stats_1d();
00026 
00027   //! ctor with a vcl_vector of samples
00028   mbl_sample_stats_1d(const vcl_vector<double> &samples);
00029 
00030   //! ctor with a vnl_vector of samples
00031   mbl_sample_stats_1d(const vnl_vector<double> &samples);
00032 
00033   ~mbl_sample_stats_1d();
00034 
00035   //: Sets type of variance normalisation
00036   // \param mvue if true (the default) the variance uses the "minimum variance unbiased estimator" which normalises by n_samples()-1
00037   //             else will normalise the variance by n_samples()
00038   void set_use_mvue(bool b) { use_mvue_=b; }
00039 
00040   //: Remove all data
00041   void clear();
00042 
00043   //: Add given observation
00044   void add_sample(double v);
00045 
00046   //: Number of samples
00047   unsigned n_samples() const;
00048 
00049   //! vector of samples
00050   const vcl_vector<double>& samples() const { return samples_; }
00051 
00052   //: Mean of current samples
00053   double mean() const ;
00054 
00055   //: Mean of absolutes current samples
00056   double mean_of_absolutes() const ;
00057 
00058   //: Median of current samples
00059   // Take care. if there are no samples, this method returns maximum double (a very large number)
00060   // Always check number of samples first
00061   double median() const;
00062 
00063   //: The last value within the n_th percentile of the distribution.
00064   // \note If there are no samples, this method returns maximum double (a very large number).
00065   // \sa quantile().
00066   double nth_percentile(int n) const;
00067 
00068   //: Calculate a value at a specified quantile \a q.
00069   // \param q Required quantile 0.0 <= q <= 1.0
00070   // \note Linearly interpolates between the 2 sample values bracketing the specified quantile position.
00071   double quantile(double q) const;
00072 
00073   //: Standard deviation of current samples
00074   double sd() const;
00075 
00076   //: Standard error (i.e. sd of estimate of the mean)
00077   double stdError() const;
00078 
00079   //: Variance of current samples
00080   double variance() const;
00081 
00082   //: Returns the skewness of the samples
00083   double skewness() const;
00084 
00085   //: Returns the kurtosis of the samples
00086   double kurtosis() const;
00087 
00088   //: Min of current samples
00089   // Take care. if there are no samples, this method returns maximum double
00090   // Always check number of samples first
00091   double min() const;
00092 
00093   //: Max of current samples
00094   // Take care. if there are no samples, this method returns -ve maximum double
00095   // Always check number of samples first
00096   double max() const;
00097 
00098   //: Sum of current samples
00099   double sum() const;
00100 
00101   //: Sum of squares of current samples
00102   double sum_squares() const;
00103 
00104   //: root mean square of current samples
00105   double rms() const;
00106 
00107   //: Add statistics together
00108   mbl_sample_stats_1d& operator+=(const mbl_sample_stats_1d& s1);
00109 
00110   //: Print summary of data
00111   void print_summary(vcl_ostream& os) const;
00112 
00113   //: Print all data.
00114   // \param delim Separate each value with this character/string.
00115   void print_all(vcl_ostream& os,
00116                  const vcl_string& delim="\n") const;
00117 
00118   //: Version number for I/O
00119   short version_no() const;
00120   void b_write(vsl_b_ostream& bfs) const;
00121   void b_read(vsl_b_istream& bfs);
00122 
00123   //: Test for equality
00124   bool operator==(const mbl_sample_stats_1d& s) const;
00125 
00126   friend
00127     mbl_sample_stats_1d operator+(const mbl_sample_stats_1d& s1, const mbl_sample_stats_1d& s2);
00128 };
00129 
00130 ///////////////////////////////////////////////////////////////////
00131 // Some functions which are convenient when using the stats object
00132 ///////////////////////////////////////////////////////////////////
00133 
00134 // Create a vector of doubles from the values in sample where the
00135 // corresponding value in mask is greater than zero
00136 template <class S, class M>
00137 vcl_vector<double> mbl_apply_mask(const S &sample, const M &mask)
00138 {
00139   if (sample.size()==0 || sample.size()!=mask.size()) throw vcl_runtime_error("Mask should be the same size as the sample and not empty\n");
00140   mbl_sample_stats_1d stats;
00141   typename S::const_iterator sit=sample.begin();
00142   typename M::const_iterator mit=mask.begin();
00143   vcl_vector<double> ret;
00144   for ( ; sit!=sample.end(); ++sit, ++mit)
00145   {
00146     if ((*mit))
00147     {
00148       ret.push_back(*sit);
00149     }
00150   }
00151   return ret;
00152 }
00153 
00154 //: Binary file stream output operator for class reference
00155 void vsl_b_write(vsl_b_ostream& bfs, const mbl_sample_stats_1d& b);
00156 
00157 //: Binary file stream input operator for class reference
00158 void vsl_b_read(vsl_b_istream& bfs, mbl_sample_stats_1d& b);
00159 
00160 //: Stream output operator for class reference
00161 vcl_ostream& operator<<(vcl_ostream& os,const mbl_sample_stats_1d& stats);
00162 
00163 //: Stream output operator for class reference
00164 void vsl_print_summary(vcl_ostream& os,const mbl_sample_stats_1d& stats);
00165 
00166 //: Print all data
00167 void vsl_print_all(vcl_ostream& os, const mbl_sample_stats_1d& stats);
00168 
00169 #endif // mbl_sample_stats_1d_h_