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

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