a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
CorrelatedGaussianObservables.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 HEPfit Collaboration
3 *
4 *
5 * For the licensing terms see doc/COPYING.
6 */
7
8#include <stdexcept>
10#include "ThObsFactory.h"
11#include <fstream>
12#include <TMatrixD.h>
13#include <TVectorD.h>
14
16 name = name_i;
17 IsPrediction = false;
18 IsEOF = false;
20}
21
23 name = "";
24 IsPrediction = false;
25 IsEOF = false;
27}
28
30 Obs = orig.Obs;
31 name = orig.name;
33 IsEOF = orig.IsEOF;
35}
36
38}
39
41 Obs.push_back(Obs_i);
42}
43
44void CorrelatedGaussianObservables::ComputeCov(const TMatrixDSym& Corr) {
45 unsigned int size = Obs.size();
46 if (Corr.GetNrows() != size)
47 throw std::runtime_error("The size of the correlated observables in " + name + " does not match the size of the correlation matrix!");
48 InvCov.ResizeTo(size, size);
49 x.ResizeTo(size);
51 for (unsigned int i = 0; i < size; i++) {
52 for (unsigned int j = 0; j < size; j++)
53 InvCov(i, j) = Corr(i, j);
54 }
55
56 // Check inverse-covariance is positive semi-definite
57 TMatrixDSymEigen icovES(InvCov);
58 TVectorD egval(icovES.GetEigenValues());
59 unsigned int EVbad = 0;
60 for (unsigned int i = 0; i < size; i++) {
61 if (egval(i) < 0.) {
62 EVbad++;
63 }
64 }
65 if (EVbad > 0) {
66 std::cout << "WARNING: Inverse-covariance matrix of the correlated observables in "<< name <<" is not a positive semi-definite matrix!" << std::endl;
67 std::cout << "("<< EVbad <<" non positive eigenvalue(s).)" << std::endl;
68 sleep(2);
69 }
70
71 } else {
72 for (unsigned int i = 0; i < size; i++) {
73 for (unsigned int j = 0; j < size; j++)
74 InvCov(i, j) = Obs.at(i).getErrg() * Corr(i, j) * Obs.at(j).getErrg();
75 }
76
77 // Check covariance is positive definite
78 TMatrixDSymEigen covES(InvCov);
79 TVectorD egval(covES.GetEigenValues());
80 unsigned int EVbad = 0;
81 for (unsigned int i = 0; i < size; i++) {
82 if (egval(i) <= 0.) {
83 EVbad++;
84 }
85 }
86 if (EVbad > 0) {
87 std::cout << "WARNING: Covariance matrix of the correlated observables in "<< name <<" is not a positive definite matrix!" << std::endl;
88 std::cout << "("<< EVbad <<" non positive eigenvalue(s).)" << std::endl;
89 sleep(2);
90 }
91
92 // Invert
93 InvCov.Invert();
94 }
95}
96
98
99 for (unsigned int i = 0; i < Obs.size(); i++)
100 x(i) = Obs.at(i).computeTheoryValue() - Obs.at(i).getAve();
101
102 return (-0.5 * x * (InvCov * x));
103}
104
106 std::vector<double> weights;
107
108 for (unsigned int i = 0; i < Obs.size(); i++)
109 x(i) = Obs.at(i).computeTheoryValue() - Obs.at(i).getAve();
110
111 weights.push_back(-0.5 * x * (InvCov * x));
112
113 for (unsigned int i = 0; i < Obs.size(); i++) {
114 double xi_backup = x(i);
115 x(i) = 0.0;
116 weights.push_back(-0.5 * x * (InvCov * x));
117 x(i) = xi_backup;
118 }
119
120 return weights;
121}
122
123int CorrelatedGaussianObservables::ParseCGO(boost::ptr_vector<Observable>& Observables,
124 std::ifstream& ifile,
125 boost::tokenizer<boost::char_separator<char> >::iterator& beg,
126 std::string& infilename,
127 ThObsFactory& myObsFactory,
128 StandardModel * myModel,
129 int lineNo,
130 int rank) {
131 if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1);
132 boost::char_separator<char> sep(" \t");
133 name = *beg;
134 ++beg;
135 int size = atoi((*beg).c_str());
136
137 int nlines = 0;
138 std::vector<bool> lines;
139 std::string line;
140 for (int i = 0; i < size; i++) {
141 IsEOF = getline(ifile, line).eof();
142 if (line.empty() || line.at(0) == '#') {
143 if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianObservables please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
144 else sleep(2);
145 }
146 lineNo++;
147 boost::tokenizer<boost::char_separator<char> > tok(line, sep);
148 beg = tok.begin();
149 std::string type = *beg;
150 ++beg;
151 if (type.compare("Observable") != 0 && type.compare("BinnedObservable") != 0 && type.compare("FunctionObservable") != 0) {
152 if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", expecting an Observable or BinnedObservable or FunctionObservable type here...\n");
153 else sleep(2);
154 }
155 Observable * tmpObs = new Observable();
157 beg = tmpObs->ParseObservable(type, &tok, beg, filepath, infilename, rank);
158 tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
159 if (!IsPrediction) {
160 if (tmpObs->isTMCMC()) {
161 AddObs(*tmpObs);
162 lines.push_back(true);
163 nlines++;
164 } else {
165 Observables.push_back(tmpObs);
166 lines.push_back(false);
167 }
168 } else {
169 if (tmpObs->isTMCMC()) {
170 if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", Observable/BinnedObservable cannot be set to MCMC in CorrelatedObservable. Use CorrelatedGaussianObservable instead.\n");
171 else sleep(2);
172 } else {
173 AddObs(*tmpObs);
174 nlines++;
175 }
176 if (tmpObs->getDistr().compare("weight") == 0 || tmpObs->getDistr().compare("file") == 0) {
177 if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", Observable/BinnedObservable cannot be set to weight or file in CorrelatedObservable. Use CorrelatedGaussianObservable instead.\n");
178 else sleep(2);
179 }
180 }
181 }
182
183 if (nlines > 1) {
184 if (!IsPrediction) {
185 TMatrixD myCorr(nlines, nlines);
186 int ni = 0;
187 for (int i = 0; i < size; i++) {
188 IsEOF = getline(ifile, line).eof();
189 if (line.empty() || line.at(0) == '#') {
190 if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianObservables please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
191 else sleep(2);
192 }
193 lineNo++;
194 if (lines.at(i)) {
195 boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
196 beg = mytok.begin();
197 int nj = 0;
198 for (int j = 0; j < size; j++) {
199 if ((*beg).compare(0, 1, "0") == 0
200 || (*beg).compare(0, 1, "1") == 0
201 || (*beg).compare(0, 1, "-") == 0 || (covarianceFromConfig == true)) {
202 if (std::distance(mytok.begin(), mytok.end()) < size) {
203 if (rank == 0) throw std::runtime_error(("ERROR: Correlation matrix is of wrong size in Correlated Gaussian Observables: " + name + +" at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n").c_str());
204 else sleep(2);
205 }
206 if (lines.at(j)) {
207 myCorr(ni, nj) = atof((*beg).c_str());
208 nj++;
209 }
210 beg++;
211 } else {
212 if (rank == 0) throw std::runtime_error("ERROR: invalid correlation matrix for " + name + ". Check element (" + boost::lexical_cast<std::string>(ni + 1) + "," + boost::lexical_cast<std::string>(nj + 1) + ") in line number " + boost::lexical_cast<std::string>(lineNo) + " in file " + infilename + ".\n");
213 else sleep(2);
214 }
215 }
216 ni++;
217 }
218 }
219 if (!myCorr.IsSymmetric()) {
220 if (rank == 0) throw std::runtime_error("ERROR: invalid correlation matrix for " + name + ". Correlation matrix is not symmetric.\n");
221 else sleep(2);
222 } else {
223 TMatrixDSym mySCorr(nlines);
224 for (int i = 0; i < nlines; i++) {
225 for (int j = 0; j <= i; j++) {
226 mySCorr(i, j) = myCorr(i, j);
227 mySCorr(j, i) = mySCorr(i, j); // Make sure TMatrixDsym element (j,i) is stored symmetrically
228 }
229 }
230 ComputeCov(mySCorr);
231 }
232 } else {
233 InvCov.ResizeTo(size, size);
234 }
235 } else {
236 if (rank == 0) std::cout << "\nWARNING: Correlated (Gaussian) Observable " << name.c_str() << " defined with less than two observables" << " in file " << infilename << ". The set is being marked as normal Observables." << std::endl;
237 if (getObs().size() == 1) Observables.push_back(new Observable(getObs(0)));
238 for (int i = 0; i < size; i++) {
239 IsEOF = getline(ifile, line).eof();
240 lineNo++;
241 }
242 }
243 return lineNo;
244}
A class for correlated Gaussian observables.
std::vector< Observable > Obs
A vector of observables whose correlation will be calculated.
bool IsPrediction
Flag to define a set of Correlated Observables to be predicted.
std::string filepath
The path to the config file being parsed.
virtual double computeWeight()
A method to compute the weight associated with the observable.
CorrelatedGaussianObservables()
The default Constructor.
int ParseCGO(boost::ptr_vector< Observable > &Observables, std::ifstream &ifile, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &infilename, ThObsFactory &myObsFactory, StandardModel *myModel, int lineNo, int rank)
The parser for CorrelatedGaussianObservables.
virtual ~CorrelatedGaussianObservables()
The default destructor.
std::vector< double > computeLeaveOneOutWeights()
A method to compute the full weight and the weights associated with the observable when one observabl...
bool IsEOF
A boolean which is true if the end of file is reached.
TVectorD x
The vector of the differences between the theory and average values of the observables in the set.
std::vector< Observable > getObs() const
A get method to access the vector of observables that are defined in one correlated Gaussian observab...
void ComputeCov(const TMatrixDSym &Corr)
Computes the covariance matrix for the correlated Gaussian observables set.
std::string name
The name of the correlated Gaussian Observables set.
void AddObs(Observable &Obs_i)
A method to add observables to the list of correlated Gaussian observables.
TMatrixDSym InvCov
The inverse covariance matrix.
A class for observables.
Definition: Observable.h:28
bool isTMCMC() const
A method to check if the observable is listed for MCMC.
Definition: Observable.h:341
void setHasInverseCovariance(bool hasInverseCovariance)
A set method to state that the Observable is a part of ObservablesWithInverseCovariance.
Definition: Observable.h:464
void setTho(ThObservable *tho_i)
A set method to fix the pointer to object of type ThObservable.
Definition: Observable.h:413
std::string getThname() const
A get method to access the thname of the observable as defined in ThFactory class.
Definition: Observable.h:368
boost::tokenizer< boost::char_separator< char > >::iterator & ParseObservable(std::string &type, boost::tokenizer< boost::char_separator< char > > *tok, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &filepath, std::string &infilename, int rank)
The parser for Observables.
Definition: Observable.cpp:202
std::string getDistr() const
A get method to access the name of the distribution of the observable.
Definition: Observable.h:140
A model class for the Standard Model.
A class for.
Definition: ThObsFactory.h:26
ThObservable * CreateThMethod(const std::string &name, StandardModel &model) const
This method checks for the existence of an observable of a specific name in the map thobs and returns...