queso-0.56.0
VectorRV.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/VectorRV.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 #include <queso/Defines.h>
30 #include <gsl/gsl_sf_psi.h> // todo: take specificity of gsl_, i.e., make it general (gsl or boost or etc)
31 
32 namespace QUESO {
33 
34 // Default constructor -----------------------------
35 template<class V, class M>
37  const char* prefix,
38  const VectorSet<V,M>& imageSet)
39  :
40  m_env (imageSet.env()),
41  m_prefix ((std::string)(prefix)+"rv_"),
42  m_imageSet (imageSet),
43  m_pdf (NULL),
44  m_realizer (NULL),
45  m_subCdf (NULL),
46  m_unifiedCdf(NULL),
47  m_mdf (NULL)
48 {
49  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
50  *m_env.subDisplayFile() << "Entering BaseVectorRV<V,M>::constructor()"
51  << ": prefix = " << m_prefix
52  << std::endl;
53  }
54 
55  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 54)) {
56  *m_env.subDisplayFile() << "Leaving BaseVectorRV<V,M>::constructor()"
57  << ": prefix = " << m_prefix
58  << std::endl;
59  }
60 }
61 // Destructor ---------------------------------------
62 template<class V, class M>
64 {
65  //if (m_mdf ) delete m_mdf;
66  //if (m_subCdf ) delete m_subCdf;
67  //if (m_unifiedCdf) delete m_unifiedCdf;
68  //if (m_realizer ) delete m_realizer;
69  //if (m_pdf ) delete m_pdf;
70 }
71 // RV handling-methods ------------------------------
72 
73 template <class V, class M>
74 const BaseEnvironment&
76 {
77  return m_env;
78 }
79 //---------------------------------------------------
80 template<class V, class M>
81 const VectorSet<V,M>&
83 {
84  return m_imageSet;
85 }
86 //---------------------------------------------------
87 template<class V, class M>
88 const BaseJointPdf<V,M>&
90 {
91  queso_require_msg(m_pdf, "m_pdf is NULL");
92 
93  return *m_pdf;
94 }
95 //---------------------------------------------------
96 template<class V, class M>
99 {
100  queso_require_msg(m_realizer, "m_realizer is NULL");
101 
102  return *m_realizer;
103 }
104 //---------------------------------------------------
105 template<class V, class M>
106 const BaseVectorCdf<V,M>&
108 {
109  queso_require_msg(m_subCdf, "m_subCdf is NULL");
110 
111  return *m_subCdf;
112 }
113 //---------------------------------------------------
114 template<class V, class M>
115 const BaseVectorCdf<V,M>&
117 {
118  queso_require_msg(m_unifiedCdf, "m_unifiedCdf is NULL");
119 
120  return *m_unifiedCdf;
121 }
122 //---------------------------------------------------
123 template<class V, class M>
124 const BaseVectorMdf<V,M>&
126 {
127  queso_require_msg(m_mdf, "m_mdf is NULL");
128 
129  return *m_mdf;
130 }
131 
132 //---------------------------------------------------
133 #ifdef QUESO_HAS_ANN
134 template <class V, class M>
135 double
137 {
138  ANNpointArray data;
139  double* dists;
140  double ENT_est;
141 
142  // FIXME: these default values should be stored in the
143  // QUESO input file ( create a InfoTheoryOptions )
144  unsigned int k = UQ_INFTH_ANN_KNN;
145  double eps = UQ_INFTH_ANN_EPS;
146 
147  // here it is assumed that the entropy for the
148  // entire joint RV will be computed
149  unsigned int dim = this->imageSet().vectorSpace().dimGlobal();
150 
151  // FIXME: get the number already stored, otherwise
152  // use the default value
153  unsigned int N = this->realizer().subPeriod();
154  if( N == 0 ) {
155  N = UQ_INFTH_ANN_NO_SMP;
156  }
157 
158  // allocate memory
159  data = annAllocPts(N,dim);
160  dists = new double[N];
161 
162  // copy samples in the ANN data structure
163  V smpRV( this->imageSet().vectorSpace().zeroVector() );
164  for( unsigned int i = 0; i < N; i++ ) {
165  // get a sample from the distribution
166  this->realizer().realization( smpRV );
167  // copy the vector values in the ANN data structure
168  for( unsigned int j = 0; j < dim; j++ ) {
169  data[ i ][ j ] = smpRV[ j ];
170  }
171  }
172 
173  // get distance to knn for each point
174  // (k+1) because the 1st nn is itself
175  distANN_XY( data, data, dists, dim, dim, N, N, k+1, eps );
176 
177  // compute the entropy estimate using the L-infinity (Max) norm
178  // so no need for the adjustment of the mass of the hyperball
179  // this has to be enforced before compiling the ANN lib
180  double sum_log_dist = 0.0;
181  for( unsigned int i = 0; i < N; i++ ) {
182  if( dists[ i ] > 0 ) {
183  sum_log_dist += log( 2.0*dists[ i ] );
184  }
185  }
186  ENT_est = - gsl_sf_psi_int( k ) + gsl_sf_psi_int( N ) + (double)dim / (double)N * sum_log_dist; // todo: take specificity of gsl_, i.e., make it general (gsl or boost or etc)
187 
188  // deallocate memory
189  delete [] dists;
190  annDeallocPts( data );
191 
192  return ENT_est;
193 }
194 #endif // QUESO_HAS_ANN
195 
196 //---------------------------------------------------
197 // Method declared outside class definition ---------
198 //---------------------------------------------------
199 template <class P_V, class P_M, class Q_V, class Q_M>
200 void
202  const BaseVectorRV<P_V,P_M>& paramRv,
203  const BaseVectorRV<Q_V,Q_M>& qoiRv,
204  unsigned int localNumSamples,
205  P_M& pqCovMatrix,
206  P_M& pqCorrMatrix)
207 {
208  // Check input data consistency
209  bool useOnlyInter0Comm = (paramRv.imageSet().vectorSpace().numOfProcsForStorage() == 1) &&
210  (qoiRv.imageSet().vectorSpace().numOfProcsForStorage() == 1);
211 
212  queso_require_msg(useOnlyInter0Comm, "parallel vectors not supported yet");
213 
214  unsigned int numRows = paramRv.imageSet().vectorSpace().dim();
215  unsigned int numCols = qoiRv.imageSet().vectorSpace().dim();
216 
217  queso_require_msg(!((numRows != pqCovMatrix.numRows()) || (numCols != pqCovMatrix.numCols())), "inconsistent dimensions for covariance matrix");
218 
219  queso_require_msg(!((numRows != pqCorrMatrix.numRows()) || (numCols != pqCorrMatrix.numCols())), "inconsistent dimensions for correlation matrix");
220 
221  queso_require_msg(!((localNumSamples > paramRv.realizer().period()) || (localNumSamples > qoiRv.realizer().period())), "localNumSamples is too large");
222 
223  // For both P and Q vector sequences: fill them
224  P_V tmpP(paramRv.imageSet().vectorSpace().zeroVector());
225  Q_V tmpQ(qoiRv.imageSet().vectorSpace().zeroVector());
226 
227  SequenceOfVectors<P_V,P_M> localWorkingPSeq(paramRv.imageSet().vectorSpace(),
228  localNumSamples,
229  "covTmpP");
230  SequenceOfVectors<Q_V,Q_M> localWorkingQSeq(qoiRv.imageSet().vectorSpace(),
231  localNumSamples,
232  "covTmpQ");
233  for (unsigned int k = 0; k < localNumSamples; ++k) {
234  paramRv.realizer().realization(tmpP);
235  localWorkingPSeq.setPositionValues(k,tmpP);
236 
237  qoiRv.realizer().realization(tmpQ);
238  localWorkingQSeq.setPositionValues(k,tmpQ);
239  }
240 
242  localWorkingQSeq,
243  localNumSamples,
244  pqCovMatrix,
245  pqCorrMatrix);
246 
247  return;
248 }
249 
250 } // End namespace QUESO
251 
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
void ComputeCovCorrMatricesBetweenVectorRvs(const BaseVectorRV< P_V, P_M > &paramRv, const BaseVectorRV< Q_V, Q_M > &qoiRv, unsigned int localNumSamples, P_M &pqCovMatrix, P_M &pqCorrMatrix)
Definition: VectorRV.C:201
ANNpoint * ANNpointArray
Definition: ANN.h:376
int dim
Definition: ann2fig.cpp:81
const BaseVectorCdf< V, M > & unifiedCdf() const
Finds the Cumulative Distribution Function of this vector RV, considering the unified sequence of dat...
Definition: VectorRV.C:116
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:197
const BaseJointPdf< V, M > & pdf() const
Posterior Density Function of the vector RV; access to private attribute m_pdf.
Definition: VectorRV.C:89
A templated base class for handling vector RV.
Definition: VectorRV.h:54
const BaseEnvironment & m_env
Definition: VectorRV.h:115
const V & zeroVector() const
Returns a vector filled with zeros.
Definition: VectorSpace.C:189
const BaseVectorMdf< V, M > & mdf() const
Finds the Marginal Density Function of this vector RV; access to private attribute m_mdf...
Definition: VectorRV.C:125
int k
Definition: ann_sample.cpp:53
virtual ~BaseVectorRV()
Virtual destructor.
Definition: VectorRV.C:63
A templated (base) class for handling joint PDFs.
Definition: JointPdf.h:55
virtual void realization(V &nextValues) const =0
Performs a realization (sample) from a probability density function. See template specialization...
DLL_API void annDeallocPts(ANNpointArray &pa)
Definition: ANN.cpp:133
const VectorSet< V, M > & imageSet() const
Image set of the vector RV; access to private attribute m_imageSet.
Definition: VectorRV.C:82
DLL_API ANNpointArray annAllocPts(int n, int dim)
Definition: ANN.cpp:117
A templated (base) class for handling CDFs of vector functions.
Definition: VectorCdf.h:53
A templated class for handling sets.
Definition: VectorSet.h:52
const BaseEnvironment & env() const
QUESO environment; access to private attribute m_env.
Definition: VectorRV.C:75
BaseVectorRV(const char *prefix, const VectorSet< V, M > &imageSet)
Constructor.
Definition: VectorRV.C:36
A templated (base) class for handling sampling from vector RVs.
double eps
Definition: ann_sample.cpp:55
unsigned int displayVerbosity() const
Definition: Environment.C:449
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
void ComputeCovCorrMatricesBetweenVectorSequences(const BaseVectorSequence< P_V, P_M > &subPSeq, const BaseVectorSequence< Q_V, Q_M > &subQSeq, unsigned int subNumSamples, P_M &pqCovMatrix, P_M &pqCorrMatrix)
std::string m_prefix
Definition: VectorRV.h:116
const BaseVectorCdf< V, M > & subCdf() const
Finds the Cumulative Distribution Function of this vector RV, considering only the sub-sequence of da...
Definition: VectorRV.C:107
const BaseVectorRealizer< V, M > & realizer() const
Finds a realization (sample) of the PDF of this vector RV; access to private attribute m_realizer...
Definition: VectorRV.C:98
virtual const VectorSpace< V, M > & vectorSpace() const =0
Vector space to which this set belongs to. See template specialization.
unsigned int numOfProcsForStorage() const
Returns total number of processes.
Definition: VectorSpace.C:164
A templated (base) class for handling MDFs of vector functions.
Definition: VectorMdf.h:52

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