LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::ip::diffim::RegularizedKernelSolution< InputT > Class Template Reference

#include <KernelSolution.h>

Inheritance diagram for lsst::ip::diffim::RegularizedKernelSolution< InputT >:
lsst::ip::diffim::StaticKernelSolution< InputT > lsst::ip::diffim::KernelSolution

Public Types

typedef boost::shared_ptr
< RegularizedKernelSolution
< InputT > > 
Ptr
 
- Public Types inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
typedef boost::shared_ptr
< StaticKernelSolution< InputT > > 
Ptr
 
- Public Types inherited from lsst::ip::diffim::KernelSolution
enum  KernelSolvedBy {
  NONE = 0, CHOLESKY_LDLT = 1, CHOLESKY_LLT = 2, LU = 3,
  EIGENVECTOR = 4
}
 
enum  ConditionNumberType { EIGENVALUE = 0, SVD = 1 }
 
typedef boost::shared_ptr
< KernelSolution
Ptr
 
typedef
lsst::afw::math::Kernel::Pixel 
PixelT
 
typedef
lsst::afw::image::Image
< lsst::afw::math::Kernel::Pixel
ImageT
 

Public Member Functions

 RegularizedKernelSolution (lsst::afw::math::KernelList const &basisList, bool fitForBackground, boost::shared_ptr< Eigen::MatrixXd > hMat, lsst::pex::policy::Policy policy)
 
virtual ~RegularizedKernelSolution ()
 
void solve ()
 
double getLambda ()
 
double estimateRisk (double maxCond)
 
boost::shared_ptr
< Eigen::MatrixXd > 
getM (bool includeHmat=true)
 
- Public Member Functions inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
 StaticKernelSolution (lsst::afw::math::KernelList const &basisList, bool fitForBackground)
 
virtual ~StaticKernelSolution ()
 
virtual void build (lsst::afw::image::Image< InputT > const &templateImage, lsst::afw::image::Image< InputT > const &scienceImage, lsst::afw::image::Image< lsst::afw::image::VariancePixel > const &varianceEstimate)
 
virtual
lsst::afw::math::Kernel::Ptr 
getKernel ()
 
virtual
lsst::afw::image::Image
< lsst::afw::math::Kernel::Pixel >
::Ptr 
makeKernelImage ()
 
virtual double getBackground ()
 
virtual double getKsum ()
 
virtual std::pair
< boost::shared_ptr
< lsst::afw::math::Kernel >
, double > 
getSolutionPair ()
 
- Public Member Functions inherited from lsst::ip::diffim::KernelSolution
 KernelSolution (boost::shared_ptr< Eigen::MatrixXd > mMat, boost::shared_ptr< Eigen::VectorXd > bVec, bool fitForBackground)
 
 KernelSolution (bool fitForBackground)
 
 KernelSolution ()
 
virtual ~KernelSolution ()
 
virtual void solve (Eigen::MatrixXd mMat, Eigen::VectorXd bVec)
 
KernelSolvedBy getSolvedBy ()
 
virtual double getConditionNumber (ConditionNumberType conditionType)
 
virtual double getConditionNumber (Eigen::MatrixXd mMat, ConditionNumberType conditionType)
 
boost::shared_ptr
< Eigen::MatrixXd > 
getM ()
 
boost::shared_ptr
< Eigen::VectorXd > 
getB ()
 
void printM ()
 
void printB ()
 
void printA ()
 
int getId () const
 

Private Member Functions

std::vector< double > _createLambdaSteps ()
 

Private Attributes

boost::shared_ptr
< Eigen::MatrixXd > 
_hMat
 Regularization weights. More...
 
double _lambda
 Overall regularization strength. More...
 
lsst::pex::policy::Policy _policy
 

Additional Inherited Members

- Protected Member Functions inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
void _setKernel ()
 Set kernel after solution. More...
 
void _setKernelUncertainty ()
 Not implemented. More...
 
- Protected Attributes inherited from lsst::ip::diffim::StaticKernelSolution< InputT >
boost::shared_ptr
< Eigen::MatrixXd > 
_cMat
 K_i x R. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_iVec
 Vectorized I. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_ivVec
 Inverse variance. More...
 
lsst::afw::math::Kernel::Ptr _kernel
 Derived single-object convolution kernel. More...
 
double _background
 Derived differential background estimate. More...
 
double _kSum
 Derived kernel sum. More...
 
- Protected Attributes inherited from lsst::ip::diffim::KernelSolution
int _id
 Unique ID for object. More...
 
boost::shared_ptr
< Eigen::MatrixXd > 
_mMat
 Derived least squares M matrix. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_bVec
 Derived least squares B vector. More...
 
