EmrysFilter 0.1.0
State estimation utilities in C++23 using Eigen
Loading...
Searching...
No Matches
Emrys::Product< Space_, Scalar_, Factors_ > Class Template Reference

The product of independent densities, each on its own coordinates of a space. More...

#include <EmrysFilter/Distributions/Product.h>

Public Types

using Scalar = Scalar_
 the scalar type
using Index = Eigen::Index
 Eigen's index type.
using SpaceType = Space_
 the space the values live in
using MomentsType = Moments<Space_, Scalar_>
 a mean and covariance together
using VectorType = typename MomentsType::VectorType
 a value
using MatrixType = typename MomentsType::MatrixType
 a matrix over differences
template<std::size_t K>
using FactorType = std::tuple_element_t<K, std::tuple<Factors_...>>
 The type of factor K.

Public Member Functions

template<typename... Placed>
requires (sizeof...(Placed) == sizeof...(Factors_) && (std::is_same_v<typename internal::PlacedFactor<Placed>::template TypeIn
<Space_>, Factors_> && ...))
 Product (Space_ space, Placed... placed)
 The product of the factors placed, made by on() or onEach(), in space.
template<typename... Placed>
 Product (Space_ space, Factors< Placed... > const &factors)
 The product of factors placed in space (see Factors).
Index valueDimension () const
Index differenceDimension () const
Space_ const & space () const
template<std::size_t K>
FactorType< K > const & factor () const
template<std::size_t K>
auto const & indices () const
template<typename Fn>
void forEachFactor (Fn &&fn) const
 Calls fn(density, indices) for each factor in order, with its density (a view, for parameters) and the coordinates it covers.
template<typename Derived>
Scalar logDensity (Eigen::MatrixBase< Derived > const &x) const
template<typename Derived>
Scalar density (Eigen::MatrixBase< Derived > const &x) const
auto moments () const -> std::conditional_t< MomentsMayFail, std::expected< MomentsType, MomentsError >, MomentsType >
template<typename Path>
decltype(auto) marginal () const &
template<typename Path>
void marginal () const &&=delete
 A marginal of a temporary would outlive it.
template<typename GivenPath, typename Derived>
auto conditional (Eigen::MatrixBase< Derived > const &values) const

Static Public Attributes

static constexpr int ValueDimensionAtCompileTime = MomentsType::ValueDimensionAtCompileTime
 the length of a value, or Eigen::Dynamic
static constexpr int MaxValueDimensionAtCompileTime = MomentsType::MaxValueDimensionAtCompileTime
 a bound on the length of a value, or Eigen::Dynamic
static constexpr int DifferenceDimensionAtCompileTime = MomentsType::DifferenceDimensionAtCompileTime
 the length of a difference and the rows of a covariance, or Eigen::Dynamic
static constexpr int MaxDifferenceDimensionAtCompileTime = MomentsType::MaxDifferenceDimensionAtCompileTime
 a bound on the length of a difference, or Eigen::Dynamic
static constexpr std::size_t FactorCount = sizeof...(Factors_)
 the number of factors

Detailed Description

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
class Emrys::Product< Space_, Scalar_, Factors_ >

The product of independent densities, each on its own coordinates of a space.

Represents \( p(x) = \prod_k p_k(x_{S_k}) \), where the index sets \( S_k \) partition the coordinates: the joint density of independent blocks. A prior that is Gaussian in position, uniform in heading, and Gamma in speed is one product:

auto const prior = Emrys::Product{State{}, on<Position>(position), // a GaussianParams<Position>
on<Speed>(Gamma<Speed>::create(4.0, 0.5).value())};
static std::expected< Gamma, GammaError > create(Space_ space, Shape const &shape, Scale const &scale)
Definition Gamma.h:158
The product of independent densities, each on its own coordinates of a space.
Definition Product.h:772
The uniform distribution over a box in a space.
Definition Uniform.h:69
auto on(Model const &model, Renames const &...)
Definition Placement.h:1067

Each factor's space must be the space of the block it covers: a factor placed by on<Path>() is built in that block's space, a tag derived from it, or with both linear. Sampling fills each factor's rows; log-densities add.

A factor is a density or parameters (see ProductFactor), held by value. The product checks parameters with toDensity() when it is built, and asserts that this succeeds; call their toDensity() first to handle a failure. A factor that is a view, such as a Gaussian, checks its parameters, which then must not change while the product is read, so prefer passing the parameters.

