QMCPACK
accumulators.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 //
11 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 /** @file accumulators.h
16  * @brief Define and declare accumulator_set
17  *
18  * A temporary implementation to handle scalar samples and will be replaced by
19  * boost.Accumulator
20  */
21 #ifndef QMCPLUSPLUS_ACCUMULATORS_H
22 #define QMCPLUSPLUS_ACCUMULATORS_H
23 
24 #include <iostream>
25 #include "CPU/math.hpp"
26 
27 /** generic accumulator of a scalar type
28  *
29  * To simplify i/o, the values are storged in contents
30  */
31 template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
33 {
34  using value_type = T;
35  using return_type = T;
36 
37  enum
38  {
39  VALUE = 0,
40  VALUESQ = 1,
41  WEIGHT = 2,
43  };
45 
46  inline accumulator_set()
47  {
48  for (int i = 0; i < CAPACITY; ++i)
49  properties[i] = value_type();
50  }
51 
52  /** add a sample */
53  inline void operator()(value_type x)
54  {
55  properties[VALUE] += x;
56  properties[VALUESQ] += x * x;
57  properties[WEIGHT] += 1.0;
58  }
59 
60  /** add a sample and */
61  inline void operator()(value_type x, value_type w)
62  {
63  properties[VALUE] += w * x;
64  properties[VALUESQ] += w * x * x;
65  properties[WEIGHT] += w;
66  }
67 
68  /** reset properties
69  * @param v cummulative value
70  * @param vv cummulative valuesq
71  * @param w cummulative weight
72  */
73  inline void reset(value_type v, value_type vv, value_type w)
74  {
75  properties[VALUE] = v;
76  properties[VALUESQ] = vv;
77  properties[WEIGHT] = w;
78  }
79 
80  /** reset properties
81  * @param v cummulative value
82  * @param w cummulative weight
83  */
84  inline void reset(value_type v, value_type w)
85  {
86  properties[VALUE] = v;
87  properties[VALUESQ] = v * v;
88  properties[WEIGHT] = w;
89  }
90 
91  /** add a value but set the weight 1
92  *
93  * @todo Jeremy provides the reasonin of having this function. Suggest rename it to make the meaning clear.
94  */
95  inline void add(value_type x)
96  {
97  properties[VALUE] += x;
98  properties[VALUESQ] += x * x;
99  properties[WEIGHT] = 1;
100  }
101 
102  /** return true if Weight!= 0 */
103  inline bool good() const { return properties[WEIGHT] > 0; }
104  /** return true if Weight== 0 */
105  inline bool bad() const { return qmcplusplus::iszero(properties[WEIGHT]); }
106 
107  /** return the sum */
108  inline return_type result() const { return properties[VALUE]; }
109 
110  /** return the sum of value squared */
111  inline return_type result2() const { return properties[VALUESQ]; }
112  /** return the count
113  *
114  * Will return the sum of weights of each sample
115  */
116  inline return_type count() const { return properties[WEIGHT]; }
117 
118  inline std::pair<return_type, return_type> mean_and_variance() const
119  {
120  value_type norm = 1.0 / properties[WEIGHT];
121  value_type avg = properties[VALUE] * norm;
122  return std::pair<return_type, return_type>(avg, norm * properties[VALUESQ] - avg * avg);
123  }
124 
125  ///return the mean
126  inline return_type mean() const { return good() ? properties[VALUE] / properties[WEIGHT] : 0.0; }
127 
128  ///return the mean of squared values
129  inline return_type mean2() const { return good() ? properties[VALUESQ] / properties[WEIGHT] : 0.0; }
130 
131  inline return_type variance() const
132  {
134  return std::numeric_limits<T>::max();
135  value_type norm = 1.0 / properties[WEIGHT];
137  }
138 
139  inline void clear()
140  {
141  for (int i = 0; i < CAPACITY; ++i)
142  properties[i] = value_type();
143  }
144 };
145 
146 template<typename ACC>
147 inline typename ACC::value_type mean(const ACC& ac)
148 {
149  return ac.mean();
150 }
151 
152 template<typename T>
153 std::ostream& operator<<(std::ostream& os, accumulator_set<T>& rhs)
154 {
155  os << "accumulator_set: "
156  << " value = " << rhs.properties[0] << " value_sq = " << rhs.properties[1] << " weight = " << rhs.properties[2];
157  return os;
158 }
159 
160 
161 #endif
std::pair< return_type, return_type > mean_and_variance() const
Definition: accumulators.h:118
return_type mean2() const
return the mean of squared values
Definition: accumulators.h:129
void reset(value_type v, value_type w)
reset properties
Definition: accumulators.h:84
void reset(value_type v, value_type vv, value_type w)
reset properties
Definition: accumulators.h:73
bool good() const
return true if Weight!= 0
Definition: accumulators.h:103
void operator()(value_type x, value_type w)
add a sample and
Definition: accumulators.h:61
void add(value_type x)
add a value but set the weight 1
Definition: accumulators.h:95
bool bad() const
return true if Weight== 0
Definition: accumulators.h:105
generic accumulator of a scalar type
Definition: accumulators.h:32
T properties[CAPACITY]
Definition: accumulators.h:44
bool iszero(T a)
Definition: math.hpp:77
double norm(const zVec &c)
Definition: VectorOps.h:118
void operator()(value_type x)
add a sample
Definition: accumulators.h:53
return_type mean() const
return the mean
Definition: accumulators.h:126
return_type result() const
return the sum
Definition: accumulators.h:108
return_type result2() const
return the sum of value squared
Definition: accumulators.h:111
return_type count() const
return the count
Definition: accumulators.h:116
return_type variance() const
Definition: accumulators.h:131
ACC::value_type mean(const ACC &ac)
Definition: accumulators.h:147
QMCTraits::FullPrecRealType value_type