boost::shared_ptr
< Eigen::VectorXd > 
_aVec
 Derived least squares solution matrix. More...
 
KernelSolvedBy _solvedBy
 Type of algorithm used to make solution. More...
 
bool _fitForBackground
 Background terms included in fit. More...
 
- Static Protected Attributes inherited from lsst::ip::diffim::KernelSolution
static int _SolutionId = 0
 Unique identifier for solution. More...
 

Detailed Description

template<typename InputT>
class lsst::ip::diffim::RegularizedKernelSolution< InputT >

Definition at line 147 of file KernelSolution.h.

Member Typedef Documentation

template<typename InputT >
typedef boost::shared_ptr<RegularizedKernelSolution<InputT> > lsst::ip::diffim::RegularizedKernelSolution< InputT >::Ptr

Definition at line 149 of file KernelSolution.h.

Constructor & Destructor Documentation

template<typename InputT >
lsst::ip::diffim::RegularizedKernelSolution< InputT >::RegularizedKernelSolution ( lsst::afw::math::KernelList const &  basisList,
bool  fitForBackground,
boost::shared_ptr< Eigen::MatrixXd >  hMat,
lsst::pex::policy::Policy  policy 
)

Definition at line 1103 of file KernelSolution.cc.

1109  :
1110  StaticKernelSolution<InputT>(basisList, fitForBackground),
1111  _hMat(hMat),
1112  _policy(policy)
1113  {};
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization weights.
template<typename InputT >
virtual lsst::ip::diffim::RegularizedKernelSolution< InputT >::~RegularizedKernelSolution ( )
inlinevirtual

Definition at line 156 of file KernelSolution.h.

156 {};

Member Function Documentation

template<typename InputT >
std::vector< double > lsst::ip::diffim::RegularizedKernelSolution< InputT >::_createLambdaSteps ( )
private

Definition at line 1305 of file KernelSolution.cc.

1305  {
1306  std::vector<double> lambdas;
1307 
1308  std::string lambdaStepType = _policy.getString("lambdaStepType");
1309  if (lambdaStepType == "linear") {
1310  double lambdaLinMin = _policy.getDouble("lambdaLinMin");
1311  double lambdaLinMax = _policy.getDouble("lambdaLinMax");
1312  double lambdaLinStep = _policy.getDouble("lambdaLinStep");
1313  for (double l = lambdaLinMin; l <= lambdaLinMax; l += lambdaLinStep) {
1314  lambdas.push_back(l);
1315  }
1316  }
1317  else if (lambdaStepType == "log") {
1318  double lambdaLogMin = _policy.getDouble("lambdaLogMin");
1319  double lambdaLogMax = _policy.getDouble("lambdaLogMax");
1320  double lambdaLogStep = _policy.getDouble("lambdaLogStep");
1321  for (double l = lambdaLogMin; l <= lambdaLogMax; l += lambdaLogStep) {
1322  lambdas.push_back(pow(10, l));
1323  }
1324  }
1325  else {
1326  throw LSST_EXCEPT(pexExcept::Exception, "lambdaStepType in Policy not recognized");
1327  }
1328  return lambdas;
1329  }
const std::string getString(const std::string &name) const
Definition: Policy.h:631
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
double getDouble(const std::string &name) const
Definition: Policy.h:617
template<typename InputT >
double lsst::ip::diffim::RegularizedKernelSolution< InputT >::estimateRisk ( double  maxCond)

Definition at line 1116 of file KernelSolution.cc.

