queso-0.56.0
BetaJointPdf.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/BetaJointPdf.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Constructor -------------------------------------
32 template<class V,class M>
34  const char* prefix,
35  const VectorSet<V,M>& domainSet,
36  const V& alpha,
37  const V& beta)
38  :
39  BaseJointPdf<V,M>(((std::string)(prefix)+"uni").c_str(),domainSet),
40  m_alpha(alpha),
41  m_beta (beta)
42 {
43  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
44  *m_env.subDisplayFile() << "Entering BetaJointPdf<V,M>::constructor()"
45  << ": prefix = " << m_prefix
46  << std::endl;
47  }
48 
49  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
50  *m_env.subDisplayFile() << "Leaving BetaJointPdf<V,M>::constructor()"
51  << ": prefix = " << m_prefix
52  << std::endl;
53  }
54 }
55 // Destructor --------------------------------------
56 template<class V,class M>
58 {
59 }
60 // Math methods-------------------------------------
61 template<class V, class M>
62 double
64  const V& domainVector,
65  const V* domainDirection,
66  V* gradVector,
67  M* hessianMatrix,
68  V* hessianEffect) const
69 {
70  queso_require_equal_to_msg(domainVector.sizeLocal(), this->m_domainSet.vectorSpace().dimLocal(), "invalid input");
71  queso_require_msg(!(domainDirection || hessianMatrix || hessianEffect), "incomplete code for hessianMatrix and hessianEffect calculations");
72 
73  // No need to multiply by exp(m_logOfNormalizationFactor) because 'lnValue()' is called
74  double value = std::exp(this->lnValue(domainVector,domainDirection,gradVector,hessianMatrix,hessianEffect));
75 
76  if (gradVector) {
77  // DM: This transformation may have issues near the boundary. I haven't
78  // fully explored this yet.
79  //
80  // This evaluation is also normalised (if m_normalizationStyle is zero) and
81  // so the gradient in physical space contains the normalisation constant.
82  (*gradVector) *= value;
83  }
84 
85  return value;
86 }
87 //--------------------------------------------------
88 template<class V, class M>
89 double
91  const V& domainVector,
92  const V* domainDirection,
93  V* gradVector,
94  M* hessianMatrix,
95  V* hessianEffect) const
96 {
97  queso_require_msg(!(domainDirection || hessianMatrix || hessianEffect), "incomplete code for gradVector, hessianMatrix and hessianEffect calculations");
98 
99  double aux = 0.;
100  double result = 0.;
101  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
102  if (m_normalizationStyle == 0) {
103  aux = log(m_env.basicPdfs()->betaPdfActualValue(domainVector[i],m_alpha[i],m_beta[i]));
104  }
105  else {
106  aux = (m_alpha[i]-1.)*log(domainVector[i]) + (m_beta[i]-1.)*log(1.-domainVector[i]);
107  }
108 
109  // The log of the beta pdf is
110  // f(x) = (alpha - 1) * log(x) + (beta - 1) * log(1 - x)
111  // Therefore
112  // df/dx (x) = ((alpha - 1) / x) + ((1 - beta) / (1 - x))
113  if (gradVector) {
114  // We're computing grad of log of p which is p' / p
115  (*gradVector)[i] = (m_alpha[i] - 1.0) / domainVector[i] +
116  (1.0 - m_beta[i]) / (1.0 - domainVector[i]);
117  }
118 
119  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
120  *m_env.subDisplayFile() << "In BetaJointPdf<V,M>::lnValue()"
121  << ", m_normalizationStyle = " << m_normalizationStyle
122  << ": domainVector[" << i << "] = " << domainVector[i]
123  << ", m_alpha[" << i << "] = " << m_alpha[i]
124  << ", m_beta[" << i << "] = " << m_beta[i]
125  << ", log(pdf)= " << aux
126  << std::endl;
127  }
128  result += aux;
129  }
130  result += m_logOfNormalizationFactor;
131 
132  return result;
133 }
134 //--------------------------------------------------
135 template<class V, class M>
136 void
138 {
139  unsigned int n_params = meanVector.sizeLocal();
140  queso_assert_equal_to (n_params, m_alpha.sizeLocal());
141 
142  for (unsigned int i = 0; i < n_params; ++i) {
143  meanVector[i] = m_alpha[i] / (m_alpha[i] + m_beta[i]);
144  }
145 }
146 //--------------------------------------------------
147 template<class V, class M>
148 void
150 {
151  unsigned int n_params = m_alpha.sizeLocal();
152  queso_assert_equal_to (n_params, m_beta.sizeLocal());
153  queso_assert_equal_to (n_params, covMatrix.numCols());
154  queso_assert_equal_to (covMatrix.numCols(), covMatrix.numRowsGlobal());
155 
156  covMatrix.zeroLower();
157  covMatrix.zeroUpper();
158 
159  for (unsigned int i = 0; i < n_params; ++i) {
160  covMatrix(i,i) = (m_alpha[i] * m_beta[i]) /
161  ((m_alpha[i] + m_beta[i]) *
162  (m_alpha[i] + m_beta[i]) *
163  (m_alpha[i] + m_beta[i] + 1));
164  }
165 }
166 //--------------------------------------------------
167 template<class V, class M>
168 double
169 BetaJointPdf<V,M>::computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
170 {
171  double value = 0.;
172 
173  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
174  *m_env.subDisplayFile() << "Entering BetaJointPdf<V,M>::computeLogOfNormalizationFactor()"
175  << std::endl;
176  }
177  value = BaseJointPdf<V,M>::commonComputeLogOfNormalizationFactor(numSamples, updateFactorInternally);
178  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
179  *m_env.subDisplayFile() << "Leaving BetaJointPdf<V,M>::computeLogOfNormalizationFactor()"
180  << ", m_logOfNormalizationFactor = " << m_logOfNormalizationFactor
181  << std::endl;
182  }
183 
184  return value;
185 }
186 
187 } // End namespace QUESO
188 
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
double actualValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Actual value of the Beta PDF.
Definition: BetaJointPdf.C:63
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:73
virtual void distributionMean(V &meanVector) const
Mean value of the underlying random variable.
Definition: BetaJointPdf.C:137
BetaJointPdf(const char *prefix, const VectorSet< V, M > &domainSet, const V &alpha, const V &beta)
Constructor.
Definition: BetaJointPdf.C:33
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:55
double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the Beta PDF.
Definition: BetaJointPdf.C:90
double computeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Computes the logarithm of the normalization factor.
Definition: BetaJointPdf.C:169
#define queso_assert_equal_to(expr1, expr2)
Definition: asserts.h:135
A templated class for handling sets.
Definition: VectorSet.h:52
double commonComputeLogOfNormalizationFactor(unsigned int numSamples, bool updateFactorInternally) const
Common method (to the derived classes) to compute the logarithm of the normalization factor...
Definition: JointPdf.C:115
unsigned int displayVerbosity() const
Definition: Environment.C:449
~BetaJointPdf()
Destructor.
Definition: BetaJointPdf.C:57
A class for handling Beta joint PDFs.
Definition: BetaJointPdf.h:47
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
const BaseEnvironment & m_env
virtual void distributionVariance(M &covMatrix) const
Covariance matrix of the underlying random variable.
Definition: BetaJointPdf.C:149

Generated on Tue Nov 29 2016 10:53:11 for queso-0.56.0 by  doxygen 1.8.5