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...
 
std::vector< double > computeLeaveOneOutWeights ()
 A method to compute the full weight and the weights associated with the observable when one observable is left out of the 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...
 
TVectorD x
 The vector of the differences between the theory and average values of the observables in the set. 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), x(orig.x) {
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.
TVectorD x
The vector of the differences between the theory and average values of the observables in the set.
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);
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}

◆ computeLeaveOneOutWeights()

std::vector< double > CorrelatedGaussianObservables::computeLeaveOneOutWeights ( )

A method to compute the full weight and the weights associated with the observable when one observable is left out of the set.

Definition at line 105 of file CorrelatedGaussianObservables.cpp.

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

◆ computeWeight()

double CorrelatedGaussianObservables::computeWeight ( )
virtual

A method to compute the weight associated with the observable.

Definition at line 97 of file CorrelatedGaussianObservables.cpp.

97 {
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 112 of file CorrelatedGaussianObservables.h.

113 {
114 return InvCov;
115 }

◆ 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 104 of file CorrelatedGaussianObservables.h.

105 {
106 return name;
107 }

◆ 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 85 of file CorrelatedGaussianObservables.h.

86 {
87 return Obs;
88 }

◆ 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 95 of file CorrelatedGaussianObservables.h.

96 {
97 return (Obs.at(i));
98 }

◆ 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 140 of file CorrelatedGaussianObservables.h.

141 {
142 return IsEOF;
143 }

◆ 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 158 of file CorrelatedGaussianObservables.h.

159 {
160 return IsPrediction;
161 }

◆ 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 123 of file CorrelatedGaussianObservables.cpp.

130 {
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}
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 176 of file CorrelatedGaussianObservables.h.

177 {
178 covarianceFromConfig = setInvCov;
179 }

◆ 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 149 of file CorrelatedGaussianObservables.h.

150 {
151 IsPrediction = IsPrediction_i;
152 }

◆ 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 167 of file CorrelatedGaussianObservables.h.

168 {
169 name = name_i;
170 }

Member Data Documentation

◆ covarianceFromConfig

bool CorrelatedGaussianObservables::covarianceFromConfig
private

Definition at line 189 of file CorrelatedGaussianObservables.h.

◆ filepath

std::string CorrelatedGaussianObservables::filepath
private

The path to the config file being parsed.

Definition at line 186 of file CorrelatedGaussianObservables.h.

◆ InvCov

TMatrixDSym CorrelatedGaussianObservables::InvCov
private

The inverse covariance matrix.

Definition at line 183 of file CorrelatedGaussianObservables.h.

◆ IsEOF

bool CorrelatedGaussianObservables::IsEOF
private

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

Definition at line 187 of file CorrelatedGaussianObservables.h.

◆ IsPrediction

bool CorrelatedGaussianObservables::IsPrediction
private

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

Definition at line 188 of file CorrelatedGaussianObservables.h.

◆ name

std::string CorrelatedGaussianObservables::name
private

The name of the correlated Gaussian Observables set.

Definition at line 185 of file CorrelatedGaussianObservables.h.

◆ Obs

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

A vector of observables whose correlation will be calculated.

Definition at line 182 of file CorrelatedGaussianObservables.h.

◆ x

TVectorD CorrelatedGaussianObservables::x
private

The vector of the differences between the theory and average values of the observables in the set.

Definition at line 184 of file CorrelatedGaussianObservables.h.


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