LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 1104 of file KernelSolution.cc.

1110  :
1111  StaticKernelSolution<InputT>(basisList, fitForBackground),
1112  _hMat(hMat),
1113  _policy(policy)
1114  {};
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 1306 of file KernelSolution.cc.

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

Definition at line 1117 of file KernelSolution.cc.

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

1183  {
1184  if (includeHmat == true) {
1185  return (boost::shared_ptr<Eigen::MatrixXd>(
1186  new Eigen::MatrixXd(*(this->_mMat) + _lambda * (*_hMat))
1187  ));
1188  }
1189  else {
1190  return this->_mMat;
1191  }
1192  }
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 1195 of file KernelSolution.cc.

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