24 #ifndef LSST_MEAS_MODELFIT_Mixture_h_INCLUDED
25 #define LSST_MEAS_MODELFIT_Mixture_h_INCLUDED
29 #include "Eigen/Cholesky"
30 #include "Eigen/StdVector"
34 #include "lsst/base.h"
35 #include "lsst/afw/math/Random.h"
36 #include "lsst/afw/table/io/Persistable.h"
38 #include "lsst/afw/table/io/python.h"
41 namespace lsst {
namespace meas {
namespace modelfit {
97 void _stream(std::ostream & os,
int offset=0)
const;
101 Eigen::LLT<Matrix> _sigmaLLT;
127 class Mixture :
public afw::table::io::PersistableFacade<Mixture>,
public afw::table::io::Persistable {
156 std::size_t
size()
const {
return _components.size(); }
196 template <
typename Derived>
198 Scalar z = _computeZ(component, x);
199 return component.
weight * _evaluate(z) / component._sqrtDet;
207 template <
typename Derived>
223 ndarray::Array<Scalar const,2,1>
const & x,
224 ndarray::Array<Scalar,1,0>
const & p
234 ndarray::Array<Scalar const,2,1>
const & x,
235 ndarray::Array<Scalar,2,1>
const & p
246 ndarray::Array<Scalar const,1,1>
const & x,
247 ndarray::Array<Scalar,1,1>
const & gradient,
248 ndarray::Array<Scalar,2,1>
const & hessian
257 void draw(afw::math::Random & rng, ndarray::Array<Scalar,2,1>
const & x)
const;
282 ndarray::Array<Scalar const,2,1>
const & x,
283 ndarray::Array<Scalar const,1,0>
const & w,
298 ndarray::Array<Scalar const,2,1>
const & x,
299 ndarray::Array<Scalar const,1,0>
const & w,
314 ndarray::Array<Scalar const,2,1>
const & x,
334 friend std::ostream & operator<<(std::ostream & os,
Mixture const & self) {
347 virtual void write(OutputArchiveHandle & handle)
const;
351 template <
typename Derived>
352 Scalar _computeZ(
Component const & component, Eigen::MatrixBase<Derived>
const & x)
const {
353 _workspace = x - component._mu;
354 component._sigmaLLT.matrixL().solveInPlace(_workspace);
355 return _workspace.squaredNorm();
359 void updateDampedSigma(
int k,
Matrix const & sigma,
double tau1,
double tau2);
363 void _stream(std::ostream & os)
const;
369 mutable Vector _workspace;
375 #endif // !LSST_MEAS_MODELFIT_Mixture_h_INCLUDED
Component & operator[](std::size_t i)
Iterator and indexed access to components.
virtual void restrictSigma(Matrix &sigma) const
std::vector< Component > ComponentList
ComponentList::iterator iterator
MixtureComponent(int dim)
Default-construct a mixture component with weight=1, mu=0, sigma=identity.
const_iterator begin() const
Iterator and indexed access to components.
MixtureUpdateRestriction UpdateRestriction
Matrix getSigma() const
Get/set the shape/size parameter.
void draw(afw::math::Random &rng, ndarray::Array< Scalar, 2, 1 > const &x) const
Draw random variates from the distribution.
MixtureComponent Component
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...
virtual std::string getPythonModule() const
double Scalar
Typedefs to be used for probability and parameter values.
virtual ~MixtureUpdateRestriction()
void normalize()
Iterate over all components, rescaling their weights so they sum to one.
MixtureComponent & operator=(MixtureComponent const &other)
MixtureComponent project(int dim) const
Project the distribution onto the given dimension (marginalize over all others)
A weighted Student's T or Gaussian distribution used as a component in a Mixture. ...
Helper class used to define restrictions to the form of the component parameters in Mixture::updateEM...
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::size_t clip(Scalar threshold=0.0)
Iterate over all components, removing those with weight less than or equal to threshold.
const_iterator end() const
Iterator and indexed access to components.
std::size_t size() const
Return the number of components.
MixtureUpdateRestriction(int dim)
Scalar evaluate(Eigen::MatrixBase< Derived > const &x) const
Evaluate the mixture distribution probability density function (PDF) at the given points...
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > Matrix
Typedefs to be used for probability and parameter values.
int getDimension() const
Return the number of dimensions.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Typedefs to be used for probability and parameter values.
virtual std::string getPersistenceName() const
Component const & operator[](std::size_t i) const
Iterator and indexed access to components.
ComponentList::const_iterator const_iterator
Scalar weight
Weight of this distribution in the mixture.
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.
virtual void restrictMu(Vector &mu) 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...
virtual void write(OutputArchiveHandle &handle) const
virtual bool isPersistable() const
Vector getMu() const
Get/set the location parameter (mean/median/mode) of this component.
void setSigma(Matrix const &sigma)
Get/set the shape/size parameter.
Scalar evaluate(Component const &component, Eigen::MatrixBase< Derived > const &x) const
Evaluate the probability density at the given point for the given component distribution.
virtual boost::shared_ptr< Mixture > clone() const
Polymorphic deep copy.
iterator end()
Iterator and indexed access to components.
void shift(int dim, Scalar offset)
Shift the mixture in the given dimension, adding the given offset to all mu vectors.
iterator begin()
Iterator and indexed access to components.
friend std::ostream & operator<<(std::ostream &os, MixtureComponent const &self)
Scalar getDegreesOfFreedom() const
Get the number of degrees of freedom in the component Student's T distributions (inf=Gaussian) ...
boost::shared_ptr< Mixture > project(int dim) const
Project the distribution onto the given dimensions (marginalize over all others)
virtual int getComponentCount() const
Return the number of components.
void setMu(Vector const &mu)
Get/set the location parameter (mean/median/mode) of this component.