LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Friends | List of all members
lsst::meas::modelfit::Mixture Class Reference

#include <Mixture.h>

Inheritance diagram for lsst::meas::modelfit::Mixture:
lsst::afw::table::io::PersistableFacade< Mixture > lsst::afw::table::io::Persistable

Public Types

typedef MixtureComponent Component
 
typedef MixtureUpdateRestriction UpdateRestriction
 
typedef std::vector< ComponentComponentList
 
typedef ComponentList::iterator iterator
 
typedef ComponentList::const_iterator const_iterator
 

Public Member Functions

iterator begin ()
 Iterator and indexed access to components.
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
Componentoperator[] (std::size_t i)
 
Component const & operator[] (std::size_t i) const
 
std::size_t size () const
 Return the number of components.
 
virtual int getComponentCount () const
 Return the number of components.
 
std::shared_ptr< Mixtureproject (int dim) const
 Project the distribution onto the given dimensions (marginalize over all others)
 
std::shared_ptr< Mixtureproject (int dim1, int dim2) const
 Project the distribution onto the given dimensions (marginalize over all others)
 
int getDimension () const
 Return the number of dimensions.
 
void normalize ()
 Iterate over all components, rescaling their weights so they sum to one.
 
void shift (int dim, Scalar offset)
 Shift the mixture in the given dimension, adding the given offset to all mu vectors.
 
std::size_t clip (Scalar threshold=0.0)
 Iterate over all components, removing those with weight less than or equal to threshold.
 
Scalar getDegreesOfFreedom () const
 Get the number of degrees of freedom in the component Student's T distributions (inf=Gaussian)
 
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)
 
template<typename Derived >
Scalar evaluate (Component const &component, Eigen::MatrixBase< Derived > const &x) const
 Evaluate the probability density at the given point for the given component distribution.
 
template<typename Derived >
Scalar evaluate (Eigen::MatrixBase< Derived > const &x) const
 Evaluate the mixture distribution probability density function (PDF) at the given points.
 
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.
 
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.
 
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.
 
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.
 
void draw (afw::math::Random &rng, ndarray::Array< Scalar, 2, 1 > const &x) const
 Draw random variates from the distribution.
 
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 weighted samples.
 
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 weighted samples.
 
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 unweighted samples.
 
virtual std::shared_ptr< Mixtureclone () const
 Polymorphic deep copy.
 
 Mixture (int dim, ComponentList &components, Scalar df=std::numeric_limits< Scalar >::infinity())
 Construct a mixture model.
 
virtual bool isPersistable () const noexcept override
 Return true if this particular object can be persisted using afw::table::io.
 
void writeFits (std::string const &fileName, std::string const &mode="w") const
 Write the object to a regular FITS file.
 
void writeFits (fits::MemFileManager &manager, std::string const &mode="w") const
 Write the object to a FITS image in memory.
 
void writeFits (fits::Fits &fitsfile) const
 Write the object to an already-open FITS object.
 

Static Public Member Functions

static std::shared_ptr< MixturereadFits (fits::Fits &fitsfile)
 Read an object from an already open FITS object.
 
static std::shared_ptr< MixturereadFits (std::string const &fileName, int hdu=fits::DEFAULT_HDU)
 Read an object from a regular FITS file.
 
