MaCh3  2.8.0
Reference Guide
Public Member Functions | List of all members
M3::RHatModule Class Reference

Module for computing the R-hat diagnostic for MCMC chains. More...

#include <CLI/Modules/RHatModule.hpp>

Inheritance diagram for M3::RHatModule:
[legend]
Collaboration diagram for M3::RHatModule:
[legend]

Public Member Functions

virtual ~RHatModule ()
 Destructor. More...
 
MaCh3ArgumentParser * get_parser () override
 Get the argument parser for this module. More...
 
int Run () override
 Execute the MCMC diagnostics. More...
 
- Public Member Functions inherited from M3::PluginBase
virtual ~PluginBase ()=default
 
- Public Member Functions inherited from M3::IPlugin
virtual ~IPlugin ()=default
 

Additional Inherited Members

- Protected Attributes inherited from M3::PluginBase
std::unique_ptr< MaCh3ArgumentParser > m_parser
 Argument parser for this plugin. More...
 

Detailed Description

Module for computing the R-hat diagnostic for MCMC chains.

This module calculates the R-hat statistic to assess the convergence of MCMC chains.

Definition at line 12 of file RHatModule.hpp.

Constructor & Destructor Documentation

◆ ~RHatModule()

M3::RHatModule::~RHatModule ( )
virtualdefault

Destructor.

Member Function Documentation

◆ get_parser()

MaCh3ArgumentParser * M3::RHatModule::get_parser ( )
overridevirtual

Get the argument parser for this module.

Returns
Pointer to the configured MaCh3ArgumentParser

Implements M3::IPlugin.

Definition at line 22 of file RHatModule.cpp.

22  {
23  m_parser = std::make_unique<MaCh3ArgumentParser>("rhat", "1.0", argparse::default_arguments::help);
24  m_parser->add_description("Tool for computing the R-hat diagnostic for MCMC chains.");
25  m_parser->add_argument("n-thin")
26  .help("if in high mem mode this is number of toys per chain, for low mem thinning setting")
27  .metavar("NTHINTOYS")
28  .scan<'i', int>()
29  .required();
30  m_parser->add_argument("--high-mem")
31  .help("Enable high memory mode.")
32  .flag();
33  m_parser->add_argument("mcmc-chain")
34  .help("MCMC chain root files")
35  .metavar("MCMC_CHAIN1 [MCMC_CHAIN2 [...]]")
36  .nargs(argparse::nargs_pattern::at_least_one)
37  .required();
38  return m_parser.get();
39 }
std::unique_ptr< MaCh3ArgumentParser > m_parser
Argument parser for this plugin.
Definition: plugin.hpp:47

◆ Run()

int M3::RHatModule::Run ( )
overridevirtual

Execute the MCMC diagnostics.

Returns
Exit code (0 on success)

Implements M3::IPlugin.

Definition at line 41 of file RHatModule.cpp.

41  {
44 
45  std::vector<std::string> MCMCFile;
46  auto mcmc_chain_args = m_parser->get<std::vector<std::string>>("mcmc-chain");
47 
48  if (mcmc_chain_args.empty()){ // No MCMC chain files provided, shouldn't be possible with the argparser req.
49  MACH3LOG_ERROR("Wrong arguments");
50  MACH3LOG_ERROR("./RHat NThin MCMCchain_1.root MCMCchain_2.root MCMCchain_3.root ... [how many you like]");
51  throw MaCh3Exception(__FILE__ , __LINE__ );
52  }
53 
54  int NThinToys = m_parser->get<int>("n-thin"); // Number of thinning for MCMC chains, Number of toys for high memory mode
55  std::size_t Nchains = mcmc_chain_args.size();
56 
57  //KS Gelman suggests to diagnose on more than one chain
58  for (const auto& file : mcmc_chain_args){
59  MACH3LOG_INFO("Adding file: {}", file);
60  }
61 
62  if(Nchains == 1){
63  MACH3LOG_WARN("Gelman is going to be sad :(. He suggested you should use more than one chain (at least 4). Code works fine for one chain, however, estimator might be biased.");
64  MACH3LOG_WARN("Multiple chains are more likely to reveal multimodality and poor adaptation or mixing:");
65  }
66 
67 
68  MACH3LOG_INFO("Diagnosing {} chains", Nchains);
69  bool high_mem = m_parser->get<bool>("--high-mem");
70  if (high_mem){
71  MACH3LOG_INFO("With {} toys", NThinToys);
72  if (NThinToys < 1){
73  MACH3LOG_ERROR("You specified {} for NToys, specify larger greater than 0", NThinToys);
74  throw MaCh3Exception(__FILE__ , __LINE__ );
75  }
76  }
77 
78  auto RHatCalc = std::make_unique<RHatCalculator>(high_mem, mcmc_chain_args, NThinToys);
79  RHatCalc->RunDiagnostic();
80 
81  return 0;
82 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:60
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
Custom exception class used throughout MaCh3.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.
Definition: Monitor.cpp:13

The documentation for this class was generated from the following files: