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