MUQ  0.4.3
KarhunenLoeveExpansion.cpp
Go to the documentation of this file.
2 
3 #include <Eigen/Eigenvalues>
4 
5 using namespace muq::Approximation;
6 
7 
8 
9 KarhunenLoeveExpansion::KarhunenLoeveExpansion(std::shared_ptr<KernelBase> kernelIn,
10  Eigen::MatrixXd const& seedPtsIn,
11  Eigen::VectorXd const& seedWtsIn,
12  boost::property_tree::ptree options) : seedPts(seedPtsIn), seedWts(seedWtsIn), covKernel(kernelIn)
13 {
14 
15  int numModes = options.get("KarhunenLoeve.NumModes", seedWts.size());
16 
17  // We will approximation the KL modes as the GP with known values at the seed points. To get those values, we first need to solve the discrete eigenvalue problem
18  Eigen::VectorXd sqrtWts = seedWts.array().sqrt();
19 
20  Eigen::MatrixXd seedCov = sqrtWts.asDiagonal()*covKernel->BuildCovariance(seedPts)*sqrtWts.asDiagonal();
21  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigSolver;
22  eigSolver.compute(seedCov);
23 
24  double minEig = eigSolver.eigenvalues().minCoeff();
25  if(minEig<=0){
26  std::cerr << "YIKES! The covariance matrix wasn't positive definite.";
27  assert(minEig>0);
28  }
29 
30  modeEigs = eigSolver.eigenvalues().tail(numModes).reverse();
31  modeVecs = eigSolver.eigenvectors().rightCols(numModes).rowwise().reverse();
32 }
33 
34 Eigen::MatrixXd KarhunenLoeveExpansion::GetModes(Eigen::Ref<const Eigen::MatrixXd> const& pts) const
35 {
36 
37  // Build the cross covariance between the seed points and the evaluation points
38  Eigen::MatrixXd crossCov = covKernel->BuildCovariance(pts,seedPts);
39 
40  //Eigen::VectorXd scale = seedWts.array().sqrt()/modeEigs.array();
41  return crossCov * seedWts.array().sqrt().matrix().asDiagonal() * modeVecs * modeEigs.array().inverse().sqrt().matrix().asDiagonal();// * scale.asDiagonal();
42 }
43 
44 
46 {
47  return modeEigs.size();
48 }
virtual Eigen::MatrixXd GetModes(Eigen::Ref< const Eigen::MatrixXd > const &pts) const override
KarhunenLoeveExpansion(std::shared_ptr< KernelBase > kernelIn, Eigen::MatrixXd const &seedPtsIn, Eigen::VectorXd const &seedWtsIn, boost::property_tree::ptree options=boost::property_tree::ptree())
virtual unsigned int NumModes() const override