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

Generated on Thu Jun 11 2015 13:52:33 for queso-0.53.0 by  doxygen 1.8.5