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

AsymmetricStatistics.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Clemens Groepl $
00032 // $Authors: $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H
00036 #define OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H
00037 
00038 #include <OpenMS/CONCEPT/Types.h>
00039 #include <OpenMS/MATH/STATISTICS/BasicStatistics.h>
00040 
00041 #include <vector>
00042 #include <ostream>
00043 #include <cmath>
00044 
00045 namespace OpenMS
00046 {
00047   namespace Math
00048   {
00049 
00058     template <typename Real = DoubleReal>
00059     class AsymmetricStatistics :
00060       public BasicStatistics<Real>
00061     {
00063       typedef BasicStatistics<Real> Base;
00064       typedef typename Base::RealType RealType;
00065 
00066       using Base::clear;
00067       using Base::sum_;
00068       using Base::mean_;
00069       using Base::variance_;
00070 
00071 public:
00072 
00074       AsymmetricStatistics() :
00075         BasicStatistics<>(),
00076         variance1_(0),
00077         variance2_(0)
00078       {}
00079 
00081       RealType variance1() const
00082       {
00083         return variance1_;
00084       }
00085 
00087       RealType variance2() const
00088       {
00089         return variance2_;
00090       }
00091 
00093       template <typename ProbabilityIterator, typename CoordinateIterator>
00094       void update(ProbabilityIterator const probability_begin,
00095                   ProbabilityIterator const probability_end,
00096                   CoordinateIterator  const coordinate_begin)
00097       {
00098         // reuse...
00099         Base::update(probability_begin, probability_end, coordinate_begin);
00100 
00101         const RealType stdev = std::sqrt(variance_);
00102 
00103         RealType sum1 = 0;
00104         RealType sum2 = 0;
00105         variance1_ = 0;
00106         variance2_ = 0;
00107         ProbabilityIterator prob_iter = probability_begin;
00108         CoordinateIterator  coord_iter = coordinate_begin;
00109         for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
00110         {
00111           RealType diff = *coord_iter - mean_;
00112           RealType diff_squared = diff * diff;
00113 
00114           if (diff_squared > variance_)
00115           {
00116             if (*coord_iter < mean_)
00117             {
00118               variance1_ += (*prob_iter * diff_squared);
00119               sum1  += *prob_iter;
00120             }
00121             else             // ( *coord_iter > mean_ )
00122             {
00123               variance2_ += (*prob_iter * diff_squared);
00124               sum2  += *prob_iter;
00125             }
00126           }
00127           else
00128           {
00129             RealType frac = (diff / stdev + 1.) / 2.;
00130             RealType prob_frac = frac * *prob_iter;
00131             variance2_ += prob_frac * diff_squared;
00132             sum2  += prob_frac;
00133             prob_frac = *prob_iter * (1. - frac);
00134             variance1_ += prob_frac * diff_squared;
00135             sum1  += prob_frac;
00136           }
00137         }
00138         variance1_ /= sum1;
00139         variance2_ /= sum2;
00140         return;
00141       }
00142 
00143 protected:
00145       RealType variance1_, variance2_;
00146     };
00147 
00148   }   // namespace Math
00149 
00150 } // namespace OpenMS
00151 
00152 #endif // OPENMS_MATH_STATISTICS_ASYMMETRICSTATISTICS_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:37 using doxygen 1.7.1