BCParameter.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 #include "BCParameter.h"
11 #include "BCPrior.h"
12 #include "BCConstantPrior.h"
13 #include "BCLog.h"
14 #include "BCAux.h"
15 
16 #include <string>
17 #include <limits>
18 
19 #include <TString.h>
20 #include <TRandom.h>
21 #include <TMath.h>
22 #include <TF1.h>
23 
24 
25 // ---------------------------------------------------------
27  BCVariable(),
28  fFixed(false),
29  fFixedValue(std::numeric_limits<double>::infinity()),
30  fPrior(NULL)
31 {
32  fPrefix = "Parameter";
33 }
34 
35 // ---------------------------------------------------------
37  BCVariable(other),
38  fFixed(other.fFixed),
39  fFixedValue(other.fFixedValue),
40  fPrior(NULL)
41 {
42  if (other.fPrior)
43  SetPrior(other.fPrior->Clone());
44 }
45 
46 // ---------------------------------------------------------
47 BCParameter::BCParameter(const std::string& name, double lowerlimit, double upperlimit, const std::string& latexname, const std::string& unitstring) :
48  BCVariable(name, lowerlimit, upperlimit, latexname, unitstring),
49  fFixed(false),
50  fFixedValue(std::numeric_limits<double>::infinity()),
51  fPrior(NULL)
52 {
53  fPrefix = "Parameter";
54  if (lowerlimit == upperlimit)
55  Fix(lowerlimit);
56 }
57 
58 // ---------------------------------------------------------
60 {
61  delete fPrior;
62 }
63 
64 // ---------------------------------------------------------
66 {
67  swap(*this, other);
68  return *this;
69 }
70 
71 // ---------------------------------------------------------
73 {
74  std::swap(static_cast<BCVariable&>(A), static_cast<BCVariable&>(B));
75  std::swap(A.fFixed, B.fFixed);
76  std::swap(A.fFixedValue, B.fFixedValue);
77  std::swap(A.fPrior, B.fPrior);
78 }
79 
80 // ---------------------------------------------------------
81 void BCParameter::SetLimits(double lowerlimit, double upperlimit)
82 {
83  BCVariable::SetLimits(lowerlimit, upperlimit);
85  fPrior->GetFunction().SetRange(fLowerLimit, fUpperLimit);
86  if (lowerlimit == upperlimit)
87  fFixedValue = lowerlimit;
88 }
89 
90 // ---------------------------------------------------------
91 double BCParameter::GetLogPrior(double x) const
92 {
93  if ( fFixed )
94  return 0;
95  if (fPrior)
96  return fPrior->GetLogPrior(x);
97  return -std::numeric_limits<double>::infinity();
98 }
99 
100 // ---------------------------------------------------------
102 {
103  if (!fPrior)
104  return std::numeric_limits<double>::quiet_NaN();
105  return fPrior->GetMode(fLowerLimit, fUpperLimit);
106 }
107 
108 // ---------------------------------------------------------
110 {
111  if (!fPrior)
112  return std::numeric_limits<double>::quiet_NaN();
113  return fPrior->GetMean(fLowerLimit, fUpperLimit);
114 }
115 
116 // ---------------------------------------------------------
118 {
119  if (!fPrior)
120  return std::numeric_limits<double>::quiet_NaN();
121  return fPrior->GetVariance(fLowerLimit, fUpperLimit);
122 }
123 
124 // ---------------------------------------------------------
125 double BCParameter::GetRandomValueAccordingToPrior(TRandom* const R) const
126 {
127  if (!fPrior) {
128  BCLog::OutError("BCParameter::GetRandomValueAccordingToPrior : no prior specified.");
129  return std::numeric_limits<double>::quiet_NaN();
130  }
131 
132  return fPrior->GetRandomValue(fLowerLimit, fUpperLimit, R);
133 }
134 
135 // ---------------------------------------------------------
136 void BCParameter::SetPrior(BCPrior* const prior)
137 {
138  delete fPrior;
139  fPrior = prior;
140  if (fPrior)
142 }
143 
144 // ---------------------------------------------------------
146 {
148 }
149 
150 // ---------------------------------------------------------
151 std::string BCParameter::OneLineSummary(bool print_prefix, int name_length) const
152 {
153  if (!Fixed())
154  return BCVariable::OneLineSummary(print_prefix, name_length);
155  return BCVariable::OneLineSummary(print_prefix, name_length) + Form(" (fixed at %.*f)", GetPrecision(), GetFixedValue());
156 }
virtual void SetPrior(BCPrior *const prior)
Set prior.
A class to represent the prior of a parameter.
Definition: BCPrior.h:49
virtual double GetRandomValueAccordingToPrior(TRandom *const rng) const
virtual double GetPriorMean() const
virtual std::string OneLineSummary(bool print_prefix=true, int name_length=-1) const
Definition: BCVariable.cxx:98
virtual BCPrior * Clone() const =0
Clone function.
virtual bool Fix(double value)
Fix parameter to value (set prior to delta).
Definition: BCParameter.h:141
virtual void SetFunctionRange(double xmin, double xmax)
Set range of ROOT TF1 function.
Definition: BCPrior.cxx:133
friend void swap(BCParameter &A, BCParameter &B)
swap
Definition: BCParameter.cxx:72
double fUpperLimit
The upper limit of the variable value.
Definition: BCVariable.h:329
virtual TF1 & GetFunction()
Return back ROOT TF1 evaluating BCPrior::GetPrior.
Definition: BCPrior.h:118
virtual double GetPriorMode() const
A class to represent a constant prior of a parameter.
virtual ~BCParameter()
Destructor.
Definition: BCParameter.cxx:59
A class representing a variable of a model.
Definition: BCVariable.h:35
BCAux::BCRange RangeType(double xmin, double xmax)
Return type of range as a BCAux::BCRange enum.
Definition: BCAux.cxx:85
virtual double GetPriorVariance() const
virtual double GetMode(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Return mode of prior (in range).
Definition: BCPrior.cxx:65
double fLowerLimit
The lower limit of the variable value.
Definition: BCVariable.h:326
virtual double GetLogPrior(double x) const
Get log of value of prior at parameter value.
Definition: BCParameter.cxx:91
A class representing a parameter of a model.
Definition: BCParameter.h:34
std::string OneLineSummary(bool print_prefix=true, int name_length=-1) const
virtual double GetFixedValue() const
Definition: BCParameter.h:86
virtual void SetLimits(double lowerlimit=0, double upperlimit=1)
Set the limits of the variable values.
Definition: BCVariable.cxx:59
virtual void SetLimits(double lowerlimit=0, double upperlimit=1)
Set the limits of the parameter values.
Definition: BCParameter.cxx:81
BCParameter & operator=(BCParameter other)
Copy operator.
Definition: BCParameter.cxx:65
virtual bool Fixed() const
Definition: BCParameter.h:81
BCParameter()
The default constructor.
Definition: BCParameter.cxx:26
std::string fPrefix
prefix for output
Definition: BCVariable.h:317
lower < upper, lower and upper limits finite
Definition: BCAux.h:89
virtual double GetLogPrior(double x)=0
Get log of prior.
virtual double GetRandomValue(double xmin, double xmax, TRandom *const R=NULL)
Definition: BCPrior.cxx:126
virtual double GetRangeWidth() const
Returns the range width of the variable values.
Definition: BCVariable.h:109
virtual double GetMean(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get mean of prior.
Definition: BCPrior.h:173
virtual void SetPriorConstant()
Set constant prior.
virtual unsigned GetPrecision() const
Definition: BCVariable.h:119
virtual double GetVariance(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Get variance of prior.
Definition: BCPrior.h:181