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

Generated on Tue Jun 5 2018 19:48:56 for queso-0.57.1 by  doxygen 1.8.5