| OpenMS 2.5.0

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-2020.
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>
38 #include <OpenMS/MATH/STATISTICS/BasicStatistics.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>
58 class AsymmetricStatistics :
59 public BasicStatistics<RealT>
60 {
62 typedef BasicStatistics<RealT> Base;
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
73 AsymmetricStatistics() :
74 BasicStatistics<>(),
75 variance1_(0),
76 variance2_(0)
77 {}
78
80 RealType variance1() const
81 {
82 return variance1_;
83 }
84
86 RealType variance2() const
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:
144 RealType variance1_, variance2_;
145 };
146
147 } // namespace Math
148
149 } // namespace OpenMS
150
OpenMS::Math::BasicStatistics::variance_
RealType variance_
Definition: BasicStatistics.h:257
OpenMS::Math::BasicStatistics::sum_
RealType sum_
Definition: BasicStatistics.h:258
OpenMS::Math::BasicStatistics::RealType
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:74
OpenMS::Math::BasicStatistics::clear
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:103
OpenMS::Math::AsymmetricStatistics::variance1_
RealType variance1_
Definition: AsymmetricStatistics.h:144
OpenMS::Math::BasicStatistics
Calculates some basic statistical parameters of a distribution: sum, mean, variance, and provides the normal approximation.
Definition: BasicStatistics.h:67
OpenMS::Math::AsymmetricStatistics::Base
BasicStatistics< RealT > Base
The real type and basic statistics specified as template argument.
Definition: AsymmetricStatistics.h:62
BasicStatistics.h
Types.h
OpenMS::Math::AsymmetricStatistics::variance2_
RealType variance2_
Definition: AsymmetricStatistics.h:144
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Math
OpenMS::Math::AsymmetricStatistics::variance1
RealType variance1() const
"variance to the left hand side"
Definition: AsymmetricStatistics.h:80
OpenMS::Math::AsymmetricStatistics::variance2
RealType variance2() const
"variance to the right hand side"
Definition: AsymmetricStatistics.h:86
OpenMS::Math::AsymmetricStatistics::update
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
OpenMS::Math::AsymmetricStatistics::RealType
Base::RealType RealType
Definition: AsymmetricStatistics.h:63
OpenMS::Math::AsymmetricStatistics
Internal class for asymmetric distributions.
Definition: AsymmetricStatistics.h:58
OpenMS::Math::AsymmetricStatistics::AsymmetricStatistics
AsymmetricStatistics()
Default constructor.
Definition: AsymmetricStatistics.h:73
OpenMS::Math::BasicStatistics::mean_
RealType mean_
Definition: BasicStatistics.h:256
OpenMS::Math::BasicStatistics::update
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:113