a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
CorrelatedGaussianParameters.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>
9#include <vector>
10#include <fstream>
11#include <sstream>
12#include <math.h>
13#include <boost/lexical_cast.hpp>
15
17 name = name_i;
18 v = NULL;
19 e = NULL;
20 IsEOF = false;
21}
22
24 v = NULL;
25 e = NULL;
26 IsEOF = false;
27}
28
30 {
31 Pars = orig.Pars;
32 name = orig.name;
33 v = new gslpp::matrix<double>(*orig.v);
34 e = new gslpp::vector<double>(*orig.e);
35 DiagPars = orig.DiagPars;
36 IsEOF = orig.IsEOF;
37}
38
40 //Cov = new gslpp::matrix<double>(10, 10, 0.); /** Put in to prevent seg fault during error handling in InputParser **/
41 if (v != NULL)
42 delete(v);
43 if (e != NULL)
44 delete(e);
45}
46
48 Pars.push_back(Par_i);
49}
50
52 unsigned int size = Pars.size();
53 if (Corr.GetNrows() != size || Corr.GetNcols() != size)
54 throw std::runtime_error("The size of the correlated parameters in " + name + " does not match the size of the correlation matrix!");
55 Cov.ResizeTo(size,size);
56 for (unsigned int i = 0; i < size; i++) {
57 for (unsigned int j = i; j < size; j++) {
58 Cov(i, j) = Pars.at(i).geterrg() * Corr(i, j) * Pars.at(j).geterrg();
59 Cov(j, i) = Cov(i, j); // Make sure TMatrixDsym element (j,i) is stored symmetrically
60 }
61 }
62
63 // *Cov = Cov->inverse();
64
65 TMatrixDSymEigen SE(Cov);
66
67 TMatrixD vv(SE.GetEigenVectors());
68 TVectorD ee(SE.GetEigenValues());
69 unsigned int EVbad = 0;
70
71 v = new gslpp::matrix<double>(size, size, 0.);
72 e = new gslpp::vector<double>(size, 0.);
73
74 for (unsigned int i = 0; i < size; i++) {
75 (*e)(i) = ee(i);
76 for (unsigned int j = 0; j < size; j++) {
77 (*v)(i, j) = vv(i, j);
78 }
79
80 if (ee(i) <= 0.) {
81 EVbad++;
82 }
83
84 }
85
86 if (EVbad > 0) {
87 std::cout << "WARNING: Covariance matrix of the correlated parameters 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 gslpp::vector<double> ave_in(size, 0.);
93
94 int ind = 0;
95 for (std::vector<ModelParameter>::iterator it = Pars.begin(); it != Pars.end(); it++) {
96 ave_in(ind) = it->getave();
97 ind++;
98 }
99
100 gslpp::vector<double> ave = v->transpose() * ave_in;
101
102 for (unsigned int i = 0; i < size; i++) {
103 std::stringstream ss;
104 ss << (i + 1);
105 std::string namei = name + ss.str();
106 // ModelParameter current(namei,ave(i),1./sqrt((*e)(i)),0.);
107 ModelParameter current(namei, ave(i), sqrt((*e)(i)), 0.);
108 current.setCgp_name(name);
109 DiagPars.push_back(current);
110 //std::cout << current << std::endl;
111 }
112}
113
114std::vector<double> CorrelatedGaussianParameters::getOrigParsValue(const std::vector<double>& DiagPars_i) const {
115 if (DiagPars_i.size() != DiagPars.size()) {
116 std::stringstream out;
117 out << DiagPars_i.size();
118 throw std::runtime_error("CorrelatedGaussianParameters::getOrigParsValue(DiagPars_i): DiagPars_i.size() = " + out.str() + " does not match the size of DiagPars");
119 }
120 gslpp::vector<double> pars_in(DiagPars_i.size(), 0.);
121
122 int ind = 0;
123 for (std::vector<double>::const_iterator it = DiagPars_i.begin(); it != DiagPars_i.end(); it++) {
124 pars_in(ind) = *it;
125 ind++;
126 }
127
128 gslpp::vector<double> val = (*v) * pars_in;
129
130 std::vector<double> res;
131
132 for (unsigned int i = 0; i < DiagPars_i.size(); i++) {
133 res.push_back(val(i));
134 }
135 return (res);
136}
137
138int CorrelatedGaussianParameters::ParseCGP(std::vector<ModelParameter>& ModPars,
139 std::string& filename,
140 std::ifstream& ifile,
141 boost::tokenizer<boost::char_separator<char> >::iterator & beg,
142 int lineNo,
143 int rank) {
144 name = *beg;
145 ++beg;
146 int size = atoi((*beg).c_str());
147 int nlines = 0;
148 std::vector<bool> lines;
149 std::string line;
150 boost::char_separator<char>sep(" \t");
151 for (int i = 0; i < size; i++) {
152 IsEOF = getline(ifile, line).eof();
153 if (line.empty() || line.at(0) == '#') {
154 if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianParameters please! In line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + filename);
155 else sleep(2);
156 }
157 lineNo++;
158 boost::tokenizer<boost::char_separator<char> > tok(line, sep);
159 beg = tok.begin();
160 std::string type = *beg;
161 ++beg;
162 if (type.compare("ModelParameter") != 0) {
163 if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + filename + ", expecting a ModelParameter type here...\n");
164 else sleep(2);
165 }
166 ModelParameter tmpMP;
167 beg = tmpMP.ParseModelParameter(beg);
168 lines.push_back(!tmpMP.IsFixed());
169 if (beg != tok.end())
170 if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
171 if (!tmpMP.IsFixed()) {
172 tmpMP.setCgp_name(name);
173 AddPar(tmpMP);
174 nlines++;
175 } else {
176 ModPars.push_back(tmpMP);
177 }
178 }
179 if (nlines > 1) {
180 TMatrixD myCorr(nlines, nlines);
181 int ni = 0;
182 for (int i = 0; i < size; i++) {
183 IsEOF = getline(ifile, line).eof();
184 if (line.empty() || line.at(0) == '#') {
185 if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianParameters please! In line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + filename);
186 else sleep(2);
187 }
188 lineNo++;
189 if (lines.at(i)) {
190 boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
191 beg = mytok.begin();
192 int nj = 0;
193 for (int j = 0; j < size; j++) {
194 if ((*beg).compare(0, 1, "0") == 0
195 || (*beg).compare(0, 1, "1") == 0
196 || (*beg).compare(0, 1, "-") == 0) {
197 if (std::distance(mytok.begin(), mytok.end()) < size) {
198 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 " + filename + ".\n");
199 else sleep(2);
200 }
201 if (lines.at(j)) {
202 myCorr(ni, nj) = atof((*beg).c_str());
203 nj++;
204 }
205 beg++;
206 } else {
207 if (rank == 0) std::cout << "ERROR: invalid correlation matrix for "
208 << name << ". Check element (" << ni + 1 << "," << nj + 1 << ") in line number " + boost::lexical_cast<std::string>(lineNo) << std::endl;
209 exit(EXIT_FAILURE);
210 }
211 }
212 ni++;
213 }
214 }
215 if (!myCorr.IsSymmetric()) {
216 if (rank == 0) std::cout << "ERROR: invalid correlation matrix for "
217 << name << ". Correlation matrix is not symmetric." << std::endl;
218 exit(EXIT_FAILURE);
219 } else {
220 TMatrixDSym mySCorr(nlines);
221 for (int i = 0; i < nlines; i++) {
222 for (int j = 0; j <= i; j++) {
223 mySCorr(i, j) = myCorr(i, j);
224 mySCorr(j, i) = mySCorr(i, j); // Make sure TMatrixDsym element (j,i) is stored symmetrically
225 }
226 }
227 DiagonalizePars(mySCorr);
228 ModPars.insert(ModPars.end(), getDiagPars().begin(), getDiagPars().end());
229 }
230 } else {
231 if (rank == 0) std::cout << "\nWARNING: Correlated Gaussian Parameters " << name.c_str() << " defined with less than two correlated parameters. The set is being marked as normal Parameters." << std::endl;
232 if (getPars().size() == 1) ModPars.push_back(ModelParameter(getPar(0)));
233 for (int i = 0; i < size; i++) {
234 IsEOF = getline(ifile, line).eof();
235 lineNo++;
236 }
237 }
238 return lineNo;
239}
A class for correlated Gaussian parameters.
std::vector< ModelParameter > DiagPars
The eigenvector of the covariance matrix.
TMatrixDSym Cov
The covariance matrix.
const std::vector< ModelParameter > & getPars() const
A get method to access the vector of parameters that are defined in one correlated Gaussian parameter...
std::string name
The name of the correlated Gaussian Parameters set.
void AddPar(ModelParameter &Par_i)
A method to add parameters to the list of correlated Gaussian parameters.
int ParseCGP(std::vector< ModelParameter > &ModPars, std::string &filename, std::ifstream &ifile, boost::tokenizer< boost::char_separator< char > >::iterator &beg, int lineNo, int rank)
The parser for CorrelatedGaussianParameters.
gslpp::matrix< double > * v
The rotation matrix form the diagonalized parameters to the original parameters.
virtual ~CorrelatedGaussianParameters()
The default destructor.
const std::vector< ModelParameter > & getDiagPars() const
A get method to access the diagonalized parameters.
CorrelatedGaussianParameters()
The default Constructor.
std::vector< double > getOrigParsValue(const std::vector< double > &DiagPars_i) const
void DiagonalizePars(TMatrixDSym Corr)
Diagonalizes the correlated Gaussian parameters set.
std::vector< ModelParameter > Pars
A vector of parameters whose correlation will be calculated.
ModelParameter getPar(int i) const
A get method to access an element of the vector of parameters that are defined in one correlated Gaus...
gslpp::vector< double > * e
The diagonalized parameters.
A class for model parameters.
boost::tokenizer< boost::char_separator< char > >::iterator & ParseModelParameter(boost::tokenizer< boost::char_separator< char > >::iterator &beg)
Parser for model parameters.
void setCgp_name(std::string cgp_name)
A set method to set the name of the set of correlated parameter.
std::string getname() const
A get method to get the name of each parameter.
bool IsFixed() const
A method to check if the parameter is fixed.