Unverified Commit 321d0397 authored by Peter Doak's avatar Peter Doak Committed by GitHub
Browse files

Merge pull request #260 from PDoakORNL/autocorrelation_testing

adding missing test and writes from autocorrelation branch
parents 81ad9b40 55e70874
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -173,6 +173,8 @@ void DcaLoop<ParametersType, DcaDataType, MCIntegratorType, DIST>::write() {
#endif

  if (concurrency.id() == concurrency.first()) {
    parameters.write(*output_file_);
    MOMS.write(*output_file_);
    monte_carlo_integrator_.write(*output_file_);
    DCA_info_struct.write(*output_file_);
    output_file_->close_file();
+1 −0
Original line number Diff line number Diff line
@@ -4,4 +4,5 @@ add_subdirectory(function_transform)
add_subdirectory(nfft)
add_subdirectory(random)
add_subdirectory(statistical_testing)
add_subdirectory(statistics)
add_subdirectory(util)
+4 −0
Original line number Diff line number Diff line
dca_add_gtest(autocorrelation_test
        FAST
        GTEST_MAIN
        LIBS)
+68 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 ETH Zurich
// Copyright (C) 2019 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE for terms of usage.
// See CITATION.md for citation guidelines, if DCA++ is used for scientific publications.
//
// Author: Giovanni Balduzzi (gbalduzz@itp.phys.ethz.ch)
//
// This file tests autocorrelation.hpp by generating a time series of known autocorrelation time.

#include "dca/math/statistics/autocorrelation.hpp"

#include <random>
#include "gtest/gtest.h"

TEST(AutocorrelationTest, All) {
  const std::vector<double> tau_exp_vals{1, 5, 3};
  const std::vector<double> means{0, 1, -1};
  const std::vector<double> stdevs{1, 0.5, 2};

  const int n_samples = 5e4;

  std::mt19937_64 rng(42);

  for (int test_id = 0; test_id < tau_exp_vals.size(); ++test_id) {
    const double tau_exp = tau_exp_vals[test_id];

    std::vector<double> independent_series(n_samples);
    std::normal_distribution<double> distro(means[test_id], stdevs[test_id]);

    const int n_corr = 100;
    dca::math::statistics::Autocorrelation<double> autocorr(n_corr);

    // Compute and accumulate time series.
    for (auto& x : independent_series) {
      x = distro(rng);
    }

    for (int i = n_corr; i < independent_series.size(); ++i) {
      double correlated = 0;

      for (int j = i - n_corr; j <= i; ++j) {
        correlated += std::exp(-(i - j) / tau_exp) * independent_series[j];
      }

      autocorr.addSample(correlated);
    }

    // See: http://www.hep.fsu.edu/~berg/teach/mcmc08/material/lecture07mcmc3.pdf
    const auto tau_integrated = 1. + (2 * std::exp(-1. / tau_exp)) / (1 - std::exp(-1. / tau_exp));

    const double tau_computed = autocorr.computeAutocorrelationTime();

    const double rel_diff = 2 * (tau_computed - tau_integrated) / (tau_computed + tau_integrated);
    EXPECT_NEAR(rel_diff, 0, 0.1);
  }
}

TEST(AutocorrelationTest, ComplexCompiles) {
  dca::math::statistics::Autocorrelation<std::complex<float>> corr(10);

  corr.addSample(std::complex<float>(1, 0));
  corr.addSample(std::complex<float>(1, -1));

  // Note: this computation will fail to converge.
  corr.computeAutocorrelationTime();
}
+9 −0
Original line number Diff line number Diff line
// Copyright (C) 2019 ETH Zurich
// Copyright (C) 2019 UT-Battelle, LLC
// All rights reserved.
//
// See LICENSE.txt for terms of usage.
// See CITATION.txt for citation guidelines if you use this code for scientific publications.
//
// Author: Giovanni Balduzzi (gbalduzz@gitp.phys.ethz.ch)
//

#include <cmath>
#include <mutex>
Loading