queso-0.50.1
VectorSequence.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,2009,2010,2011,2012 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 
26 #include <queso/VectorSequence.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
29 
30 namespace QUESO {
31 
32 // Default constructor -----------------------------
33 template <class V, class M>
35  const VectorSpace<V,M>& vectorSpace,
36  unsigned int subSequenceSize,
37  const std::string& name)
38  :
39  m_env (vectorSpace.env()),
40  m_vectorSpace (vectorSpace),
41  m_name (name),
42  m_fftObj (new Fft<double>(m_env)),
43  m_subMinPlain (NULL),
44  m_unifiedMinPlain (NULL),
45  m_subMaxPlain (NULL),
46  m_unifiedMaxPlain (NULL),
47  m_subMeanPlain (NULL),
48  m_unifiedMeanPlain (NULL),
49  m_subMedianPlain (NULL),
50  m_unifiedMedianPlain (NULL),
51  m_subSampleVariancePlain (NULL),
52  m_unifiedSampleVariancePlain(NULL),
53  m_subBoxPlain (NULL),
54  m_unifiedBoxPlain (NULL)
55 {
56  if (subSequenceSize) {}; // just to avoid compiler warning
57 }
58 // Destructor ---------------------------------------
59 template <class V, class M>
61 {
62  //clear();
63  this->deleteStoredVectors();
64  if (m_fftObj != NULL) delete m_fftObj;
65 }
66 
67 // Sequence methods----------------------------------
68 template <class V, class M>
69 unsigned int
71 {
72  unsigned int unifiedNumSamples = 0;
73 
74  bool useOnlyInter0Comm = (m_vectorSpace.numOfProcsForStorage() == 1);
75 
76  if (useOnlyInter0Comm) {
77  if (m_env.inter0Rank() >= 0) {
78  unsigned int subNumSamples = this->subSequenceSize();
79  m_env.inter0Comm().Allreduce((void *) &subNumSamples, (void *) &unifiedNumSamples, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
80  "BaseVectorSequence<V,M>::unifiedSequenceSize()",
81  "failed MPI.Allreduce() for unifiedSequenceSize()");
82  }
83  else {
84  // Node not in the 'inter0' communicator
85  unifiedNumSamples = this->subSequenceSize();
86  }
87  }
88  else {
89  UQ_FATAL_TEST_MACRO((useOnlyInter0Comm == false),
90  m_env.worldRank(),
91  "BaseVectorSequence<V,M>::unifiedSequenceSize()",
92  "parallel vectors not supported yet");
93  }
94 
95  return unifiedNumSamples;
96 }
97 // --------------------------------------------------
98 template <class V, class M>
99 unsigned int
101 {
102  return m_vectorSpace.dimLocal();
103 }
104 // --------------------------------------------------
105 template <class V, class M>
106 unsigned int
108 {
109  return m_vectorSpace.dimGlobal();
110 }
111 // --------------------------------------------------
112 template <class V, class M>
113 const VectorSpace<V,M>&
115 {
116  return m_vectorSpace;
117 }
118 // --------------------------------------------------
119 template <class V, class M>
120 const std::string&
122 {
123  return m_name;
124 }
125 // --------------------------------------------------
126 template <class V, class M>
127 void
128 BaseVectorSequence<V,M>::setName(const std::string& newName)
129 {
130  m_name = newName;
131  return;
132 }
133 // --------------------------------------------------
134 template <class V, class M>
135 void
137 {
138  unsigned int numPos = this->subSequenceSize();
139  if (numPos) {
140  this->resetValues(0,numPos);
141  this->resizeSequence(0);
142  }
143 
144  return;
145 }
146 // --------------------------------------------------
147 template <class V, class M>
148 const V&
150 {
151  if (m_subMinPlain == NULL) {
152  m_subMinPlain = m_vectorSpace.newVector();
153  if (m_subMaxPlain == NULL) m_subMaxPlain = m_vectorSpace.newVector();
154  subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
155  }
156 
157  return *m_subMinPlain;
158 }
159 // --------------------------------------------------
160 template <class V, class M>
161 const V&
163 {
164  if (m_unifiedMinPlain == NULL) {
165  m_unifiedMinPlain = m_vectorSpace.newVector();
166  if (m_unifiedMaxPlain == NULL) m_unifiedMaxPlain = m_vectorSpace.newVector();
167  unifiedMinMaxExtra(0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
168  }
169 
170  return *m_unifiedMinPlain;
171 }
172 // --------------------------------------------------
173 template <class V, class M>
174 const V&
176 {
177  if (m_subMaxPlain == NULL) {
178  if (m_subMinPlain == NULL) m_subMinPlain = m_vectorSpace.newVector();
179  m_subMaxPlain = m_vectorSpace.newVector();
180  subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
181  }
182 
183  return *m_subMaxPlain;
184 }
185 // --------------------------------------------------
186 template <class V, class M>
187 const V&
189 {
190  if (m_unifiedMaxPlain == NULL) {
191  if (m_unifiedMinPlain == NULL) m_unifiedMinPlain = m_vectorSpace.newVector();
192  m_unifiedMaxPlain = m_vectorSpace.newVector();
193  unifiedMinMaxExtra(0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
194  }
195 
196  return *m_unifiedMaxPlain;
197 }
198 // --------------------------------------------------
199 template <class V, class M>
200 const V&
202 {
203  if (m_subMeanPlain == NULL) {
204  m_subMeanPlain = m_vectorSpace.newVector();
205  subMeanExtra(0,subSequenceSize(),*m_subMeanPlain);
206  }
207 
208  return *m_subMeanPlain;
209 }
210 // --------------------------------------------------
211 template <class V, class M>
212 const V&
214 {
215  if (m_unifiedMeanPlain == NULL) {
216  m_unifiedMeanPlain = m_vectorSpace.newVector();
217  unifiedMeanExtra(0,subSequenceSize(),*m_unifiedMeanPlain);
218  }
219 
220  return *m_unifiedMeanPlain;
221 }
222 // --------------------------------------------------
223 template <class V, class M>
224 const V&
226 {
227  if (m_subMedianPlain == NULL) {
228  m_subMedianPlain = m_vectorSpace.newVector();
229  subMedianExtra(0, subSequenceSize(), *m_subMedianPlain);
230  }
231 
232  return *m_subMedianPlain;
233 }
234 // --------------------------------------------------
235 template <class V, class M>
236 const V&
238 {
239  if (m_unifiedMedianPlain == NULL) {
240  m_unifiedMedianPlain = m_vectorSpace.newVector();
241  unifiedMedianExtra(0, subSequenceSize(), *m_unifiedMedianPlain);
242  }
243 
244  return *m_unifiedMedianPlain;
245 }
246 // --------------------------------------------------
247 template <class V, class M>
248 const V&
250 {
251  if (m_subSampleVariancePlain == NULL) {
252  m_subSampleVariancePlain = m_vectorSpace.newVector();
253  subSampleVarianceExtra(0,subSequenceSize(),subMeanPlain(),*m_subSampleVariancePlain);
254  }
255 
256  return *m_subSampleVariancePlain;
257 }
258 // --------------------------------------------------
259 template <class V, class M>
260 const V&
262 {
263  if (m_unifiedSampleVariancePlain == NULL) {
264  m_unifiedSampleVariancePlain = m_vectorSpace.newVector();
265  unifiedSampleVarianceExtra(0,subSequenceSize(),unifiedMeanPlain(),*m_unifiedSampleVariancePlain);
266  }
267 
268  return *m_unifiedSampleVariancePlain;
269 }
270 // --------------------------------------------------
271 template <class V, class M>
272 const BoxSubset<V,M>&
274 {
275  if (m_subBoxPlain == NULL) {
276  m_subBoxPlain = new BoxSubset<V,M>(m_name.c_str(),
277  m_vectorSpace,
278  this->subMinPlain(),
279  this->subMaxPlain());
280  }
281 
282  return *m_subBoxPlain;
283 }
284 // --------------------------------------------------
285 template <class V, class M>
286 const BoxSubset<V,M>&
288 {
289  if (m_unifiedBoxPlain == NULL) {
290  m_unifiedBoxPlain = new BoxSubset<V,M>(m_name.c_str(),
291  m_vectorSpace,
292  this->unifiedMinPlain(),
293  this->unifiedMaxPlain());
294  }
295 
296  return *m_unifiedBoxPlain;
297 }
298 // --------------------------------------------------
299 template <class V, class M>
300 void
302 {
303  if (m_subMinPlain) {
304  delete m_subMinPlain;
305  m_subMinPlain = NULL;
306  }
307  if (m_unifiedMinPlain) {
308  delete m_unifiedMinPlain;
309  m_unifiedMinPlain = NULL;
310  }
311  if (m_subMaxPlain) {
312  delete m_subMaxPlain;
313  m_subMaxPlain = NULL;
314  }
315  if (m_unifiedMaxPlain) {
316  delete m_unifiedMaxPlain;
317  m_unifiedMaxPlain = NULL;
318  }
319  if (m_subMeanPlain) {
320  delete m_subMeanPlain;
321  m_subMeanPlain = NULL;
322  }
323  if (m_unifiedMeanPlain) {
324  delete m_unifiedMeanPlain;
325  m_unifiedMeanPlain = NULL;
326  }
327  if (m_subMedianPlain) {
328  delete m_subMedianPlain;
329  m_subMedianPlain = NULL;
330  }
331  if (m_unifiedMedianPlain) {
332  delete m_unifiedMedianPlain;
333  m_unifiedMedianPlain = NULL;
334  }
335  if (m_subSampleVariancePlain) {
336  delete m_subSampleVariancePlain;
337  m_subSampleVariancePlain = NULL;
338  }
339  if (m_unifiedSampleVariancePlain) {
340  delete m_unifiedSampleVariancePlain;
341  m_unifiedSampleVariancePlain = NULL;
342  }
343  if (m_subBoxPlain) {
344  delete m_subBoxPlain;
345  m_subBoxPlain = NULL;
346  }
347  if (m_unifiedBoxPlain) {
348  delete m_unifiedBoxPlain;
349  m_unifiedBoxPlain = NULL;
350  }
351 
352  return;
353 }
354 // --------------------------------------------------
355 template <class V, class M>
356 void
358  const BaseVectorSequence<V,M>& src,
359  unsigned int initialPos,
360  unsigned int numPos)
361 {
362  UQ_FATAL_TEST_MACRO((src.subSequenceSize() < (initialPos+1)),
363  m_env.worldRank(),
364  "BaseVectorSequence<V,M>::append()",
365  "initialPos is too big");
366 
367  UQ_FATAL_TEST_MACRO((src.subSequenceSize() < (initialPos+numPos)),
368  m_env.worldRank(),
369  "BaseVectorSequence<V,M>::append()",
370  "numPos is too big");
371 
372  this->deleteStoredVectors();
373  unsigned int currentSize = this->subSequenceSize();
374  this->resizeSequence(currentSize+numPos);
375  V tmpVec(src.vectorSpace().zeroVector());
376  for (unsigned int i = 0; i < numPos; ++i) {
377  src.getPositionValues(initialPos+i,tmpVec);
378  this->setPositionValues(currentSize+i,tmpVec);
379  }
380 
381  return;
382 }
383 // --------------------------------------------------
384 template <class V, class M>
385 double
387  const ScalarSequence<double>& subCorrespondingScalarValues,
388  BaseVectorSequence<V,M>& subPositionsOfMaximum)
389 {
390  if (m_env.subDisplayFile()) {
391  *m_env.subDisplayFile() << "Entering BaseVectorSequence<V,M>::subPositionsOfMaximum()"
392  << ": subCorrespondingScalarValues,subSequenceSize() = " << subCorrespondingScalarValues.subSequenceSize()
393  << ", this->subSequenceSize = " << this->subSequenceSize()
394  << std::endl;
395  }
396 
397  UQ_FATAL_TEST_MACRO(subCorrespondingScalarValues.subSequenceSize() != this->subSequenceSize(),
398  m_env.worldRank(),
399  "BaseVectorSequence<V,M>::subPositionsOfMaximum()",
400  "invalid input");
401 
402  double subMaxValue = subCorrespondingScalarValues.subMaxPlain();
403  unsigned int iMax = subCorrespondingScalarValues.subSequenceSize();
404 
405  unsigned int subNumPos = 0;
406  for (unsigned int i = 0; i < iMax; ++i) {
407  if (subCorrespondingScalarValues[i] == subMaxValue) {
408  subNumPos++;
409  }
410  }
411 
412  V tmpVec(this->vectorSpace().zeroVector());
413  subPositionsOfMaximum.resizeSequence(subNumPos);
414  unsigned int j = 0;
415  for (unsigned int i = 0; i < iMax; ++i) {
416  if (subCorrespondingScalarValues[i] == subMaxValue) {
417  this->getPositionValues (i,tmpVec);
418  subPositionsOfMaximum.setPositionValues(j,tmpVec);
419  j++;
420  }
421  }
422 
423  if (m_env.subDisplayFile()) {
424  *m_env.subDisplayFile() << "Leaving BaseVectorSequence<V,M>::subPositionsOfMaximum()"
425  << std::endl;
426  }
427 
428  return subMaxValue;
429 }
430 // --------------------------------------------------
431 template <class V, class M>
432 double
434  const ScalarSequence<double>& subCorrespondingScalarValues,
435  BaseVectorSequence<V,M>& unifiedPositionsOfMaximum)
436 {
437  if (m_env.subDisplayFile()) {
438  *m_env.subDisplayFile() << "Entering BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()"
439  << ": subCorrespondingScalarValues,subSequenceSize() = " << subCorrespondingScalarValues.subSequenceSize()
440  << ", this->subSequenceSize = " << this->subSequenceSize()
441  << std::endl;
442  }
443 
444  UQ_FATAL_TEST_MACRO(subCorrespondingScalarValues.subSequenceSize() != this->subSequenceSize(),
445  m_env.worldRank(),
446  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
447  "invalid input");
448 
449  double subMaxValue = subCorrespondingScalarValues.subMaxPlain();
450 
451  //******************************************************************
452  // Get overall max
453  //******************************************************************
454  double unifiedMaxValue;
455  std::vector<double> sendbufPos(1,0.);
456  for (unsigned int i = 0; i < sendbufPos.size(); ++i) {
457  sendbufPos[i] = subMaxValue;
458  }
459  m_env.inter0Comm().Allreduce((void *) &sendbufPos[0], (void *) &unifiedMaxValue, (int) sendbufPos.size(), RawValue_MPI_DOUBLE, RawValue_MPI_MAX,
460  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
461  "failed MPI.Allreduce() for max");
462 
463  unsigned int iMax = subCorrespondingScalarValues.subSequenceSize();
464  int subNumPos = 0; // Yes, 'int', due to MPI to be used soon
465  for (unsigned int i = 0; i < iMax; ++i) {
466  if (subCorrespondingScalarValues[i] == unifiedMaxValue) {
467  subNumPos++;
468  }
469  }
470 
471  V tmpVec(this->vectorSpace().zeroVector());
472  unifiedPositionsOfMaximum.resizeSequence(subNumPos);
473  unsigned int j = 0;
474  for (unsigned int i = 0; i < iMax; ++i) {
475  if (subCorrespondingScalarValues[i] == unifiedMaxValue) {
476  this->getPositionValues (i,tmpVec);
477  unifiedPositionsOfMaximum.setPositionValues(j,tmpVec);
478  j++;
479  }
480  }
481 
482  std::vector<int> auxBuf(1,0);
483  int unifiedNumPos = 0; // Yes, 'int', due to MPI to be used soon
484  auxBuf[0] = subNumPos;
485  m_env.inter0Comm().Allreduce((void *) &auxBuf[0], (void *) &unifiedNumPos, (int) auxBuf.size(), RawValue_MPI_INT, RawValue_MPI_SUM,
486  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
487  "failed MPI.Allreduce() for sum");
488  unifiedPositionsOfMaximum.resizeSequence(unifiedNumPos);
489 
490  //******************************************************************
491  // Use MPI_Gatherv for number of positions
492  //******************************************************************
493  unsigned int Np = (unsigned int) m_env.inter0Comm().NumProc();
494 
495  std::vector<int> recvcntsPos(Np,0); // '0' is NOT the correct value for recvcntsPos[0]
496  m_env.inter0Comm().Gather((void *) &subNumPos, 1, RawValue_MPI_INT, (void *) &recvcntsPos[0], (int) 1, RawValue_MPI_INT, 0,
497  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
498  "failed MPI.Gatherv()");
499  if (m_env.inter0Rank() == 0) {
500  UQ_FATAL_TEST_MACRO(recvcntsPos[0] != (int) subNumPos,
501  m_env.worldRank(),
502  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
503  "failed MPI.Gather() result at proc 0 (recvcntsPos[0])");
504  }
505 
506  std::vector<int> displsPos(Np,0);
507  for (unsigned int nodeId = 1; nodeId < Np; ++nodeId) { // Yes, from '1' on
508  displsPos[nodeId] = displsPos[nodeId-1] + recvcntsPos[nodeId-1];
509  }
510  if (m_env.inter0Rank() == 0) {
511  UQ_FATAL_TEST_MACRO(unifiedNumPos != (displsPos[Np - 1] + recvcntsPos[Np - 1]),
512  m_env.worldRank(),
513  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
514  "failed MPI.Gather() result at proc 0 (unifiedNumPos)");
515  }
516 
517  //******************************************************************
518  // Use MPI_Gatherv for number of doubles
519  //******************************************************************
520  unsigned int dimSize = m_vectorSpace.dimLocal();
521  int subNumDbs = subNumPos * dimSize; // Yes, 'int', due to MPI to be used soon
522  std::vector<int> recvcntsDbs(Np,0); // '0' is NOT the correct value for recvcntsDbs[0]
523  m_env.inter0Comm().Gather((void *) &subNumDbs, 1, RawValue_MPI_INT, (void *) &recvcntsDbs[0], (int) 1, RawValue_MPI_INT, 0,
524  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
525  "failed MPI.Gatherv()");
526  if (m_env.inter0Rank() == 0) {
527  UQ_FATAL_TEST_MACRO(recvcntsDbs[0] != (int) subNumDbs,
528  m_env.worldRank(),
529  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
530  "failed MPI.Gather() result at proc 0 (recvcntsDbs[0])");
531  }
532 
533  std::vector<int> displsDbs(Np,0);
534  for (unsigned int nodeId = 1; nodeId < Np; ++nodeId) { // Yes, from '1' on
535  displsDbs[nodeId] = displsDbs[nodeId-1] + recvcntsDbs[nodeId-1];
536  }
537  if (m_env.inter0Rank() == 0) {
538  UQ_FATAL_TEST_MACRO(((int) (unifiedNumPos*dimSize)) != (displsDbs[Np - 1] + recvcntsDbs[Np - 1]),
539  m_env.worldRank(),
540  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
541  "failed MPI.Gather() result at proc 0 (unifiedNumPos*dimSize)");
542  }
543 
544  //******************************************************************
545  // Prepare counters and buffers for gatherv of maximum positions
546  //******************************************************************
547  std::vector<double> sendbufDbs(subNumDbs,0.);
548  for (unsigned int i = 0; i < (unsigned int) subNumPos; ++i) {
549  unifiedPositionsOfMaximum.getPositionValues(i,tmpVec);
550  for (unsigned int j = 0; j < dimSize; ++j) {
551  sendbufDbs[i*dimSize + j] = tmpVec[j];
552  }
553  }
554 
555  std::vector<double> recvbufDbs(unifiedNumPos * dimSize);
556 
557  m_env.inter0Comm().Gatherv((void *) &sendbufDbs[0], (int) subNumDbs, RawValue_MPI_DOUBLE, (void *) &recvbufDbs[0], (int *) &recvcntsDbs[0], (int *) &displsDbs[0], RawValue_MPI_DOUBLE, 0,
558  "BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()",
559  "failed MPI.Gatherv()");
560 
561  //******************************************************************
562  // Transfer data from 'recvbuf' to 'unifiedPositionsOfMaximum'
563  //******************************************************************
564  if (m_env.inter0Rank() == (int) 0) {
565  for (unsigned int i = 0; i < (unsigned int) unifiedNumPos; ++i) {
566  for (unsigned int j = 0; j < dimSize; ++j) {
567  tmpVec[j] = recvbufDbs[i*dimSize + j];
568  }
569  unifiedPositionsOfMaximum.setPositionValues(i,tmpVec);
570  }
571  }
572 
573  if (m_env.subDisplayFile()) {
574  *m_env.subDisplayFile() << "Leaving BaseVectorSequence<V,M>::unifiedPositionsOfMaximum()"
575  << std::endl;
576  }
577 
578  return unifiedMaxValue;
579 }
580 // --------------------------------------------------
581 template <class V, class M>
582 void
583 BaseVectorSequence<V,M>::setGaussian(const V& meanVec, const V& stdDevVec)
584 {
585  V gaussianVector(m_vectorSpace.zeroVector());
586  for (unsigned int j = 0; j < this->subSequenceSize(); ++j) {
587  gaussianVector.cwSetGaussian(meanVec,stdDevVec);
588  this->setPositionValues(j,gaussianVector);
589  }
590 
591  this->deleteStoredVectors();
592 
593  return;
594 }
595 // --------------------------------------------------
596 template <class V, class M>
597 void
598 BaseVectorSequence<V,M>::setUniform(const V& aVec, const V& bVec)
599 {
600  V uniformVector(m_vectorSpace.zeroVector());
601  for (unsigned int j = 0; j < this->subSequenceSize(); ++j) {
602  uniformVector.cwSetUniform(aVec,bVec);
603  this->setPositionValues(j,uniformVector);
604  }
605 
606  this->deleteStoredVectors();
607 
608  return;
609 }
610 // --------------------------------------------------
611 template<class V, class M>
612 void
614  std::ofstream* passedOfs,
615  unsigned int& initialPos,
616  unsigned int& spacing)
617 {
618  if (m_env.subDisplayFile()) {
619  *m_env.subDisplayFile() << "\n"
620  << "\n-----------------------------------------------------"
621  << "\n Computing filter parameters for chain '" << m_name << "' ..."
622  << "\n-----------------------------------------------------"
623  << "\n"
624  << std::endl;
625  }
626 
627  bool okSituation = ((passedOfs == NULL ) ||
628  ((passedOfs != NULL) && (m_env.subRank() >= 0)));
629  UQ_FATAL_TEST_MACRO(!okSituation,
630  m_env.worldRank(),
631  "BaseVectorSequence<V,M>::computeFilterParams()",
632  "unexpected combination of file pointer and subRank");
633 
634  //initialPos = 0;
635  spacing = 1;
636 
637  if (m_env.subDisplayFile()) {
638  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
639  << "\n Finished computing filter parameters for chain '" << m_name << "'"
640  << ": initialPos = " << initialPos
641  << ", spacing = " << spacing
642  << "\n-----------------------------------------------------"
643  << "\n"
644  << std::endl;
645  }
646 
647  return;
648 }
649 
650 // Protected methods---------------------------------
651 template <class V, class M>
652 void
654 {
655  // FIX ME: should check environments as well ???
656 
657  UQ_FATAL_TEST_MACRO(m_vectorSpace.dimLocal() != src.m_vectorSpace.dimLocal(),
658  m_env.worldRank(),
659  "SequenceOfVectors<V,M>::copy()",
660  "incompatible vector space dimensions");
661 
662  m_name = src.m_name;
663  this->deleteStoredVectors();
664 
665  return;
666 }
667 
668 // --------------------------------------------------
669 // Methods conditionally available ------------------
670 // --------------------------------------------------
671 // --------------------------------------------------
672 
673 #ifdef QUESO_USES_SEQUENCE_STATISTICAL_OPTIONS
674 template<class V, class M>
675 void
677  const SequenceStatisticalOptions& statisticalOptions,
678  std::ofstream* passedOfs)
679 {
680  if (m_env.subDisplayFile()) {
681  *m_env.subDisplayFile() << "\n"
682  << "\n-----------------------------------------------------"
683  << "\n Computing statistics for chain '" << m_name << "' ..."
684  << "\n-----------------------------------------------------"
685  << "\n"
686  << std::endl;
687  }
688 
689  bool okSituation = ((passedOfs == NULL ) ||
690  ((passedOfs != NULL) && (m_env.subRank() >= 0)));
691  UQ_FATAL_TEST_MACRO(!okSituation,
692  m_env.worldRank(),
693  "BaseVectorSequence<V,M>::computeStatistics()",
694  "unexpected combination of file pointer and subRank");
695 
696  int iRC = UQ_OK_RC;
697  struct timeval timevalTmp;
698  iRC = gettimeofday(&timevalTmp, NULL);
699  double tmpRunTime = 0.;
700 
701  // Set initial positions for the computation of chain statistics
702  std::vector<unsigned int> initialPosForStatistics(statisticalOptions.initialDiscardedPortions().size(),0);
703  for (unsigned int i = 0; i < initialPosForStatistics.size(); ++i) {
704  initialPosForStatistics[i] = (unsigned int) (statisticalOptions.initialDiscardedPortions()[i] * (double) (this->subSequenceSize()-1));
705  if (m_env.subDisplayFile()) {
706  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeStatistics()"
707  << ": statisticalOptions.initialDiscardedPortions()[" << i << "] = " << statisticalOptions.initialDiscardedPortions()[i]
708  << ", initialPosForStatistics[" << i << "] = " << initialPosForStatistics[i]
709  << std::endl;
710  }
711  }
712  if (m_env.subDisplayFile()) {
713  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeStatistics(): initial positions for statistics =";
714  for (unsigned int i = 0; i < initialPosForStatistics.size(); ++i) {
715  *m_env.subDisplayFile() << " " << initialPosForStatistics[i];
716  }
717  *m_env.subDisplayFile() << std::endl;
718  }
719 
720  //****************************************************
721  // Compute mean, median, sample std, population std
722  //****************************************************
723  this->computeMeanVars(statisticalOptions,
724  passedOfs,
725  NULL,
726  NULL,
727  NULL,
728  NULL);
729 
730 #ifdef UQ_CODE_HAS_MONITORS
731  if (statisticalOptions.meanMonitorPeriod() != 0) {
732  this->computeMeanEvolution(statisticalOptions,
733  passedOfs);
734  }
735 #endif
736 
737  //****************************************************
738  // Compute variance of sample mean through the 'batch means method' (BMM)
739  //****************************************************
740 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
741  if ((statisticalOptions.bmmRun() ) &&
742  (initialPosForStatistics.size() > 0) &&
743  (statisticalOptions.bmmLengths().size() > 0)) {
744  this->computeBMM(statisticalOptions,
745  initialPosForStatistics,
746  passedOfs);
747  }
748 #endif
749  //****************************************************
750  // Compute FFT of chain, for one parameter only
751  //****************************************************
752 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
753  if ((statisticalOptions.fftCompute() ) &&
754  (initialPosForStatistics.size() > 0)) {
755  this->computeFFT(statisticalOptions,
756  initialPosForStatistics,
757  passedOfs);
758  }
759 #endif
760  //****************************************************
761  // Compute power spectral density (PSD) of chain, for one parameter only
762  //****************************************************
763 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
764  if ((statisticalOptions.psdCompute() ) &&
765  (initialPosForStatistics.size() > 0)) {
766  this->computePSD(statisticalOptions,
767  initialPosForStatistics,
768  passedOfs);
769  }
770 #endif
771  //****************************************************
772  // Compute power spectral density (PSD) of chain at zero frequency
773  //****************************************************
774 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
775  if ((statisticalOptions.psdAtZeroCompute() ) &&
776  (initialPosForStatistics.size() > 0) &&
777  (statisticalOptions.psdAtZeroNumBlocks().size() > 0)) {
778  this->computePSDAtZero(statisticalOptions,
779  initialPosForStatistics,
780  passedOfs);
781  }
782 #endif
783  //****************************************************
784  // Compute Geweke
785  //****************************************************
786 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
787  if ((statisticalOptions.gewekeCompute()) &&
788  (initialPosForStatistics.size() > 0)) {
789  this->computeGeweke(statisticalOptions,
790  initialPosForStatistics,
791  passedOfs);
792  }
793 #endif
794  //****************************************************
795  // Compute mean statistical accuracy
796  //****************************************************
797 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
798  if ((statisticalOptions.meanStaccCompute()) &&
799  (initialPosForStatistics.size() > 0 )) {
800  this->computeMeanStacc(statisticalOptions,
801  initialPosForStatistics,
802  passedOfs);
803  }
804 #endif
805  // Set lags for the computation of chain autocorrelations
806  std::vector<unsigned int> lagsForCorrs(statisticalOptions.autoCorrNumLags(),1);
807  for (unsigned int i = 1; i < lagsForCorrs.size(); ++i) {
808  lagsForCorrs[i] = statisticalOptions.autoCorrSecondLag() + (i-1)*statisticalOptions.autoCorrLagSpacing();
809  }
810 
811  //****************************************************
812  // Compute autocorrelation coefficients via definition
813  //****************************************************
814  if ((statisticalOptions.autoCorrComputeViaDef()) &&
815  (initialPosForStatistics.size() > 0 ) &&
816  (lagsForCorrs.size() > 0 )) {
817  this->computeAutoCorrViaDef(statisticalOptions,
818  initialPosForStatistics,
819  lagsForCorrs,
820  passedOfs);
821  }
822 
823  //****************************************************
824  // Compute autocorrelation coefficients via FFT
825  //****************************************************
826  if ((statisticalOptions.autoCorrComputeViaFft()) &&
827  (initialPosForStatistics.size() > 0 ) &&
828  (lagsForCorrs.size() > 0 )) {
829  this->computeAutoCorrViaFFT(statisticalOptions,
830  initialPosForStatistics,
831  lagsForCorrs,
832  passedOfs);
833  }
834 
835  //****************************************************
836  // Compute histogram and/or cdf stacc and/or Kde
837  //****************************************************
838 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
839  if ((statisticalOptions.histCompute ()) ||
840  (statisticalOptions.cdfStaccCompute()) ||
841  (statisticalOptions.kdeCompute ())) {
842 #else
843  if (statisticalOptions.kdeCompute()) //kemelli deleted single '{' on 5/28/13 - compare to queso-0.45.2
844 #endif
845  this->computeHistCdfstaccKde(statisticalOptions,
846  passedOfs);
847  }
848 
849  //****************************************************
850  // Compute covariance and correlation matrices
851  //****************************************************
852  if ((statisticalOptions.covMatrixCompute ()) ||
853  (statisticalOptions.corrMatrixCompute())) {
854  this->computeCovCorrMatrices(statisticalOptions,
855  passedOfs);
856  }
857 
858  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
859  if (m_env.subDisplayFile()) {
860  *m_env.subDisplayFile() << "All statistics of chain '" << m_name << "'"
861  << " took " << tmpRunTime
862  << " seconds"
863  << std::endl;
864  }
865 
866  if (m_env.subDisplayFile()) {
867  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
868  << "\n Finished computing statistics for chain '" << m_name << "'"
869  << "\n-----------------------------------------------------"
870  << "\n"
871  << std::endl;
872  }
873 
874  return;
875 }
876 //----------------------------------------------
877 template<class V, class M>
878 void
879 BaseVectorSequence<V,M>::computeMeanVars(
880  const SequenceStatisticalOptions& statisticalOptions,
881  std::ofstream* passedOfs,
882  V* subMeanPtr,
883  V* subMedianPtr,
884  V* subSampleVarPtr,
885  V* subPopulVarPtr)
886 {
887  int iRC = UQ_OK_RC;
888  struct timeval timevalTmp;
889  iRC = gettimeofday(&timevalTmp, NULL);
890  double tmpRunTime = 0.;
891 
892  if (m_env.subDisplayFile()) {
893  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
894  << "\nComputing mean, sample variance and population variance"
895  << std::endl;
896  }
897 
898  V subChainMean(m_vectorSpace.zeroVector());
899  this->subMeanExtra(0,
900  this->subSequenceSize(),
901  subChainMean);
902 
903  V subChainMedian(m_vectorSpace.zeroVector());
904  this->subMedianExtra(0,
905  this->subSequenceSize(),
906  subChainMedian);
907 
908  V subChainSampleVariance(m_vectorSpace.zeroVector());
909  this->subSampleVarianceExtra(0,
910  this->subSequenceSize(),
911  subChainMean,
912  subChainSampleVariance);
913 
914  if ((m_env.displayVerbosity() >= 5) && (m_env.subDisplayFile())) {
915  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeMeanVars()"
916  << ": subChainMean.sizeLocal() = " << subChainMean.sizeLocal()
917  << ", subChainMean = " << subChainMean
918  << ", subChainMedian = " << subChainMedian
919  << ", subChainSampleVariance.sizeLocal() = " << subChainSampleVariance.sizeLocal()
920  << ", subChainSampleVariance = " << subChainSampleVariance
921  << std::endl;
922  }
923 
924  V estimatedVarianceOfSampleMean(subChainSampleVariance);
925  estimatedVarianceOfSampleMean /= (double) this->subSequenceSize();
926  bool savedVectorPrintState = estimatedVarianceOfSampleMean.getPrintHorizontally();
927  estimatedVarianceOfSampleMean.setPrintHorizontally(false);
928  if (m_env.subDisplayFile()) {
929  *m_env.subDisplayFile() << "\nEstimated variance of sample mean for the whole chain '" << m_name << "'"
930  << ", under independence assumption:"
931  << "\n"
932  << estimatedVarianceOfSampleMean
933  << std::endl;
934  }
935  estimatedVarianceOfSampleMean.setPrintHorizontally(savedVectorPrintState);
936 
937  V estimatedStdOfSampleMean(estimatedVarianceOfSampleMean);
938  estimatedStdOfSampleMean.cwSqrt();
939  savedVectorPrintState = estimatedStdOfSampleMean.getPrintHorizontally();
940  estimatedStdOfSampleMean.setPrintHorizontally(false);
941  if (m_env.subDisplayFile()) {
942  *m_env.subDisplayFile() << "\nEstimated standard deviation of sample mean for the whole chain '" << m_name << "'"
943  << ", under independence assumption:"
944  << "\n"
945  << estimatedStdOfSampleMean
946  << std::endl;
947  }
948  estimatedStdOfSampleMean.setPrintHorizontally(savedVectorPrintState);
949 
950  V subChainPopulationVariance(m_vectorSpace.zeroVector());
951  this->subPopulationVariance(0,
952  this->subSequenceSize(),
953  subChainMean,
954  subChainPopulationVariance);
955 
956  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
957  if (m_env.subDisplayFile()) {
958  *m_env.subDisplayFile() << "Sub Mean, median, and variances took " << tmpRunTime
959  << " seconds"
960  << std::endl;
961  }
962 
963  if (m_env.subDisplayFile()) {
964  *m_env.subDisplayFile() << "\nSub mean, median, sample std, population std"
965  << std::endl;
966  char line[512];
967  sprintf(line,"%s%4s%s%6s%s%9s%s%9s%s",
968  "Parameter",
969  " ",
970  "Mean",
971  " ",
972  "Median",
973  " ",
974  "SampleStd",
975  " ",
976  "Popul.Std");
977  *m_env.subDisplayFile() << line;
978 
979  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
980  sprintf(line,"\n%8.8s%2s%11.4e%2s%11.4e%2s%11.4e%2s%11.4e",
981  m_vectorSpace.localComponentName(i).c_str(), /*.*/
982  " ",
983  subChainMean[i],
984  " ",
985  subChainMedian[i],
986  " ",
987  std::sqrt(subChainSampleVariance[i]),
988  " ",
989  std::sqrt(subChainPopulationVariance[i]));
990  *m_env.subDisplayFile() << line;
991  }
992  *m_env.subDisplayFile() << std::endl;
993  }
994 
995  if (subMeanPtr ) *subMeanPtr = subChainMean;
996  if (subMedianPtr ) *subMedianPtr = subChainMedian;
997  if (subSampleVarPtr) *subSampleVarPtr = subChainSampleVariance;
998  if (subPopulVarPtr ) *subPopulVarPtr = subChainPopulationVariance;
999 
1000  if (m_env.numSubEnvironments() > 1) {
1001  // Write unified min-max
1002  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1003  V unifiedChainMean(m_vectorSpace.zeroVector());
1004  this->unifiedMeanExtra(0,
1005  this->subSequenceSize(),
1006  unifiedChainMean);
1007 
1008  V unifiedChainMedian(m_vectorSpace.zeroVector());
1009  this->unifiedMedianExtra(0,
1010  this->subSequenceSize(),
1011  unifiedChainMedian);
1012 
1013  V unifiedChainSampleVariance(m_vectorSpace.zeroVector());
1014  this->unifiedSampleVarianceExtra(0,
1015  this->subSequenceSize(),
1016  unifiedChainMean,
1017  unifiedChainSampleVariance);
1018 
1019  V unifiedChainPopulationVariance(m_vectorSpace.zeroVector());
1020  this->unifiedPopulationVariance(0,
1021  this->subSequenceSize(),
1022  unifiedChainMean,
1023  unifiedChainPopulationVariance);
1024 
1025  if (m_env.inter0Rank() == 0) {
1026  if (m_env.subDisplayFile()) {
1027  *m_env.subDisplayFile() << "\nUnif mean, median, sample std, population std"
1028  << std::endl;
1029  char line[512];
1030  sprintf(line,"%s%4s%s%6s%s%9s%s%9s%s",
1031  "Parameter",
1032  " ",
1033  "Mean",
1034  " ",
1035  "Median",
1036  " ",
1037  "SampleStd",
1038  " ",
1039  "Popul.Std");
1040  *m_env.subDisplayFile() << line;
1041 
1042  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1043  sprintf(line,"\n%8.8s%2s%11.4e%2s%11.4e%2s%11.4e%2s%11.4e",
1044  m_vectorSpace.localComponentName(i).c_str(), /*.*/
1045  " ",
1046  unifiedChainMean[i],
1047  " ",
1048  unifiedChainMedian[i],
1049  " ",
1050  std::sqrt(unifiedChainSampleVariance[i]),
1051  " ",
1052  std::sqrt(unifiedChainPopulationVariance[i]));
1053  *m_env.subDisplayFile() << line;
1054  }
1055  *m_env.subDisplayFile() << std::endl;
1056  } // if subDisplayFile
1057  }
1058  }
1059  else {
1060  UQ_FATAL_TEST_MACRO(true,
1061  m_env.worldRank(),
1062  "BaseVectorSequence<V,M>::computeMeanVars()",
1063  "unified min-max writing, parallel vectors not supported yet");
1064  }
1065  } // if numSubEnvs > 1
1066 
1067  if (m_env.subDisplayFile()) {
1068  *m_env.subDisplayFile() << "\nEnded computing mean, sample variance and population variance"
1069  << "\n-----------------------------------------------------"
1070  << std::endl;
1071  }
1072 
1073  return;
1074 }
1075 //--------------------------------------------------------
1076 template<class V, class M>
1077 void
1078 BaseVectorSequence<V,M>::computeAutoCorrViaDef(
1079  const SequenceStatisticalOptions& statisticalOptions,
1080  const std::vector<unsigned int>& initialPosForStatistics,
1081  const std::vector<unsigned int>& lagsForCorrs,
1082  std::ofstream* passedOfs)
1083 {
1084  int iRC = UQ_OK_RC;
1085  struct timeval timevalTmp;
1086  iRC = gettimeofday(&timevalTmp, NULL);
1087  double tmpRunTime = 0.;
1088 
1089  if (m_env.subDisplayFile()) {
1090  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1091  << "\nComputing autocorrelation coefficients (via def)"
1092  << std::endl;
1093  }
1094 
1095  if (statisticalOptions.autoCorrDisplay() && (m_env.subDisplayFile())) {
1096  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeAutoCorrViaDef(): lags for autocorrelation (via def) = ";
1097  for (unsigned int i = 0; i < lagsForCorrs.size(); ++i) {
1098  *m_env.subDisplayFile() << " " << lagsForCorrs[i];
1099  }
1100  *m_env.subDisplayFile() << std::endl;
1101  }
1102 
1103  TwoDArray<V> _2dArrayOfAutoCorrs(initialPosForStatistics.size(),lagsForCorrs.size());
1104  for (unsigned int i = 0; i < _2dArrayOfAutoCorrs.numRows(); ++i) {
1105  for (unsigned int j = 0; j < _2dArrayOfAutoCorrs.numCols(); ++j) {
1106  _2dArrayOfAutoCorrs.setLocation(i,j,new V(m_vectorSpace.zeroVector()) /*.*/);
1107  }
1108  }
1109  //V corrVec(m_vectorSpace.zeroVector());
1110  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1111  unsigned int initialPos = initialPosForStatistics[initialPosId];
1112  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1113  unsigned int lag = lagsForCorrs[lagId];
1114  this->autoCorrViaDef(initialPos,
1115  this->subSequenceSize()-initialPos,
1116  lag,
1117  _2dArrayOfAutoCorrs(initialPosId,lagId));
1118  //_2dArrayOfAutoCorrs(initialPosId,lagId) = corrVec;
1119  }
1120  }
1121 
1122  // It is not practical to compute the variance of sample mean by computing the autocorrelations via definition for each lag
1123  // The code computes the variance of sample mean by computing the autocorrelations via fft, below, in another routine
1124 
1125  if ((statisticalOptions.autoCorrDisplay()) && (m_env.subDisplayFile())) {
1126  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1127  *m_env.subDisplayFile() << "\nComputed autocorrelation coefficients (via def), for subchain beginning at position " << initialPosForStatistics[initialPosId]
1128  << " (each column corresponds to a different lag)"
1129  << std::endl;
1130  char line[512];
1131  sprintf(line,"%s",
1132  "Parameter");
1133  *m_env.subDisplayFile() << line;
1134  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1135  sprintf(line,"%10s%3d",
1136  " ",
1137  lagsForCorrs[lagId]);
1138  *m_env.subDisplayFile() << line;
1139  }
1140 
1141  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1142  sprintf(line,"\n%9.9s",
1143  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1144  *m_env.subDisplayFile() << line;
1145  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1146  sprintf(line,"%2s%11.4e",
1147  " ",
1148  _2dArrayOfAutoCorrs(initialPosId,lagId)[i]);
1149  *m_env.subDisplayFile() << line;
1150  }
1151  }
1152  *m_env.subDisplayFile() << std::endl;
1153  }
1154  }
1155 
1156  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1157  if (m_env.subDisplayFile()) {
1158  *m_env.subDisplayFile() << "Chain autocorrelation (via def) took " << tmpRunTime
1159  << " seconds"
1160  << std::endl;
1161  }
1162 
1163  // Write autocorrelations
1164  if (statisticalOptions.autoCorrWrite() && passedOfs) {
1165  std::ofstream& ofsvar = *passedOfs;
1166  ofsvar << m_name << "_corrViaDefLags_sub" << m_env.subIdString() << " = zeros(" << 1
1167  << "," << lagsForCorrs.size()
1168  << ");"
1169  << std::endl;
1170  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1171  ofsvar << m_name << "_corrViaDefLags_sub" << m_env.subIdString() << "(" << 1
1172  << "," << lagId+1
1173  << ") = " << lagsForCorrs[lagId]
1174  << ";"
1175  << std::endl;
1176  }
1177 
1178  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1179  ofsvar << m_name << "_corrViaDefInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << this->vectorSizeLocal() /*.*/
1180  << "," << lagsForCorrs.size()
1181  << ");"
1182  << std::endl;
1183  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1184  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1185  ofsvar << m_name << "_corrViaDefInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << i+1
1186  << "," << lagId+1
1187  << ") = " << _2dArrayOfAutoCorrs(initialPosId,lagId)[i]
1188  << ";"
1189  << std::endl;
1190  }
1191  }
1192  }
1193  }
1194 
1195  return;
1196 }
1197 // --------------------------------------------------
1198 template<class V, class M>
1199 void
1200 BaseVectorSequence<V,M>::computeAutoCorrViaFFT(
1201  const SequenceStatisticalOptions& statisticalOptions,
1202  const std::vector<unsigned int>& initialPosForStatistics,
1203  const std::vector<unsigned int>& lagsForCorrs,
1204  std::ofstream* passedOfs)
1205 {
1206  int iRC = UQ_OK_RC;
1207  struct timeval timevalTmp;
1208  iRC = gettimeofday(&timevalTmp, NULL);
1209  double tmpRunTime = 0.;
1210 
1211  if (m_env.subDisplayFile()) {
1212  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1213  << "\nComputing autocorrelation coefficients (via fft)"
1214  << std::endl;
1215  }
1216 
1217  if (statisticalOptions.autoCorrDisplay() && (m_env.subDisplayFile())) {
1218  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeAutoCorrViaFFT(): lags for autocorrelation (via fft) = ";
1219  for (unsigned int i = 0; i < lagsForCorrs.size(); ++i) {
1220  *m_env.subDisplayFile() << " " << lagsForCorrs[i];
1221  }
1222  *m_env.subDisplayFile() << std::endl;
1223  }
1224 
1225  TwoDArray<V> _2dArrayOfAutoCorrs(initialPosForStatistics.size(),lagsForCorrs.size());
1226  for (unsigned int i = 0; i < _2dArrayOfAutoCorrs.numRows(); ++i) {
1227  for (unsigned int j = 0; j < _2dArrayOfAutoCorrs.numCols(); ++j) {
1228  _2dArrayOfAutoCorrs.setLocation(i,j,new V(m_vectorSpace.zeroVector()) /*.*/);
1229  }
1230  }
1231  std::vector<V*> corrVecs(lagsForCorrs.size(),NULL);
1232  std::vector<V*> corrSumVecs(initialPosForStatistics.size(),NULL);
1233  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1234  corrSumVecs[initialPosId] = new V(m_vectorSpace.zeroVector()) /*.*/;
1235  unsigned int initialPos = initialPosForStatistics[initialPosId];
1236  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1237  corrVecs[lagId] = new V(m_vectorSpace.zeroVector()) /*.*/;
1238  }
1239  if (m_env.subDisplayFile()) {
1240  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeAutoCorrViaFFT()"
1241  << ": about to call chain.autoCorrViaFft()"
1242  << " with initialPos = " << initialPos
1243  << ", numPos = " << this->subSequenceSize()-initialPos
1244  << ", lagsForCorrs.size() = " << lagsForCorrs.size()
1245  << ", corrVecs.size() = " << corrVecs.size()
1246  << std::endl;
1247  }
1248  this->autoCorrViaFft(initialPos,
1249  this->subSequenceSize()-initialPos, // Use all possible data positions
1250  lagsForCorrs,
1251  corrVecs);
1252  this->autoCorrViaFft(initialPos,
1253  this->subSequenceSize()-initialPos, // Use all possible data positions
1254  (unsigned int) (1.0 * (double) (this->subSequenceSize()-initialPos)), // CHECK
1255  *corrSumVecs[initialPosId]); // Sum of all possibly computable autocorrelations, not only the asked ones in lagsForCorrs
1256  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1257  _2dArrayOfAutoCorrs(initialPosId,lagId) = *(corrVecs[lagId]);
1258  }
1259  }
1260  for (unsigned int j = 0; j < corrVecs.size(); ++j) {
1261  if (corrVecs[j] != NULL) delete corrVecs[j];
1262  }
1263 
1264  if (statisticalOptions.autoCorrDisplay()) {
1265  V subChainMean (m_vectorSpace.zeroVector());
1266  V subChainSampleVariance (m_vectorSpace.zeroVector());
1267  V estimatedVarianceOfSampleMean(m_vectorSpace.zeroVector());
1268  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1269  unsigned int initialPos = initialPosForStatistics[initialPosId];
1270 
1271  this->subMeanExtra(initialPos,
1272  this->subSequenceSize()-initialPos,
1273  subChainMean);
1274 
1275  this->subSampleVarianceExtra(initialPos,
1276  this->subSequenceSize()-initialPos,
1277  subChainMean,
1278  subChainSampleVariance);
1279 
1280  if (m_env.subDisplayFile()) {
1281  *m_env.subDisplayFile() << "\nEstimated variance of sample mean, through autocorrelation (via fft), for subchain beginning at position " << initialPosForStatistics[initialPosId]
1282  << std::endl;
1283  }
1284  estimatedVarianceOfSampleMean.cwSet(-1.); // Yes, '-1' because the autocorrelation at lag 0, which values '+1', is already counted in the sum
1285  estimatedVarianceOfSampleMean += 2.* (*corrSumVecs[initialPosId]);
1286  estimatedVarianceOfSampleMean *= subChainSampleVariance;
1287  estimatedVarianceOfSampleMean /= (double) (this->subSequenceSize() - initialPos);
1288  bool savedVectorPrintState = estimatedVarianceOfSampleMean.getPrintHorizontally();
1289  estimatedVarianceOfSampleMean.setPrintHorizontally(false);
1290  if (m_env.subDisplayFile()) {
1291  *m_env.subDisplayFile() << estimatedVarianceOfSampleMean
1292  << std::endl;
1293  }
1294  estimatedVarianceOfSampleMean.setPrintHorizontally(savedVectorPrintState);
1295 
1296  if (m_env.subDisplayFile()) {
1297  *m_env.subDisplayFile() << "\nComputed autocorrelation coefficients (via fft), for subchain beginning at position " << initialPosForStatistics[initialPosId]
1298  << " (each column corresponds to a different lag)"
1299  << std::endl;
1300 
1301  char line[512];
1302  sprintf(line,"%s",
1303  "Parameter");
1304  *m_env.subDisplayFile() << line;
1305  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1306  sprintf(line,"%10s%3d",
1307  " ",
1308  lagsForCorrs[lagId]);
1309  *m_env.subDisplayFile() << line;
1310  }
1311 
1312  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1313  sprintf(line,"\n%9.9s",
1314  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1315  *m_env.subDisplayFile() << line;
1316  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1317  sprintf(line,"%2s%11.4e",
1318  " ",
1319  _2dArrayOfAutoCorrs(initialPosId,lagId)[i]);
1320  *m_env.subDisplayFile() << line;
1321  }
1322  }
1323  *m_env.subDisplayFile() << std::endl;
1324  }
1325  }
1326  }
1327 
1328  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1329  if (m_env.subDisplayFile()) {
1330  *m_env.subDisplayFile() << "Chain autocorrelation (via fft) took " << tmpRunTime
1331  << " seconds"
1332  << std::endl;
1333  }
1334 
1335  // Write autocorrelations
1336  if (statisticalOptions.autoCorrWrite() && passedOfs) {
1337  std::ofstream& ofsvar = *passedOfs;
1338  ofsvar << m_name << "_corrViaFftLags_sub" << m_env.subIdString() << " = zeros(" << 1
1339  << "," << lagsForCorrs.size()
1340  << ");"
1341  << std::endl;
1342  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1343  ofsvar << m_name << "_corrViaFftLags_sub" << m_env.subIdString() << "(" << 1
1344  << "," << lagId+1
1345  << ") = " << lagsForCorrs[lagId]
1346  << ";"
1347  << std::endl;
1348  }
1349 
1350  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
1351  ofsvar << m_name << "_corrViaFftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << this->vectorSizeLocal() /*.*/
1352  << "," << lagsForCorrs.size()
1353  << ");"
1354  << std::endl;
1355  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1356  for (unsigned int lagId = 0; lagId < lagsForCorrs.size(); lagId++) {
1357  ofsvar << m_name << "_corrViaFftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << i+1
1358  << "," << lagId+1
1359  << ") = " << _2dArrayOfAutoCorrs(initialPosId,lagId)[i]
1360  << ";"
1361  << std::endl;
1362  }
1363  }
1364  }
1365  }
1366 
1367  return;
1368 }
1369 // --------------------------------------------------
1370 template<class V, class M>
1371 void
1372 BaseVectorSequence<V,M>::computeHistCdfstaccKde( // Use the whole chain
1373  const SequenceStatisticalOptions& statisticalOptions,
1374  std::ofstream* passedOfs)
1375 {
1376  if (m_env.subDisplayFile()) {
1377  *m_env.subDisplayFile() << "\n"
1378  << "\n-----------------------------------------------------"
1379  << "\n Computing histogram and/or cdf stacc and/or Kde for chain '" << m_name << "' ..."
1380  << "\n-----------------------------------------------------"
1381  << "\n"
1382  << std::endl;
1383  }
1384 
1385  int iRC = UQ_OK_RC;
1386  struct timeval timevalTmp;
1387 
1388  //****************************************************
1389  // Compute MIN and MAX: for histograms and Kde
1390  //****************************************************
1391  double tmpRunTime = 0.;
1392  iRC = gettimeofday(&timevalTmp, NULL);
1393  if (m_env.subDisplayFile()) {
1394  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1395  << "\nComputing min and max for histograms and Kde"
1396  << std::endl;
1397  }
1398 
1399  V statsMinPositions(m_vectorSpace.zeroVector());
1400  V statsMaxPositions(m_vectorSpace.zeroVector());
1401  this->subMinMaxExtra(0, // Use the whole chain
1402  this->subSequenceSize(),
1403  statsMinPositions,
1404  statsMaxPositions);
1405 
1406  if (m_env.subDisplayFile()) {
1407  *m_env.subDisplayFile() << "\nComputed min values and max values for chain '" << m_name << "'"
1408  << std::endl;
1409 
1410  char line[512];
1411  sprintf(line,"%s",
1412  "Parameter");
1413  *m_env.subDisplayFile() << line;
1414 
1415  sprintf(line,"%9s%s%9s%s",
1416  " ",
1417  "min",
1418  " ",
1419  "max");
1420  *m_env.subDisplayFile() << line;
1421 
1422  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1423  sprintf(line,"\n%8.8s",
1424  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1425  *m_env.subDisplayFile() << line;
1426 
1427  sprintf(line,"%2s%11.4e%2s%11.4e",
1428  " ",
1429  statsMinPositions[i],
1430  " ",
1431  statsMaxPositions[i]);
1432  *m_env.subDisplayFile() << line;
1433  }
1434  *m_env.subDisplayFile() << std::endl;
1435  }
1436 
1437  V unifiedStatsMinPositions(statsMinPositions);
1438  V unifiedStatsMaxPositions(statsMaxPositions);
1439  if (m_env.numSubEnvironments() > 1) {
1440  // Compute unified min-max
1441  this->unifiedMinMaxExtra(0, // Use the whole chain
1442  this->subSequenceSize(),
1443  unifiedStatsMinPositions,
1444  unifiedStatsMaxPositions);
1445 
1446  // Write unified min-max
1447  if (m_env.subDisplayFile()) {
1448  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1449  if (m_env.inter0Rank() == 0) {
1450  *m_env.subDisplayFile() << "\nComputed unified min values and max values for chain '" << m_name << "'"
1451  << std::endl;
1452 
1453  char line[512];
1454  sprintf(line,"%s",
1455  "Parameter");
1456  *m_env.subDisplayFile() << line;
1457 
1458  sprintf(line,"%9s%s%9s%s",
1459  " ",
1460  "min",
1461  " ",
1462  "max");
1463  *m_env.subDisplayFile() << line;
1464 
1465  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1466  sprintf(line,"\n%8.8s",
1467  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1468  *m_env.subDisplayFile() << line;
1469 
1470  sprintf(line,"%2s%11.4e%2s%11.4e",
1471  " ",
1472  unifiedStatsMinPositions[i],
1473  " ",
1474  unifiedStatsMaxPositions[i]);
1475  *m_env.subDisplayFile() << line;
1476  }
1477  *m_env.subDisplayFile() << std::endl;
1478  }
1479  }
1480  else {
1481  UQ_FATAL_TEST_MACRO(true,
1482  m_env.worldRank(),
1483  "BaseVectorSequence<V,M>::computeHistCdfstaccKde()",
1484  "unified min-max writing, parallel vectors not supported yet");
1485  }
1486  } // if subDisplayFile
1487  } // if numSubEnvs > 1
1488 
1489  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1490  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1491  if (m_env.subDisplayFile()) {
1492  *m_env.subDisplayFile() << "Chain min and max took " << tmpRunTime
1493  << " seconds"
1494  << std::endl;
1495  }
1496 
1497  //****************************************************
1498  // Compute histograms
1499  //****************************************************
1500 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
1501  if ((statisticalOptions.histCompute() ) &&
1502  (statisticalOptions.histNumInternalBins() > 0)) {
1503  tmpRunTime = 0.;
1504  iRC = gettimeofday(&timevalTmp, NULL);
1505  if (m_env.subDisplayFile()) {
1506  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1507  << "\nComputing histograms"
1508  << std::endl;
1509  }
1510 
1511  std::string subCoreName_HistCenters((std::string)( "_HistCenters_sub")+m_env.subIdString());
1512  std::string uniCoreName_HistCenters((std::string)("_unifHistCenters_sub")+m_env.subIdString());
1513  if (m_env.numSubEnvironments() == 1) subCoreName_HistCenters = uniCoreName_HistCenters;
1514 
1515  std::string subCoreName_HistQuantts((std::string)( "_HistQuantts_sub")+m_env.subIdString());
1516  std::string uniCoreName_HistQuantts((std::string)("_unifHistQuantts_sub")+m_env.subIdString());
1517  if (m_env.numSubEnvironments() == 1) subCoreName_HistQuantts = uniCoreName_HistQuantts;
1518 
1519  for (unsigned int i = 0; i < statsMaxPositions.sizeLocal(); ++i) {
1520  statsMaxPositions[i] *= (1. + 1.e-15);
1521  }
1522 
1523  // Compute histograms
1524  std::vector<V*> histCentersForAllBins(statisticalOptions.histNumInternalBins()+2,NULL); // IMPORTANT: +2
1525  std::vector<V*> histQuanttsForAllBins(statisticalOptions.histNumInternalBins()+2,NULL); // IMPORTANT: +2
1526  this->subHistogram(0, // Use the whole chain
1527  statsMinPositions,
1528  statsMaxPositions,
1529  histCentersForAllBins,
1530  histQuanttsForAllBins);
1531 
1532  // Write histograms
1533  if (passedOfs) {
1534  std::ofstream& ofsvar = *passedOfs;
1535  ofsvar << m_name << subCoreName_HistCenters << " = zeros(" << this->vectorSizeLocal() /*.*/
1536  << "," << histCentersForAllBins.size()
1537  << ");"
1538  << std::endl;
1539  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1540  for (unsigned int j = 0; j < histCentersForAllBins.size(); ++j) {
1541  ofsvar << m_name << subCoreName_HistCenters << "(" << i+1
1542  << "," << j+1
1543  << ") = " << (*(histCentersForAllBins[j]))[i]
1544  << ";"
1545  << std::endl;
1546  }
1547  }
1548 
1549  ofsvar << m_name << subCoreName_HistQuantts << " = zeros(" << this->vectorSizeLocal() /*.*/
1550  << "," << histQuanttsForAllBins.size()
1551  << ");"
1552  << std::endl;
1553  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1554  for (unsigned int j = 0; j < histQuanttsForAllBins.size(); ++j) {
1555  ofsvar << m_name << subCoreName_HistQuantts << "(" << i+1
1556  << "," << j+1
1557  << ") = " << (*(histQuanttsForAllBins[j]))[i]
1558  << ";"
1559  << std::endl;
1560  }
1561  }
1562  }
1563 
1564  for (unsigned int i = 0; i < histQuanttsForAllBins.size(); ++i) {
1565  if (histQuanttsForAllBins[i] != NULL) delete histQuanttsForAllBins[i];
1566  }
1567  for (unsigned int i = 0; i < histCentersForAllBins.size(); ++i) {
1568  if (histCentersForAllBins[i] != NULL) delete histCentersForAllBins[i];
1569  }
1570 
1571  std::vector<V*> unifiedHistCentersForAllBins(statisticalOptions.histNumInternalBins()+2,NULL); // IMPORTANT: +2
1572  std::vector<V*> unifiedHistQuanttsForAllBins(statisticalOptions.histNumInternalBins()+2,NULL); // IMPORTANT: +2
1573  if (m_env.numSubEnvironments() > 1) {
1574  // Compute unified histogram
1575  this->unifiedHistogram(0, // Use the whole chain
1576  unifiedStatsMinPositions,
1577  unifiedStatsMaxPositions,
1578  unifiedHistCentersForAllBins,
1579  unifiedHistQuanttsForAllBins);
1580 
1581  // Write unified histogram
1582  if (passedOfs) {
1583  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1584  if (m_env.inter0Rank() == 0) {
1585  std::ofstream& ofsvar = *passedOfs;
1586  ofsvar << m_name << uniCoreName_HistCenters << " = zeros(" << this->vectorSizeLocal() /*.*/
1587  << "," << unifiedHistCentersForAllBins.size()
1588  << ");"
1589  << std::endl;
1590  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1591  for (unsigned int j = 0; j < unifiedHistCentersForAllBins.size(); ++j) {
1592  ofsvar << m_name << uniCoreName_HistCenters << "(" << i+1
1593  << "," << j+1
1594  << ") = " << (*(unifiedHistCentersForAllBins[j]))[i]
1595  << ";"
1596  << std::endl;
1597  }
1598  }
1599 
1600  ofsvar << m_name << uniCoreName_HistQuantts << " = zeros(" << this->vectorSizeLocal() /*.*/
1601  << "," << unifiedHistQuanttsForAllBins.size()
1602  << ");"
1603  << std::endl;
1604  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1605  for (unsigned int j = 0; j < unifiedHistQuanttsForAllBins.size(); ++j) {
1606  ofsvar << m_name << uniCoreName_HistQuantts << "(" << i+1
1607  << "," << j+1
1608  << ") = " << (*(unifiedHistQuanttsForAllBins[j]))[i]
1609  << ";"
1610  << std::endl;
1611  }
1612  }
1613  }
1614  }
1615  else {
1616  UQ_FATAL_TEST_MACRO(true,
1617  m_env.worldRank(),
1618  "BaseVectorSequence<V,M>::computeHistCdfstaccKde()",
1619  "unified histogram writing, parallel vectors not supported yet");
1620  }
1621  } // if passedOfs
1622 
1623  for (unsigned int i = 0; i < unifiedHistQuanttsForAllBins.size(); ++i) {
1624  if (unifiedHistQuanttsForAllBins[i] != NULL) delete unifiedHistQuanttsForAllBins[i];
1625  }
1626  for (unsigned int i = 0; i < unifiedHistCentersForAllBins.size(); ++i) {
1627  if (unifiedHistCentersForAllBins[i] != NULL) delete unifiedHistCentersForAllBins[i];
1628  }
1629  } // if numSubEnvs > 1
1630 
1631  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1632  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1633  if (m_env.subDisplayFile()) {
1634  *m_env.subDisplayFile() << "Chain histograms took " << tmpRunTime
1635  << " seconds"
1636  << std::endl;
1637  }
1638  }
1639 #endif
1640 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
1641  //****************************************************
1642  // Compute cdf statistical accuracy
1643  //****************************************************
1644  if ((statisticalOptions.cdfStaccCompute() ) &&
1645  (statisticalOptions.cdfStaccNumEvalPositions() > 0)) {
1646  tmpRunTime = 0.;
1647  iRC = gettimeofday(&timevalTmp, NULL);
1648  if (m_env.subDisplayFile()) {
1649  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1650  << "\nComputing cdf statistical accuracy"
1651  << std::endl;
1652  }
1653 
1654  std::vector<V*> cdfStaccEvalPositions(statisticalOptions.cdfStaccNumEvalPositions(),NULL);
1655  MiscComputePositionsBetweenMinMax(statsMinPositions,
1656  statsMaxPositions,
1657  cdfStaccEvalPositions);
1658 
1659  std::vector<V*> cdfStaccValues(statisticalOptions.cdfStaccNumEvalPositions(),NULL);
1660  this->subCdfStacc(0, // Use the whole chain
1661  cdfStaccEvalPositions,
1662  cdfStaccValues);
1663 
1664  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1665  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1666  if (m_env.subDisplayFile()) {
1667  *m_env.subDisplayFile() << "Chain cdf statistical accuracy took " << tmpRunTime
1668  << " seconds"
1669  << std::endl;
1670  }
1671  }
1672 #endif
1673  //****************************************************
1674  // Compute estimations of probability densities
1675  //****************************************************
1676  if ((statisticalOptions.kdeCompute() ) &&
1677  (statisticalOptions.kdeNumEvalPositions() > 0)) {
1678  tmpRunTime = 0.;
1679  iRC = gettimeofday(&timevalTmp, NULL);
1680  if (m_env.subDisplayFile()) {
1681  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1682  << "\nComputing Kde"
1683  << std::endl;
1684  }
1685 
1686  std::string subCoreName_GaussianKdePositions((std::string)( "_GkdePosits_sub")+m_env.subIdString());
1687  std::string uniCoreName_GaussianKdePositions((std::string)("_unifGkdePosits_sub")+m_env.subIdString());
1688  if (m_env.numSubEnvironments() == 1) subCoreName_GaussianKdePositions = uniCoreName_GaussianKdePositions; // avoid temporarily (see '< -1' below)
1689 
1690  std::string subCoreName_GaussianKdeScaleVec ((std::string)( "_GkdeScalev_sub")+m_env.subIdString());
1691  std::string uniCoreName_GaussianKdeScaleVec ((std::string)("_unifGkdeScalev_sub")+m_env.subIdString());
1692  if (m_env.numSubEnvironments() == 1) subCoreName_GaussianKdeScaleVec = uniCoreName_GaussianKdeScaleVec; // avoid temporarily (see '< -1' below)
1693 
1694  std::string subCoreName_GaussianKdeValues ((std::string)( "_GkdeValues_sub")+m_env.subIdString());
1695  std::string uniCoreName_GaussianKdeValues ((std::string)("_unifGkdeValues_sub")+m_env.subIdString());
1696  if (m_env.numSubEnvironments() == 1) subCoreName_GaussianKdeValues = uniCoreName_GaussianKdeValues; // avoid temporarily (see '< -1' below)
1697 
1698  V iqrVec(m_vectorSpace.zeroVector());
1699  this->subInterQuantileRange(0, // Use the whole chain
1700  iqrVec);
1701 
1702  V gaussianKdeScaleVec(m_vectorSpace.zeroVector());
1703  this->subScalesForKde(0, // Use the whole chain
1704  iqrVec,
1705  1,
1706  gaussianKdeScaleVec);
1707 
1708  std::vector<V*> kdeEvalPositions(statisticalOptions.kdeNumEvalPositions(),NULL);
1709  MiscComputePositionsBetweenMinMax(statsMinPositions,
1710  statsMaxPositions,
1711  kdeEvalPositions);
1712 
1713  std::vector<V*> gaussianKdeDensities(statisticalOptions.kdeNumEvalPositions(),NULL);
1714  this->subGaussian1dKde(0, // Use the whole chain
1715  gaussianKdeScaleVec,
1716  kdeEvalPositions,
1717  gaussianKdeDensities);
1718 
1719  // Write iqr
1720  if (m_env.subDisplayFile()) {
1721  *m_env.subDisplayFile() << "\nComputed inter quantile ranges for chain '" << m_name << "'"
1722  << std::endl;
1723 
1724  char line[512];
1725  sprintf(line,"%s",
1726  "Parameter");
1727  *m_env.subDisplayFile() << line;
1728 
1729  sprintf(line,"%9s%s",
1730  " ",
1731  "iqr");
1732  *m_env.subDisplayFile() << line;
1733 
1734  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1735  sprintf(line,"\n%8.8s",
1736  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1737  *m_env.subDisplayFile() << line;
1738 
1739  sprintf(line,"%2s%11.4e",
1740  " ",
1741  iqrVec[i]);
1742  *m_env.subDisplayFile() << line;
1743  }
1744  *m_env.subDisplayFile() << std::endl;
1745  }
1746 
1747  // Write Kde
1748  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() > 10)) { // output debug
1749  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeHistCdfstaccKde()"
1750  << ", for chain '" << m_name << "'"
1751  << ", about to write sub kde to ofstream with pointer = " << passedOfs
1752  << std::endl;
1753  }
1754  if (passedOfs) {
1755  std::ofstream& ofsvar = *passedOfs;
1756  ofsvar << m_name << subCoreName_GaussianKdePositions << " = zeros(" << this->vectorSizeLocal() /*.*/
1757  << "," << kdeEvalPositions.size()
1758  << ");"
1759  << std::endl;
1760  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1761  for (unsigned int j = 0; j < kdeEvalPositions.size(); ++j) {
1762  ofsvar << m_name << subCoreName_GaussianKdePositions << "(" << i+1
1763  << "," << j+1
1764  << ") = " << (*(kdeEvalPositions[j]))[i]
1765  << ";"
1766  << std::endl;
1767  }
1768  }
1769 
1770  ofsvar << m_name << subCoreName_GaussianKdeScaleVec << " = zeros(" << this->vectorSizeLocal() /*.*/
1771  << ");"
1772  << std::endl;
1773  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1774  ofsvar << m_name << subCoreName_GaussianKdeScaleVec << "(" << i+1
1775  << ") = " << gaussianKdeScaleVec[i]
1776  << ";"
1777  << std::endl;
1778  }
1779 
1780  ofsvar << m_name << subCoreName_GaussianKdeValues << " = zeros(" << this->vectorSizeLocal() /*.*/
1781  << "," << gaussianKdeDensities.size()
1782  << ");"
1783  << std::endl;
1784  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1785  for (unsigned int j = 0; j < gaussianKdeDensities.size(); ++j) {
1786  ofsvar << m_name << subCoreName_GaussianKdeValues << "(" << i+1
1787  << "," << j+1
1788  << ") = " << (*(gaussianKdeDensities[j]))[i]
1789  << ";"
1790  << std::endl;
1791  }
1792  }
1793  }
1794 
1795  for (unsigned int i = 0; i < gaussianKdeDensities.size(); ++i) {
1796  if (gaussianKdeDensities[i] != NULL) delete gaussianKdeDensities[i];
1797  }
1798  for (unsigned int i = 0; i < kdeEvalPositions.size(); ++i) {
1799  if (kdeEvalPositions[i] != NULL) delete kdeEvalPositions[i];
1800  }
1801 
1802  if ((int) m_env.numSubEnvironments() > 1) { // avoid code temporarily
1803  // Compute unified Kde
1804  V unifiedIqrVec(m_vectorSpace.zeroVector());
1805  this->unifiedInterQuantileRange(0, // Use the whole chain
1806  unifiedIqrVec);
1807  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1808 
1809  V unifiedGaussianKdeScaleVec(m_vectorSpace.zeroVector());
1810  this->unifiedScalesForKde(0, // Use the whole chain
1811  unifiedIqrVec,
1812  1,
1813  unifiedGaussianKdeScaleVec);
1814  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1815 
1816  std::vector<V*> unifiedKdeEvalPositions(statisticalOptions.kdeNumEvalPositions(),NULL);
1817  MiscComputePositionsBetweenMinMax(unifiedStatsMinPositions,
1818  unifiedStatsMaxPositions,
1819  unifiedKdeEvalPositions);
1820 
1821  std::vector<V*> unifiedGaussianKdeDensities(statisticalOptions.kdeNumEvalPositions(),NULL);
1822  this->unifiedGaussian1dKde(0, // Use the whole chain
1823  unifiedGaussianKdeScaleVec,
1824  unifiedKdeEvalPositions,
1825  unifiedGaussianKdeDensities);
1826  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1827 
1828  // Write unified iqr
1829  if (m_env.subDisplayFile()) {
1830  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1831  if (m_env.inter0Rank() == 0) {
1832  *m_env.subDisplayFile() << "\nComputed unified inter quantile ranges for chain '" << m_name << "'"
1833  << std::endl;
1834 
1835  char line[512];
1836  sprintf(line,"%s",
1837  "Parameter");
1838  *m_env.subDisplayFile() << line;
1839 
1840  sprintf(line,"%9s%s",
1841  " ",
1842  "iqr");
1843  *m_env.subDisplayFile() << line;
1844 
1845  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1846  sprintf(line,"\n%8.8s",
1847  m_vectorSpace.localComponentName(i).c_str() /*.*/);
1848  *m_env.subDisplayFile() << line;
1849 
1850  sprintf(line,"%2s%11.4e",
1851  " ",
1852  unifiedIqrVec[i]);
1853  *m_env.subDisplayFile() << line;
1854  }
1855  *m_env.subDisplayFile() << std::endl;
1856  }
1857  }
1858  else {
1859  UQ_FATAL_TEST_MACRO(true,
1860  m_env.worldRank(),
1861  "BaseVectorSequence<V,M>::computeHistCdfstaccKde()",
1862  "unified iqr writing, parallel vectors not supported yet");
1863  }
1864  }
1865 
1866  // Write unified Kde
1867  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() > 10)) { // output debug
1868  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeHistCdfstaccKde()"
1869  << ", for chain '" << m_name << "'"
1870  << ", about to write unified kde to ofstream with pointer = " << passedOfs
1871  << std::endl;
1872  }
1873  if (passedOfs) {
1874  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1875  if (m_env.inter0Rank() == 0) {
1876  std::ofstream& ofsvar = *passedOfs;
1877  ofsvar << m_name << uniCoreName_GaussianKdePositions << " = zeros(" << this->vectorSizeLocal() /*.*/
1878  << "," << unifiedKdeEvalPositions.size()
1879  << ");"
1880  << std::endl;
1881  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() > 10)) { // output debug
1882  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeHistCdfstaccKde()"
1883  << ", for chain '" << m_name << "'"
1884  << ": just wrote '... = zeros(.,.);' line to output file, which has pointer = " << passedOfs
1885  << std::endl;
1886  }
1887  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1888  for (unsigned int j = 0; j < unifiedKdeEvalPositions.size(); ++j) {
1889  ofsvar << m_name << uniCoreName_GaussianKdePositions << "(" << i+1
1890  << "," << j+1
1891  << ") = " << (*(unifiedKdeEvalPositions[j]))[i]
1892  << ";"
1893  << std::endl;
1894  }
1895  }
1896 
1897  ofsvar << m_name << uniCoreName_GaussianKdeScaleVec << " = zeros(" << this->vectorSizeLocal() /*.*/
1898  << ");"
1899  << std::endl;
1900  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1901  ofsvar << m_name << uniCoreName_GaussianKdeScaleVec << "(" << i+1
1902  << ") = " << unifiedGaussianKdeScaleVec[i]
1903  << ";"
1904  << std::endl;
1905  }
1906 
1907  ofsvar << m_name << uniCoreName_GaussianKdeValues << " = zeros(" << this->vectorSizeLocal() /*.*/
1908  << "," << unifiedGaussianKdeDensities.size()
1909  << ");"
1910  << std::endl;
1911  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
1912  for (unsigned int j = 0; j < unifiedGaussianKdeDensities.size(); ++j) {
1913  ofsvar << m_name << uniCoreName_GaussianKdeValues << "(" << i+1
1914  << "," << j+1
1915  << ") = " << (*(unifiedGaussianKdeDensities[j]))[i]
1916  << ";"
1917  << std::endl;
1918  }
1919  }
1920  }
1921  }
1922  else {
1923  UQ_FATAL_TEST_MACRO(true,
1924  m_env.worldRank(),
1925  "BaseVectorSequence<V,M>::computeHistCdfstaccKde()",
1926  "unified Kde writing, parallel vectors not supported yet");
1927  }
1928  }
1929 
1930  for (unsigned int i = 0; i < unifiedGaussianKdeDensities.size(); ++i) {
1931  if (unifiedGaussianKdeDensities[i] != NULL) delete unifiedGaussianKdeDensities[i];
1932  }
1933  for (unsigned int i = 0; i < unifiedKdeEvalPositions.size(); ++i) {
1934  if (unifiedKdeEvalPositions[i] != NULL) delete unifiedKdeEvalPositions[i];
1935  }
1936  }
1937 
1938  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
1939  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
1940  if (m_env.subDisplayFile()) {
1941  *m_env.subDisplayFile() << "Chain Kde took " << tmpRunTime
1942  << " seconds"
1943  << std::endl;
1944  }
1945  }
1946 
1947  if (m_env.subDisplayFile()) {
1948  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
1949  << "\n Finished computing histogram and/or cdf stacc and/or Kde for chain '" << m_name << "'"
1950  << "\n-----------------------------------------------------"
1951  << "\n"
1952  << std::endl;
1953  }
1954 
1955  return;
1956 }
1957 // --------------------------------------------------
1958 template<class V, class M>
1959 void
1960 BaseVectorSequence<V,M>::computeCovCorrMatrices( // Use the whole chain
1961  const SequenceStatisticalOptions& statisticalOptions,
1962  std::ofstream* passedOfs)
1963 {
1964  if (m_env.subDisplayFile()) {
1965  *m_env.subDisplayFile() << "\n"
1966  << "\n-----------------------------------------------------"
1967  << "\n Computing covariance and correlation matrices for chain '" << m_name << "' ..."
1968  << "\n-----------------------------------------------------"
1969  << "\n"
1970  << std::endl;
1971  }
1972 
1973  //int iRC = UQ_OK_RC;
1974  //struct timeval timevalTmp;
1975  M* covarianceMatrix = new M(m_env,
1976  m_vectorSpace.map(), // number of rows
1977  m_vectorSpace.dimGlobal()); // number of cols
1978  M* correlationMatrix = new M(m_env,
1979  m_vectorSpace.map(), // number of rows
1980  m_vectorSpace.dimGlobal()); // number of cols
1981 
1983  *this,
1984  this->subSequenceSize(),
1985  *covarianceMatrix,
1986  *correlationMatrix);
1987 
1988  if (m_env.subDisplayFile()) {
1989  if (m_vectorSpace.numOfProcsForStorage() == 1) {
1990  // Only unified covariance matrix is written. And only one processor writes it.
1991  if (m_env.inter0Rank() == 0) {
1992  *m_env.subDisplayFile() << "\nBaseVectorSequence<V,M>::computeCovCorrMatrices"
1993  << ", chain " << m_name
1994  << ": contents of covariance matrix are\n" << *covarianceMatrix
1995  << std::endl;
1996 
1997  *m_env.subDisplayFile() << "\nBaseVectorSequence<V,M>::computeCovCorrMatrices"
1998  << ", chain " << m_name
1999  << ": contents of correlation matrix are\n" << *correlationMatrix
2000  << std::endl;
2001  }
2002  }
2003  else {
2004  UQ_FATAL_TEST_MACRO(true,
2005  m_env.worldRank(),
2006  "BaseVectorSequence<V,M>::computeCovCorrMatrices()",
2007  "parallel vectors not supported yet");
2008  }
2009  }
2010 
2011  delete correlationMatrix;
2012  delete covarianceMatrix;
2013 
2014  if (m_env.subDisplayFile()) {
2015  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2016  << "\n Finished computing covariance and correlation matrices for chain '" << m_name << "'"
2017  << "\n-----------------------------------------------------"
2018  << "\n"
2019  << std::endl;
2020  }
2021 
2022  return;
2023 }
2024 #endif // #ifdef QUESO_USES_SEQUENCE_STATISTICAL_OPTIONS
2025 
2026 // --------------------------------------------------
2027 // --------------------------------------------------
2028 // --------------------------------------------------
2029 
2030 #ifdef UQ_CODE_HAS_MONITORS
2031 template<class V, class M>
2032 void
2033 BaseVectorSequence<V,M>::computeMeanEvolution(
2034  const SequenceStatisticalOptions& statisticalOptions,
2035  std::ofstream* passedOfs)
2036 {
2037  int iRC = UQ_OK_RC;
2038  struct timeval timevalTmp;
2039  iRC = gettimeofday(&timevalTmp, NULL);
2040  double tmpRunTime = 0.;
2041 
2042  if (m_env.subDisplayFile()) {
2043  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2044  << "\nComputing mean evolution"
2045  << std::endl;
2046  }
2047 
2048  unsigned int monitorPeriod = statisticalOptions.meanMonitorPeriod();
2049  unsigned int iMin = 0;
2050  unsigned int iMax = (unsigned int) ( ((double) this->subSequenceSize())/((double) monitorPeriod) );
2051 
2052  if (monitorPeriod == 1) {
2053  iMin = 1;
2054  }
2055 
2056  if (m_env.subDisplayFile()) {
2057  *m_env.subDisplayFile() << " Sub sequence size = " << this->subSequenceSize()
2058  << "\n Monitor period = " << monitorPeriod
2059  << "\n Number of monitoring positions = " << iMax
2060  << std::endl;
2061  }
2062 
2063  this->subMeanMonitorAlloc(iMax);
2064  if (m_env.numSubEnvironments() > 1) {
2065  this->subMeanInter0MonitorAlloc(iMax);
2066  this->unifiedMeanMonitorAlloc(iMax);
2067  }
2068 
2069  for (unsigned int i = iMin; i < iMax; ++i) {
2070  unsigned int currentMonitoredFinalPosition = (i+1)*monitorPeriod - 1; // Yes, '-1'
2071  V subMeanVec (m_vectorSpace.zeroVector());
2072  V subMeanCltStd(m_vectorSpace.zeroVector());
2073  this->subMeanMonitorRun(currentMonitoredFinalPosition,
2074  subMeanVec,
2075  subMeanCltStd);
2076  this->subMeanMonitorStore(i,
2077  currentMonitoredFinalPosition,
2078  subMeanVec,
2079  subMeanCltStd);
2080 
2081  if (m_env.numSubEnvironments() > 1) {
2082  V subMeanInter0Mean (m_vectorSpace.zeroVector());
2083  V subMeanInter0Clt95 (m_vectorSpace.zeroVector());
2084  V subMeanInter0Empirical90(m_vectorSpace.zeroVector());
2085  V subMeanInter0Min (m_vectorSpace.zeroVector());
2086  V subMeanInter0Max (m_vectorSpace.zeroVector());
2087  this->subMeanInter0MonitorRun(currentMonitoredFinalPosition,
2088  subMeanInter0Mean,
2089  subMeanInter0Clt95,
2090  subMeanInter0Empirical90,
2091  subMeanInter0Min,
2092  subMeanInter0Max);
2093  this->subMeanInter0MonitorStore(i,
2094  currentMonitoredFinalPosition,
2095  subMeanInter0Mean,
2096  subMeanInter0Clt95,
2097  subMeanInter0Empirical90,
2098  subMeanInter0Min,
2099  subMeanInter0Max);
2100 
2101  V unifiedMeanVec (m_vectorSpace.zeroVector());
2102  V unifiedMeanCltStd(m_vectorSpace.zeroVector());
2103  this->unifiedMeanMonitorRun(currentMonitoredFinalPosition,
2104  unifiedMeanVec,
2105  unifiedMeanCltStd);
2106  this->unifiedMeanMonitorStore(i,
2107  currentMonitoredFinalPosition,
2108  unifiedMeanVec,
2109  unifiedMeanCltStd);
2110  }
2111  }
2112 
2113  if (passedOfs) {
2114  this->subMeanMonitorWrite(*passedOfs);
2115  if (m_env.numSubEnvironments() > 1) {
2116  this->subMeanInter0MonitorWrite(*passedOfs);
2117  this->unifiedMeanMonitorWrite(*passedOfs); // Yes, call 'unified' even though not all nodes might have 'passedOfs != NULL' ????????????? Ernesto
2118  }
2119  }
2120 
2121  this->subMeanMonitorFree();
2122  if (m_env.numSubEnvironments() > 1) {
2123  this->subMeanInter0MonitorFree();
2124  this->unifiedMeanMonitorFree();
2125  }
2126 
2127  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2128  if (m_env.subDisplayFile()) {
2129  *m_env.subDisplayFile() << "Mean evolution took " << tmpRunTime
2130  << " seconds"
2131  << std::endl;
2132  }
2133 
2134  return;
2135 }
2136 #endif //#ifdef UQ_CODE_HAS_MONITORS
2137 
2138 // --------------------------------------------------
2139 // --------------------------------------------------
2140 // --------------------------------------------------
2141 
2142 
2143 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
2144 template<class V, class M>
2145 void
2146 BaseVectorSequence<V,M>::computeBMM(
2147  const SequenceStatisticalOptions& statisticalOptions,
2148  const std::vector<unsigned int>& initialPosForStatistics,
2149  std::ofstream* passedOfs)
2150 {
2151  int iRC = UQ_OK_RC;
2152  struct timeval timevalTmp;
2153  iRC = gettimeofday(&timevalTmp, NULL);
2154  double tmpRunTime = 0.;
2155 
2156  if (m_env.subDisplayFile()) {
2157  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2158  << "\nComputing variance of sample mean through BMM"
2159  << std::endl;
2160  }
2161 
2162  if (m_env.subDisplayFile()) {
2163  *m_env.subDisplayFile() << "In BaseVectorSequence<V,M>::computeBMM(): lengths for batchs in BMM =";
2164  for (unsigned int i = 0; i < statisticalOptions.bmmLengths().size(); ++i) {
2165  *m_env.subDisplayFile() << " " << statisticalOptions.bmmLengths()[i];
2166  }
2167  *m_env.subDisplayFile() << std::endl;
2168  }
2169 
2170  TwoDArray<V> _2dArrayOfBMM(initialPosForStatistics.size(),statisticalOptions.bmmLengths().size());
2171  for (unsigned int i = 0; i < _2dArrayOfBMM.numRows(); ++i) {
2172  for (unsigned int j = 0; j < _2dArrayOfBMM.numCols(); ++j) {
2173  _2dArrayOfBMM.setLocation(i,j,new V(m_vectorSpace.zeroVector()) /*.*/);
2174  }
2175  }
2176  V bmmVec(m_vectorSpace.zeroVector());
2177  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2178  unsigned int initialPos = initialPosForStatistics[initialPosId];
2179  for (unsigned int batchLengthId = 0; batchLengthId < statisticalOptions.bmmLengths().size(); batchLengthId++) {
2180  unsigned int batchLength = statisticalOptions.bmmLengths()[batchLengthId];
2181  this->bmm(initialPos,
2182  batchLength,
2183  bmmVec);
2184  _2dArrayOfBMM(initialPosId,batchLengthId) = bmmVec;
2185  }
2186  }
2187 
2188  if (m_env.subDisplayFile()) {
2189  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2190  *m_env.subDisplayFile() << "\nEstimated variance of sample mean, through batch means method, for subchain beginning at position " << initialPosForStatistics[initialPosId]
2191  << " (each column corresponds to a batch length)"
2192  << std::endl;
2193 
2194  char line[512];
2195  sprintf(line,"%s",
2196  "Parameter");
2197  *m_env.subDisplayFile() << line;
2198  for (unsigned int batchLengthId = 0; batchLengthId < statisticalOptions.bmmLengths().size(); batchLengthId++) {
2199  sprintf(line,"%10s%3d",
2200  " ",
2201  statisticalOptions.bmmLengths()[batchLengthId]);
2202  *m_env.subDisplayFile() << line;
2203  }
2204 
2205  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2206  sprintf(line,"\n%9.9s",
2207  m_vectorSpace.localComponentName(i).c_str() /*.*/);
2208  *m_env.subDisplayFile() << line;
2209  for (unsigned int batchLengthId = 0; batchLengthId < statisticalOptions.bmmLengths().size(); batchLengthId++) {
2210  sprintf(line,"%2s%11.4e",
2211  " ",
2212  _2dArrayOfBMM(initialPosId,batchLengthId)[i]);
2213  *m_env.subDisplayFile() << line;
2214  }
2215  }
2216  *m_env.subDisplayFile() << std::endl;
2217  }
2218  }
2219 
2220  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2221  if (m_env.subDisplayFile()) {
2222  *m_env.subDisplayFile() << "Chain BMM took " << tmpRunTime
2223  << " seconds"
2224  << std::endl;
2225  }
2226 
2227  return;
2228 }
2229 // --------------------------------------------------
2230 template<class V, class M>
2231 void
2232 BaseVectorSequence<V,M>::computeFFT(
2233  const SequenceStatisticalOptions& statisticalOptions,
2234  const std::vector<unsigned int>& initialPosForStatistics,
2235  std::ofstream* passedOfs)
2236 {
2237  int iRC = UQ_OK_RC;
2238  struct timeval timevalTmp;
2239  iRC = gettimeofday(&timevalTmp, NULL);
2240  double tmpRunTime = 0.;
2241 
2242  if (m_env.subDisplayFile()) {
2243  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2244  << "\nComputing FFT of chain on parameter of id = " << statisticalOptions.fftParamId()
2245  << std::endl;
2246  }
2247 
2248  std::vector<std::complex<double> > forwardResult(0,std::complex<double>(0.,0.));
2249  std::vector<std::complex<double> > inverseResult(0,std::complex<double>(0.,0.));
2250  Fft<std::complex<double> > fftObj(m_env);
2251  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2252  unsigned int initialPosition = initialPosForStatistics[initialPosId];
2253  this->fftForward(initialPosition,
2254  statisticalOptions.fftSize(),
2255  statisticalOptions.fftParamId(),
2256  forwardResult);
2257 
2258  if (statisticalOptions.fftWrite() && passedOfs) {
2259  std::ofstream& ofsvar = *passedOfs;
2260  ofsvar << m_name << "_fftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << 1
2261  << "," << forwardResult.size()
2262  << ");"
2263  << std::endl;
2264  for (unsigned int j = 0; j < forwardResult.size(); ++j) {
2265  ofsvar << m_name << "_fftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << 1
2266  << "," << j+1
2267  << ") = " << forwardResult[j].real()
2268  << " + i*" << forwardResult[j].imag()
2269  << ";"
2270  << std::endl;
2271  }
2272  } // if write
2273 
2274  if (statisticalOptions.fftTestInversion()) {
2275  fftObj.inverse(forwardResult,
2276  statisticalOptions.fftSize(),
2277  inverseResult);
2278  if (statisticalOptions.fftWrite() && passedOfs) {
2279  std::ofstream& ofsvar = *passedOfs;
2280  ofsvar << m_name << "_iftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << 1
2281  << "," << inverseResult.size()
2282  << ");"
2283  << std::endl;
2284  for (unsigned int j = 0; j < inverseResult.size(); ++j) {
2285  ofsvar << m_name << "_iftInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << 1
2286  << "," << j+1
2287  << ") = " << inverseResult[j].real()
2288  << " + i*" << inverseResult[j].imag()
2289  << ";"
2290  << std::endl;
2291  }
2292  } // if write
2293  }
2294  } // for initialPosId
2295 
2296  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2297  if (m_env.subDisplayFile()) {
2298  *m_env.subDisplayFile() << "Chain FFT took " << tmpRunTime
2299  << " seconds"
2300  << std::endl;
2301  }
2302 
2303  return;
2304 }
2305 // --------------------------------------------------
2306 template<class V, class M>
2307 void
2308 BaseVectorSequence<V,M>::computePSD(
2309  const SequenceStatisticalOptions& statisticalOptions,
2310  const std::vector<unsigned int>& initialPosForStatistics,
2311  std::ofstream* passedOfs)
2312 {
2313  int iRC = UQ_OK_RC;
2314  struct timeval timevalTmp;
2315  iRC = gettimeofday(&timevalTmp, NULL);
2316  double tmpRunTime = 0.;
2317 
2318  if (m_env.subDisplayFile()) {
2319  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2320  << "\nComputing PSD of chain on parameter of id = " << statisticalOptions.psdParamId()
2321  << std::endl;
2322  }
2323 
2324  std::vector<double> psdResult(0,0.);
2325  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2326  unsigned int initialPosition = initialPosForStatistics[initialPosId];
2327  this->psd(initialPosition,
2328  statisticalOptions.psdNumBlocks(),
2329  statisticalOptions.psdHopSizeRatio(),
2330  statisticalOptions.psdParamId(),
2331  psdResult);
2332 
2333  if (statisticalOptions.psdWrite() && passedOfs) {
2334  std::ofstream& ofsvar = *passedOfs;
2335  ofsvar << m_name << "_psdInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << 1
2336  << "," << psdResult.size()
2337  << ");"
2338  << std::endl;
2339  for (unsigned int j = 0; j < psdResult.size(); ++j) {
2340  ofsvar << m_name << "_psdInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << 1
2341  << "," << j+1
2342  << ") = " << psdResult[j]
2343  << ";"
2344  << std::endl;
2345  }
2346  } // if write
2347  }
2348 
2349  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2350  if (m_env.subDisplayFile()) {
2351  *m_env.subDisplayFile() << "Chain PSD took " << tmpRunTime
2352  << " seconds"
2353  << std::endl;
2354  }
2355 
2356  return;
2357 }
2358 // --------------------------------------------------
2359 template<class V, class M>
2360 void
2361 BaseVectorSequence<V,M>::computePSDAtZero(
2362  const SequenceStatisticalOptions& statisticalOptions,
2363  const std::vector<unsigned int>& initialPosForStatistics,
2364  std::ofstream* passedOfs)
2365 {
2366  int iRC = UQ_OK_RC;
2367  struct timeval timevalTmp;
2368  iRC = gettimeofday(&timevalTmp, NULL);
2369  double tmpRunTime = 0.;
2370 
2371  if (m_env.subDisplayFile()) {
2372  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2373  << "\nComputing PSD at frequency zero for all parameters"
2374  << std::endl;
2375  }
2376 
2377  TwoDArray<V> _2dArrayOfPSDAtZero(initialPosForStatistics.size(),statisticalOptions.psdAtZeroNumBlocks().size());
2378  for (unsigned int i = 0; i < _2dArrayOfPSDAtZero.numRows(); ++i) {
2379  for (unsigned int j = 0; j < _2dArrayOfPSDAtZero.numCols(); ++j) {
2380  _2dArrayOfPSDAtZero.setLocation(i,j,new V(m_vectorSpace.zeroVector()) /*.*/);
2381  }
2382  }
2383  V psdVec(m_vectorSpace.zeroVector());
2384  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2385  unsigned int initialPosition = initialPosForStatistics[initialPosId];
2386  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2387  unsigned int numBlocks = statisticalOptions.psdAtZeroNumBlocks()[numBlocksId];
2388  this->psdAtZero(initialPosition,
2389  numBlocks,
2390  statisticalOptions.psdAtZeroHopSizeRatio(),
2391  psdVec);
2392  _2dArrayOfPSDAtZero(initialPosId,numBlocksId) = psdVec;
2393  }
2394  }
2395 
2396  // Display PSD at frequency zero
2397  if ((statisticalOptions.psdAtZeroDisplay()) && (m_env.subDisplayFile())) {
2398  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2399  unsigned int initialPos = initialPosForStatistics[initialPosId];
2400  *m_env.subDisplayFile() << "\nComputed PSD at frequency zero for subchain beginning at position " << initialPos
2401  << ", so effective data size = " << this->subSequenceSize() - initialPos
2402  << " (each column corresponds to a number of blocks)"
2403  << std::endl;
2404 
2405  char line[512];
2406  sprintf(line,"%s",
2407  "Parameter");
2408  *m_env.subDisplayFile() << line;
2409  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2410  sprintf(line,"%10s%3d",
2411  " ",
2412  statisticalOptions.psdAtZeroNumBlocks()[numBlocksId]);
2413  *m_env.subDisplayFile() << line;
2414  }
2415 
2416  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2417  sprintf(line,"\n%9.9s",
2418  m_vectorSpace.localComponentName(i).c_str() /*.*/);
2419  *m_env.subDisplayFile() << line;
2420  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2421  sprintf(line,"%2s%11.4e",
2422  " ",
2423  _2dArrayOfPSDAtZero(initialPosId,numBlocksId)[i]);
2424  *m_env.subDisplayFile() << line;
2425  }
2426  }
2427  *m_env.subDisplayFile() << std::endl;
2428  }
2429  }
2430 
2431  // Display estimated variance of sample mean through PSD
2432  if (/*(statisticalOptions.psdAtZeroDisplay()) &&*/ (m_env.subDisplayFile())) {
2433  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2434  unsigned int initialPos = initialPosForStatistics[initialPosId];
2435  *m_env.subDisplayFile() << "\nEstimated variance of sample mean, through psd, for subchain beginning at position " << initialPos
2436  << ", so effective data size = " << this->subSequenceSize() - initialPos
2437  << " (each column corresponds to a number of blocks)"
2438  << std::endl;
2439 
2440  char line[512];
2441  sprintf(line,"%s",
2442  "Parameter");
2443  *m_env.subDisplayFile() << line;
2444  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2445  sprintf(line,"%10s%3d",
2446  " ",
2447  statisticalOptions.psdAtZeroNumBlocks()[numBlocksId]);
2448  *m_env.subDisplayFile() << line;
2449  }
2450 
2451  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2452  sprintf(line,"\n%9.9s",
2453  m_vectorSpace.localComponentName(i).c_str() /*.*/);
2454  *m_env.subDisplayFile() << line;
2455  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2456  sprintf(line,"%2s%11.4e",
2457  " ",
2458  2.*M_PI*_2dArrayOfPSDAtZero(initialPosId,numBlocksId)[i]/(double) (this->subSequenceSize() - initialPos));
2459  *m_env.subDisplayFile() << line;
2460  }
2461  }
2462  *m_env.subDisplayFile() << std::endl;
2463  }
2464  }
2465 
2466  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2467  if (m_env.subDisplayFile()) {
2468  *m_env.subDisplayFile() << "Chain PSD at frequency zero took " << tmpRunTime
2469  << " seconds"
2470  << std::endl;
2471  }
2472 
2473  // Write PSD at frequency zero
2474  if (statisticalOptions.psdAtZeroWrite() && passedOfs) {
2475  std::ofstream& ofsvar = *passedOfs;
2476  ofsvar << m_name << "_psdAtZeroNumBlocks_sub" << m_env.subIdString() << " = zeros(" << 1
2477  << "," << statisticalOptions.psdAtZeroNumBlocks().size()
2478  << ");"
2479  << std::endl;
2480  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2481  ofsvar << m_name << "_psdAtZeroNumBlocks_sub" << m_env.subIdString() << "(" << 1
2482  << "," << numBlocksId+1
2483  << ") = " << statisticalOptions.psdAtZeroNumBlocks()[numBlocksId]
2484  << ";"
2485  << std::endl;
2486  }
2487 
2488  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2489  ofsvar << m_name << "_psdAtZeroInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << " = zeros(" << this->vectorSizeLocal() /*.*/
2490  << "," << statisticalOptions.psdAtZeroNumBlocks().size()
2491  << ");"
2492  << std::endl;
2493  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2494  for (unsigned int numBlocksId = 0; numBlocksId < statisticalOptions.psdAtZeroNumBlocks().size(); numBlocksId++) {
2495  ofsvar << m_name << "_psdAtZeroInitPos" << initialPosForStatistics[initialPosId] << "_sub" << m_env.subIdString() << "(" << i+1
2496  << "," << numBlocksId+1
2497  << ") = " << _2dArrayOfPSDAtZero(initialPosId,numBlocksId)[i]
2498  << ";"
2499  << std::endl;
2500  }
2501  }
2502  }
2503  }
2504 
2505  return;
2506 }
2507 // --------------------------------------------------
2508 template<class V, class M>
2509 void
2510 BaseVectorSequence<V,M>::computeGeweke(
2511  const SequenceStatisticalOptions& statisticalOptions,
2512  const std::vector<unsigned int>& initialPosForStatistics,
2513  std::ofstream* passedOfs)
2514 {
2515  int iRC = UQ_OK_RC;
2516  struct timeval timevalTmp;
2517  iRC = gettimeofday(&timevalTmp, NULL);
2518  double tmpRunTime = 0.;
2519 
2520  if (m_env.subDisplayFile()) {
2521  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2522  << "\nComputing Geweke coefficients"
2523  << std::endl;
2524  }
2525 
2526  std::vector<V*> vectorOfGeweke(initialPosForStatistics.size(),NULL);
2527  V gewVec(m_vectorSpace.zeroVector());
2528  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2529  unsigned int initialPosition = initialPosForStatistics[initialPosId];
2530  this->geweke(initialPosition,
2531  statisticalOptions.gewekeNaRatio(),
2532  statisticalOptions.gewekeNbRatio(),
2533  gewVec);
2534  vectorOfGeweke[initialPosId] = new V(gewVec);
2535  }
2536 
2537  if (m_env.subDisplayFile()) {
2538  *m_env.subDisplayFile() << "\nComputed Geweke coefficients with 10% and 50% percentages"
2539  << " (each column corresponds to a different initial position on the full chain)"
2540  << std::endl;
2541 
2542  char line[512];
2543  sprintf(line,"%s",
2544  "Parameter");
2545  *m_env.subDisplayFile() << line;
2546  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2547  sprintf(line,"%10s%3d",
2548  " ",
2549  initialPosForStatistics[initialPosId]);
2550  *m_env.subDisplayFile() << line;
2551  }
2552 
2553  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2554  sprintf(line,"\n%9.9s",
2555  m_vectorSpace.localComponentName(i).c_str() /*.*/);
2556  *m_env.subDisplayFile() << line;
2557  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2558  sprintf(line,"%2s%11.4e",
2559  " ",
2560  (*(vectorOfGeweke[initialPosId]))[i]);
2561  *m_env.subDisplayFile() << line;
2562  }
2563  }
2564  *m_env.subDisplayFile() << std::endl;
2565  }
2566 
2567  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2568  if (m_env.subDisplayFile()) {
2569  *m_env.subDisplayFile() << "Chain Geweke took " << tmpRunTime
2570  << " seconds"
2571  << std::endl;
2572  }
2573 
2574  return;
2575 }
2576 // --------------------------------------------------
2577 template<class V, class M>
2578 void
2579 BaseVectorSequence<V,M>::computeMeanStacc(
2580  const SequenceStatisticalOptions& statisticalOptions,
2581  const std::vector<unsigned int>& initialPosForStatistics,
2582  std::ofstream* passedOfs)
2583 {
2584  int iRC = UQ_OK_RC;
2585  struct timeval timevalTmp;
2586  iRC = gettimeofday(&timevalTmp, NULL);
2587  double tmpRunTime = 0.;
2588 
2589  if (m_env.subDisplayFile()) {
2590  *m_env.subDisplayFile() << "\n-----------------------------------------------------"
2591  << "\nComputing mean statistical accuracy"
2592  << std::endl;
2593  }
2594 
2595  std::vector<V*> vectorOfMeanStacc(initialPosForStatistics.size(),NULL);
2596  V meanStaccVec(m_vectorSpace.zeroVector());
2597  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2598  unsigned int initialPosition = initialPosForStatistics[initialPosId];
2599  this->meanStacc(initialPosition,
2600  meanStaccVec);
2601  vectorOfMeanStacc[initialPosId] = new V(meanStaccVec);
2602  }
2603 
2604  if (m_env.subDisplayFile()) {
2605  *m_env.subDisplayFile() << "\nComputed mean statistical accuracy"
2606  << " (each column corresponds to a different initial position on the full chain)"
2607  << std::endl;
2608 
2609  char line[512];
2610  sprintf(line,"%s",
2611  "Parameter");
2612  *m_env.subDisplayFile() << line;
2613  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2614  sprintf(line,"%10s%3d",
2615  " ",
2616  initialPosForStatistics[initialPosId]);
2617  *m_env.subDisplayFile() << line;
2618  }
2619 
2620  for (unsigned int i = 0; i < this->vectorSizeLocal() /*.*/; ++i) {
2621  sprintf(line,"\n%9.9s",
2622  m_vectorSpace.localComponentName(i).c_str() /*.*/);
2623  *m_env.subDisplayFile() << line;
2624  for (unsigned int initialPosId = 0; initialPosId < initialPosForStatistics.size(); initialPosId++) {
2625  sprintf(line,"%2s%11.4e",
2626  " ",
2627  (*(vectorOfMeanStacc[initialPosId]))[i]);
2628  *m_env.subDisplayFile() << line;
2629  }
2630  }
2631  *m_env.subDisplayFile() << std::endl;
2632  }
2633 
2634  tmpRunTime += MiscGetEllapsedSeconds(&timevalTmp);
2635  if (m_env.subDisplayFile()) {
2636  *m_env.subDisplayFile() << "Chain mean statistical accuracy took " << tmpRunTime
2637  << " seconds"
2638  << std::endl;
2639  }
2640 
2641  return;
2642 }
2643 #endif // #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
2644 
2645 // --------------------------------------------------
2646 // Additional methods --------------------------------
2647 // (outside class declaration) ----------------------
2648 //---------------------------------------------------
2649 template <class P_V, class P_M, class Q_V, class Q_M>
2650 void
2652  const BaseVectorSequence<P_V,P_M>& subPSeq,
2653  const BaseVectorSequence<Q_V,Q_M>& subQSeq,
2654  unsigned int subNumSamples,
2655  P_M& pqCovMatrix,
2656  P_M& pqCorrMatrix)
2657 {
2658  // Check input data consistency
2659  const BaseEnvironment& env = subPSeq.vectorSpace().zeroVector().env();
2660 
2661  bool useOnlyInter0Comm = (subPSeq.vectorSpace().numOfProcsForStorage() == 1) &&
2662  (subQSeq.vectorSpace().numOfProcsForStorage() == 1);
2663 
2664  UQ_FATAL_TEST_MACRO((useOnlyInter0Comm == false),
2665  env.worldRank(),
2666  "ComputeCovCorrMatricesBetweenVectorSequences()",
2667  "parallel vectors not supported yet");
2668 
2669  unsigned int numRowsLocal = subPSeq.vectorSpace().dimLocal();
2670  unsigned int numCols = subQSeq.vectorSpace().dimGlobal();
2671 
2672  UQ_FATAL_TEST_MACRO((numRowsLocal != pqCovMatrix.numRowsLocal()) || (numCols != pqCovMatrix.numCols()),
2673  env.worldRank(),
2674  "ComputeCovCorrMatricesBetweenVectorSequences()",
2675  "inconsistent dimensions for covariance matrix");
2676 
2677  UQ_FATAL_TEST_MACRO((numRowsLocal != pqCorrMatrix.numRowsLocal()) || (numCols != pqCorrMatrix.numCols()),
2678  env.worldRank(),
2679  "ComputeCorrelationBetweenVectorSequences()",
2680  "inconsistent dimensions for correlation matrix");
2681 
2682  UQ_FATAL_TEST_MACRO((subNumSamples > subPSeq.subSequenceSize()) || (subNumSamples > subQSeq.subSequenceSize()),
2683  env.worldRank(),
2684  "ComputeCovCorrMatricesBetweenVectorSequences()",
2685  "subNumSamples is too large");
2686 
2687  // For both P and Q vector sequences: fill them
2688  P_V tmpP(subPSeq.vectorSpace().zeroVector());
2689  Q_V tmpQ(subQSeq.vectorSpace().zeroVector());
2690 
2691  // For both P and Q vector sequences: compute the unified mean
2692  P_V unifiedMeanP(subPSeq.vectorSpace().zeroVector());
2693  subPSeq.unifiedMeanExtra(0,subNumSamples,unifiedMeanP);
2694 
2695  Q_V unifiedMeanQ(subQSeq.vectorSpace().zeroVector());
2696  subQSeq.unifiedMeanExtra(0,subNumSamples,unifiedMeanQ);
2697 
2698  // Compute "sub" covariance matrix
2699  for (unsigned i = 0; i < numRowsLocal; ++i) {
2700  for (unsigned j = 0; j < numCols; ++j) {
2701  pqCovMatrix(i,j) = 0.;
2702  }
2703  }
2704  for (unsigned k = 0; k < subNumSamples; ++k) {
2705  // For both P and Q vector sequences: get the difference (wrt the unified mean) in them
2706  subPSeq.getPositionValues(k,tmpP);
2707  tmpP -= unifiedMeanP;
2708 
2709  subQSeq.getPositionValues(k,tmpQ);
2710  tmpQ -= unifiedMeanQ;
2711 
2712  for (unsigned i = 0; i < numRowsLocal; ++i) {
2713  for (unsigned j = 0; j < numCols; ++j) {
2714  pqCovMatrix(i,j) += tmpP[i]*tmpQ[j];
2715  }
2716  }
2717  }
2718 
2719  // For both P and Q vector sequences: compute the unified variance
2720  P_V unifiedSampleVarianceP(subPSeq.vectorSpace().zeroVector());
2721  subPSeq.unifiedSampleVarianceExtra(0,
2722  subNumSamples,
2723  unifiedMeanP,
2724  unifiedSampleVarianceP);
2725 
2726  Q_V unifiedSampleVarianceQ(subQSeq.vectorSpace().zeroVector());
2727  subQSeq.unifiedSampleVarianceExtra(0,
2728  subNumSamples,
2729  unifiedMeanQ,
2730  unifiedSampleVarianceQ);
2731 
2732  // Compute unified covariance matrix
2733  if (useOnlyInter0Comm) {
2734  if (env.inter0Rank() >= 0) {
2735  unsigned int unifiedNumSamples = 0;
2736  env.inter0Comm().Allreduce((void *) &subNumSamples, (void *) &unifiedNumSamples, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
2737  "ComputeCovCorrMatricesBetweenVectorSequences()",
2738  "failed MPI.Allreduce() for subNumSamples");
2739 
2740  for (unsigned i = 0; i < numRowsLocal; ++i) {
2741  for (unsigned j = 0; j < numCols; ++j) {
2742  double aux = 0.;
2743  env.inter0Comm().Allreduce((void *) &pqCovMatrix(i,j), (void *) &aux, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
2744  "ComputeCovCorrMatricesBetweenVectorSequences()",
2745  "failed MPI.Allreduce() for a matrix position");
2746  pqCovMatrix(i,j) = aux/((double) (unifiedNumSamples-1)); // Yes, '-1' in order to compensate for the 'N-1' denominator factor in the calculations of sample variances above (whose square roots will be used below)
2747  }
2748  }
2749 
2750  for (unsigned i = 0; i < numRowsLocal; ++i) {
2751  for (unsigned j = 0; j < numCols; ++j) {
2752  pqCorrMatrix(i,j) = pqCovMatrix(i,j)/std::sqrt(unifiedSampleVarianceP[i])/std::sqrt(unifiedSampleVarianceQ[j]);
2753  if (((pqCorrMatrix(i,j) + 1.) < -1.e-8) ||
2754  ((pqCorrMatrix(i,j) - 1.) > 1.e-8)) {
2755  if (env.inter0Rank() == 0) {
2756  std::cerr << "In ComputeCovCorrMatricesBetweenVectorSequences()"
2757  << ": worldRank = " << env.worldRank()
2758  << ", i = " << i
2759  << ", j = " << j
2760  << ", pqCorrMatrix(i,j)+1 = " << pqCorrMatrix(i,j)+1.
2761  << ", pqCorrMatrix(i,j)-1 = " << pqCorrMatrix(i,j)-1.
2762  << std::endl;
2763  }
2764  env.inter0Comm().Barrier();
2765  }
2766  UQ_FATAL_TEST_MACRO(((pqCorrMatrix(i,j) + 1.) < -1.e-8) ||
2767  ((pqCorrMatrix(i,j) - 1.) > 1.e-8),
2768  env.worldRank(),
2769  "ComputeCovCorrMatricesBetweenVectorSequences()",
2770  "computed correlation is out of range");
2771  }
2772  }
2773  }
2774  else {
2775  // Node not in the 'inter0' communicator: do nothing extra
2776  }
2777  }
2778  else {
2779  UQ_FATAL_TEST_MACRO((useOnlyInter0Comm == false),
2780  env.worldRank(),
2781  "ComputeCovCorrMatricesBetweenVectorSequences()",
2782  "parallel vectors not supported yet (2)");
2783  }
2784 
2785  return;
2786 }
2787 
2788 } // End namespace QUESO
2789 
2791 template void QUESO::ComputeCovCorrMatricesBetweenVectorSequences<QUESO::GslVector, QUESO::GslMatrix, QUESO::GslVector, QUESO::GslMatrix>(QUESO::BaseVectorSequence<QUESO::GslVector, QUESO::GslMatrix> const&, QUESO::BaseVectorSequence<QUESO::GslVector, QUESO::GslMatrix> const&, unsigned int, QUESO::GslMatrix&, QUESO::GslMatrix&);
const V & subMedianPlain() const
Finds the median value of the sub-sequence.
const std::string & name() const
Access to protected attribute m_name: name of the sequence of vectors.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:41
unsigned int dimGlobal() const
Definition: VectorSpace.C:205
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:289
virtual void setPositionValues(unsigned int posId, const V &vec)=0
Set the values in vec at position posId of the sequence. See template specialization.
virtual void unifiedSampleVarianceExtra(unsigned int initialPos, unsigned int numPos, const V &unifiedMeanVec, V &unifiedSamVec) const =0
Finds the sample variance of the unified sequence, considering numPos positions starting at position ...
const T & subMaxPlain() const
Finds the maximum value of the sub-sequence of scalars.
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
const BoxSubset< V, M > & subBoxPlain() const
Finds a box subset of the sub-sequence (given by its min and max values calculated via subMinPlain an...
A class representing a vector space.
Definition: VectorSet.h:46
void setName(const std::string &newName)
Changes the name of the sequence of vectors.
virtual void resizeSequence(unsigned int newSubSequenceSize)=0
Resize the sequence. See template specialization.
const V & subMinPlain() const
Finds the minimum value of the sub-sequence.
const V & zeroVector() const
Returns a vector filled with zeros.
Definition: VectorSpace.C:218
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:131
const V & subMeanPlain() const
Finds the mean value of the sub-sequence.
const BoxSubset< V, M > & unifiedBoxPlain() const
Finds a box subset of the unified-sequence (given by the min and max values of the unified sequence c...
#define RawValue_MPI_INT
Definition: MpiComm.h:47
void append(const BaseVectorSequence< V, M > &src, unsigned int initialPos, unsigned int numPos)
Appends the vector src to this vector.
Class for matrix operations using GSL library.
Definition: GslMatrix.h:47
Class for a Fast Fourier Transform (FFT) algorithm.
Definition: Fft.h:66
const int UQ_OK_RC
Definition: Defines.h:75
const V & unifiedSampleVariancePlain() const
Finds the variance of a sample of the unified sequence.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
unsigned int unifiedSequenceSize() const
Calculates the size of the unified sequence of vectors.
unsigned int vectorSizeLocal() const
Local dimension (size) of the vector space.
double MiscGetEllapsedSeconds(struct timeval *timeval0)
const V & subSampleVariancePlain() const
Finds the variance of a sample of the sub-sequence.
unsigned int dimLocal() const
Definition: VectorSpace.C:199
void deleteStoredVectors()
Deletes all the stored vectors.
unsigned int numOfProcsForStorage() const
Returns total number of processes.
Definition: VectorSpace.C:193
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:222
void MiscComputePositionsBetweenMinMax(V minValues, V maxValues, std::vector< V * > &positions)
const V & unifiedMinPlain() const
Finds the minimum value of the unified sequence.
const V & subMaxPlain() const
Finds the maximum value of the sub-sequence.
const VectorSpace< V, M > & m_vectorSpace
virtual void unifiedMeanExtra(unsigned int initialPos, unsigned int numPos, V &unifiedMeanVec) const =0
Finds the mean value of the unified sequence of numPos positions starting at position initialPos...
#define RawValue_MPI_MAX
Definition: MpiComm.h:51
const V & unifiedMeanPlain() const
Finds the mean value of the unified sequence.
virtual void getPositionValues(unsigned int posId, V &vec) const =0
Gets the values of the sequence at position posId and stores them at vec. See template specialization...
const V & unifiedMedianPlain() const
Finds the median value of the unified sequence.
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:143
void setUniform(const V &aVec, const V &bVec)
Sets the values of the sequence as a uniform distribution between the values given by vectors aVec an...
void computeFilterParams(std::ofstream *passedOfs, unsigned int &initialPos, unsigned int &spacing)
Computes the filtering parameters spacing for the sequence of vectors.
Base class for handling vector and array samples (sequence of vectors or arrays). ...
void clear()
Reset the values and the size of the sequence of vectors.
double subPositionsOfMaximum(const ScalarSequence< double > &subCorrespondingScalarValues, BaseVectorSequence< V, M > &subPositionsOfMaximum)
Finds the positions where the maximum element occurs in the sub-sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void setGaussian(const V &meanVec, const V &stdDevVec)
Sets the values of the sequence as a Gaussian distribution of mean given by meanVec and standard devi...
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:295
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)
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
unsigned int vectorSizeGlobal() const
Global dimension (size) of the vector space.
const VectorSpace< V, M > & vectorSpace() const
Vector space; access to protected attribute VectorSpace&lt;V,M&gt;&amp; m_vectorSpace.
virtual ~BaseVectorSequence()
Destructor.
void copy(const BaseVectorSequence< V, M > &src)
Copies vector sequence src to this.
BaseVectorSequence(const VectorSpace< V, M > &vectorSpace, unsigned int subSequenceSize, const std::string &name)
Default constructor.
const V & unifiedMaxPlain() const
Finds the maximum value of the unified sequence.
double unifiedPositionsOfMaximum(const ScalarSequence< double > &subCorrespondingScalarValues, BaseVectorSequence< V, M > &unifiedPositionsOfMaximum)
Finds the positions where the maximum element occurs in the unified sequence.
virtual unsigned int subSequenceSize() const =0
Size of the sub-sequence of vectors. See template specialization.

Generated on Thu Apr 23 2015 19:18:34 for queso-0.50.1 by  doxygen 1.8.5