A library for working with phylogenetic and population genetic data.
v0.32.0
moments.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_MOMENTS_H_
2 #define GENESIS_UTILS_MATH_MOMENTS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2023 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
34 #include <cmath>
35 #include <vector>
36 
37 namespace genesis {
38 namespace utils {
39 
40 // ================================================================================================
41 // Moments
42 // ================================================================================================
43 
54 class Moments
55 {
56 public:
57 
58  // ---------------------------------------------------------
59  // Constructor and Rule of Five
60  // ---------------------------------------------------------
61 
71  explicit Moments( size_t ddof = 0 )
72  : ddof_(ddof)
73  {}
74 
81  template<class It>
82  Moments( It first, It last, size_t ddof = 0 )
83  : ddof_(ddof)
84  {
85  while( first != last ) {
86  push( *first );
87  ++first;
88  }
89  }
90 
94  Moments( std::initializer_list<double> list, size_t ddof = 0 )
95  : ddof_(ddof)
96  {
97  for( auto const e : list ) {
98  push( e );
99  }
100  }
101 
102  ~Moments() = default;
103 
104  Moments(Moments const&) = default;
105  Moments(Moments&&) = default;
106 
107  Moments& operator= (Moments const&) = default;
108  Moments& operator= (Moments&&) = default;
109 
110  // ---------------------------------------------------------
111  // Member Functions
112  // ---------------------------------------------------------
113 
114  void push( double val )
115  {
116  count_++;
117  if( count_ == 1 ) {
118  m_old_ = m_new_ = val;
119  s_old_ = 0.0;
120  } else {
121  m_new_ = m_old_ + ( val - m_old_ ) / static_cast<double>( count_ );
122  s_new_ = s_old_ + ( val - m_old_ ) * ( val - m_new_ );
123  m_old_ = m_new_;
124  s_old_ = s_new_;
125  }
126  }
127 
128  size_t count() const
129  {
130  return count_;
131  }
132 
133  double mean() const
134  {
135  return m_new_;
136  }
137 
138  double variance() const
139  {
140  return (( count_ > 1 ) ? s_new_ / ( static_cast<double>( count_ - ddof_ )) : 0.0 );
141  }
142 
143  double stddev() const
144  {
145  return std::sqrt( variance() );
146  }
147 
148  double standard_deviation() const
149  {
150  return std::sqrt( variance() );
151  }
152 
153  // ---------------------------------------------------------
154  // Data Members
155  // ---------------------------------------------------------
156 
157 private:
158 
159  size_t count_ = 0;
160  double m_old_ = 0.0;
161  double m_new_ = 0.0;
162  double s_old_ = 0.0;
163  double s_new_ = 0.0;
164  size_t ddof_ = 0.0;
165 
166 };
167 
168 
169 } // namespace utils
170 } // namespace genesis
171 
172 #endif // include guard
genesis::utils::Moments::variance
double variance() const
Definition: moments.hpp:138
genesis::utils::Moments::operator=
Moments & operator=(Moments const &)=default
genesis::utils::Moments::standard_deviation
double standard_deviation() const
Definition: moments.hpp:148
genesis::utils::Moments::mean
double mean() const
Definition: moments.hpp:133
genesis::utils::Moments::count
size_t count() const
Definition: moments.hpp:128
genesis::utils::Moments::Moments
Moments(It first, It last, size_t ddof=0)
Compute Momemnts by iteration over a range of double.
Definition: moments.hpp:82
genesis::utils::Moments::stddev
double stddev() const
Definition: moments.hpp:143
genesis::utils::Moments::push
void push(double val)
Definition: moments.hpp:114
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::Moments::~Moments
~Moments()=default
genesis::utils::Moments::Moments
Moments(std::initializer_list< double > list, size_t ddof=0)
Compute Moments, over an initializer list of values.
Definition: moments.hpp:94
genesis::utils::Moments
Compute running mean and variance for an input.
Definition: moments.hpp:54
genesis::utils::Moments::Moments
Moments(size_t ddof=0)
Construct empty Moments.
Definition: moments.hpp:71