30 inPi.rows() !=
static_cast<Eigen::Index
>(
p),
31 "MppcaT::set: inPi has {} rows, expected {} (p == sigma2.rows())",
35 mmu.rows() !=
static_cast<Eigen::Index
>(
p),
36 "MppcaT::set: mmu has {} rows, expected {} (p == sigma2.rows())",
41 "MppcaT::set: W has {} components, expected {} (p == sigma2.rows())",
44 for (
size_t c = 0; c <
p; ++c) {
48 W[c].rows() !=
static_cast<Eigen::Index
>(
d),
49 "MppcaT::set: W[{}] has {} rows, expected {} (d == mmu.cols())",
59 CovarianceMatrix covariance;
66 const T half_d_log_2pi = T(0.5) *
static_cast<T
>(
d) * std::log(
twopi<T>());
68 for (
size_t c = 0; c <
p; c++) {
72 covariance.reset(std::sqrt(sigma2(c)), W[c].transpose());
76 const Eigen::MatrixX<T> L_tmp = covariance.R()
77 .template triangularView<Eigen::Upper>()
78 .solve(Eigen::MatrixX<T>::Identity(
d,
d))
81 Cinv[c] = covariance.R().template triangularView<Eigen::Upper>().solve(L_tmp);
84 const T C_logDeterminant = covariance.logDeterminant();
92 Rpre(c) = std::log(inPi(c)) - T(0.5) * C_logDeterminant - half_d_log_2pi;
98 names = std::vector<std::string>(
d);
101template <
typename T,
typename T2>
103 std::vector<Eigen::MatrixX<T2>> result;
104 result.reserve(m_in.size());
105 for (
const auto& m : m_in) {
106 result.emplace_back(m.template
cast<T2>());
112template <
typename T2>
129 auto vecIsApprox = [](
auto& vL,
auto& vR) {
130 if (vL.size() != vR.size()) {
133 for (
size_t i = 0; i < vL.size(); i++) {
141 (
d == mppcaT.
d) && (
p == mppcaT.
p) && (
names == mppcaT.
names) &&
mu.isApprox(mppcaT.
mu) &&
142 vecIsApprox(
Cinv, mppcaT.
Cinv) && vecIsApprox(
L, mppcaT.
L) &&
Rpre.isApprox(mppcaT.
Rpre));
Efficient and numerically stable representation of a low-rank-plus-diagonal Gaussian covariance matri...
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
bool isApprox(T l, T r, T eps=Eps< T >(1e-4, 1e-6)) noexcept
Determines if two values are approximately equal within a given tolerance.
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
std::vector< Eigen::MatrixX< T2 > > castMatrices(momentum::span< const Eigen::MatrixX< T > > m_in)
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
constexpr T twopi() noexcept
Returns 2 * pi.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Mixture of Probabilistic Principal Component Analyzers (MPPCA).
size_t d
Dimensionality of the data space x.
void set(const VectorX< T > &pi, const MatrixX< T > &mmu, momentum::span< const MatrixX< T > > W, const VectorX< T > &sigma2)
Initializes the model from raw mixture parameters and precomputes Cinv, L, and Rpre for fast log-like...
std::vector< Eigen::MatrixX< T > > L
Per-component lower-triangular factors derived from the Cholesky-like decomposition of C_c (used as a...
std::vector< Eigen::MatrixX< T > > Cinv
Per-component inverse covariance matrices C_c^{-1} of size d x d.
bool isApprox(const MppcaT< T > &mppcaT) const
Returns true if every member matches mppcaT exactly (d, p, names) or approximately (mu,...
MppcaT< T2 > cast() const
Returns a copy of this model with all numeric data converted to scalar type T2.
Eigen::VectorX< T > Rpre
Per-component constant term of the log-likelihood: Rpre(c) = log(pi_c) - 0.5 * log|C_c| - (d/2) * log...
std::vector< std::string > names
Optional human-readable names for each of the d data dimensions.
Eigen::MatrixX< T > mu
Per-component mean vectors stored as a p x d matrix; row c is mu_c.
size_t p
Number of mixture components.