1116  {
1117  Eigen::MatrixXd vMat = (this->_cMat)->jacobiSvd().matrixV();
1118  Eigen::MatrixXd vMatvMatT = vMat * vMat.transpose();
1119 
1120  /* Find pseudo inverse of mMat, which may be ill conditioned */
1121  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eVecValues(*(this->_mMat));
1122  Eigen::MatrixXd const& rMat = eVecValues.eigenvectors();
1123  Eigen::VectorXd eValues = eVecValues.eigenvalues();
1124  double eMax = eValues.maxCoeff();
1125  for (int i = 0; i != eValues.rows(); ++i) {
1126  if (eValues(i) == 0.0) {
1127  eValues(i) = 0.0;
1128  }
1129  else if ((eMax / eValues(i)) > maxCond) {
1130  pexLog::TTrace<5>("lsst.ip.diffim.RegularizedKernelSolution.estimateRisk",
1131  "Truncating eValue %d; %.5e / %.5e = %.5e vs. %.5e",
1132  i, eMax, eValues(i), eMax / eValues(i), maxCond);
1133  eValues(i) = 0.0;
1134  }
1135  else {
1136  eValues(i) = 1.0 / eValues(i);
1137  }
1138  }
1139  Eigen::MatrixXd mInv = rMat * eValues.asDiagonal() * rMat.transpose();
1140 
1141  std::vector<double> lambdas = _createLambdaSteps();
1142  std::vector<double> risks;
1143  for (unsigned int i = 0; i < lambdas.size(); i++) {
1144  double l = lambdas[i];
1145  Eigen::MatrixXd mLambda = *(this->_mMat) + l * (*_hMat);
1146 
1147  try {
1148  KernelSolution::solve(mLambda, *(this->_bVec));
1149  } catch (pexExcept::Exception &e) {
1150  LSST_EXCEPT_ADD(e, "Unable to solve regularized kernel matrix");
1151  throw e;
1152  }
1153  Eigen::VectorXd term1 = (this->_aVec->transpose() * vMatvMatT * *(this->_aVec));
1154  if (term1.size() != 1)
1155  throw LSST_EXCEPT(pexExcept::Exception, "Matrix size mismatch");
1156 
1157  double term2a = (vMatvMatT * mLambda.inverse()).trace();
1158 
1159  Eigen::VectorXd term2b = (this->_aVec->transpose() * (mInv * *(this->_bVec)));
1160  if (term2b.size() != 1)
1161  throw LSST_EXCEPT(pexExcept::Exception, "Matrix size mismatch");
1162 
1163  double risk = term1(0) + 2 * (term2a - term2b(0));
1164  pexLog::TTrace<6>("lsst.ip.diffim.RegularizedKernelSolution.estimateRisk",
1165  "Lambda = %.3f, Risk = %.5e",
1166  l, risk);
1167  pexLog::TTrace<7>("lsst.ip.diffim.RegularizedKernelSolution.estimateRisk",
1168  "%.5e + 2 * (%.5e - %.5e)",
1169  term1(0), term2a, term2b(0));
1170  risks.push_back(risk);
1171  }
1172  std::vector<double>::iterator it = min_element(risks.begin(), risks.end());
1173  int index = distance(risks.begin(), it);
1174  pexLog::TTrace<5>("lsst.ip.diffim.RegularizedKernelSolution.estimateRisk",
1175  "Minimum Risk = %.3e at lambda = %.3e", risks[index], lambdas[index]);
1176 
1177  return lambdas[index];
1178 
1179  }
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization weights.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
boost::shared_ptr< Eigen::VectorXd > _aVec
Derived least squares solution matrix.
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
boost::shared_ptr< Eigen::MatrixXd > _cMat
K_i x R.
#define LSST_EXCEPT_ADD(e, m)
Definition: Exception.h:51
template<typename InputT >
double lsst::ip::diffim::RegularizedKernelSolution< InputT >::getLambda ( )
inline

Definition at line 158 of file KernelSolution.h.

158 {return _lambda;}
double _lambda
Overall regularization strength.
template<typename InputT >
boost::shared_ptr< Eigen::MatrixXd > lsst::ip::diffim::RegularizedKernelSolution< InputT >::getM ( bool  includeHmat = true)

Definition at line 1182 of file KernelSolution.cc.

1182  {
1183  if (includeHmat == true) {
1184  return (boost::shared_ptr<Eigen::MatrixXd>(
1185  new Eigen::MatrixXd(*(this->_mMat) + _lambda * (*_hMat))
1186  ));
1187  }
1188  else {
1189  return this->_mMat;
1190  }
1191  }
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization weights.
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
double _lambda
Overall regularization strength.
template<typename InputT >
void lsst::ip::diffim::RegularizedKernelSolution< InputT >::solve ( )
virtual

Reimplemented from lsst::ip::diffim::StaticKernelSolution< InputT >.

Definition at line 1194 of file KernelSolution.cc.

