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 {
18  name = name_i;
19  Cov = NULL;
20  v = NULL;
21  e = NULL;
22  IsEOF = false;
23 }
24 
26 {
27  Cov = NULL;
28  v = NULL;
29  e = NULL;
30  IsEOF = false;
31 }
32 
34 {
35  Pars = orig.Pars;
36  name = orig.name;
37  Cov = new gslpp::matrix<double>(*orig.Cov);
38  v = new gslpp::matrix<double>(*orig.v);
39  e = new gslpp::vector<double>(*orig.e);
40  DiagPars = orig.DiagPars;
41  IsEOF = orig.IsEOF;
42 }
43 
45 {
46  //Cov = new gslpp::matrix<double>(10, 10, 0.); /** Put in to prevent seg fault during error handling in InputParser **/
47  if(Cov != NULL)
48  delete(Cov);
49  if(v != NULL)
50  delete(v);
51  if(e != NULL)
52  delete(e);
53 }
54 
56 {
57  Pars.push_back(Par_i);
58 }
59 
61 {
62  unsigned int size = Pars.size();
63  if (Corr.size_i() != size || Corr.size_j() != size)
64  throw std::runtime_error("The size of the correlated parameters in " + name + " does not match the size of the correlation matrix!");
65  Cov = new gslpp::matrix<double>(size, size, 0.);
66  for (unsigned int i = 0; i < size; i++)
67  for (unsigned int j = 0; j < size; j++)
68  (*Cov)(i, j) = Pars.at(i).geterrg() * Corr(i, j) * Pars.at(j).geterrg();
69 
70  *Cov = Cov->inverse();
71 
72  gslpp::matrix<gslpp::complex>vv(size, size, 0.);
74 
75  Cov->eigensystem(vv, ee);
76 
77  v = new gslpp::matrix<double>(size, size, 0.);
78  e = new gslpp::vector<double>(size, 0.);
79  *v = vv.real();
80  *e = ee.real();
81 
82  gslpp::vector<double> ave_in(size, 0.);
83 
84  int ind = 0;
85  for(std::vector<ModelParameter>::iterator it = Pars.begin(); it != Pars.end(); it++)
86  {
87  ave_in(ind) = it->getave();
88  ind++;
89  }
90 
91  gslpp::vector<double> ave = v->transpose() * ave_in;
92 
93  for (unsigned int i = 0; i < size; i++)
94  {
95  std::stringstream ss;
96  ss << (i+1);
97  std::string namei = name + ss.str();
98  ModelParameter current(namei,ave(i),1./sqrt((*e)(i)),0.);
99  current.setCgp_name(name);
100  DiagPars.push_back(current);
101  }
102 }
103 
104 std::vector<double> CorrelatedGaussianParameters::getOrigParsValue(const std::vector<double>& DiagPars_i) const
105 {
106  if (DiagPars_i.size() != DiagPars.size()) {
107  std::stringstream out;
108  out << DiagPars_i.size();
109  throw std::runtime_error("CorrelatedGaussianParameters::getOrigParsValue(DiagPars_i): DiagPars_i.size() = " + out.str() + " does not match the size of DiagPars");
110  }
111  gslpp::vector<double> pars_in(DiagPars_i.size(), 0.);
112 
113  int ind = 0;
114  for (std::vector<double>::const_iterator it = DiagPars_i.begin(); it != DiagPars_i.end(); it++) {
115  pars_in(ind) = *it;
116  ind++;
117  }
118 
119  gslpp::vector<double> val = (*v) * pars_in;
120 
121  std::vector<double> res;
122 
123  for (unsigned int i = 0; i < DiagPars_i.size(); i++) {
124  res.push_back(val(i));
125  }
126  return (res);
127 }
128 
129 int CorrelatedGaussianParameters::ParseCGP(std::vector<ModelParameter>& ModPars,
130  std::string& filename,
131  std::ifstream& ifile,
132  boost::tokenizer<boost::char_separator<char> >::iterator & beg,
133  int lineNo,
134  int rank)
135 {
136  name = *beg;
137  ++beg;
138  int size = atoi((*beg).c_str());
139  int nlines = 0;
140  std::vector<bool> lines;
141  std::string line;
142  boost::char_separator<char>sep(" \t");
143  for (int i = 0; i < size; i++) {
144  IsEOF = getline(ifile, line).eof();
145  if (line.empty() || line.at(0) == '#') {
146  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);
147  else sleep(2);
148  }
149  lineNo++;
150  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
151  beg = tok.begin();
152  std::string type = *beg;
153  ++beg;
154  if (type.compare("ModelParameter") != 0){
155  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");
156  else sleep(2);
157  }
158  ModelParameter tmpMP;
159  beg = tmpMP.ParseModelParameter(beg);
160  lines.push_back(!tmpMP.IsFixed());
161  if (beg != tok.end())
162  if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
163  if (!tmpMP.IsFixed()) {
164  tmpMP.setCgp_name(name);
165  AddPar(tmpMP);
166  nlines++;
167  } else {
168  ModPars.push_back(tmpMP);
169  }
170  }
171  if (nlines > 1) {
173  int ni = 0;
174  for (int i = 0; i < size; i++) {
175  IsEOF = getline(ifile, line).eof();
176  if (line.empty() || line.at(0) == '#') {
177  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);
178  else sleep(2);
179  }
180  lineNo++;
181  if (lines.at(i)) {
182  boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
183  beg = mytok.begin();
184  int nj = 0;
185  for (int j = 0; j < size; j++) {
186  if ((*beg).compare(0, 1, "0") == 0
187  || (*beg).compare(0, 1, "1") == 0
188  || (*beg).compare(0, 1, "-") == 0) {
189  if (std::distance(mytok.begin(), mytok.end()) < size) {
190  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");
191  else sleep(2);
192  }
193  if (lines.at(j)) {
194  myCorr(ni, nj) = atof((*beg).c_str());
195  nj++;
196  }
197  beg++;
198  } else {
199  if (rank == 0) std::cout << "ERROR: invalid correlation matrix for "
200  << name << ". Check element (" << ni + 1 << "," << nj + 1 << ") in line number " + boost::lexical_cast<std::string>(lineNo) << std::endl;
201  exit(EXIT_FAILURE);
202  }
203  }
204  ni++;
205  }
206  }
207  DiagonalizePars(myCorr);
208  ModPars.insert(ModPars.end(), getDiagPars().begin(), getDiagPars().end());
209 
210  } else {
211  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;
212  if (getPars().size() == 1) ModPars.push_back(ModelParameter(getPar(0)));
213  for (int i = 0; i < size; i++) {
214  IsEOF = getline(ifile, line).eof();
215  lineNo++;
216  }
217  }
218  return lineNo;
219 }
gslpp::matrix< double >
A class for constructing and defining operations on real matrices.
Definition: gslpp_matrix_double.h:48
CorrelatedGaussianParameters
A class for correlated Gaussian parameters.
Definition: CorrelatedGaussianParameters.h:23
CorrelatedGaussianParameters::getPars
const std::vector< ModelParameter > & getPars() const
A get method to access the vector of parameters that are defined in one correlated Gaussian parameter...
Definition: CorrelatedGaussianParameters.h:64
gslpp::matrix< double >::size_j
size_t size_j() const
Definition: gslpp_matrix_double.cpp:137
gslpp::matrix< double >::transpose
matrix< double > transpose() const
Definition: gslpp_matrix_double.cpp:166
gslpp::matrix< gslpp::complex >
CorrelatedGaussianParameters::ParseCGP
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.
Definition: CorrelatedGaussianParameters.cpp:129
ModelParameter::ParseModelParameter
boost::tokenizer< boost::char_separator< char > >::iterator & ParseModelParameter(boost::tokenizer< boost::char_separator< char > >::iterator &beg)
Parser for model parameters.
Definition: ModelParameter.cpp:39
CorrelatedGaussianParameters::e
gslpp::vector< double > * e
The diagonalized parameters.
Definition: CorrelatedGaussianParameters.h:139
CorrelatedGaussianParameters::v
gslpp::matrix< double > * v
The rotation matrix form the diagonalized parameters to the original parameters.
Definition: CorrelatedGaussianParameters.h:138
ModelParameter::IsFixed
bool IsFixed() const
A method to check if the parameter is fixed.
Definition: ModelParameter.h:123
CorrelatedGaussianParameters::DiagonalizePars
void DiagonalizePars(gslpp::matrix< double > Corr)
Diagonalizes the correlated Gaussian parameters set.
Definition: CorrelatedGaussianParameters.cpp:60
CorrelatedGaussianParameters::DiagPars
std::vector< ModelParameter > DiagPars
The eigenvector of the covariance matrix.
Definition: CorrelatedGaussianParameters.h:140
ModelParameter::getname
std::string getname() const
A get method to get the name of each parameter.
Definition: ModelParameter.h:69
gslpp::matrix< double >::inverse
matrix< double > inverse()
Definition: gslpp_matrix_double.cpp:178
CorrelatedGaussianParameters::AddPar
void AddPar(ModelParameter &Par_i)
A method to add parameters to the list of correlated Gaussian parameters.
Definition: CorrelatedGaussianParameters.cpp:55
gslpp::sqrt
complex sqrt(const complex &z)
Definition: gslpp_complex.cpp:385
ModelParameter
A class for model parameters.
Definition: ModelParameter.h:28
gslpp::matrix< double >::size_i
size_t size_i() const
Definition: gslpp_matrix_double.cpp:132
CorrelatedGaussianParameters::Pars
std::vector< ModelParameter > Pars
A vector of parameters whose correlation will be calculated.
Definition: CorrelatedGaussianParameters.h:135
gslpp::vector< double >
A class for constructing and defining operations on real vectors.
Definition: gslpp_vector_double.h:33
ModelParameter::setCgp_name
void setCgp_name(std::string cgp_name)
A set method to set the name of the set of correlated parameter.
Definition: ModelParameter.h:140
CorrelatedGaussianParameters::CorrelatedGaussianParameters
CorrelatedGaussianParameters()
The default Constructor.
Definition: CorrelatedGaussianParameters.cpp:25
CorrelatedGaussianParameters::name
std::string name
The name of the correlated Gaussian Parameters set.
Definition: CorrelatedGaussianParameters.h:137
CorrelatedGaussianParameters::getOrigParsValue
std::vector< double > getOrigParsValue(const std::vector< double > &DiagPars_i) const
Definition: CorrelatedGaussianParameters.cpp:104
CorrelatedGaussianParameters::Cov
gslpp::matrix< double > * Cov
The covariance matrix.
Definition: CorrelatedGaussianParameters.h:136
CorrelatedGaussianParameters::~CorrelatedGaussianParameters
virtual ~CorrelatedGaussianParameters()
The default destructor.
Definition: CorrelatedGaussianParameters.cpp:44
gslpp::matrix< double >::eigensystem
void eigensystem(matrix< complex > &U, vector< complex > &S)
Definition: gslpp_matrix_double.cpp:280
gslpp::vector< gslpp::complex >
CorrelatedGaussianParameters::getDiagPars
const std::vector< ModelParameter > & getDiagPars() const
A get method to access the diagonalized parameters.
Definition: CorrelatedGaussianParameters.h:100
CorrelatedGaussianParameters::getPar
ModelParameter getPar(int i) const
A get method to access an element of the vector of parameters that are defined in one correlated Gaus...
Definition: CorrelatedGaussianParameters.h:74
CorrelatedGaussianParameters.h
CorrelatedGaussianParameters::IsEOF
bool IsEOF
Definition: CorrelatedGaussianParameters.h:141