1 #ifndef mbl_stats_1d_h_
2 #define mbl_stats_1d_h_
3 
4 //:
5 // \file
6 // \brief Simple statistics on a 1D variable.
7 // \author Tim Cootes
8 
9 #include <iostream>
10 #include <iosfwd>
11 #include <vector>
12 #ifdef _MSC_VER
13 #  include <vcl_msvc_warnings.h>
14 #endif
15 #include <vsl/vsl_binary_io.h>
16 
17 // windows thinks min and max are macros in this file, but they
18 // are not, don't know where they are defined (somewhere in vxl!)
19 #undef min
20 #undef max
21 
22 //: Simple statistics on a 1D variable
23 // \code
24 //   // A rather trivial example
25 //   mbl_stats_1d stats,odd_stats,even_stats,sum_stats;
26 //
27 //   const int n = 10;
28 //   for (unsigned i=0;i<n;i++)
29 //   {
30 //     stats.obs(i);
31 //     if (i%2) even_stats.obs(i);
32 //     else     odd_stats.obs(i);
33 //   }
34 //
35 //   std::cout << stats << "\nStats of odd numbers :\n" << odd_stats;
36 //
37 //   sum_stats = odd_stats + even_stats;
38 //
39 //   std::cout << "Sum of odd and even stats\n" << sum_stats;
40 // \endcode
41 class mbl_stats_1d
42 {
43   double sum_;
44   double sum_sq_;
45   double min_v_;
46   double max_v_;
47   unsigned n_obs_;
48   double w_obs_;
49 
50 
51 public:
52 
53   //: Default constructor
54   mbl_stats_1d();
55 
56   //: Construct with a set of observations
57   mbl_stats_1d(const std::vector<double>& observations);
58 
59     //: Remove all data
60   void clear();
61 
62     //: Add given observation
63   void obs(double v);
64 
65     //: Add given weighted observation
66     // \p weight doesn't affect the max or min records.
67   void obs(double v, double weight);
68 
69     //: Number of discrete observations.
nObs()70   int nObs() const { return n_obs_; }
71 
72     //: Weight of all observations
wObs()73   double wObs() const { return w_obs_; }
74 
75     //: Mean of current observations
76   double mean() const ;
77 
78 
79     //: Standard deviation of current observations
80   double sd() const;
81     //: Standard error (sd of estimate of mean) of current observations
82   double stdError() const;
83     //: Variance of current observations
84   double variance() const;
85     //: Min of current observations
86   double min() const;
87     //: Max of current observations
88   double max() const;
89     //: Sum of current observations
90   double sum() const;
91     //: Sum of squares of current observations
92   double sumSq() const;
93 
94   //: RMS of current observations;
95   // \note If nobs==0, returns -1.0
96   double rms() const;
97 
98     //: Add statistics together
99   mbl_stats_1d& operator+=(const mbl_stats_1d& s1);
100   void print_summary(std::ostream& os) const;
101     //: Version number for I/O
102   void b_write(vsl_b_ostream& bfs) const;
103   void b_read(vsl_b_istream& bfs);
104 
105     //: Test for equality
106   bool operator==(const mbl_stats_1d& s) const;
107 
108   friend
109   mbl_stats_1d operator+(const mbl_stats_1d& s1, const mbl_stats_1d& s2);
110 };
111 
112 //: Binary file stream output operator for class reference
113 void vsl_b_write(vsl_b_ostream& bfs, const mbl_stats_1d& b);
114 
115 //: Binary file stream input operator for class reference
116 void vsl_b_read(vsl_b_istream& bfs, mbl_stats_1d& b);
117 
118 //: Stream output operator for class reference
119 std::ostream& operator<<(std::ostream& os,const mbl_stats_1d& stats);
120 
121 //: Stream output operator for class reference
122 void vsl_print_summary(std::ostream& os,const mbl_stats_1d& stats);
123 
124 #endif
125