BCParameterSet.cxx
1 /*
2  * Copyright (C) 2007-2018, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include "BCParameterSet.h"
12 #include "BCLog.h"
13 
14 #include <Math/Util.h>
15 
16 // ---------------------------------------------------------
19 {
20 }
21 
22 // ---------------------------------------------------------
24 {
25  unsigned int n = 0;
26  for (unsigned int i = 0; i < Size(); ++i)
27  if (fVars[i].Fixed())
28  ++n;
29  return n;
30 }
31 
32 // ---------------------------------------------------------
33 double BCParameterSet::Volume() const
34 {
35  double volume = -1;
36  for (unsigned i = 0; i < fVars.size(); ++i)
37  if (!fVars[i].Fixed()) {
38  if ( volume < 0 )
39  volume = 1;
40  volume *= fVars[i].GetRangeWidth();
41  }
42  if (volume < 0)
43  return 0;
44  return volume;
45 }
46 
47 // ---------------------------------------------------------
49 {
50  for (unsigned i = 0; i < fVars.size(); ++i)
51  fVars[i].SetPriorConstant();
52 }
53 
54 // ---------------------------------------------------------
55 double BCParameterSet::GetLogPrior(const std::vector<double>& parameters) const
56 {
57  if (parameters.size() < fVars.size()) {
58  std::string msg("incorrect size of parameter set provided. Expected ");
59  msg += ROOT::Math::Util::ToString(fVars.size()) + ", got ";
60  msg += ROOT::Math::Util::ToString(parameters.size());
61  BCLOG_ERROR(msg);
62  return -std::numeric_limits<double>::infinity();
63  }
64  double logprob = 0;
65  for (unsigned i = 0; i < fVars.size(); ++i)
66  logprob += fVars[i].GetLogPrior(parameters[i]);
67  return logprob;
68 }
69 
70 // ---------------------------------------------------------
71 std::vector<double> BCParameterSet::GetFixedValues(bool include_unfixed) const
72 {
73  std::vector<double> v (fVars.size(), std::numeric_limits<double>::infinity());
74  for (unsigned i = 0; i < fVars.size(); ++i)
75  if (include_unfixed || fVars[i].Fixed())
76  v[i] = fVars[i].GetFixedValue();
77  return v;
78 }
79 
80 // ---------------------------------------------------------
81 bool BCParameterSet::ArePriorsSet(bool ignore_fixed) const
82 {
83  for (unsigned i = 0; i < fVars.size(); ++i)
84  if (ignore_fixed && fVars[i].Fixed())
85  continue;
86  else if (fVars[i].GetPrior() == NULL)
87  return false;
88  return true;
89 }
90 
91 // ---------------------------------------------------------
92 bool BCParameterSet::IsWithinLimits(const std::vector<double>& x) const
93 {
94  if (x.size() != fVars.size())
95  return false;
96  for (unsigned i = 0; i < fVars.size(); ++i)
97  if (fVars[i].Fixed()) {
98  if (fabs(x[i] - fVars[i].GetFixedValue()) > std::numeric_limits<double>::epsilon())
99  return false;
100  } else if (!fVars[i].IsWithinLimits(x[i]))
101  return false;
102  return true;
103 }
104 
105 // ---------------------------------------------------------
106 bool BCParameterSet::IsAtFixedValues(const std::vector<double>& x) const
107 {
108  if (x.size() < fVars.size())
109  return false;
110  for (unsigned i = 0; i < fVars.size(); ++i)
111  if (fVars[i].Fixed() && (fVars[i].GetFixedValue() - x[i]) > std::numeric_limits<double>::epsilon())
112  return false;
113  return true;
114 }
115 
116 // ---------------------------------------------------------
117 void BCParameterSet::ValueFromPositionInRange(std::vector<double>& p) const
118 {
119  if ( p.size() != fVars.size() )
120  return;
121  for (unsigned i = 0; i < fVars.size(); ++i)
122  p[i] = (fVars[i].Fixed()) ? fVars[i].GetFixedValue() : fVars[i].ValueFromPositionInRange(p[i]);
123 }
124 
125 // ---------------------------------------------------------
126 std::vector<double> BCParameterSet::GetRangeCenters() const
127 {
128  std::vector<double> p;
129  for (unsigned i = 0; i < fVars.size(); ++i)
130  p.push_back((fVars[i].Fixed()) ? fVars[i].GetFixedValue() : fVars[i].GetRangeCenter());
131  return p;
132 }
133 
134 // ---------------------------------------------------------
135 std::vector<double> BCParameterSet::GetUniformRandomValues(TRandom* const R) const
136 {
137  std::vector<double> p;
138  for (unsigned i = 0; i < fVars.size(); ++i)
139  p.push_back((fVars[i].Fixed()) ? fVars[i].GetFixedValue() : fVars[i].GetUniformRandomValue(R));
140  return p;
141 }
142 
143 // ---------------------------------------------------------
144 std::vector<double> BCParameterSet::GetRandomValuesAccordingToPriors(TRandom* const R) const
145 {
146  std::vector<double> p;
147  for (unsigned i = 0; i < fVars.size(); ++i)
148  p.push_back((fVars[i].Fixed()) ? fVars[i].GetFixedValue() : fVars[i].GetRandomValueAccordingToPrior(R));
149  return p;
150 }
151 
152 // ---------------------------------------------------------
153 bool BCParameterSet::ApplyFixedValues(std::vector<double>& x) const
154 {
155  if (x.size() != fVars.size())
156  return false;
157  for (unsigned i = 0; i < fVars.size(); ++i)
158  if (fVars[i].Fixed())
159  x[i] = fVars[i].GetFixedValue();
160  return true;
161 }
virtual std::vector< double > GetUniformRandomValues(TRandom *const R) const
Get vector of values uniformly distributed in parameter ranges (or at fixed values, if fixed)
virtual unsigned Size() const
Number of variables contained.
virtual unsigned int GetNFixedParameters() const
virtual void ValueFromPositionInRange(std::vector< double > &p) const
Translate from unit interval to values in variable ranges, fixing fixed parameters along the way...
virtual double Volume() const
virtual std::vector< double > GetFixedValues(bool include_unfixed=true) const
Get vector of fixed values.
virtual bool IsWithinLimits(const std::vector< double > &x) const
Check if vector of values is within limits.
virtual bool IsAtFixedValues(const std::vector< double > &x) const
Check if fixed parameters in vector of values are at fixed values.
BCParameterSet()
Constructor.
A class representing a parameter of a model.
Definition: BCParameter.h:34
virtual double GetLogPrior(const std::vector< double > &parameters) const
Get log of prior; assumes independent priors given for all parameters in set.
virtual void SetPriorConstantAll()
Set all priors to constant.
std::vector< BCParameter > fVars
Vector of BCVariables that forms the set.
virtual std::vector< double > GetRangeCenters() const
Get range centers, leaving fixed parameters at fixed values.
virtual bool ArePriorsSet(bool ignore_fixed=true) const
Check whether all parameters have factorized priors set.
virtual bool ApplyFixedValues(std::vector< double > &x) const
Change values to fixed values for fixed parameters.
virtual std::vector< double > GetRandomValuesAccordingToPriors(TRandom *const R) const
Get vector values distributed randomly by the parameter priors.
Wrapper to allow access by name into list of BCVariable.
Definition: BCVariableSet.h:35