43namespace triqs::stat {
60 template <AccCompatible T>
auto compress_bins(std::vector<T>
const &bins,
int fac) {
62 if (fac < 2)
return bins;
65 int const nbins = bins.size() / fac;
66 std::vector<T> res(nbins);
69 for (
int i = 0; i < nbins; ++i) {
70 res[i] = bins[fac * i];
71 for (
int j = 1; j < fac; ++j) { res[i] += bins[fac * i + j]; }
140 if (
max_n_bins != -1 &&
max_n_bins < 2)
throw std::runtime_error(
"lin_binning: max_n_bins must be -1 (unbounded) or >= 2.");
141 if (
bin_capacity < 1)
throw std::runtime_error(
"lin_binning: bin_capacity must be >= 1.");
151 [[nodiscard]]
long max_n_bins()
const {
return max_n_bins_; }
157 [[nodiscard]]
long n_bins()
const {
return mean_bins_.size(); }
163 [[nodiscard]]
auto const &
bins()
const {
return mean_bins_; }
167 return (last_bin_count_ == bin_capacity_ ? mean_bins_ : std::vector<T>(mean_bins_.begin(), std::prev(mean_bins_.end())));
177 [[nodiscard]]
long count()
const {
return count_; }
180 [[nodiscard]]
auto const &
mean()
const {
return mean_; }
183 [[nodiscard]]
auto const &
var_data()
const {
return var_; }
186 [[nodiscard]]
auto const &
callback()
const {
return callback_; }
218 if (mean_bins_.empty()) {
226 if (callback_) callback_(*
this);
230 var_ +=
abs_square(x - mean_) * (
static_cast<double>(count_ - 1) / count_);
231 mean_ += (x - mean_) / count_;
233 if (last_bin_count_ == bin_capacity_) {
234 mean_bins_.emplace_back(std::forward<U>(x));
238 mean_bins_.back() += (x - mean_bins_.back()) / last_bin_count_;
260 if (
n_bins() == 1 || fac < 2)
return;
264 for (
int i = 0; i < nbins; ++i) {
265 mean_bins_[i] = mean_bins_[fac * i];
266 for (
int j = 1; j < fac; ++j) { mean_bins_[i] += mean_bins_[fac * i + j]; }
267 mean_bins_[i] /= fac;
271 int const left_over =
n_bins() - fac * nbins;
272 if (left_over != 0) {
274 auto const bc = last_bin_count_ + (left_over - 1) * bin_capacity_;
275 value_t tmp_bin = mean_bins_.back() * (
static_cast<double>(last_bin_count_) / bc);
276 for (
int j = 0; j < left_over - 1; ++j) { tmp_bin += mean_bins_[fac * nbins + j] * (
static_cast<double>(bin_capacity_) / bc); }
277 mean_bins_[nbins] = tmp_bin;
278 last_bin_count_ = bc;
282 last_bin_count_ = bin_capacity_ * fac;
286 mean_bins_.resize(nbins);
287 bin_capacity_ *= fac;
300 auto [err, tau] = calculate_error_and_tau(mean_bins_, var_, count_);
301 return std::make_tuple(mean_, err, tau);
323 auto [err, tau] = calculate_error_and_tau(bins_gathered, var_red, count_red);
324 return std::make_tuple(mean_red, err, tau);
337 auto count_red = mpi::all_reduce(count_, c);
342 value_t mean_red = mean_ * (
static_cast<double>(count_) /
static_cast<double>(count_red));
343 mpi::all_reduce_in_place(mean_red, c);
346 mpi::all_reduce_in_place(var_red, c);
348 return std::make_tuple(mean_red, var_red, count_red);
361 [[nodiscard]]
auto mpi_all_gather(mpi::communicator c,
bool same_capacity =
true)
const {
365 auto bc_max = mpi::all_reduce(bin_capacity_, c, MPI_MAX);
367 if (bc_max % bin_capacity_ == 0)
373 auto nbins = mpi::all_gather(fbins.size(), c);
374 auto bins_gathered = std::vector<value_t>(std::accumulate(nbins.begin(), nbins.end(), 0L));
376 for (
int i = 0; i < c.size(); ++i) {
377 auto const end = start + nbins[i];
378 if (c.rank() == i) std::copy(fbins.begin(), fbins.end(), bins_gathered.begin() + start);
379 mpi::broadcast_range(std::span{bins_gathered.begin() + start, bins_gathered.begin() +
end}, c, i);
383 return bins_gathered;
387 [[nodiscard]]
static std::string
hdf5_format() {
return "lin_binning"; }
397 auto gr = g.create_group(name);
398 h5::write_hdf5_format(gr, acc);
399 h5::write(gr,
"max_n_bins", acc.max_n_bins_);
400 h5::write(gr,
"bin_capacity", acc.bin_capacity_);
401 h5::write(gr,
"last_bin_count", acc.last_bin_count_);
402 h5::write(gr,
"count", acc.count_);
403 h5::write(gr,
"mean_bins", acc.mean_bins_);
404 h5::write(gr,
"mean", acc.mean_);
405 h5::write(gr,
"var", acc.var_);
416 auto gr = g.open_group(name);
417 h5::assert_hdf5_format(gr, acc);
418 h5::read(gr,
"max_n_bins", acc.max_n_bins_);
419 h5::read(gr,
"bin_capacity", acc.bin_capacity_);
420 h5::read(gr,
"last_bin_count", acc.last_bin_count_);
421 h5::read(gr,
"count", acc.count_);
422 h5::read(gr,
"mean_bins", acc.mean_bins_);
423 h5::read(gr,
"mean", acc.mean_);
424 h5::read(gr,
"var", acc.var_);
426 if (acc.max_n_bins_ != -1 && (acc.max_n_bins_ < 2 || acc.bin_capacity_ < 1))
427 throw std::runtime_error(
"h5_read: invalid lin_binning state (max_n_bins must be -1 or >= 2, bin_capacity must be >= 1).");
432 [[nodiscard]]
auto calculate_error_and_tau(std::vector<value_t>
const &
bins,
real_t const &q0,
long ct)
const {
434 if (ct < 2 ||
bins.size() < 2) {
436 return std::make_pair(nan, nan);
440 real_t var0 = q0 / (
static_cast<double>(ct) *
static_cast<double>(ct - 1));
448 return std::make_pair(err, tau);
451 long max_n_bins_{-1};
452 long bin_capacity_{1};
453 long last_bin_count_{0};
455 std::vector<value_t> mean_bins_{};
iterator end()
Get an iterator past the last block.
auto mpi_all_reduce(mpi::communicator c) const
Allreduce the overall mean and sum of squared deviations from the mean over multiple MPI processes.
auto const & mean() const
Get the overall mean.
auto const & var_data() const
Get the overall sum of the squared deviations from the mean.
get_real_t< T > real_t
Real type of the accumulated data type.
std::vector< T > full_bins() const
Get only the full bins, i.e. bins that are filled up to the bin capacity.
long max_n_bins() const
Get the maximum number of bins.
friend void h5_read(h5::group g, std::string const &name, lin_binning &acc)
Read a triqs::stat::lin_binning accumulator from HDF5.
static std::string hdf5_format()
Get the HDF5 format string.
auto mean_error_and_tau(mpi::communicator c) const
Get the mean, its standard error and an estimate for the integrated autocorrelation time from accumul...
auto mean_error_and_tau() const
Get the mean, its standard error and an estimate for the integrated autocorrelation time.
friend void h5_write(h5::group g, std::string const &name, lin_binning const &acc)
Write a triqs::stat::lin_binning accumulator to HDF5.
auto & operator<<(U &&x)
Accumulate a new sample.
long n_bins() const
Get the number of bins containing data including the currently active bin.
bool is_unbounded() const
True if the accumulator was constructed with an unbounded number of bins.
long bin_capacity() const
Get the current bin capacity.
long count() const
Get the total number of accumulated samples.
lin_binning()=default
Default construct a linear binning accumulator.
long last_bin_count() const
Get the number of samples in the currently active bin.
long n_full_bins() const
Get the number of full bins, i.e. bins that are filled up to the bin capacity.
std::function< void(lin_binning const &)> callback_t
Type of the callback function that is called when bins are compressed.
auto mpi_all_gather(mpi::communicator c, bool same_capacity=true) const
Allgather full bins from multiple MPI processes.
T value_t
Type of the accumulated data.
auto const & bins() const
Get all the bins containing data including the currently active bin.
auto const & callback() const
Get the callback function.
lin_binning(T const &sample, long max_n_bins, long bin_capacity, callback_t callback={})
Construct a linear binning accumulator with a dummy sample, the maximum number of bins,...
void compress(int fac)
Compress bins and increase bin capacity.
auto compress_bins(std::vector< T > const &bins, int fac)
Compress a given number of adjacent bins.
auto mean_and_err(R &&rg)
Calculate the arithmetic mean or the simple sum as well as a corresponding error estimate of some ran...
auto tau_estimate_from_vars(auto const &var, auto const &var0)
Compute an estimate for the integrated auto-correlation time from variances.
std::remove_cvref_t< decltype(make_real(std::declval< T >()))> get_real_t
Type trait to get the type that would be returned by triqs::stat::make_real.
auto nan_sample(T const &sample)
Get a sample with all elements set to NaN.
auto zeroed_sample(T const &sample)
Get a sample with all elements set to zero.
auto abs_square(auto const &x)
Calculate the (elementwise) absolute square of an array/view/scalar.
auto make_real(T &&t)
Make a given object real and regular.
Provides functions to calculate the arithmetic mean and standard error of a range of values.
Provides various concepts for the Utilities.
Provides various utilities for the Utilities.