ALPSCore reference
covariance.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 cov_data;
21  template <typename T, typename Str> class cov_acc;
22  template <typename T, typename Str> class cov_result;
23 
24  template <typename T> class batch_result;
25 
26  template <typename T, typename Str>
27  void serialize(serializer &, const std::string &, const cov_result<T,Str> &);
28 
29  template <typename T, typename Str>
30  void deserialize(deserializer &, const std::string &, cov_result<T,Str> &);
31 
32  template <typename T, typename Str>
33  std::ostream &operator<<(std::ostream &, const cov_result<T,Str> &);
34 }}
35 
36 // Actual declarations
37 
38 namespace alps { namespace alea {
39 
48 template <typename T, typename Strategy=circular_var>
49 class cov_data
50 {
51 public:
55 
56 public:
57  cov_data(size_t size);
58 
60  void reset();
61 
63  size_t size() const { return data_.rows(); }
64 
66  size_t count() const { return count_; }
67 
69  size_t &count() { return count_; }
70 
72  double count2() const { return count2_; }
73 
75  double &count2() { return count2_; }
76 
77  const column<value_type> &data() const { return data_; }
78 
79  column<value_type> &data() { return data_; }
80 
81  const cov_matrix_type &data2() const { return data2_; }
82 
83  cov_matrix_type &data2() { return data2_; }
84 
85  void convert_to_mean();
86 
87  void convert_to_sum();
88 
89 private:
90  column<T> data_;
91  cov_matrix_type data2_;
92  size_t count_;
93  double count2_;
94 
95  friend class cov_acc<T, Strategy>;
96  friend class cov_result<T, Strategy>;
97  friend void serialize<>(serializer &, const std::string &, const cov_result<T,Strategy> &);
98  friend void deserialize<>(deserializer &, const std::string &, cov_result<T,Strategy> &);
99 };
100 
101 template <typename T, typename Strategy>
102 struct traits< cov_data<T,Strategy> >
103 {
104  typedef Strategy strategy_type;
108 };
109 
110 extern template class cov_data<double>;
111 extern template class cov_data<std::complex<double>, circular_var>;
112 extern template class cov_data<std::complex<double>, elliptic_var>;
113 
114 
118 template <typename T, typename Strategy=circular_var>
119 class cov_acc
120 {
121 public:
122  using value_type = T;
126 
127 public:
128  cov_acc(size_t size=1, size_t batch_size=1);
129 
130  cov_acc(const cov_acc &other);
131 
132  cov_acc &operator=(const cov_acc &other);
133 
135  void reset();
136 
138  void set_size(size_t size);
139 
141  void set_batch_size(size_t batch_size);
142 
144  bool valid() const { return (bool)store_; }
145 
147  size_t size() const { return current_.size(); }
148 
150  size_t batch_size() const { return current_.target(); }
151 
153  cov_acc& operator<<(const computed<T>& src){ add(src, 1); return *this; }
154 
156  cov_acc &operator<<(const cov_result<T,Strategy> &result);
157 
159  size_t count() const { return store_->count(); }
160 
163 
166 
167  const bundle<value_type> &current() const { return current_; }
168 
170  const cov_data<T,Strategy> &store() const { return *store_; }
171 
172 protected:
173  void add(const computed<T> &source, size_t count);
174 
175  void add_bundle();
176 
177  void finalize_to(cov_result<T,Strategy> &result);
178 
179 private:
180  std::unique_ptr<cov_data<T,Strategy> > store_;
181  bundle<value_type> current_;
182 
183  friend class batch_result<T>;
184 };
185 
186 template <typename T, typename Strategy>
187 struct traits< cov_acc<T,Strategy> >
188 {
189  typedef Strategy strategy_type;
195 };
196 
197 extern template class cov_acc<double>;
198 extern template class cov_acc<std::complex<double>, circular_var>;
199 extern template class cov_acc<std::complex<double>, elliptic_var>;
200 
201 
205 template <typename T, typename Strategy=circular_var>
206 class cov_result
207 {
208 public:
212 
213 public:
215 
217  : store_(new cov_data<T,Strategy>(acc_data))
218  { }
219 
220  cov_result(const cov_result &other);
221 
222  cov_result &operator=(const cov_result &other);
223 
225  bool valid() const { return (bool)store_; }
226 
228  size_t size() const { return store_->size(); }
229 
231  size_t count() const { return store_->count(); }
232 
234  double count2() const { return store_->count2(); }
235 
237  double batch_size() const { return store_->count2() / store_->count(); }
238 
240  double observations() const { return count() / batch_size(); }
241 
243  const column<T> &mean() const { return store_->data(); }
244 
245  // TODO: this is essentially a weighted variance thing. The weighted
246  // variance differs from the pooled on by a factor. We should probably
247  // split the two things.
248 
250  column<var_type> var() const { return batch_size() * store_->data2().diagonal().real(); }
251 
253  typename eigen<cov_type>::matrix cov() const { return batch_size() * store_->data2(); }
254 
256  column<var_type> stderror() const;
257 
259  const cov_data<T,Strategy> &store() const { return *store_; }
260 
262  cov_data<T,Strategy> &store() { return *store_; }
263 
265  void reduce(const reducer &r) { reduce(r, true, true); }
266 
268  friend void serialize<>(serializer &, const std::string &, const cov_result &);
269 
271  friend void deserialize<>(deserializer &, const std::string &, cov_result &);
272 
274  friend std::ostream &operator<< <>(std::ostream &, const cov_result &);
275 
276 protected:
277  void reduce(const reducer &, bool do_pre_commit, bool do_post_commit);
278 
279 private:
280  std::unique_ptr<cov_data<T,Strategy> > store_;
281 
282  friend class cov_acc<T,Strategy>;
283 };
284 
286 template <typename T, typename Strategy>
288 template <typename T, typename Strategy>
290 {
291  return !operator==(r1, r2);
292 }
293 
294 template<typename T> struct is_alea_acc<cov_acc<T, circular_var>> :
295  std::true_type {};
296 template<typename T> struct is_alea_acc<cov_acc<T, elliptic_var>> :
297  std::true_type {};
298 template<typename T> struct is_alea_result<cov_result<T, circular_var>> :
299  std::true_type {};
300 template<typename T> struct is_alea_result<cov_result<T, elliptic_var>> :
301  std::true_type {};
302 
303 template <typename T, typename Strategy>
304 struct traits< cov_result<T,Strategy> >
305 {
306  typedef Strategy strategy_type;
310 
311  const static bool HAVE_MEAN = true;
312  const static bool HAVE_VAR = true;
313  const static bool HAVE_COV = true;
314  const static bool HAVE_TAU = false;
315  const static bool HAVE_BATCH = false;
316 };
317 
318 extern template class cov_result<double>;
319 extern template class cov_result<std::complex<double>, circular_var>;
320 extern template class cov_result<std::complex<double>, elliptic_var>;
321 
322 }} /* namespace alps::alea */
size_t batch_size() const
Definition: covariance.hpp:150
cov_result(const cov_data< T, Strategy > &acc_data)
Definition: covariance.hpp:216
bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:53
const cov_matrix_type & data2() const
Definition: covariance.hpp:81
cov_matrix_type & data2()
Definition: covariance.hpp:83
column< var_type > var() const
Definition: covariance.hpp:250
typename eigen< cov_type >::matrix cov_matrix_type
Definition: covariance.hpp:125
bind< Strategy, T >::var_type var_type
Definition: covariance.hpp:308
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
bind< Strategy, T >::var_type var_type
Definition: covariance.hpp:106
bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:107
bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:211
double batch_size() const
Definition: covariance.hpp:237
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
const bundle< value_type > & current() const
Definition: covariance.hpp:167
bind< Strategy, T >::value_type value_type
Definition: covariance.hpp:307
bind< Strategy, T >::value_type value_type
Definition: covariance.hpp:105
bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:192
size_t size() const
Definition: covariance.hpp:228
size_t size() const
Definition: covariance.hpp:147
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
typename bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:124
cov_data< T, Strategy > & store()
Definition: covariance.hpp:262
bind< Strategy, T >::var_type var_type
Definition: covariance.hpp:210
bool valid() const
Definition: covariance.hpp:144
void reset(accumulator_wrapper &arg)
eigen< cov_type >::matrix cov() const
Definition: covariance.hpp:253
column< value_type > & data()
Definition: covariance.hpp:79
size_t count() const
Definition: covariance.hpp:159
double observations() const
Definition: covariance.hpp:240
bind< Strategy, T >::var_type var_type
Definition: covariance.hpp:191
double count2() const
Definition: covariance.hpp:234
bind< Strategy, T >::value_type value_type
Definition: covariance.hpp:52
const column< value_type > & data() const
Definition: covariance.hpp:77
bind< Strategy, T >::value_type value_type
Definition: covariance.hpp:209
void reduce(const reducer &r)
Definition: covariance.hpp:265
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
eigen< cov_type >::matrix cov_matrix_type
Definition: covariance.hpp:54
const column< T > & mean() const
Definition: covariance.hpp:243
bind< Strategy, T >::cov_type cov_type
Definition: covariance.hpp:309
bind< Strategy, T >::value_type value_type
Definition: covariance.hpp:190
const cov_data< T, Strategy > & store() const
Definition: covariance.hpp:170
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
T r(T x, T y=T(), T z=T())
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45
typename bind< Strategy, T >::var_type var_type
Definition: covariance.hpp:123
double count2() const
Definition: covariance.hpp:72
size_t size() const
Definition: covariance.hpp:63
bool operator!=(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.hpp:220
size_t count() const
Definition: covariance.hpp:231
size_t count() const
Definition: covariance.hpp:66
const cov_data< T, Strategy > & store() const
Definition: covariance.hpp:259
count_type< T >::type count(T const &arg)
Definition: count.hpp:39