running_stat_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup running_stat
00017 //! @{
00018 
00019 
00020 
00021 template<typename eT>
00022 running_stat<eT>::running_stat()
00023   : N           (typename running_stat<eT>::T(0))
00024   , acc1        (                          eT(0))
00025   , acc2        (typename running_stat<eT>::T(0))
00026   , min_val     (                          eT(0))
00027   , max_val     (                          eT(0))
00028   , min_val_norm(typename running_stat<eT>::T(0))
00029   , max_val_norm(typename running_stat<eT>::T(0))
00030   {
00031   arma_extra_debug_sigprint_this(this);
00032   }
00033 
00034 
00035 
00036 //! update statistics to reflect new sample
00037 template<typename eT>
00038 inline
00039 void
00040 running_stat<eT>::operator() (const typename running_stat<eT>::T sample)
00041   {
00042   arma_extra_debug_sigprint();
00043 
00044   running_stat_aux::update_stats(*this, sample);
00045   }
00046 
00047 
00048 
00049 //! update statistics to reflect new sample (version for complex numbers)
00050 template<typename eT>
00051 inline
00052 void
00053 running_stat<eT>::operator() (const std::complex< typename running_stat<eT>::T >& sample)
00054   {
00055   arma_extra_debug_sigprint();
00056 
00057   isnt_same_type<eT, std::complex< typename running_stat<eT>::T > >::check();
00058 
00059   running_stat_aux::update_stats(*this, sample);
00060   }
00061 
00062 
00063 
00064 //! set all statistics to zero
00065 template<typename eT>
00066 inline
00067 void
00068 running_stat<eT>::reset()
00069   {
00070   arma_extra_debug_sigprint();
00071 
00072   typedef typename running_stat<eT>::T T;
00073   
00074   N            =  T(0);
00075   
00076   acc1         = eT(0);
00077   acc2         =  T(0);
00078   
00079   min_val      = eT(0);
00080   max_val      = eT(0);
00081   
00082   min_val_norm =  T(0);
00083   max_val_norm =  T(0);
00084   }
00085 
00086 
00087 
00088 //! mean or average value
00089 template<typename eT>
00090 inline
00091 eT
00092 running_stat<eT>::mean() const
00093   {
00094   arma_extra_debug_sigprint();
00095 
00096   typedef typename running_stat<eT>::T T;
00097   
00098   if(N > T(0))
00099     {
00100     return acc1 / N;
00101     }
00102   else
00103     {
00104     return eT(0);
00105     }
00106   }
00107 
00108 
00109 
00110 //! variance
00111 template<typename eT>
00112 inline
00113 typename running_stat<eT>::T
00114 running_stat<eT>::var(const u32 norm_type) const
00115   {
00116   arma_extra_debug_sigprint();
00117 
00118   return running_stat_aux::var(*this, norm_type);
00119   }
00120 
00121 
00122 
00123 //! standard deviation
00124 template<typename eT>
00125 inline
00126 typename running_stat<eT>::T
00127 running_stat<eT>::stddev(const u32 norm_type) const
00128   {
00129   arma_extra_debug_sigprint();
00130 
00131   return std::sqrt( running_stat_aux::var(*this, norm_type) );
00132   }
00133 
00134 
00135 
00136 //! minimum value
00137 template<typename eT>
00138 inline
00139 eT
00140 running_stat<eT>::min() const
00141   {
00142   arma_extra_debug_sigprint();
00143 
00144   return min_val;
00145   }
00146 
00147 
00148 
00149 //! maximum value
00150 template<typename eT>
00151 inline
00152 eT
00153 running_stat<eT>::max() const
00154   {
00155   arma_extra_debug_sigprint();
00156 
00157   return max_val;
00158   }
00159 
00160 
00161 
00162 //! update statistics to reflect new sample
00163 template<typename eT>
00164 inline
00165 void
00166 running_stat_aux::update_stats(running_stat<eT>& x, const eT sample)
00167   {
00168   arma_extra_debug_sigprint();
00169 
00170   if(x.N > eT(0))
00171     {
00172     if(sample < x.min_val)
00173       {
00174       x.min_val = sample;
00175       }
00176     
00177     if(sample > x.max_val)
00178       {
00179       x.max_val = sample;
00180       }
00181     }
00182   else
00183     {
00184     x.min_val = sample;
00185     x.max_val = sample;
00186     }
00187   
00188   x.N++;
00189   
00190   x.acc1 += sample;
00191   x.acc2 += sample*sample;
00192   }
00193 
00194 
00195 
00196 //! update statistics to reflect new sample (version for complex numbers)
00197 template<typename T>
00198 inline
00199 void
00200 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const T sample)
00201   {
00202   arma_extra_debug_sigprint();
00203 
00204   running_stat_aux::update_stats(x, std::complex<T>(sample));
00205   }
00206 
00207 
00208 
00209 //! alter statistics to reflect new sample (version for complex numbers)
00210 template<typename T>
00211 inline
00212 void
00213 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const std::complex<T>& sample)
00214   {
00215   arma_extra_debug_sigprint();
00216 
00217   const T sample_norm = std::norm(sample);
00218   
00219   if(x.N > T(0))
00220     {
00221     if(sample_norm < x.min_val_norm)
00222       {
00223       x.min_val_norm = sample_norm;
00224       x.min_val      = sample;
00225       }
00226     
00227     if(sample_norm > x.max_val_norm)
00228       {
00229       x.max_val_norm = sample_norm;
00230       x.max_val      = sample;
00231       }
00232     }
00233   else
00234     {
00235     x.min_val      = sample;
00236     x.max_val      = sample;
00237     x.min_val_norm = sample_norm;
00238     x.max_val_norm = sample_norm;
00239     }
00240   
00241   x.N++;
00242   
00243   x.acc1 += sample;
00244   x.acc2 += sample_norm;
00245   }
00246 
00247 
00248 
00249 //! variance
00250 template<typename eT>
00251 inline
00252 eT
00253 running_stat_aux::var(const running_stat<eT>& x, const u32 norm_type)
00254   {
00255   arma_extra_debug_sigprint();
00256 
00257   if(x.N > eT(1))
00258     {
00259     const eT norm_val = (norm_type == 0) ? (x.N-eT(1)) : x.N;
00260     const eT var_val  = (x.acc2 - x.acc1*x.acc1/x.N) / norm_val;
00261     return var_val;
00262     }
00263   else
00264     {
00265     return eT(0);
00266     }
00267   }
00268 
00269 
00270 
00271 //! variance (version for complex numbers)
00272 template<typename T>
00273 inline
00274 T
00275 running_stat_aux::var(const running_stat< std::complex<T> >& x, const u32 norm_type)
00276   {
00277   arma_extra_debug_sigprint();
00278 
00279   if(x.N > T(1))
00280     {
00281     const T norm_val = (norm_type == 0) ? (x.N-T(1)) : x.N;
00282     const T var_val  = (x.acc2 - std::norm(x.acc1)/x.N) / norm_val;
00283     return var_val;
00284     }
00285   else
00286     {
00287     return T(0);
00288     }
00289   }
00290 
00291 
00292 
00293 //! @}