1 #ifndef mbl_stats_nd_h_ 2 #define mbl_stats_nd_h_ 3 4 //: 5 // \file 6 // \brief Simple statistics (mean, variance) on vectors. 7 // \author Tim Cootes 8 9 #include <iostream> 10 #include <iosfwd> 11 #ifdef _MSC_VER 12 # include <vcl_msvc_warnings.h> 13 #endif 14 #include <vsl/vsl_binary_io.h> 15 #include <vnl/vnl_vector.h> 16 17 //: Simple statistics (mean, variance) on vectors. 18 // Note: Uses unbiased estimate of variance (ie divide by (n_obs()-1)) 19 class mbl_stats_nd 20 { 21 vnl_vector<double> sum_; 22 vnl_vector<double> sum_sq_; 23 unsigned n_obs_; 24 public: 25 mbl_stats_nd() ; 26 27 //: Remove all data 28 void clear(); 29 30 //: Add given observation 31 void obs(const vnl_vector<double>& v); 32 33 //: Number of observations n_obs()34 unsigned n_obs() const { return n_obs_; } 35 36 //: Mean of current observations 37 vnl_vector<double> mean() const ; 38 39 //: Standard deviation of current observations 40 vnl_vector<double> sd() const; 41 //: Standard error (sd of estimate of mean) of current observations 42 vnl_vector<double> stdError() const; 43 //: Variance of current observations 44 vnl_vector<double> variance() const; 45 46 //: Sum of current observations sum()47 const vnl_vector<double>& sum() const { return sum_; } 48 49 //: Sum of squares of current observations sumSq()50 const vnl_vector<double> & sumSq() const { return sum_sq_; } 51 52 //: Add statistics together 53 mbl_stats_nd& operator+=(const mbl_stats_nd& s1); 54 void print_summary(std::ostream& os) const; 55 //: Version number for I/O 56 short version_no() const; 57 void b_write(vsl_b_ostream& bfs) const; 58 void b_read(vsl_b_istream& bfs); 59 60 //: Test for equality 61 bool operator==(const mbl_stats_nd& s) const; 62 63 friend 64 mbl_stats_nd operator+(const mbl_stats_nd& s1, const mbl_stats_nd& s2); 65 }; 66 67 //: Binary file stream output operator for class reference 68 void vsl_b_write(vsl_b_ostream& bfs, const mbl_stats_nd& b); 69 70 //: Binary file stream input operator for class reference 71 void vsl_b_read(vsl_b_istream& bfs, mbl_stats_nd& b); 72 73 //: Stream output operator for class reference 74 std::ostream& operator<<(std::ostream& os,const mbl_stats_nd& stats); 75 76 //: Stream output operator for class reference 77 void vsl_print_summary(std::ostream& os,const mbl_stats_nd& stats); 78 79 #endif 80