Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

AsymmetricStatistics.h (Maintainer: Marcel Grunert)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marcel Grunert $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H
00028 #define OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H
00029 
00030 #include <OpenMS/CONCEPT/Types.h>
00031 #include <OpenMS/MATH/STATISTICS/BasicStatistics.h>
00032 
00033 #include <vector>
00034 #include <ostream>
00035 #include <cmath>
00036 
00037 namespace OpenMS
00038 {
00039   namespace Math
00040   {
00041 
00050     template < typename Real = DoubleReal > class AsymmetricStatistics :
00051       public BasicStatistics<Real>
00052     {
00054       typedef BasicStatistics<Real> Base;
00055       typedef typename Base::RealType RealType;
00056 
00057       Base::clear;
00058       Base::sum_;
00059       Base::mean_;
00060       Base::variance_;
00061       
00062      public:
00063 
00065       AsymmetricStatistics()
00066         : BasicStatistics<>(),
00067           variance1_(0),
00068           variance2_(0)
00069       {}
00070 
00072       RealType variance1() const
00073       {
00074         return variance1_;
00075       }
00076       
00078       RealType variance2() const
00079       {
00080         return variance2_;
00081       }
00082 
00084       template < typename ProbabilityIterator, typename CoordinateIterator > void update( ProbabilityIterator const probability_begin,
00085           ProbabilityIterator const probability_end,
00086           CoordinateIterator  const coordinate_begin)
00087       {
00088         // reuse...
00089         Base::update(probability_begin, probability_end, coordinate_begin);
00090         
00091         const RealType stdev = std::sqrt(variance_);  
00092         
00093         RealType sum1 = 0;
00094         RealType sum2 = 0;
00095         variance1_ = 0;
00096         variance2_ = 0;
00097         ProbabilityIterator prob_iter = probability_begin;
00098         CoordinateIterator  coord_iter = coordinate_begin;
00099         for ( ; prob_iter != probability_end; ++prob_iter, ++coord_iter )
00100         {
00101           RealType diff = *coord_iter - mean_;
00102           RealType diff_squared = diff * diff;
00103 
00104           if (diff_squared > variance_)
00105           {
00106             if (  *coord_iter < mean_ )
00107             {
00108               variance1_ += (*prob_iter * diff_squared);
00109               sum1  += *prob_iter;
00110             }
00111             else // ( *coord_iter > mean_ )
00112             {
00113               variance2_ += (*prob_iter * diff_squared);
00114               sum2  += *prob_iter;
00115             }
00116           }
00117           else
00118           {
00119             RealType frac = ( diff / stdev + 1. ) / 2.;
00120             RealType prob_frac = frac * *prob_iter;
00121             variance2_ += prob_frac * diff_squared;
00122             sum2  += prob_frac;
00123             prob_frac = *prob_iter * (1. - frac);
00124             variance1_ += prob_frac * diff_squared;
00125             sum1  += prob_frac;
00126           }
00127         }
00128         variance1_ /= sum1;
00129         variance2_ /= sum2;
00130         return;
00131       }
00132 
00133      protected:
00135       RealType variance1_, variance2_;
00136     };
00137 
00138   } // namespace Math 
00139 
00140 } // namespace OpenMS
00141 
00142 #endif // OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H

Generated Tue Apr 1 15:36:32 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1