OpenMS
AsymmetricStatistics.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Types.h>
13 
14 #include <vector>
15 #include <ostream>
16 #include <cmath>
17 
18 namespace OpenMS
19 {
20  namespace Math
21  {
22 
31  template <typename RealT = double>
33  public BasicStatistics<RealT>
34  {
37  typedef typename Base::RealType RealType;
38 
39  using Base::clear;
40  using Base::sum_;
41  using Base::mean_;
42  using Base::variance_;
43 
44 public:
45 
48  BasicStatistics<>(),
49  variance1_(0),
50  variance2_(0)
51  {}
52 
55  {
56  return variance1_;
57  }
58 
61  {
62  return variance2_;
63  }
64 
66  template <typename ProbabilityIterator, typename CoordinateIterator>
67  void update(ProbabilityIterator const probability_begin,
68  ProbabilityIterator const probability_end,
69  CoordinateIterator const coordinate_begin)
70  {
71  // reuse...
72  Base::update(probability_begin, probability_end, coordinate_begin);
73 
74  const RealType stdev = std::sqrt(variance_);
75 
76  RealType sum1 = 0;
77  RealType sum2 = 0;
78  variance1_ = 0;
79  variance2_ = 0;
80  ProbabilityIterator prob_iter = probability_begin;
81  CoordinateIterator coord_iter = coordinate_begin;
82  for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
83  {
84  RealType diff = *coord_iter - mean_;
85  RealType diff_squared = diff * diff;
86 
87  if (diff_squared > variance_)
88  {
89  if (*coord_iter < mean_)
90  {
91  variance1_ += (*prob_iter * diff_squared);
92  sum1 += *prob_iter;
93  }
94  else // ( *coord_iter > mean_ )
95  {
96  variance2_ += (*prob_iter * diff_squared);
97  sum2 += *prob_iter;
98  }
99  }
100  else
101  {
102  RealType frac = (diff / stdev + 1.) / 2.;
103  RealType prob_frac = frac * *prob_iter;
104  variance2_ += prob_frac * diff_squared;
105  sum2 += prob_frac;
106  prob_frac = *prob_iter * (1. - frac);
107  variance1_ += prob_frac * diff_squared;
108  sum1 += prob_frac;
109  }
110  }
111  variance1_ /= sum1;
112  variance2_ /= sum2;
113  return;
114  }
115 
116 protected:
119  };
120 
121  } // namespace Math
122 
123 } // namespace OpenMS
124 
Internal class for asymmetric distributions.
Definition: AsymmetricStatistics.h:34
RealType variance1_
Definition: AsymmetricStatistics.h:118
RealType variance2_
Definition: AsymmetricStatistics.h:118
RealType variance1() const
"variance to the left hand side"
Definition: AsymmetricStatistics.h:54
RealType variance2() const
"variance to the right hand side"
Definition: AsymmetricStatistics.h:60
AsymmetricStatistics()
Default constructor.
Definition: AsymmetricStatistics.h:47
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:67
RealType variance_
Definition: BasicStatistics.h:237
Base::RealType RealType
Definition: AsymmetricStatistics.h:37
BasicStatistics< RealT > Base
The real type and basic statistics specified as template argument.
Definition: AsymmetricStatistics.h:36
RealType mean_
Definition: BasicStatistics.h:236
Calculates some basic statistical parameters of a distribution: sum, mean, variance,...
Definition: BasicStatistics.h:43
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:48
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:87
RealType sum_
Definition: BasicStatistics.h:238
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:77
RealType variance_
Definition: BasicStatistics.h:237
RealType mean_
Definition: BasicStatistics.h:236
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22