BCConstantPrior.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 #include "BCConstantPrior.h"
10 #include "BCAux.h"
11 
12 #include <TRandom.h>
13 
14 // ---------------------------------------------------------
16  : BCPrior()
17  , fLogRangeWidth(0)
18 {}
19 
20 // ---------------------------------------------------------
22  : BCPrior()
23  , fLogRangeWidth(0)
24 {
25  if (range_width > 0)
26  fLogRangeWidth = log(range_width);
27 }
28 
29 // ---------------------------------------------------------
30 BCConstantPrior::BCConstantPrior(double xmin, double xmax)
31  : BCPrior()
32  , fLogRangeWidth(0)
33 {
34  if ((xmax - xmin) > 0)
35  fLogRangeWidth = log(xmax - xmin);
36 }
37 
38 // ---------------------------------------------------------
39 double BCConstantPrior::GetMode(double xmin, double xmax)
40 {
41  BCAux::BCRange r = BCAux::RangeType(xmin, xmax);
42 
43  if (r == BCAux::kReverseRange)
44  return GetMode(xmax, xmin);
45 
46  if (r == BCAux::kFiniteRange || r == BCAux::kEmptyRange)
47  return 0.5 * (xmin + xmax);
48 
49  if (r == BCAux::kInfiniteRange)
50  return 0;
51 
53  return std::numeric_limits<double>::infinity();
54 
56  return -std::numeric_limits<double>::infinity();
57 
58  return std::numeric_limits<double>::quiet_NaN();
59 }
60 
61 // ---------------------------------------------------------
62 double BCConstantPrior::GetRawMoment(unsigned n, double xmin, double xmax)
63 {
64  if (n == 0)
65  return 1;
66 
67  BCAux::BCRange r = BCAux::RangeType(xmin, xmax);
68 
69  if (r == BCAux::kReverseRange)
70  return GetRawMoment(n, xmax, xmin);
71 
72  if (r == BCAux::kEmptyRange)
73  return (n == 1) ? xmin : 0;
74 
75  if (r == BCAux::kInfiniteRange)
76  return (n == 1) ? 0 : std::numeric_limits<double>::infinity();
77 
79  return ((n == 1) ? -1 : 1) * std::numeric_limits<double>::infinity();
80 
82  return std::numeric_limits<double>::infinity();
83 
84  double rm = 0;
85  for (unsigned i = 0; i <= n; ++i)
86  rm += pow(xmin, i) * pow(xmax, n - i);
87  return rm / (n + 1);
88 }
89 
90 // ---------------------------------------------------------
91 double BCConstantPrior::GetRandomValue(double xmin, double xmax, TRandom* const R)
92 {
93  if (!R)
94  return std::numeric_limits<double>::quiet_NaN();
95  return xmin + R->Rndm() * (xmax - xmin);
96 }
A class to represent the prior of a parameter.
Definition: BCPrior.h:49
BCConstantPrior()
Constructor for constant unit prior.
virtual double GetRandomValue(double xmin, double xmax, TRandom *const R=NULL)
BCRange
Range types.
Definition: BCAux.h:88
virtual double GetRawMoment(unsigned n, double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
virtual double GetMode(double xmin=-std::numeric_limits< double >::infinity(), double xmax=std::numeric_limits< double >::infinity())
Return mode of prior (in range) — center of interval for constant prior.
BCAux::BCRange RangeType(double xmin, double xmax)
Return type of range as a BCAux::BCRange enum.
Definition: BCAux.cxx:85
lower < upper, lower limit finite, upper limit infinite
Definition: BCAux.h:91
lower > upper
Definition: BCAux.h:94
lower limit == upper limit
Definition: BCAux.h:93
lower < upper, lower and upper limits finite
Definition: BCAux.h:89
lower < upper, lower limit infinite, upper limit finite
Definition: BCAux.h:90
lower < upper, lower and upper limits infinite
Definition: BCAux.h:92
double fLogRangeWidth
log of the width of the parameter range