00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_ACCU_STAT_VAR_HH
00027 # define MLN_ACCU_STAT_VAR_HH
00028
00033
00034 # include <cmath>
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/algebra/vec.hh>
00037 # include <mln/algebra/mat.hh>
00038 # include <mln/fun/i2v/all_to.hh>
00039 # include <mln/util/pix.hh>
00040
00041
00042 namespace mln
00043 {
00044
00045 namespace accu
00046 {
00047
00048 namespace stat
00049 {
00050
00056
00057 template <typename T>
00058 struct var : public mln::accu::internal::base< algebra::mat< T::dim, T::dim, float >,
00059 var<T> >
00060 {
00061 enum { dim = T::dim };
00062 typedef T argument;
00063 typedef algebra::mat<dim,dim,float> result;
00064
00065 var();
00066
00069 void init();
00070 void take(const argument& v);
00071 void take(const var<T>& other);
00072
00073 void take_as_init_(const argument& v);
00074 void take_n_times_(unsigned n_times, const argument& v);
00076
00078 result to_result() const;
00079
00081 result variance() const;
00082
00084 unsigned n_items() const;
00085
00086
00088 typedef algebra::vec<dim,float> mean_t;
00089
00090
00092 mean_t mean() const;
00093
00095 bool is_valid() const;
00096
00097 protected:
00098
00099 unsigned n_;
00100 algebra::vec<dim,float> sum_;
00101 algebra::mat<dim,dim,float> cov_;
00102 };
00103
00104
00105
00106 # ifndef MLN_INCLUDE_ONLY
00107
00108 template <typename T>
00109 inline
00110 var<T>::var()
00111 {
00112 init();
00113 }
00114
00115 template <typename T>
00116 inline
00117 void
00118 var<T>::init()
00119 {
00120 n_ = 0;
00121 sum_.set_all(0);
00122 cov_.set_all(0);
00123 }
00124
00125 template <typename T>
00126 inline
00127 void
00128 var<T>::take(const argument& v)
00129 {
00130 ++n_;
00131 sum_ += v;
00132 cov_ += v * v.t();
00133 }
00134
00135 template <typename T>
00136 inline
00137 void
00138 var<T>::take(const var<T>& other)
00139 {
00140 n_ += other.n_;
00141 cov_ += other.cov_;
00142 }
00143
00144 template <typename T>
00145 inline
00146 void
00147 var<T>::take_as_init_(const argument& v)
00148 {
00149 n_ = 1;
00150 sum_ = v;
00151 cov_ = v * v.t();
00152 }
00153
00154 template <typename T>
00155 inline
00156 void
00157 var<T>::take_n_times_(unsigned n_times, const argument& v)
00158 {
00159 n_ += n_times;
00160 sum_ += n_times * v;
00161 cov_ += n_times * v * v.t();
00162 }
00163
00164
00165 template <typename T>
00166 inline
00167 mln_result(var<T>)
00168 var<T>::to_result() const
00169 {
00170 static result null_ = literal::zero;
00171
00172 if (n_ == 0u)
00173 return null_;
00174
00175 return (cov_ - sum_ * sum_.t() / n_) / n_;
00176
00177
00178
00179 }
00180
00181 template <typename T>
00182 inline
00183 mln_result(var<T>)
00184 var<T>::variance() const
00185 {
00186 return to_result();
00187 }
00188
00189 template <typename T>
00190 inline
00191 unsigned
00192 var<T>::n_items() const
00193 {
00194 return n_;
00195 }
00196
00197 template <typename T>
00198 inline
00199 typename var<T>::mean_t
00200 var<T>::mean() const
00201 {
00202 static algebra::vec<dim,float> null_ = literal::zero;
00203
00204 if (n_ == 0u)
00205 return null_;
00206
00207 return sum_ / n_;
00208 }
00209
00210 template <typename T>
00211 inline
00212 bool
00213 var<T>::is_valid() const
00214 {
00215 return n_ != 0;
00216 }
00217
00218 # endif // ! MLN_INCLUDE_ONLY
00219
00220 }
00221
00222 }
00223
00224 }
00225
00226
00227 #endif // ! MLN_ACCU_STAT_VAR_HH