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);
50 for (unsigned int i = 0; i < size; i++) {
51 for (unsigned int j = 0; j < size; j++)
52 InvCov(i, j) = Corr(i, j);
53 }
54
55 // Check inverse-covariance is positive semi-definite
56 TMatrixDSymEigen icovES(InvCov);
57 TVectorD egval(icovES.GetEigenValues());
58 unsigned int EVbad = 0;
59 for (unsigned int i = 0; i < size; i++) {
60 if (egval(i) < 0.) {
61 EVbad++;
62 }
63 }
64 if (EVbad > 0) {
65 std::cout << "WARNING: Inverse-covariance matrix of the correlated observables in "<< name <<" is not a positive semi-definite matrix!" << std::endl;
66 std::cout << "("<< EVbad <<" non positive eigenvalue(s).)" << std::endl;
67 sleep(2);
68 }
69
70 } else {
71 for (unsigned int i = 0; i < size; i++) {
72 for (unsigned int j = 0; j < size; j++)
73 InvCov(i, j) = Obs.at(i).getErrg() * Corr(i, j) * Obs.at(j).getErrg();
74 }
75
76 // Check covariance is positive definite
77 TMatrixDSymEigen covES(InvCov);
78 TVectorD egval(covES.GetEigenValues());
79 unsigned int EVbad = 0;
80 for (unsigned int i = 0; i < size; i++) {
81 if (egval(i) <= 0.) {
82 EVbad++;
83 }
84 }
85 if (EVbad > 0) {
86 std::cout << "WARNING: Covariance matrix of the correlated observables in "<< name <<" is not a positive definite matrix!" << std::endl;
87 std::cout << "("<< EVbad <<" non positive eigenvalue(s).)" << std::endl;
88 sleep(2);
89 }
90
91 // Invert
92 InvCov.Invert();
93 }
94}
95
97 TVectorD x(Obs.size());
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
105int CorrelatedGaussianObservables::ParseCGO(boost::ptr_vector<Observable>& Observables,
106 std::ifstream& ifile,
107 boost::tokenizer<boost::char_separator<char> >::iterator& beg,
108 std::string& infilename,
109 ThObsFactory& myObsFactory,
110 StandardModel * myModel,
111 int lineNo,
112 int rank) {
113 if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1);
114 boost::char_separator<char> sep(" \t");
115 name = *beg;
116 ++beg;
117 int size = atoi((*beg).c_str());
118
119 int nlines = 0;
120 std::vector<bool> lines;
121 std::string line;
122 for (int i = 0; i < size; i++) {
123 IsEOF = getline(ifile, line).eof();
124 if (line.empty() || line.at(0) == '#') {
125 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");
126 else sleep(2);
127 }
128 lineNo++;
129 boost::tokenizer<boost::char_separator<char> > tok(line, sep);
130 beg = tok.begin();
131 std::string type = *beg;
132 ++beg;
133 if (type.compare("Observable") != 0 && type.compare("BinnedObservable") != 0 && type.compare("FunctionObservable") != 0) {
134 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");
135 else sleep(2);
136 }
137 Observable * tmpObs = new Observable();
139 beg = tmpObs->ParseObservable(type, &tok, beg, filepath, infilename, rank);
140 tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
141 if (!IsPrediction) {
142 if (tmpObs->isTMCMC()) {
143 AddObs(*tmpObs);
144 lines.push_back(true);
145 nlines++;
146 } else {
147 Observables.push_back(tmpObs);
148 lines.push_back(false);
149 }
150 } else {
151 if (tmpObs->isTMCMC()) {
152 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");
153 else sleep(2);
154 } else {
155 AddObs(*tmpObs);
156 nlines++;
157 }
158 if (tmpObs->getDistr().compare("weight") == 0 || tmpObs->getDistr().compare("file") == 0) {
159 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");
160 else sleep(2);
161 }
162 }
163 }
164
165 if (nlines > 1) {
166 if (!IsPrediction) {
167 TMatrixD myCorr(nlines, nlines);
168 int ni = 0;
169 for (int i = 0; i < size; i++) {
170 IsEOF = getline(ifile, line).eof();
171 if (line.empty() || line.at(0) == '#') {
172 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");
173 else sleep(2);
174 }
175 lineNo++;
176 if (lines.at(i)) {
177 boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
178 beg = mytok.begin();
179 int nj = 0;
180 for (int j = 0; j < size; j++) {
181 if ((*beg).compare(0, 1, "0") == 0
182 || (*beg).compare(0, 1, "1") == 0
183 || (*beg).compare(0, 1, "-") == 0 || (covarianceFromConfig == true)) {
184 if (std::distance(mytok.begin(), mytok.end()) < size) {
185 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());
186 else sleep(2);
187 }
188 if (lines.at(j)) {
189 myCorr(ni, nj) = atof((*beg).c_str());
190 nj++;
191 }
192 beg++;
193 } else {
194 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");
195 else sleep(2);
196 }
197 }
198 ni++;
199 }
200 }
201 if (!myCorr.IsSymmetric()) {
202 if (rank == 0) throw std::runtime_error("ERROR: invalid correlation matrix for " + name + ". Correlation matrix is not symmetric.\n");
203 else sleep(2);
204 } else {
205 TMatrixDSym mySCorr(nlines);
206 for (int i = 0; i < nlines; i++) {
207 for (int j = 0; j <= i; j++) {
208 mySCorr(i, j) = myCorr(i, j);
209 mySCorr(j, i) = mySCorr(i, j); // Make sure TMatrixDsym element (j,i) is stored symmetrically
210 }
211 }
212 ComputeCov(mySCorr);
213 }
214 } else {
215 InvCov.ResizeTo(size, size);
216 }
217 } else {
218 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;
219 if (getObs().size() == 1) Observables.push_back(new Observable(getObs(0)));
220 for (int i = 0; i < size; i++) {
221 IsEOF = getline(ifile, line).eof();
222 lineNo++;
223 }
224 }
225 return lineNo;
226}
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.
bool IsEOF
A boolean which is true if the end of file is reached.
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...