queso-0.56.1
InfiniteDimensionalMCMCSampler.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/Defines.h>
26 
27 #ifdef QUESO_HAVE_HDF5
28 
29 #ifndef QUESO_INFINITE_SAMPLER_H
30 #define QUESO_INFINITE_SAMPLER_H
31 
32 // Queso includes
33 #include <queso/SharedPtr.h>
34 #include <queso/Environment.h>
35 #include <queso/InfiniteDimensionalMeasureBase.h>
36 #include <queso/InfiniteDimensionalLikelihoodBase.h>
37 #include <queso/InfiniteDimensionalMCMCSamplerOptions.h>
38 
39 // GSL includes
40 #include <gsl/gsl_rng.h>
41 
42 // HDF5 includes
43 #include <hdf5.h>
44 
45 namespace QUESO {
46 
54 class InfiniteDimensionalMCMCSampler
55 {
56 public:
58  InfiniteDimensionalMCMCSampler(
59  const BaseEnvironment& env,
60  InfiniteDimensionalMeasureBase & prior,
61  InfiniteDimensionalLikelihoodBase & llhd,
62  InfiniteDimensionalMCMCSamplerOptions * ov);
63 
65  ~InfiniteDimensionalMCMCSampler();
66 
68  void step();
69 
71  double llhd_val() const;
72 
74  double acc_prob();
75 
77  double avg_acc_prob();
78 
80  unsigned int iteration() const;
81 
83  SharedPtr<InfiniteDimensionalMCMCSampler>::Type clone_and_reset() const;
84 
85 private:
86  // Current iteration
87  unsigned int _iteration;
88 
89  // The current value of the negative log-likelihood functional
90  double _llhd_val;
91 
92  // The current acceptance probability
93  double _acc_prob;
94 
95  // The current running mean of the acceptance probability
96  double _avg_acc_prob;
97 
98  // The prior measure from which to draw
99  InfiniteDimensionalMeasureBase & prior;
100 
101  // The negative log-likelihood functional. Operates on functions.
102  // Should be const?
103  InfiniteDimensionalLikelihoodBase & llhd;
104 
105  // Aggregate options object
106  InfiniteDimensionalMCMCSamplerOptions * m_ov;
107 
108  // The QUESO environment
109  const BaseEnvironment& m_env;
110 
111  // Pointer to the current physical state
112  SharedPtr<FunctionBase>::Type current_physical_state;
113 
114  // Pointer to the current proposed state
115  SharedPtr<FunctionBase>::Type proposed_physical_state;
116 
117  // Pointer to the current physical mean
118  SharedPtr<FunctionBase>::Type current_physical_mean;
119 
120  // Pointer to the current physical variance
121  SharedPtr<FunctionBase>::Type current_physical_var;
122 
123  // Stores the differences from the mean
124  SharedPtr<FunctionBase>::Type _delta;
125 
126  // Stores a running sum-of-squares (kinda)
127  SharedPtr<FunctionBase>::Type _M2;
128 
129  // A pointer to the random number generator to use.
130  // Should probably use the one in the queso environment.
131  gsl_rng *r;
132 
133  // HDF5 file identifier
134  hid_t _outfile;
135 
136  // HDF5 datasets
137  hid_t _acc_dset;
138  hid_t _avg_acc_dset;
139  hid_t _neg_log_llhd_dset;
140  hid_t _L2_norm_samples_dset;
141  hid_t _L2_norm_mean_dset;
142  hid_t _L2_norm_var_dset;
143 
147  void _propose();
148 
149  // Do a metropolis random walk step
150  void _metropolis_hastings();
151 
152  // Increments the current iteration and updates the running mean and variance.
153  void _update_moments();
154 
155  // Write the current state of the chain to disk
156  void _write_state();
157 
158  // Creates a scalar dataset called \c name in \c _outfile file and returns it
159  hid_t _create_scalar_dataset(const std::string & name);
160 
161  // Appends to a scalar dataset in the hdf5 file
162  void _append_scalar_dataset(hid_t dset, double data);
163 };
164 
165 } // End namespace QUESO
166 
167 #endif // QUESO_INFINITE_SAMPLER_H
168 
169 #endif // QUESO_HAVE_HDF5

Generated on Thu Dec 15 2016 13:23:10 for queso-0.56.1 by  doxygen 1.8.5