ALPSCore reference
variance.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 #pragma once
7 
8 #include <alps/alea/core.hpp>
9 #include <alps/alea/util.hpp>
10 #include <alps/alea/bundle.hpp>
11 #include <alps/alea/complex_op.hpp>
12 #include <alps/alea/computed.hpp>
14 
15 #include <memory>
16 
17 // Forward declarations
18 
19 namespace alps { namespace alea {
20  template <typename T, typename Str> class var_data;
21  template <typename T, typename Str> class var_acc;
22  template <typename T, typename Str> class var_result;
23 
24  template <typename T> class autocorr_acc;
25  template <typename T> class autocorr_result;
26 
27  template <typename T> class batch_result;
28 
29  template <typename T, typename Str>
30  void serialize(serializer &, const std::string &, const var_result<T,Str> &);
31 
32  template <typename T, typename Str>
33  void deserialize(deserializer &, const std::string &, var_result<T,Str> &);
34 
35  template <typename T, typename Str>
36  std::ostream &operator<<(std::ostream &, const var_result<T,Str> &);
37 }}
38 
39 // Actual declarations
40 
41 namespace alps { namespace alea {
42 
51 template <typename T, typename Strategy=circular_var>
52 class var_data
53 {
54 public:
57 
58 public:
59  var_data(size_t size);
60 
62  void reset();
63 
65  size_t size() const { return data_.rows(); }
66 
68  size_t count() const { return count_; }
69 
71  size_t &count() { return count_; }
72 
74  double count2() const { return count2_; }
75 
77  double &count2() { return count2_; }
78 
79  const column<value_type> &data() const { return data_; }
80 
81  column<value_type> &data() { return data_; }
82 
83  const column<var_type> &data2() const { return data2_; }
84 
85  column<var_type> &data2() { return data2_; }
86 
87  void convert_to_mean();
88 
89  void convert_to_sum();
90 
91 private:
92  column<T> data_;
93  column<var_type> data2_;
94  size_t count_;
95  double count2_;
96 
97  friend class var_acc<T, Strategy>;
98  friend class var_result<T, Strategy>;
99  friend void serialize<>(serializer &, const std::string &, const var_result<T,Strategy> &);
100  friend void deserialize<>(deserializer &, const std::string &, var_result<T,Strategy> &);
101 };
102 
103 template <typename T, typename Strategy>
104 struct traits< var_data<T,Strategy> >
105 {
106  typedef Strategy strategy_type;
109 };
110 
111 extern template class var_data<double>;
112 extern template class var_data<std::complex<double>, circular_var>;
113 extern template class var_data<std::complex<double>, elliptic_var>;
114 
118 template <typename T, typename Strategy=circular_var>
119 class var_acc
120 {
121 public:
122  using value_type = T;
124 
125 public:
126  var_acc(size_t size=1, size_t batch_size=1);
127 
128  var_acc(const var_acc &other);
129 
130  var_acc &operator=(const var_acc &other);
131 
133  void reset();
134 
136  void set_size(size_t size);
137 
139  void set_batch_size(size_t batch_size);
140 
142  bool valid() const { return (bool)store_; }
143 
145  size_t size() const { return current_.size(); }
146 
148  size_t batch_size() const { return current_.target(); }
149 
151  var_acc &operator<<(const computed<T> &src) { add(src, 1, nullptr); return *this; }
152 
154  var_acc &operator<<(const var_result<T,Strategy> &result);
155 
157  size_t count() const { return store_->count(); }
158 
161 
164 
165  const bundle<value_type> &current() const { return current_; }
166 
168  const var_data<T,Strategy> &store() const { return *store_; }
169 
170 protected:
171  void add(const computed<T> &source, size_t count, var_acc *cascade);
172 
173  void add_bundle(var_acc *cascade);
174 
175  void finalize_to(var_result<T,Strategy> &result, var_acc *cascade);
176 
177 private:
178  std::unique_ptr< var_data<value_type, Strategy> > store_;
179  bundle<value_type> current_;
180 
181  friend class autocorr_acc<T>;
182  friend class batch_result<T>;
183 };
184 
185 template <typename T, typename Strategy>
186 struct traits< var_acc<T,Strategy> >
187 {
188  typedef Strategy strategy_type;
193 };
194 
195 extern template class var_acc<double>;
196 extern template class var_acc<std::complex<double>, circular_var>;
197 extern template class var_acc<std::complex<double>, elliptic_var>;
198 
202 template <typename T, typename Strategy=circular_var>
203 class var_result
204 {
205 public:
208 
209 public:
211 
213  : store_(new var_data<T,Strategy>(acc_data))
214  { }
215 
216  var_result(const var_result &other);
217 
218  var_result &operator=(const var_result &other);
219 
221  bool valid() const { return (bool)store_; }
222 
224  size_t size() const { return store_->size(); }
225 
227  double batch_size() const { return store_->count2() / store_->count(); }
228 
230  size_t count() const { return store_->count(); }
231 
233  double count2() const { return store_->count2(); }
234 
236  double observations() const { return count() / batch_size(); }
237 
239  const column<T> &mean() const { return store_->data(); }
240 
241  // TODO: this is essentially a weighted variance thing. The weighted
242  // variance differs from the pooled on by a factor. We should probably
243  // split the two things.
244 
246  column<var_type> var() const { return batch_size() * store_->data2(); }
247 
249  column<var_type> stderror() const;
250 
252  const var_data<T,Strategy> &store() const { return *store_; }
253 
255  var_data<T,Strategy> &store() { return *store_; }
256 
258  void reduce(const reducer &r) { reduce(r, true, true); }
259 
261  friend void serialize<>(serializer &, const std::string &, const var_result &);
262 
264  friend void deserialize<>(deserializer &, const std::string &, var_result &);
265 
267  friend std::ostream &operator<< <>(std::ostream &, const var_result &);
268 
269 protected:
270  void reduce(const reducer &, bool do_pre_commit, bool do_post_commit);
271 
272 private:
273  std::unique_ptr< var_data<T,Strategy> > store_;
274 
275  friend class var_acc<T,Strategy>;
276  friend class autocorr_result<T>;
277 };
278 
280 template <typename T, typename Strategy>
282 template <typename T, typename Strategy>
284 {
285  return !operator==(r1, r2);
286 }
287 
288 template<typename T> struct is_alea_acc<var_acc<T, circular_var>> :
289  std::true_type {};
290 template<typename T> struct is_alea_acc<var_acc<T, elliptic_var>> :
291  std::true_type {};
292 template<typename T> struct is_alea_result<var_result<T, circular_var>> :
293  std::true_type {};
294 template<typename T> struct is_alea_result<var_result<T, elliptic_var>> :
295  std::true_type {};
296 
297 template <typename T, typename Strategy>
298 struct traits< var_result<T,Strategy> >
299 {
300  typedef Strategy strategy_type;
303 
304  const static bool HAVE_MEAN = true;
305  const static bool HAVE_VAR = true;
306  const static bool HAVE_COV = false;
307  const static bool HAVE_TAU = false;
308  const static bool HAVE_BATCH = false;
309 };
310 
311 extern template class var_result<double>;
312 extern template class var_result<std::complex<double>, circular_var>;
313 extern template class var_result<std::complex<double>, elliptic_var>;
314 
315 }} /* namespace alps::alea */
size_t size() const
Definition: variance.hpp:145
bind< Strategy, T >::var_type var_type
Definition: variance.hpp:190
size_t count() const
Definition: variance.hpp:157
bind< Strategy, T >::value_type value_type
Definition: variance.hpp:55
bind< Strategy, T >::var_type var_type
Definition: variance.hpp:108
bind< Strategy, T >::value_type value_type
Definition: variance.hpp:206
size_t size() const
Definition: variance.hpp:224
size_t size() const
Definition: variance.hpp:65
const column< value_type > & data() const
Definition: variance.hpp:79
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
const var_data< T, Strategy > & store() const
Definition: variance.hpp:252
double count2() const
Definition: variance.hpp:233
size_t batch_size() const
Definition: variance.hpp:148
column< var_type > var() const
Definition: variance.hpp:246
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
void reduce(const reducer &r)
Definition: variance.hpp:258
size_t count() const
Definition: variance.hpp:68
const bundle< value_type > & current() const
Definition: variance.hpp:165
bool valid() const
Definition: variance.hpp:142
double count2() const
Definition: variance.hpp:74
bind< Strategy, T >::var_type var_type
Definition: variance.hpp:207
var_result(const var_data< T, Strategy > &acc_data)
Definition: variance.hpp:212
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
const column< var_type > & data2() const
Definition: variance.hpp:83
void reset(accumulator_wrapper &arg)
bind< Strategy, T >::var_type var_type
Definition: variance.hpp:302
bind< Strategy, T >::var_type var_type
Definition: variance.hpp:56
size_t & count()
Definition: variance.hpp:71
typename bind< Strategy, T >::var_type var_type
Definition: variance.hpp:123
size_t count() const
Definition: variance.hpp:230
const var_data< T, Strategy > & store() const
Definition: variance.hpp:168
double batch_size() const
Definition: variance.hpp:227
var_result< T, Strategy > result_type
Definition: variance.hpp:191
double & count2()
Definition: variance.hpp:77
const column< T > & mean() const
Definition: variance.hpp:239
bind< Strategy, T >::value_type value_type
Definition: variance.hpp:107
column< var_type > & data2()
Definition: variance.hpp:85
bool valid() const
Definition: variance.hpp:221
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
bind< Strategy, T >::value_type value_type
Definition: variance.hpp:189
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
column< value_type > & data()
Definition: variance.hpp:81
T r(T x, T y=T(), T z=T())
bind< Strategy, T >::value_type value_type
Definition: variance.hpp:301
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45
var_data< T, Strategy > & store()
Definition: variance.hpp:255
bool operator!=(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.hpp:220
count_type< T >::type count(T const &arg)
Definition: count.hpp:39
double observations() const
Definition: variance.hpp:236