gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
examples.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
16 
17 #include <iostream>
18 #include <fstream>
19 #include <sstream>
20 #include <vector>
21 #include <utility>
22 #include <algorithm>
23 
26 
27 objective_plugin(uniform, version(1,0,0))
28 {
29  double plugin_main (std::unordered_map<std::string, double> /*&map*/)
30  {
31  return 0;
32  }
33 }
34 
35 objective_plugin(gaussian, version(1, 0, 0))
36 {
37  Gambit::Cholesky chol;
38  std::vector <double> mean;
39  std::vector<std::vector<double>> cov;
40  unsigned int dim;
41 
43  {
44  dim = get_keys().size();
45  std::vector<std::vector<double>> cov(dim, std::vector<double>(dim, 0));
46 
47  cov = get_inifile_value<std::vector<std::vector<double>>> ("cov", cov);
48 
49  if (cov.size() != dim)
50  {
51  scan_err << "Gaussian: Coverance matrix size of " << cov.size() << " is different than the parameter size of " << dim << scan_end;
52  }
53 
54  for (std::vector<std::vector<double>>::iterator it = cov.begin(); it != cov.end(); it++)
55  {
56  if (it->size() != cov.size())
57  {
58  scan_err << "Gaussian: Coverance matrix is not square" << scan_end;
59  }
60  }
61 
62  if (!chol.EnterMat(cov))
63  {
64  std::vector <double> sigs = get_inifile_value <std::vector <double>> ("sigs", std::vector<double>(dim, 1.0));
65  if (sigs.size() != dim)
66  {
67  scan_err << "Gaussian: Sigma vector size of " << sigs.size() << " is different than the parameter size of " << dim << scan_end;
68  }
69  else
70  {
71  for (int i = 0, end = sigs.size(); i < end; i++)
72  {
73  cov[i][i] = sigs[i]*sigs[i];
74  }
75  }
76  }
77 
78  mean = get_inifile_value<std::vector <double>> ("mean", std::vector <double>(dim, 0));
79 
80  if (!chol.EnterMat(cov))
81  {
82  scan_err << "Gaussian: Covariance matrix is not positive definite." << scan_end;
83  }
84  }
85 
86  double plugin_main(std::unordered_map<std::string, double> &map)
87  {
88  static std::vector<double> params(get_keys().size());
89 
90  print_parameters(map);
91 
92  auto it_p = params.begin();
93  for (auto it = get_keys().begin(), end = get_keys().end(); it != end; ++it, ++it_p)
94  {
95  *it_p = map[*it];
96  }
97 
98  return -chol.Square(params, mean)/2.0;
99  }
100 }
101 
102 objective_plugin(normal_example, version(1, 0, 0))
103 {
105  {
106  int dim = get_keys().size();
107 
108  if (dim != 2)
109  {
110  scan_err << "Normal: Need to have two parameters." << scan_end;
111  }
112  }
113 
114  constexpr double SQ(double a) {return a*a;}
115 
116  double plugin_main(std::unordered_map<std::string, double> &map)
117  {
118  print_parameters(map);
119 
120  static const int N = 20;
121  static const double samples [] = {
122  21.32034213, 20.39713359, 19.27957134, 19.81839231,
123  20.89474358, 20.11058756, 22.38214557, 21.41479798,
124  23.49896999, 17.55991187, 24.9921142 , 23.90166585,
125  20.97913273, 18.59180551, 23.49038072, 19.08201714,
126  21.19538797, 16.42544039, 18.93568891, 22.40925288
127  };
128 
129  double ret = 0.0;
130  for (int i = 0; i < N; i++)
131  ret += SQ((samples[i] - map["Normal::mu"])/map["Normal::sigma"]);
132 
133  return -ret/2.0 - N*std::log(map["Normal::sigma"]);
134  }
135 }
136 
137 objective_plugin(EggBox, version(1, 0, 0))
138 {
139  std::pair <double, double> length;
140  unsigned int dim;
141 
143  {
144  dim = get_keys().size();
145 
146  if (dim != 2)
147  {
148  scan_err << "EggBox: Need to have two parameters." << scan_end;
149  }
150  length = get_inifile_value<std::pair<double, double> > ("length", std::pair<double, double>(10.0, 10.0));
151  }
152 
153  double plugin_main(std::unordered_map<std::string, double> &map)
154  {
155  print_parameters(map);
156 
157  double params[2];
158  params[0] = map[get_keys()[0]]*length.first;
159  params[1] = map[get_keys()[1]]*length.second;
160 
161  return 5.0*std::log(2.0 + std::cos(params[0]*M_PI_2)*std::cos(params[1]*M_PI_2));
162  }
163 }
164 
165 objective_plugin(flat_prior, version(1, 0, 0))
166 {
167  std::vector<std::string> keys;
168  std::pair<double, double> range;
169 
171  {
172  keys = get_keys();
173  set_dimension(keys.size());
174  range = get_inifile_value<decltype(range)>("range", decltype(range)(0.0, 1.0));
175  }
176 
177  void plugin_main(const std::vector<double> &unitpars, std::unordered_map<std::string, double> &outputMap)
178  {
179  auto u_it = unitpars.begin();
180  for (auto it = keys.begin(), end = keys.end(); it != end; it++)
181  outputMap[*it] = range.first + (range.second - range.first)*(*u_it++);
182  }
183 
184  double plugin_main(const std::vector<double>&)
185  {
186  return 0.0;
187  }
188 }
bool EnterMat(std::vector< std::vector< double >> &a)
Definition: cholesky.hpp:36
Macros and related classes for declaring scanner test functions.
declaration for scanner module
const T SQ(const T a)
double Square(const std::vector< double > &y, const std::vector< double > &y0)
Definition: cholesky.hpp:86
#define plugin_constructor
Runs when the plugin is loaded.
#define scan_err
Defined to macros to output errors in the form: scan_err << "error" << scan_end; scan_warn << "warnin...
#define plugin_main(...)
Declaration of the main function which will be ran by the interface.
objective_plugin(uniform, version(1, 0, 0))
Definition: examples.cpp:27
#define scan_end