24#ifndef LSST_MEAS_MODELFIT_Mixture_h_INCLUDED
25#define LSST_MEAS_MODELFIT_Mixture_h_INCLUDED
29#include "Eigen/Cholesky"
30#include "Eigen/StdVector"
42namespace lsst {
namespace meas {
namespace modelfit {
102 Eigen::LLT<Matrix> _sigmaLLT;
197 template <
typename Derived>
200 return component.
weight * _evaluate(
z) / component._sqrtDet;
208 template <
typename Derived>
224 ndarray::Array<Scalar const,2,1>
const &
x,
225 ndarray::Array<Scalar,1,0>
const & p
235 ndarray::Array<Scalar const,2,1>
const &
x,
236 ndarray::Array<Scalar,2,1>
const & p
247 ndarray::Array<Scalar const,1,1>
const &
x,
248 ndarray::Array<Scalar,1,1>
const & gradient,
249 ndarray::Array<Scalar,2,1>
const & hessian
260 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &
x,
261 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> & gradient,
262 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> & hessian
272 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &
x,
273 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> & gradient
307 ndarray::Array<Scalar const,2,1>
const &
x,
308 ndarray::Array<Scalar const,1,0>
const &
w,
323 ndarray::Array<Scalar const,2,1>
const &
x,
324 ndarray::Array<Scalar const,1,0>
const &
w,
339 ndarray::Array<Scalar const,2,1>
const &
x,
375 template <
typename A,
typename B,
typename C>
376 void _evaluateDerivativesImpl(A
const &
x,
379 bool computeHessian =
true)
const;
381 template <
typename Derived>
382 Scalar _computeZ(
Component const & component, Eigen::MatrixBase<Derived>
const &
x)
const {
383 _workspace =
x - component._mu;
384 component._sigmaLLT.matrixL().solveInPlace(_workspace);
385 return _workspace.squaredNorm();
389 void updateDampedSigma(
int k,
Matrix const &
sigma,
double tau1,
double tau2);
399 mutable Vector _workspace;
afw::table::Key< double > sigma
table::Key< table::Array< int > > components
A class that can be used to generate sequences of random numbers according to a number of different a...
An object passed to Persistable::write to allow it to persist itself.
A CRTP facade class for subclasses of Persistable.
A base class for objects that can be persisted via afw::table::io Archive classes.
A weighted Student's T or Gaussian distribution used as a component in a Mixture.
friend std::ostream & operator<<(std::ostream &os, MixtureComponent const &self)
MixtureComponent(Scalar weight_, Vector const &mu, Matrix const &sigma)
Default-construct a mixture component with the given parameters.
MixtureComponent project(int dim) const
Project the distribution onto the given dimension (marginalize over all others)
MixtureComponent project(int dim1, int dim2) const
Project the distribution onto the given dimensions (marginalize over all others)
Vector getMu() const
Get/set the location parameter (mean/median/mode) of this component.
Scalar weight
Weight of this distribution in the mixture.
void setSigma(Matrix const &sigma)
void setMu(Vector const &mu)
Matrix getSigma() const
Get/set the shape/size parameter.
MixtureComponent & operator=(MixtureComponent const &other)
int getDimension() const
Return the number of dimensions.
MixtureComponent(int dim)
Default-construct a mixture component with weight=1, mu=0, sigma=identity.
Component const & operator[](std::size_t i) const
void updateEM(ndarray::Array< Scalar const, 2, 1 > const &x, ndarray::Array< Scalar const, 1, 0 > const &w, Scalar tau1=0.0, Scalar tau2=0.5)
Perform an Expectation-Maximization step, updating the component parameters to match the given weight...
Scalar getDegreesOfFreedom() const
Get the number of degrees of freedom in the component Student's T distributions (inf=Gaussian)
void updateEM(ndarray::Array< Scalar const, 2, 1 > const &x, UpdateRestriction const &restriction, Scalar tau1=0.0, Scalar tau2=0.5)
Perform an Expectation-Maximization step, updating the component parameters to match the given unweig...
std::size_t size() const
Return the number of components.
friend std::ostream & operator<<(std::ostream &os, Mixture const &self)
const_iterator end() const
Scalar evaluate(Eigen::MatrixBase< Derived > const &x) const
Evaluate the mixture distribution probability density function (PDF) at the given points.
void evaluateDerivatives(Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &gradient) const
Evaluate the derivative of the distribution at the given point.
std::shared_ptr< Mixture > project(int dim) const
Project the distribution onto the given dimensions (marginalize over all others)
virtual bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Scalar evaluate(Component const &component, Eigen::MatrixBase< Derived > const &x) const
Evaluate the probability density at the given point for the given component distribution.
const_iterator begin() const
Component & operator[](std::size_t i)
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
void normalize()
Iterate over all components, rescaling their weights so they sum to one.
void evaluateComponents(ndarray::Array< Scalar const, 2, 1 > const &x, ndarray::Array< Scalar, 2, 1 > const &p) const
Evaluate the contributions of each component to the full probability at the given points.
void evaluateDerivatives(ndarray::Array< Scalar const, 1, 1 > const &x, ndarray::Array< Scalar, 1, 1 > const &gradient, ndarray::Array< Scalar, 2, 1 > const &hessian) const
Evaluate the derivative of the distribution at the given point.
ComponentList::iterator iterator
void shift(int dim, Scalar offset)
Shift the mixture in the given dimension, adding the given offset to all mu vectors.
ComponentList::const_iterator const_iterator
virtual std::shared_ptr< Mixture > clone() const
Polymorphic deep copy.
iterator begin()
Iterator and indexed access to components.
virtual int getComponentCount() const
Return the number of components.
void evaluateDerivatives(Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &gradient, Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &hessian) const
Evaluate the derivative of the distribution at the given point.
Mixture(int dim, ComponentList &components, Scalar df=std::numeric_limits< Scalar >::infinity())
Construct a mixture model.
MixtureUpdateRestriction UpdateRestriction
std::shared_ptr< Mixture > project(int dim1, int dim2) const
Project the distribution onto the given dimensions (marginalize over all others)
void draw(afw::math::Random &rng, ndarray::Array< Scalar, 2, 1 > const &x) const
Draw random variates from the distribution.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
void updateEM(ndarray::Array< Scalar const, 2, 1 > const &x, ndarray::Array< Scalar const, 1, 0 > const &w, UpdateRestriction const &restriction, Scalar tau1=0.0, Scalar tau2=0.5)
Perform an Expectation-Maximization step, updating the component parameters to match the given weight...
int getDimension() const
Return the number of dimensions.
void setDegreesOfFreedom(Scalar df=std::numeric_limits< Scalar >::infinity())
Set the number of degrees of freedom in the component Student's T distributions (inf=Gaussian)
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
MixtureComponent Component
void evaluate(ndarray::Array< Scalar const, 2, 1 > const &x, ndarray::Array< Scalar, 1, 0 > const &p) const
Evaluate the distribution probability density function (PDF) at the given points.
std::size_t clip(Scalar threshold=0.0)
Iterate over all components, removing those with weight less than or equal to threshold.
std::vector< Component > ComponentList
Helper class used to define restrictions to the form of the component parameters in Mixture::updateEM...
virtual void restrictMu(Vector &mu) const
virtual ~MixtureUpdateRestriction()
MixtureUpdateRestriction(int dim)
virtual void restrictSigma(Matrix &sigma) const
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > Matrix
double Scalar
Typedefs to be used for probability and parameter values.