1194  {
1195 
1196  pexLog::TTrace<5>("lsst.ip.diffim.RegularizedKernelSolution.solve",
1197  "cMat is %d x %d; vVec is %d; iVec is %d; hMat is %d x %d",
1198  (*this->_cMat).rows(), (*this->_cMat).cols(), (*this->_ivVec).size(),
1199  (*this->_iVec).size(), (*_hMat).rows(), (*_hMat).cols());
1200 
1201  if (DEBUG_MATRIX2) {
1202  std::cout << "ID: " << (this->_id) << std::endl;
1203  std::cout << "C:" << std::endl;
1204  std::cout << (*this->_cMat) << std::endl;
1205  std::cout << "Sigma^{-1}:" << std::endl;
1206  std::cout << Eigen::MatrixXd((*this->_ivVec).asDiagonal()) << std::endl;
1207  std::cout << "Y:" << std::endl;
1208  std::cout << (*this->_iVec) << std::endl;
1209  std::cout << "H:" << std::endl;
1210  std::cout << (*_hMat) << std::endl;
1211  }
1212 
1213 
1214  this->_mMat.reset(
1215  new Eigen::MatrixXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_cMat))
1216  );
1217  this->_bVec.reset(
1218  new Eigen::VectorXd(this->_cMat->transpose() * this->_ivVec->asDiagonal() * *(this->_iVec))
1219  );
1220 
1221 
1222  /* See N.R. 18.5
1223 
1224  Matrix equation to solve is Y = C a + N
1225  Y = vectorized version of I (I = image to not convolve)
1226  C_i = K_i (x) R (R = image to convolve)
1227  a = kernel coefficients
1228  N = noise
1229 
1230  If we reweight everything by the inverse square root of the noise
1231  covariance, we get a linear model with the identity matrix for
1232  the noise. The problem can then be solved using least squares,
1233  with the normal equations
1234 
1235  C^T Y = C^T C a
1236 
1237  or
1238 
1239  b = M a
1240 
1241  with
1242 
1243  b = C^T Y
1244  M = C^T C
1245  a = (C^T C)^{-1} C^T Y
1246 
1247 
1248  We can regularize the least square problem
1249 
1250  Y = C a + lambda a^T H a (+ N, which can be ignored)
1251 
1252  or the normal equations
1253 
1254  (C^T C + lambda H) a = C^T Y
1255 
1256 
1257  The solution to the regularization of the least squares problem is
1258 
1259  a = (C^T C + lambda H)^{-1} C^T Y
1260 
1261  The approximation to Y is
1262 
1263  C a = C (C^T C + lambda H)^{-1} C^T Y
1264 
1265  with smoothing matrix
1266 
1267  S = C (C^T C + lambda H)^{-1} C^T
1268 
1269  */
1270 
1271  std::string lambdaType = _policy.getString("lambdaType");
1272  if (lambdaType == "absolute") {
1273  _lambda = _policy.getDouble("lambdaValue");
1274  }
1275  else if (lambdaType == "relative") {
1276  _lambda = this->_mMat->trace() / this->_hMat->trace();
1277  _lambda *= _policy.getDouble("lambdaScaling");
1278  }
1279  else if (lambdaType == "minimizeBiasedRisk") {
1280  double tol = _policy.getDouble("maxConditionNumber");
1281  _lambda = estimateRisk(tol);
1282  }
1283  else if (lambdaType == "minimizeUnbiasedRisk") {
1285  }
1286  else {
1287  throw LSST_EXCEPT(pexExcept::Exception, "lambdaType in Policy not recognized");
1288  }
1289 
1290  pexLog::TTrace<5>("lsst.ip.diffim.KernelCandidate.build",
1291  "Applying kernel regularization with lambda = %.2e", _lambda);
1292 
1293 
1294  try {
1295  KernelSolution::solve(*(this->_mMat) + _lambda * (*_hMat), *(this->_bVec));
1296  } catch (pexExcept::Exception &e) {
1297  LSST_EXCEPT_ADD(e, "Unable to solve static kernel matrix");
1298  throw e;
1299  }
1300  /* Turn matrices into _kernel and _background */
1302  }
const std::string getString(const std::string &name) const
Definition: Policy.h:631
double max
Definition: attributes.cc:218
void _setKernel()
Set kernel after solution.
int _id
Unique ID for object.
boost::shared_ptr< Eigen::VectorXd > _iVec
Vectorized I.
boost::shared_ptr< Eigen::VectorXd > _bVec
Derived least squares B vector.
boost::shared_ptr< Eigen::MatrixXd > _hMat
Regularization weights.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
double getDouble(const std::string &name) const
Definition: Policy.h:617
boost::shared_ptr< Eigen::MatrixXd > _mMat
Derived least squares M matrix.
boost::shared_ptr< Eigen::MatrixXd > _cMat
K_i x R.
double _lambda
Overall regularization strength.
boost::shared_ptr< Eigen::VectorXd > _ivVec
Inverse variance.
#define LSST_EXCEPT_ADD(e, m)
Definition: Exception.h:51
#define DEBUG_MATRIX2

Member Data Documentation

template<typename InputT >
boost::shared_ptr<Eigen::MatrixXd> lsst::ip::diffim::RegularizedKernelSolution< InputT >::_hMat
private

Regularization weights.

Definition at line 165 of file KernelSolution.h.

template<typename InputT >
double lsst::ip::diffim::RegularizedKernelSolution< InputT >::_lambda
private

Overall regularization strength.

Definition at line 166 of file KernelSolution.h.

template<typename InputT >
lsst::pex::policy::Policy lsst::ip::diffim::RegularizedKernelSolution< InputT >::_policy
private

Definition at line 167 of file KernelSolution.h.


The documentation for this class was generated from the following files: