a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
CorrelatedGaussianObservables Class Reference

A class for correlated Gaussian observables. More...

#include <CorrelatedGaussianObservables.h>

Detailed Description

A class for correlated Gaussian observables.

Author
HEPfit Collaboration

This class builds the correlated Gaussian observables that are specified in the SomeModel.conf file or specified by the user.

Definition at line 34 of file CorrelatedGaussianObservables.h.

Public Member Functions

void AddObs (Observable &Obs_i)
 A method to add observables to the list of correlated Gaussian observables. More...
 
void ComputeCov (const TMatrixDSym &Corr)
 Computes the covariance matrix for the correlated Gaussian observables set. More...
 
virtual double computeWeight ()
 A method to compute the weight associated with the observable. More...
 
 CorrelatedGaussianObservables ()
 The default Constructor. More...
 
 CorrelatedGaussianObservables (const CorrelatedGaussianObservables &orig)
 The copy constructor. More...
 
 CorrelatedGaussianObservables (std::string name_i)
 Constructor. More...
 
const TMatrixDSym & getCov () const
 A get method to access the covariance matrix of the correlated Gaussian observables. More...
 
std::string getName () const
 A get method to access the name of the correlated Gaussian observables set. More...
 
std::vector< ObservablegetObs () const
 A get method to access the vector of observables that are defined in one correlated Gaussian observables set. More...
 
Observable getObs (int i) const
 A get method to access an element of the vector of observables that are defined in one correlated Gaussian observables set. More...
 
bool isEOF ()
 A method to check if the end of file has been reached. More...
 
bool isPrediction ()
 A method to check if the Correlated Observables are set for prediction. More...
 
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. More...
 
void setCovarianceFromConfig (bool setInvCov)
 A method to specify whether the inverse covariance is being set from the config file. More...
 
void setIsPrediction (bool IsPrediction_i)
 A method to set a set of CGO to be predicted. More...
 
void setName (std::string name_i)
 A method to set a set of CGO to be predicted. More...
 
virtual ~CorrelatedGaussianObservables ()
 The default destructor. More...
 

Private Attributes

bool covarianceFromConfig
 
std::string filepath
 The path to the config file being parsed. More...
 
TMatrixDSym InvCov
 The inverse covariance matrix. More...
 
bool IsEOF
 A boolean which is true if the end of file is reached. More...
 
bool IsPrediction
 Flag to define a set of Correlated Observables to be predicted. More...
 
std::string name
 The name of the correlated Gaussian Observables set. More...
 
std::vector< ObservableObs
 A vector of observables whose correlation will be calculated. More...
 

Constructor & Destructor Documentation

◆ CorrelatedGaussianObservables() [1/3]

CorrelatedGaussianObservables::CorrelatedGaussianObservables ( std::string  name_i)

Constructor.

Parameters
[in]name_ia given name for the set of correlated Gaussian observables

Definition at line 15 of file CorrelatedGaussianObservables.cpp.

15 {
16 name = name_i;
17 IsPrediction = false;
18 IsEOF = false;
20}
bool IsPrediction
Flag to define a set of Correlated Observables to be predicted.
bool IsEOF
A boolean which is true if the end of file is reached.
std::string name
The name of the correlated Gaussian Observables set.

◆ CorrelatedGaussianObservables() [2/3]

CorrelatedGaussianObservables::CorrelatedGaussianObservables ( )

The default Constructor.

Definition at line 22 of file CorrelatedGaussianObservables.cpp.

22 {
23 name = "";
24 IsPrediction = false;
25 IsEOF = false;
27}

◆ CorrelatedGaussianObservables() [3/3]

CorrelatedGaussianObservables::CorrelatedGaussianObservables ( const CorrelatedGaussianObservables orig)

The copy constructor.

Definition at line 29 of file CorrelatedGaussianObservables.cpp.

29 : InvCov(orig.InvCov) {
30 Obs = orig.Obs;
31 name = orig.name;
33 IsEOF = orig.IsEOF;
35}
std::vector< Observable > Obs
A vector of observables whose correlation will be calculated.
TMatrixDSym InvCov
The inverse covariance matrix.

◆ ~CorrelatedGaussianObservables()

CorrelatedGaussianObservables::~CorrelatedGaussianObservables ( )
virtual

The default destructor.

Definition at line 37 of file CorrelatedGaussianObservables.cpp.

37 {
38}

Member Function Documentation

◆ AddObs()

void CorrelatedGaussianObservables::AddObs ( Observable Obs_i)

A method to add observables to the list of correlated Gaussian observables.

Parameters
Obs_ireference to an object of type Observable

Definition at line 40 of file CorrelatedGaussianObservables.cpp.

40 {
41 Obs.push_back(Obs_i);
42}

◆ ComputeCov()

void CorrelatedGaussianObservables::ComputeCov ( const TMatrixDSym &  Corr)

Computes the covariance matrix for the correlated Gaussian observables set.

Parameters
Corrthe correlation matrix for the correlated Gassian observables set

Definition at line 44 of file CorrelatedGaussianObservables.cpp.

44 {
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}

◆ computeWeight()

double CorrelatedGaussianObservables::computeWeight ( )
virtual

A method to compute the weight associated with the observable.

Definition at line 96 of file CorrelatedGaussianObservables.cpp.

96 {
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}

◆ getCov()

const TMatrixDSym & CorrelatedGaussianObservables::getCov ( ) const
inline

A get method to access the covariance matrix of the correlated Gaussian observables.

Definition at line 107 of file CorrelatedGaussianObservables.h.

108 {
109 return InvCov;
110 }

◆ getName()

std::string CorrelatedGaussianObservables::getName ( ) const
inline

A get method to access the name of the correlated Gaussian observables set.

Returns
the name

Definition at line 99 of file CorrelatedGaussianObservables.h.

100 {
101 return name;
102 }

◆ getObs() [1/2]

std::vector< Observable > CorrelatedGaussianObservables::getObs ( ) const
inline

A get method to access the vector of observables that are defined in one correlated Gaussian observables set.

Returns
a vector of type Observable()

Definition at line 80 of file CorrelatedGaussianObservables.h.

81 {
82 return Obs;
83 }

◆ getObs() [2/2]

Observable CorrelatedGaussianObservables::getObs ( int  i) const
inline

A get method to access an element of the vector of observables that are defined in one correlated Gaussian observables set.

Returns
an element of the vector of type Observable()

Definition at line 90 of file CorrelatedGaussianObservables.h.

91 {
92 return (Obs.at(i));
93 }

◆ isEOF()

bool CorrelatedGaussianObservables::isEOF ( )
inline

A method to check if the end of file has been reached.

Returns
a boolean which is true if the end of file has been reached

Definition at line 135 of file CorrelatedGaussianObservables.h.

136 {
137 return IsEOF;
138 }

◆ isPrediction()

bool CorrelatedGaussianObservables::isPrediction ( )
inline

A method to check if the Correlated Observables are set for prediction.

Returns
a boolean which is true if the set of CGO are to be predicted

Definition at line 153 of file CorrelatedGaussianObservables.h.

154 {
155 return IsPrediction;
156 }

◆ ParseCGO()

int CorrelatedGaussianObservables::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.

Parameters
[in]Observablesthe pointer vector containing the Observables
[in]ifilethe stream containing the config file to be parsed
[in]begthe iterator that parses a line in the config file
[in]infilenamethe name of the config file being parsed
[in]myModela pointer to the model
[in]lineNothe current line number at which the file is being parsed
[in]rankthe rank of the process that is using the parser
Returns
the line number (integer) after the parsing is done

Definition at line 105 of file CorrelatedGaussianObservables.cpp.

112 {
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}
std::string filepath
The path to the config file being parsed.
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.
void AddObs(Observable &Obs_i)
A method to add observables to the list of correlated Gaussian observables.
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
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...

◆ setCovarianceFromConfig()

void CorrelatedGaussianObservables::setCovarianceFromConfig ( bool  setInvCov)
inline

A method to specify whether the inverse covariance is being set from the config file.

Parameters
[in]setInvCova boolean which is true if the inverse covariance matrix is set from file

Definition at line 171 of file CorrelatedGaussianObservables.h.

172 {
173 covarianceFromConfig = setInvCov;
174 }

◆ setIsPrediction()

void CorrelatedGaussianObservables::setIsPrediction ( bool  IsPrediction_i)
inline

A method to set a set of CGO to be predicted.

Parameters
[in]IsPrediction_ia boolean which is true if the set of CGO is set for prediction

Definition at line 144 of file CorrelatedGaussianObservables.h.

145 {
146 IsPrediction = IsPrediction_i;
147 }

◆ setName()

void CorrelatedGaussianObservables::setName ( std::string  name_i)
inline

A method to set a set of CGO to be predicted.

Parameters
[in]IsPrediction_ia boolean which is true if the set of CGO is set for prediction

Definition at line 162 of file CorrelatedGaussianObservables.h.

163 {
164 name = name_i;
165 }

Member Data Documentation

◆ covarianceFromConfig

bool CorrelatedGaussianObservables::covarianceFromConfig
private

Definition at line 183 of file CorrelatedGaussianObservables.h.

◆ filepath

std::string CorrelatedGaussianObservables::filepath
private

The path to the config file being parsed.

Definition at line 180 of file CorrelatedGaussianObservables.h.

◆ InvCov

TMatrixDSym CorrelatedGaussianObservables::InvCov
private

The inverse covariance matrix.

Definition at line 178 of file CorrelatedGaussianObservables.h.

◆ IsEOF

bool CorrelatedGaussianObservables::IsEOF
private

A boolean which is true if the end of file is reached.

Definition at line 181 of file CorrelatedGaussianObservables.h.

◆ IsPrediction

bool CorrelatedGaussianObservables::IsPrediction
private

Flag to define a set of Correlated Observables to be predicted.

Definition at line 182 of file CorrelatedGaussianObservables.h.

◆ name

std::string CorrelatedGaussianObservables::name
private

The name of the correlated Gaussian Observables set.

Definition at line 179 of file CorrelatedGaussianObservables.h.

◆ Obs

std::vector<Observable> CorrelatedGaussianObservables::Obs
private

A vector of observables whose correlation will be calculated.

Definition at line 177 of file CorrelatedGaussianObservables.h.


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