marginal<Path>() and conditional<GivenPath>() work on the factors' blocks, matched by the factors' space types:

  • a path through a factor's space type, or to a block inside it, is that factor's own marginal or conditional, and the other factors are unchanged;
  • a path to a block made of whole factors selects those factors, or drops them when given. A marginal or conditional with one factor left returns that factor alone. Marginals return parameters or values, never views of this product. A conditional that may fail, such as a Student-t's or a Gaussian's that must factorize, returns a std::expected.
Template Parameters
Space_space of the whole vector (see Spaces)
Scalar_scalar type, shared by every factor
Factors_the factors' types, in order
See also
on(), drawSamples(Product const &, Eigen::Index, G &)

Constructor & Destructor Documentation

◆ Product()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<typename... Placed>
requires (sizeof...(Placed) == sizeof...(Factors_) && (std::is_same_v<typename internal::PlacedFactor<Placed>::template TypeIn
<Space_>, Factors_> && ...))
Emrys::Product< Space_, Scalar_, Factors_ >::Product ( Space_ space,
Placed... placed )
inlineexplicit

The product of the factors placed, made by on() or onEach(), in space.

Their indices must partition the space, and each factor's space must be that of the block it covers.

Member Function Documentation

◆ conditional()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<typename GivenPath, typename Derived>
auto Emrys::Product< Space_, Scalar_, Factors_ >::conditional ( Eigen::MatrixBase< Derived > const & values) const
inlinenodiscard
Returns
the distribution of the other coordinates given that the subspace named by the path GivenPath (see idx()) equals values:
  • for a path to a block inside a factor's space, that factor's conditional in place of the factor, and the other factors unchanged;
  • for a factor's whole block, or a block made of whole factors, the remaining factors, since the factors are independent.

The result is in the space of the remaining blocks (see GaussianParams::conditional<GivenPath>()); with one factor left, it is that factor (or its conditional) alone. When the conditioned factor's conditional can fail, the result is a std::expected with its error, including a failure to factorize a Gaussian it returns.

◆ density()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<typename Derived>
Scalar Emrys::Product< Space_, Scalar_, Factors_ >::density ( Eigen::MatrixBase< Derived > const & x) const
inlinenodiscard
Returns
the density at x
Warning
Underflows easily in high dimensions; prefer logDensity().

◆ differenceDimension()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
Index Emrys::Product< Space_, Scalar_, Factors_ >::differenceDimension ( ) const
inlinenodiscard
Returns
the same: a product's space has plain differences

◆ factor()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<std::size_t K>
FactorType< K > const & Emrys::Product< Space_, Scalar_, Factors_ >::factor ( ) const
inlinenodiscard
Returns
factor K, as stored

◆ indices()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<std::size_t K>
auto const & Emrys::Product< Space_, Scalar_, Factors_ >::indices ( ) const
inlinenodiscard
Returns
the coordinates factor K covers

◆ logDensity()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<typename Derived>
Scalar Emrys::Product< Space_, Scalar_, Factors_ >::logDensity ( Eigen::MatrixBase< Derived > const & x) const
inlinenodiscard
Returns
the log of the density at x: the sum of the factors' log-densities

◆ marginal()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
template<typename Path>
decltype(auto) Emrys::Product< Space_, Scalar_, Factors_ >::marginal ( ) const &
inlinenodiscard
Returns
the distribution of the subspace named by the tag path Path (see idx()), read from this product rather than copied where it can be (decisions.md [element-marginals]):
  • for a factor's whole block, a reference to that factor;
  • for a block inside a factor, that factor's own marginal, a view for a factor that gives one, such as a GaussianParams, and a value for one that gives a value;
  • for a block made of several whole factors, the product of those factors in that subspace, which copies them.

◆ moments()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
auto Emrys::Product< Space_, Scalar_, Factors_ >::moments ( ) const -> std::conditional_t< MomentsMayFail, std::expected< MomentsType, MomentsError >, MomentsType >
inlinenodiscard
Returns
the mean and covariance: each factor's moments in its coordinates, and zero covariance between factors. If any factor's moments() can fail, so can this, with the first factor's error.

◆ space()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
Space_ const & Emrys::Product< Space_, Scalar_, Factors_ >::space ( ) const
inlinenodiscard
Returns
the space of the whole vector, with its sizes

◆ valueDimension()

template<Space Space_, RealScalar Scalar_, ProductFactor... Factors_>
Index Emrys::Product< Space_, Scalar_, Factors_ >::valueDimension ( ) const
inlinenodiscard
Returns
the number of coordinates of the space

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