static std::shared_ptr< MixturereadFits (fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
 Read an object from a FITS file in memory.
 
static std::shared_ptr< MixturedynamicCast (std::shared_ptr< Persistable > const &ptr)
 Dynamically cast a shared_ptr.
 

Protected Types

using OutputArchiveHandle = io::OutputArchiveHandle
 

Protected Member Functions

std::string getPythonModule () const override
 Return the fully-qualified Python module that should be imported to guarantee that its factory is registered.
 
std::string getPersistenceName () const override
 Return the unique name used to persist this object and look up its factory.
 
void write (OutputArchiveHandle &handle) const override
 Write the object to one or more catalogs.
 

Friends

std::ostreamoperator<< (std::ostream &os, Mixture const &self)
 

Detailed Description

Definition at line 128 of file Mixture.h.

Member Typedef Documentation

◆ Component

Definition at line 131 of file Mixture.h.

◆ ComponentList

Definition at line 133 of file Mixture.h.

◆ const_iterator

typedef ComponentList::const_iterator lsst::meas::modelfit::Mixture::const_iterator

Definition at line 135 of file Mixture.h.

◆ iterator

typedef ComponentList::iterator lsst::meas::modelfit::Mixture::iterator

Definition at line 134 of file Mixture.h.

◆ OutputArchiveHandle

using lsst::afw::table::io::Persistable::OutputArchiveHandle = io::OutputArchiveHandle
protectedinherited

Definition at line 108 of file Persistable.h.

◆ UpdateRestriction

Definition at line 132 of file Mixture.h.

Constructor & Destructor Documentation

◆ Mixture()

lsst::meas::modelfit::Mixture::Mixture ( int dim,
ComponentList & components,
Scalar df = std::numeric_limitsScalar >::infinity() )
explicit

Construct a mixture model.

Parameters
[in]dimDimensionality of the distribution
[in]dfNumber of degrees of freedom for component Student's T distributions (inf=Gaussian)
[in]componentsList of components; will be emptied on return.

The components will be automatically normalized after construction.

Member Function Documentation

◆ begin() [1/2]

iterator lsst::meas::modelfit::Mixture::begin ( )
inline

Iterator and indexed access to components.

While mutable iterators and accessors are provided, any modifications to the component weights should be followed by a call to normalize(), as other member functions will not work properly if the mixture is not normalized.

Definition at line 146 of file Mixture.h.

146{ return _components.begin(); }
T begin(T... args)

◆ begin() [2/2]

const_iterator lsst::meas::modelfit::Mixture::begin ( ) const
inline

Definition at line 149 of file Mixture.h.

149{ return _components.begin(); }

◆ clip()

std::size_t lsst::meas::modelfit::Mixture::clip ( Scalar threshold = 0.0)

Iterate over all components, removing those with weight less than or equal to threshold.

The weights will be normalized if any are removed.

Returns
the number of components removed.

◆ clone()

virtual std::shared_ptr< Mixture > lsst::meas::modelfit::Mixture::clone ( ) const
virtual

Polymorphic deep copy.

◆ draw()

void lsst::meas::modelfit::Mixture::draw ( afw::math::Random & rng,
ndarray::Array< Scalar, 2, 1 > const & x ) const

Draw random variates from the distribution.

Parameters
[in,out]rngrandom number generator
[out]xarray of points, shape=(numSamples, dim)

◆ dynamicCast()

Dynamically cast a shared_ptr.

Dynamically cast a shared pointer and raise on failure.

You must provide an explicit template instantiation in the .cc file for each class that inherits from PersistableFacade. Designed to work around RTTI issues on macOS with hidden symbols;

Exceptions
lsst::pex::exceptions::LogicErrorif the cast fails

param[in] ptr The pointer to be cast.

Returns
The cast pointer.
Exceptions
lsst::pex::exceptions::TypeErrorIf the dynamic cast fails.

Definition at line 218 of file Persistable.cc.

◆ end() [1/2]

iterator lsst::meas::modelfit::Mixture::end ( )
inline

Definition at line 147 of file Mixture.h.

147{ return _components.end(); }
T end(T... args)

◆ end() [2/2]

const_iterator lsst::meas::modelfit::Mixture::end ( ) const
inline

Definition at line 150 of file Mixture.h.

150{ return _components.end(); }

◆ evaluate() [1/3]

template<typename Derived >
Scalar lsst::meas::modelfit::Mixture::evaluate ( Component const & component,
Eigen::MatrixBase< Derived > const & x ) const
inline

Evaluate the probability density at the given point for the given component distribution.

This evaluates the probability of a single component, including the current weight of that component.

Definition at line 198 of file Mixture.h.

198 {
199 Scalar z = _computeZ(component, x);
200 return component.weight * _evaluate(z) / component._sqrtDet;
201 }
double z
Definition Match.cc:44
double Scalar
Typedefs to be used for probability and parameter values.
Definition common.h:44

◆ evaluate() [2/3]

template<typename Derived >
Scalar lsst::meas::modelfit::Mixture::evaluate ( Eigen::MatrixBase< Derived > const & x) const
inline

Evaluate the mixture distribution probability density function (PDF) at the given points.

Parameters
[in]xpoint to evaluate, as an Eigen expression, shape=(dim,)

Definition at line 209 of file Mixture.h.

209 {
210 Scalar p = 0.0;
211 for (const_iterator i = begin(); i != end(); ++i) {
212 p += evaluate(*i, x);
213 }
214 return p;
215 }
Scalar evaluate(Component const &component, Eigen::MatrixBase< Derived > const &x) const
Evaluate the probability density at the given point for the given component distribution.
Definition Mixture.h:198
ComponentList::const_iterator const_iterator
Definition Mixture.h:135
iterator begin()
Iterator and indexed access to components.
Definition Mixture.h:146

◆ evaluate() [3/3]

void lsst::meas::modelfit::Mixture::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.

Parameters
[in]xarray of points, shape=(numSamples, dim)
[out]parray of probability values, shape=(numSamples,)

◆ evaluateComponents()

void lsst::meas::modelfit::Mixture::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.

Parameters
[in]xpoints to evaluate at, with number of columns equal to the number of dimensions
[in]parray to fill, with number of columns equal to the number of components

◆ evaluateDerivatives() [1/3]

void lsst::meas::modelfit::Mixture::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.

Parameters
[in]xpoint to evaluate the derivative, with size equal to the number of dimensions
[in]gradient1st derivative array to fill

◆ evaluateDerivatives() [2/3]

void lsst::meas::modelfit::Mixture::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.

Parameters
[in]xpoint to evaluate the derivative, with size equal to the number of dimensions
[in]gradient1st derivative array to fill
[in]hessian2nd derivative array to fill

◆ evaluateDerivatives() [3/3]

void lsst::meas::modelfit::Mixture::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.

Parameters
[in]xpoint to evaluate the derivative, with size equal to the number of dimensions
[in]gradient1st derivative array to fill
[in]hessian2nd derivative array to fill

◆ getComponentCount()

virtual int lsst::meas::modelfit::Mixture::getComponentCount ( ) const
inlinevirtual

Return the number of components.

Definition at line 160 of file Mixture.h.

160{ return size(); }
std::size_t size() const
Return the number of components.
Definition Mixture.h:157

◆ getDegreesOfFreedom()

Scalar lsst::meas::modelfit::Mixture::getDegreesOfFreedom ( ) const
inline

Get the number of degrees of freedom in the component Student's T distributions (inf=Gaussian)

Definition at line 187 of file Mixture.h.

187{ return _df; }

◆ getDimension()

int lsst::meas::modelfit::Mixture::getDimension ( ) const
inline

Return the number of dimensions.

Definition at line 169 of file Mixture.h.

169{ return _dim; }

◆ getPersistenceName()

std::string lsst::meas::modelfit::Mixture::getPersistenceName ( ) const
overrideprotectedvirtual

Return the unique name used to persist this object and look up its factory.

Must be less than ArchiveIndexSchema::MAX_NAME_LENGTH characters.

Reimplemented from lsst::afw::table::io::Persistable.

◆ getPythonModule()

std::string lsst::meas::modelfit::Mixture::getPythonModule ( ) const
inlineoverrideprotectedvirtual

Return the fully-qualified Python module that should be imported to guarantee that its factory is registered.

Must be less than ArchiveIndexSchema::MAX_MODULE_LENGTH characters.

Will be ignored if empty.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 368 of file Mixture.h.

368{ return "lsst.meas.modelfit"; }

◆ isPersistable()

virtual bool lsst::meas::modelfit::Mixture::isPersistable ( ) const
inlineoverridevirtualnoexcept

Return true if this particular object can be persisted using afw::table::io.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 364 of file Mixture.h.

364{ return true; }

◆ normalize()

void lsst::meas::modelfit::Mixture::normalize ( )

Iterate over all components, rescaling their weights so they sum to one.

◆ operator[]() [1/2]

Component & lsst::meas::modelfit::Mixture::operator[] ( std::size_t i)
inline

Definition at line 152 of file Mixture.h.

152{ return _components[i]; }

◆ operator[]() [2/2]

Component const & lsst::meas::modelfit::Mixture::operator[] ( std::size_t i) const
inline

Definition at line 153 of file Mixture.h.

153{ return _components[i]; }

◆ project() [1/2]

std::shared_ptr< Mixture > lsst::meas::modelfit::Mixture::project ( int dim) const

Project the distribution onto the given dimensions (marginalize over all others)

◆ project() [2/2]

std::shared_ptr< Mixture > lsst::meas::modelfit::Mixture::project ( int dim1,
int dim2 ) const

Project the distribution onto the given dimensions (marginalize over all others)

◆ readFits() [1/3]

static std::shared_ptr< Mixture > lsst::afw::table::io::PersistableFacade< Mixture >::readFits ( fits::Fits & fitsfile)
inlinestaticinherited

Read an object from an already open FITS object.

Parameters
[in]fitsfileFITS object to read from, already positioned at the desired HDU.

Definition at line 183 of file Persistable.h.

◆ readFits() [2/3]

static std::shared_ptr< Mixture > lsst::afw::table::io::PersistableFacade< Mixture >::readFits ( fits::MemFileManager & manager,
int hdu = fits::DEFAULT_HDU )
inlinestaticinherited

Read an object from a FITS file in memory.

Parameters
[in]managerManager for the memory to read from.
[in]hduHDU to read, where 0 is the primary. The special value of afw::fits::DEFAULT_HDU skips the primary HDU if it is empty.

Definition at line 205 of file Persistable.h.

◆ readFits() [3/3]

static std::shared_ptr< Mixture > lsst::afw::table::io::PersistableFacade< Mixture >::readFits ( std::string const & fileName,
int hdu = fits::DEFAULT_HDU )
inlinestaticinherited

Read an object from a regular FITS file.

Parameters
[in]fileNameName of the file to read.
[in]hduHDU to read, where 0 is the primary. The special value of afw::fits::DEFAULT_HDU skips the primary HDU if it is empty.

Definition at line 194 of file Persistable.h.

◆ setDegreesOfFreedom()

void lsst::meas::modelfit::Mixture::setDegreesOfFreedom ( Scalar df = std::numeric_limitsScalar >::infinity())

Set the number of degrees of freedom in the component Student's T distributions (inf=Gaussian)

◆ shift()

void lsst::meas::modelfit::Mixture::shift ( int dim,
Scalar offset )

Shift the mixture in the given dimension, adding the given offset to all mu vectors.

◆ size()

std::size_t lsst::meas::modelfit::Mixture::size ( ) const
inline

Return the number of components.

Definition at line 157 of file Mixture.h.

157{ return _components.size(); }
T size(T... args)

◆ updateEM() [1/3]

void lsst::meas::modelfit::Mixture::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 weighted samples.

Parameters
[in]xarray of variables, shape=(numSamples, dim)
[in]warray of weights, shape=(numSamples,)
[in]tau1damping parameter (see below)
[in]tau2damping parameter (see below)

The updates to the \(\sigma\) matrices are damped according to:

\[ \sigma_d = \alpha\sigma_1 + (1-\alpha)\sigma_0 \]

Where \(\sigma_0\) is the previous matrix, \(\sigma_1\) is the undamped update, and \(\sigma_d\) is the damped update. The parameter \(\alpha\) is set by the ratio of the determinants:

\[ r \equiv \frac{|\sigma_1|}{|\sigma_0|} \]

When \(r \ge \tau_1\), \(\alpha=1\); when \(r \lt \tau_1\), it is rolled off quadratically to \(\tau_2\).

◆ updateEM() [2/3]

void lsst::meas::modelfit::Mixture::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 weighted samples.

Parameters
[in]xarray of variables, shape=(numSamples, dim)
[in]warray of weights, shape=(numSamples,)
[in]restrictionFunctor used to restrict the form of the updated mu and sigma
[in]tau1damping parameter (see Mixture::updateEM)
[in]tau2damping parameter (see Mixture::updateEM)

◆ updateEM() [3/3]

void lsst::meas::modelfit::Mixture::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 unweighted samples.

Parameters
[in]xarray of variables, shape=(numSamples, dim)
[in]restrictionFunctor used to restrict the form of the updated mu and sigma
[in]tau1damping parameter (see Mixture::updateEM)
[in]tau2damping parameter (see Mixture::updateEM)

◆ write()

void lsst::meas::modelfit::Mixture::write ( OutputArchiveHandle & handle) const
overrideprotectedvirtual

Write the object to one or more catalogs.

The handle object passed to this function provides an interface for adding new catalogs and adding nested objects to the same archive (while checking for duplicates). See OutputArchiveHandle for more information.

Reimplemented from lsst::afw::table::io::Persistable.

◆ writeFits() [1/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::Fits & fitsfile) const
inherited

Write the object to an already-open FITS object.

Parameters
[in]fitsfileOpen FITS object to write to.

Definition at line 18 of file Persistable.cc.

18 {
19 OutputArchive archive;
20 archive.put(this);
21 archive.writeFits(fitsfile);
22}

◆ writeFits() [2/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::MemFileManager & manager,
std::string const & mode = "w" ) const
inherited

Write the object to a FITS image in memory.

Parameters
[in]managerName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 29 of file Persistable.cc.

29 {
30 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
31 writeFits(fitsfile);
32}
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.

◆ writeFits() [3/3]

void lsst::afw::table::io::Persistable::writeFits ( std::string const & fileName,
std::string const & mode = "w" ) const
inherited

Write the object to a regular FITS file.

Parameters
[in]fileNameName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 24 of file Persistable.cc.

24 {
25 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
26 writeFits(fitsfile);
27}

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
Mixture const & self )
friend

Definition at line 359 of file Mixture.h.

359 {
360 self._stream(os);
361 return os;
362 }
std::ostream * os
Definition Schema.cc:557

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