gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
composite.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
22 
23 #ifndef PRIOR_COMPOSITE_HPP
24 #define PRIOR_COMPOSITE_HPP
25 
26 #include <vector>
27 #include <set>
28 #include <map>
29 #include <unordered_set>
30 #include <unordered_map>
31 #include <algorithm>
32 #include <iostream>
33 
36 
37 
38 namespace Gambit
39 {
40  namespace Priors
41  {
46 
47  class CompositePrior : public BasePrior
48  {
49 
50  private:
51  // References to component prior objects
52  std::vector<BasePrior*> my_subpriors;
53  std::vector<std::string> shown_param_names;
54 
55  public:
56 
57  // Constructors defined in composite.cpp
58  CompositePrior(const Options &model_options, const Options &prior_options);
59 
60  CompositePrior(const std::vector<std::string> &params, const Options &options);
61 
62  inline std::vector<std::string> getShownParameters() const { return shown_param_names; }
63 
64  // Transformation from unit hypercube to physical parameters
65  void transform(const std::vector<double> &unitPars, std::unordered_map<std::string,double> &outputMap) const
66  {
67  std::vector<double>::const_iterator unit_it = unitPars.begin(), unit_next;
68  for (auto it = my_subpriors.begin(), end = my_subpriors.end(); it != end; it++)
69  {
70  unit_next = unit_it + (*it)->size();
71  std::vector<double> subUnit(unit_it, unit_next);
72  unit_it = unit_next;
73  (*it)->transform(subUnit, outputMap);
74  }
75  }
76 
77  // Transformation from physical parameters back to unit hypercube
78  std::vector<double> inverse_transform(const std::unordered_map<std::string, double> &physical) const override
79  {
80  std::vector<double> u;
81  for (auto it = my_subpriors.begin(), end = my_subpriors.end(); it != end; it++)
82  {
83  auto ublock = (*it)->inverse_transform(physical);
84  u.insert(u.end(), ublock.begin(), ublock.end());
85  }
86 
87  // check it
88 
89  for (const auto &p : u)
90  {
91  if (p > 1. || p < 0.)
92  {
93  throw std::runtime_error("unit hypercube outside 0 and 1");
94  }
95  }
96 
97  auto round_trip = physical;
98  transform(u, round_trip);
99  const double rtol = 1e-4;
100  for (const auto &s : physical)
101  {
102  const double a = round_trip.at(s.first);
103  const double b = s.second;
104  const double rdiff = std::abs(a - b) / std::max(std::abs(a), std::abs(b));
105  if (rdiff > rtol)
106  {
107  throw std::runtime_error("could not convert physical parameters to hypercube");
108  }
109  }
110 
111  return u;
112  }
113 
114  //~CompositePrior() noexcept
116  {
117  // Need to destroy all the prior objects that we created using 'new'
118  for (auto it = my_subpriors.begin(), end = my_subpriors.end(); it != end; it++)
119  {
120  // Delete prior object
121  delete *it;
122  }
123  }
124  };
125 
126  LOAD_PRIOR(composite, CompositePrior)
127  } // end namespace Priors
128 } // end namespace Gambit
129 
130 #endif
Abstract base class for priors.
Definition: base_prior.hpp:40
std::vector< std::string > shown_param_names
Definition: composite.hpp:53
CompositePrior(const Options &model_options, const Options &prior_options)
Special "build-a-prior" classi.
Definition: composite.cpp:99
Prior object construction routines.
START_MODEL b
Definition: demo.hpp:270
Declarations for the YAML options class.
std::vector< std::string > getShownParameters() const
Definition: composite.hpp:62
void transform(const std::vector< double > &unitPars, std::unordered_map< std::string, double > &outputMap) const
Transform from unit hypercube to parameter.
Definition: composite.hpp:65
std::vector< BasePrior * > my_subpriors
Definition: composite.hpp:52
RangePrior1D< flatprior > LOAD_PRIOR(cos, RangePrior1D< cosprior >) LOAD_PRIOR(sin
TODO: see if we can use this one:
Definition: Analysis.hpp:33
A small wrapper object for &#39;options&#39; nodes.
std::vector< double > inverse_transform(const std::unordered_map< std::string, double > &physical) const override
Transform from parameter back to unit hypercube.
Definition: composite.hpp:78
Special "build-a-prior" class This is the class to use for setting simple 1D priors (from the library...
Definition: composite.hpp:47