MUQ  0.4.3
DILIKernel.h
Go to the documentation of this file.
1 #ifndef DILIKERNEL_H_
2 #define DILIKERNEL_H_
3 
5 
9 
10 namespace muq {
11  namespace SamplingAlgorithms {
12 
47  {
48  public:
49  AverageHessian(unsigned int numOldSamps,
50  std::shared_ptr<Eigen::ColPivHouseholderQR<Eigen::MatrixXd>> const& uQRIn,
51  std::shared_ptr<Eigen::MatrixXd> const& oldWIn,
52  std::shared_ptr<Eigen::VectorXd> const& oldValsIn,
53  std::shared_ptr<muq::Modeling::LinearOperator> const& newHess);
54 
56  virtual Eigen::MatrixXd Apply(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
57 
59  virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
60 
61  private:
62  const double numSamps;
63  std::shared_ptr<Eigen::MatrixXd> oldW;
64  std::shared_ptr<Eigen::ColPivHouseholderQR<Eigen::MatrixXd>> uQR; // holds the QR decomp of U
65 
66  std::shared_ptr<Eigen::VectorXd> oldEigVals;
67  std::shared_ptr<muq::Modeling::LinearOperator> newHess;
68  Eigen::MatrixXd thinQ;
69 
70  };
71 
73  {
74  public:
75 
76  CSProjector(std::shared_ptr<Eigen::MatrixXd> const& Uin,
77  std::shared_ptr<Eigen::MatrixXd> const& Win,
78  unsigned int lisDimIn) : LinearOperator(Uin->rows(), Win->rows()),
79  U(Uin), W(Win), lisDim(lisDimIn){};
80 
81  virtual ~CSProjector() = default;
82 
84  virtual Eigen::MatrixXd Apply(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
85 
87  virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
88 
89  private:
90  std::shared_ptr<Eigen::MatrixXd> U, W;
91  unsigned int lisDim;
92  };
93 
94 
96  {
97  public:
98  LIS2Full(std::shared_ptr<Eigen::MatrixXd> const& Uin,
99  std::shared_ptr<Eigen::VectorXd> const& Lin) : LinearOperator(Uin->rows(), Lin->rows()),
100  U(Uin), L(Lin), lisDim(Lin->rows()){};
101 
102  virtual ~LIS2Full() = default;
103 
105  virtual Eigen::MatrixXd Apply(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
106 
108  virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x) override;
109 
110  private:
111  std::shared_ptr<Eigen::MatrixXd> U;
112  std::shared_ptr<Eigen::VectorXd> L;
113  const unsigned int lisDim;
114  };
115 
116 
141  class DILIKernel : public TransitionKernel {
142  public:
143 
144  DILIKernel(boost::property_tree::ptree const& pt,
145  std::shared_ptr<AbstractSamplingProblem> problem);
146 
147  DILIKernel(boost::property_tree::ptree const& pt,
148  std::shared_ptr<AbstractSamplingProblem> problem,
149  Eigen::VectorXd const& genEigVals,
150  Eigen::MatrixXd const& genEigVecs);
151 
152  DILIKernel(boost::property_tree::ptree const& pt,
153  std::shared_ptr<AbstractSamplingProblem> problem,
154  std::shared_ptr<muq::Modeling::GaussianBase> const& prior,
155  std::shared_ptr<muq::Modeling::ModPiece> const& noiseModel,
156  std::shared_ptr<muq::Modeling::ModPiece> const& forwardModelIn);
157 
158  DILIKernel(boost::property_tree::ptree const& pt,
159  std::shared_ptr<AbstractSamplingProblem> problem,
160  std::shared_ptr<muq::Modeling::GaussianBase> const& prior,
161  std::shared_ptr<muq::Modeling::ModPiece> const& noiseModel,
162  std::shared_ptr<muq::Modeling::ModPiece> const& forwardModelIn,
163  Eigen::VectorXd const& genEigVals,
164  Eigen::MatrixXd const& genEigVecs);
165 
166 
167  DILIKernel(boost::property_tree::ptree const& pt,
168  std::shared_ptr<AbstractSamplingProblem> problem,
169  std::shared_ptr<muq::Modeling::GaussianBase> const& prior,
170  std::shared_ptr<muq::Modeling::ModPiece> const& likelihood);
171 
172  DILIKernel(boost::property_tree::ptree const& pt,
173  std::shared_ptr<AbstractSamplingProblem> problem,
174  std::shared_ptr<muq::Modeling::GaussianBase> const& prior,
175  std::shared_ptr<muq::Modeling::ModPiece> const& likelihood,
176  Eigen::VectorXd const& genEigVals,
177  Eigen::MatrixXd const& genEigVecs);
178 
179  virtual ~DILIKernel() = default;
180 
181  virtual inline std::shared_ptr<TransitionKernel> LISKernel() {return lisKernel;};
182  virtual inline std::shared_ptr<TransitionKernel> CSKernel() {return csKernel;};
183 
184  virtual void PostStep(unsigned int const t,
185  std::vector<std::shared_ptr<SamplingState>> const& state) override;
186 
187  virtual std::vector<std::shared_ptr<SamplingState>> Step(unsigned int const t,
188  std::shared_ptr<SamplingState> prevState) override;
189 
190  virtual void PrintStatus(std::string prefix) const override;
191 
195  static std::shared_ptr<muq::Modeling::ModPiece> ExtractLikelihood(std::shared_ptr<AbstractSamplingProblem> const& problem,
196  std::string const& nodeName);
197 
198 
202  static std::shared_ptr<muq::Modeling::GaussianBase> ExtractPrior(std::shared_ptr<AbstractSamplingProblem> const& problem,
203  std::string const& nodeName);
204 
205  static std::shared_ptr<muq::Modeling::ModPiece> ExtractNoiseModel(std::shared_ptr<muq::Modeling::ModPiece> const& likelihood);
206 
207  // Extract the forward model from the likelihood. This function assumes the graph has only one node between the output of the likelihood function and the output of the forward model
208  static std::shared_ptr<muq::Modeling::ModPiece> ExtractForwardModel(std::shared_ptr<muq::Modeling::ModPiece> const& likelihoodIn);
209 
210  static std::shared_ptr<muq::Modeling::ModPiece> CreateLikelihood(std::shared_ptr<muq::Modeling::ModPiece> const& forwardModel,
211  std::shared_ptr<muq::Modeling::ModPiece> const& noiseDensity);
212 
213  Eigen::MatrixXd const& LISVecs() const{return *hessU;};
214  Eigen::VectorXd const& LISVals() const{return *hessEigVals;};
215  Eigen::MatrixXd const& LISW() const{return *hessW;};
216  Eigen::VectorXd const& LISL() const{return *lisL;};
217 
219  unsigned int LISDim() const{return lisDim;};
220 
222  void CreateLIS(std::vector<Eigen::VectorXd> const& currState);
223 
227  Eigen::VectorXd ToLIS(Eigen::VectorXd const& x) const;
228 
232  Eigen::VectorXd FromLIS(Eigen::VectorXd const& r) const;
233 
238  Eigen::VectorXd ToCS(Eigen::VectorXd const& x) const;
239 
240 
241  protected:
242 
243  std::pair<Eigen::MatrixXd, Eigen::VectorXd> ComputeLocalLIS(std::vector<Eigen::VectorXd> const& currState);
244 
245 
248  void SetLIS(Eigen::VectorXd const& eigVals, Eigen::MatrixXd const& eigVecs);
249 
250 
251 
257  void UpdateLIS(unsigned int numSamps,
258  std::vector<Eigen::VectorXd> const& currState);
259 
260  // Used to reconstruct the LIS and CS kernels when lisU, lisW, or lisL changes.
261  void UpdateKernels();
262 
263  boost::property_tree::ptree lisKernelOpts;
264  boost::property_tree::ptree csKernelOpts;
265 
266  std::shared_ptr<muq::Modeling::ModPiece> logLikelihood;
267  std::shared_ptr<muq::Modeling::GaussianBase> prior;
268 
269  std::shared_ptr<muq::Modeling::ModPiece> forwardModel;
270  std::shared_ptr<muq::Modeling::ModPiece> noiseDensity;
271 
272  // A matrix containing the eigenvectors of the generalized eigenvalue problem Hv = lam*\Gamma^{-1}v
273  std::shared_ptr<Eigen::MatrixXd> hessU;
274 
275  // The following matrices hold a QR decomposition of hessU
276  std::shared_ptr<Eigen::ColPivHouseholderQR<Eigen::MatrixXd>> hessUQR;
277 
278  // A vector of eigenvalues of the generalized eigenvalue problem. Only values above hessValTol are kept.
279  std::shared_ptr<Eigen::VectorXd> hessEigVals;
280 
281  // W = \Gamma^{-1} v
282  std::shared_ptr<Eigen::MatrixXd> hessW;
283 
284  // L is the Cholesky factor of the approximate posterior covariance projected onto the LIS
285  std::shared_ptr<Eigen::VectorXd> lisL;
286 
287  // Defines the operation from the LIS to the full space
288  std::shared_ptr<muq::Modeling::LinearOperator> lisToFull;
289 
290  // Defines the projection onto the complementary space
291  std::shared_ptr<muq::Modeling::LinearOperator> fullToCS;
292 
293  // Transition kernel for updating the LIS
294  std::shared_ptr<TransitionKernel> lisKernel;
295 
296  // Transition kernel for updating the CS
297  std::shared_ptr<TransitionKernel> csKernel;
298 
299  // The type of Hessian approximation to use. "Exact" or "GaussNewton"
300  std::string hessType;
301 
302  // Options for the Generalized eigenvalue solver
303  boost::property_tree::ptree eigOpts;
304 
305  const int updateInterval;
306  const int adaptStart;
307  const int adaptEnd;
308  const int initialHessSamps;
309 
310  /* The size of the LIS. Note that the LIS can have a smaller dimension
311  than the number of eigenvalue/eigenvector pairs kept to approximate the
312  global hessian. The number of eigenvalues greater than lisValTol dictates
313  the LIS dimension
314  */
315  unsigned int lisDim=0;
316 
317  /* Threshold on eigenvalues defining the low rank approximation of the global
318  hessian. Note that this toleran is typically smaller than the tolerance
319  used to define the LIS. In Cui et al., this was set to 1e-4.
320  */
321  const double hessValTol;
322 
323  /* Threshold on eigenvalues used to define local and global LIS
324  approximations. Modes with eigenavalues greater than this threshold
325  will be used. In Cui et al., this was set to 0.1
326  */
327  const double lisValTol;
328 
329  unsigned int numLisUpdates;
330  };
331  } // namespace SamplingAlgorithms
332 } // namespace muq
333 
334 #endif
Generic linear operator base class.
LinearOperator(int rowsIn, int colsIn, int numInputCols=1)
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:35
AverageHessian(unsigned int numOldSamps, std::shared_ptr< Eigen::ColPivHouseholderQR< Eigen::MatrixXd >> const &uQRIn, std::shared_ptr< Eigen::MatrixXd > const &oldWIn, std::shared_ptr< Eigen::VectorXd > const &oldValsIn, std::shared_ptr< muq::Modeling::LinearOperator > const &newHess)
Definition: DILIKernel.cpp:17
std::shared_ptr< muq::Modeling::LinearOperator > newHess
Definition: DILIKernel.h:67
std::shared_ptr< Eigen::VectorXd > oldEigVals
Definition: DILIKernel.h:66
std::shared_ptr< Eigen::MatrixXd > oldW
Definition: DILIKernel.h:63
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:43
std::shared_ptr< Eigen::ColPivHouseholderQR< Eigen::MatrixXd > > uQR
Definition: DILIKernel.h:64
std::shared_ptr< Eigen::MatrixXd > W
Definition: DILIKernel.h:90
CSProjector(std::shared_ptr< Eigen::MatrixXd > const &Uin, std::shared_ptr< Eigen::MatrixXd > const &Win, unsigned int lisDimIn)
Definition: DILIKernel.h:76
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:54
std::shared_ptr< Eigen::MatrixXd > U
Definition: DILIKernel.h:90
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:49
An implementation of the Dimension Independent Likelihood Informed subspace (DILI) MCMC sampler.
Definition: DILIKernel.h:141
boost::property_tree::ptree csKernelOpts
Definition: DILIKernel.h:264
virtual std::vector< std::shared_ptr< SamplingState > > Step(unsigned int const t, std::shared_ptr< SamplingState > prevState) override
Definition: DILIKernel.cpp:235
Eigen::MatrixXd const & LISW() const
Definition: DILIKernel.h:215
std::shared_ptr< muq::Modeling::LinearOperator > lisToFull
Definition: DILIKernel.h:288
virtual std::shared_ptr< TransitionKernel > LISKernel()
Definition: DILIKernel.h:181
std::shared_ptr< Eigen::ColPivHouseholderQR< Eigen::MatrixXd > > hessUQR
Definition: DILIKernel.h:276
static std::shared_ptr< muq::Modeling::ModPiece > CreateLikelihood(std::shared_ptr< muq::Modeling::ModPiece > const &forwardModel, std::shared_ptr< muq::Modeling::ModPiece > const &noiseDensity)
Definition: DILIKernel.cpp:519
std::shared_ptr< muq::Modeling::GaussianBase > prior
Definition: DILIKernel.h:267
void UpdateLIS(unsigned int numSamps, std::vector< Eigen::VectorXd > const &currState)
Definition: DILIKernel.cpp:413
std::shared_ptr< Eigen::MatrixXd > hessW
Definition: DILIKernel.h:282
std::shared_ptr< TransitionKernel > lisKernel
Definition: DILIKernel.h:294
Eigen::VectorXd const & LISL() const
Definition: DILIKernel.h:216
boost::property_tree::ptree eigOpts
Definition: DILIKernel.h:303
std::pair< Eigen::MatrixXd, Eigen::VectorXd > ComputeLocalLIS(std::vector< Eigen::VectorXd > const &currState)
Definition: DILIKernel.cpp:338
Eigen::VectorXd FromLIS(Eigen::VectorXd const &r) const
Definition: DILIKernel.cpp:223
std::shared_ptr< muq::Modeling::ModPiece > noiseDensity
Definition: DILIKernel.h:270
std::shared_ptr< Eigen::VectorXd > hessEigVals
Definition: DILIKernel.h:279
Eigen::VectorXd const & LISVals() const
Definition: DILIKernel.h:214
static std::shared_ptr< muq::Modeling::ModPiece > ExtractLikelihood(std::shared_ptr< AbstractSamplingProblem > const &problem, std::string const &nodeName)
Definition: DILIKernel.cpp:498
void SetLIS(Eigen::VectorXd const &eigVals, Eigen::MatrixXd const &eigVecs)
Definition: DILIKernel.cpp:305
boost::property_tree::ptree lisKernelOpts
Definition: DILIKernel.h:263
std::shared_ptr< muq::Modeling::ModPiece > logLikelihood
Definition: DILIKernel.h:266
Eigen::MatrixXd const & LISVecs() const
Definition: DILIKernel.h:213
static std::shared_ptr< muq::Modeling::ModPiece > ExtractForwardModel(std::shared_ptr< muq::Modeling::ModPiece > const &likelihoodIn)
Definition: DILIKernel.cpp:481
std::shared_ptr< Eigen::MatrixXd > hessU
Definition: DILIKernel.h:273
virtual std::shared_ptr< TransitionKernel > CSKernel()
Definition: DILIKernel.h:182
static std::shared_ptr< muq::Modeling::ModPiece > ExtractNoiseModel(std::shared_ptr< muq::Modeling::ModPiece > const &likelihood)
Definition: DILIKernel.cpp:471
Eigen::VectorXd ToLIS(Eigen::VectorXd const &x) const
Definition: DILIKernel.cpp:215
std::shared_ptr< TransitionKernel > csKernel
Definition: DILIKernel.h:297
static std::shared_ptr< muq::Modeling::GaussianBase > ExtractPrior(std::shared_ptr< AbstractSamplingProblem > const &problem, std::string const &nodeName)
Definition: DILIKernel.cpp:441
void CreateLIS(std::vector< Eigen::VectorXd > const &currState)
Definition: DILIKernel.cpp:372
std::shared_ptr< muq::Modeling::ModPiece > forwardModel
Definition: DILIKernel.h:269
virtual void PostStep(unsigned int const t, std::vector< std::shared_ptr< SamplingState >> const &state) override
Allow the kernel to adapt given a new state.
Definition: DILIKernel.cpp:206
Eigen::VectorXd ToCS(Eigen::VectorXd const &x) const
Definition: DILIKernel.cpp:229
DILIKernel(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem)
Definition: DILIKernel.cpp:71
std::shared_ptr< Eigen::VectorXd > lisL
Definition: DILIKernel.h:285
std::shared_ptr< muq::Modeling::LinearOperator > fullToCS
Definition: DILIKernel.h:291
LIS2Full(std::shared_ptr< Eigen::MatrixXd > const &Uin, std::shared_ptr< Eigen::VectorXd > const &Lin)
Definition: DILIKernel.h:98
std::shared_ptr< Eigen::MatrixXd > U
Definition: DILIKernel.h:111
std::shared_ptr< Eigen::VectorXd > L
Definition: DILIKernel.h:112
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:64
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: DILIKernel.cpp:59
Defines the transition kernel used by an MCMC algorithm.
std::shared_ptr< AbstractSamplingProblem > problem
The sampling problem that evaluates/samples the target distribution.