OpenMS
AsymmetricStatistics.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
39 
40 #include <vector>
41 #include <ostream>
42 #include <cmath>
43 
44 namespace OpenMS
45 {
46  namespace Math
47  {
48 
57  template <typename RealT = double>
59  public BasicStatistics<RealT>
60  {
63  typedef typename Base::RealType RealType;
64 
65  using Base::clear;
66  using Base::sum_;
67  using Base::mean_;
68  using Base::variance_;
69 
70 public:
71 
74  BasicStatistics<>(),
75  variance1_(0),
76  variance2_(0)
77  {}
78 
81  {
82  return variance1_;
83  }
84 
87  {
88  return variance2_;
89  }
90 
92  template <typename ProbabilityIterator, typename CoordinateIterator>
93  void update(ProbabilityIterator const probability_begin,
94  ProbabilityIterator const probability_end,
95  CoordinateIterator const coordinate_begin)
96  {
97  // reuse...
98  Base::update(probability_begin, probability_end, coordinate_begin);
99 
100  const RealType stdev = std::sqrt(variance_);
101 
102  RealType sum1 = 0;
103  RealType sum2 = 0;
104  variance1_ = 0;
105  variance2_ = 0;
106  ProbabilityIterator prob_iter = probability_begin;
107  CoordinateIterator coord_iter = coordinate_begin;
108  for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
109  {
110  RealType diff = *coord_iter - mean_;
111  RealType diff_squared = diff * diff;
112 
113  if (diff_squared > variance_)
114  {
115  if (*coord_iter < mean_)
116  {
117  variance1_ += (*prob_iter * diff_squared);
118  sum1 += *prob_iter;
119  }
120  else // ( *coord_iter > mean_ )
121  {
122  variance2_ += (*prob_iter * diff_squared);
123  sum2 += *prob_iter;
124  }
125  }
126  else
127  {
128  RealType frac = (diff / stdev + 1.) / 2.;
129  RealType prob_frac = frac * *prob_iter;
130  variance2_ += prob_frac * diff_squared;
131  sum2 += prob_frac;
132  prob_frac = *prob_iter * (1. - frac);
133  variance1_ += prob_frac * diff_squared;
134  sum1 += prob_frac;
135  }
136  }
137  variance1_ /= sum1;
138  variance2_ /= sum2;
139  return;
140  }
141 
142 protected:
145  };
146 
147  } // namespace Math
148 
149 } // namespace OpenMS
150 
Internal class for asymmetric distributions.
Definition: AsymmetricStatistics.h:60
RealType variance1_
Definition: AsymmetricStatistics.h:144
RealType variance2_
Definition: AsymmetricStatistics.h:144
RealType variance1() const
"variance to the left hand side"
Definition: AsymmetricStatistics.h:80
RealType variance2() const
"variance to the right hand side"
Definition: AsymmetricStatistics.h:86
AsymmetricStatistics()
Default constructor.
Definition: AsymmetricStatistics.h:73
void update(ProbabilityIterator const probability_begin, ProbabilityIterator const probability_end, CoordinateIterator const coordinate_begin)
You can call this as often as you like, using different input vectors.
Definition: AsymmetricStatistics.h:93
RealType variance_
Definition: BasicStatistics.h:263
Base::RealType RealType
Definition: AsymmetricStatistics.h:63
BasicStatistics< RealT > Base
The real type and basic statistics specified as template argument.
Definition: AsymmetricStatistics.h:62
RealType mean_
Definition: BasicStatistics.h:262
Calculates some basic statistical parameters of a distribution: sum, mean, variance,...
Definition: BasicStatistics.h:69
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:74
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:113
RealType sum_
Definition: BasicStatistics.h:264
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:103
RealType variance_
Definition: BasicStatistics.h:263
RealType mean_
Definition: BasicStatistics.h:262
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48