18#include <Eigen/Cholesky>
38template <Space Space_, RealScalar Scalar_>
41template <Space Space_, RealScalar Scalar_>
44template <
typename Source,
typename Selection,
typename SubSpace>
54template <Space Space_, RealScalar Scalar_ =
double>
73 [[nodiscard]]
static typename G::MomentsType &
76 gaussian.m_stamp.touchValues();
77 gaussian.m_factorValid =
false;
78 return gaussian.m_moments;
88struct FactorizedGaussian
91 template <
typename Space,
typename Scalar>
92 [[nodiscard]]
static Scalar
93 logDeterminant(GaussianParams<Space, Scalar>
const & gaussian)
95 EMRYS_FILTER_ASSERT(gaussian.m_factorValid &&
"the Gaussian must be factorized");
96 return gaussian.m_logDeterminant;
100 template <
typename Space,
typename Scalar>
101 [[nodiscard]]
static Scalar
102 logPeak(GaussianParams<Space, Scalar>
const & gaussian)
104 EMRYS_FILTER_ASSERT(gaussian.m_factorValid &&
"the Gaussian must be factorized");
107 * (internal::scalarFromCount<Scalar>(gaussian.differenceDimension()) *
log2Pi + gaussian.m_logDeterminant);
111 template <
typename Space,
typename Scalar,
typename Derived>
112 [[nodiscard]]
static Scalar
113 squaredMahalanobisDistance(GaussianParams<Space, Scalar>
const & gaussian, Eigen::MatrixBase<Derived>
const & x)
115 EMRYS_FILTER_ASSERT(gaussian.m_factorValid &&
"the Gaussian must be factorized");
118 gaussian.m_llt->matrixL().solveInPlace(residual);
123 template <
typename Space,
typename Scalar,
typename Derived>
124 [[nodiscard]]
static Scalar
125 logDensity(GaussianParams<Space, Scalar>
const & gaussian, Eigen::MatrixBase<Derived>
const & x)
127 return logPeak(gaussian) - Scalar(0.5) * squaredMahalanobisDistance(gaussian, x);
131 template <
typename Space,
typename Scalar>
132 [[nodiscard]]
static Eigen::LLT<typename GaussianParams<Space, Scalar>::MatrixType>
const &
133 llt(GaussianParams<Space, Scalar>
const & gaussian)
135 EMRYS_FILTER_ASSERT(gaussian.m_factorValid &&
"the Gaussian must be factorized");
136 auto const & llt = gaussian.m_llt;
141template <
template <
typename,
typename,
typename>
typename Marginal,
typename Path,
typename Source>
143tagMarginal(Source
const & source);
145template <
template <
typename,
typename,
typename>
typename Marginal,
typename Path,
typename Source>
147elementMarginal(Source
const & source, Eigen::Index element);
159 typename TargetSpace,
162conditionalGaussianIn(
163 Source
const & source,
164 Target
const & target,
166 Eigen::MatrixBase<Derived>
const & values,
167 TargetSpace
const & targetSpace,
168 GivenSpace
const & givenSpace
171 using Scalar =
typename Source::Scalar;
172 EMRYS_FILTER_ASSERT(distinctIndices(target.values,
given.values) &&
"target and given indices must be distinct");
173 if constexpr (WithEvidence)
175 using Result = std::expected<GaussianConditional<TargetSpace, Scalar>,
GaussianError>;
176 auto result = conditionMomentsWithEvidence(
178 source.covarianceLower(),
189 return Result{GaussianConditional<TargetSpace, Scalar>{
190 {targetSpace, std::move(result->moments.mean), std::move(result->moments.cov)},
191 result->logLikelihood
197 conditionMoments(source.mean(), source.covarianceLower(), target,
given, values, givenSpace, targetSpace);
198 return GaussianParams<TargetSpace, Scalar>{targetSpace, std::move(result.mean), std::move(result.cov)};
205template <
bool WithEv
idence,
typename GivenPath,
typename Source,
typename Derived>
207tagConditionalGaussian(Source
const & source, Eigen::MatrixBase<Derived>
const & values)
209 return tagConditional<GivenPath>(
211 [&](
auto const & target,
auto const &
given,
auto const & targetSpace,
auto const & givenSpace) {
212 return conditionalGaussianIn<WithEvidence>(source, target, given, values, targetSpace, givenSpace);
257template <Space Space_, RealScalar Scalar_ =
double>
299 requires internal::SizedByLength<Space_>
301 fitValueDimension(n);
307 requires internal::SizedByLength<Space_>
308 : m_moments{std::move(
moments)}
317 template <MatrixExpression Mean, CovarianceExpression Cov>
318 requires internal::SizedByLength<Space_>
320 : m_moments{std::forward<Mean>(
mean), std::forward<Cov>(cov)}
328 , m_space{std::move(
space)}
339 : m_space{std::move(
space)}
341 fitValueDimension(n);
349 : m_moments{std::move(
moments)}
350 , m_space{std::move(
space)}
356 template <MatrixExpression Mean, CovarianceExpression Cov>
358 : m_moments{std::forward<Mean>(
mean), std::forward<Cov>(cov)}
359 , m_space{std::move(
space)}
366 : m_moments{other.m_moments}
367 , m_space{other.m_space}
368 , m_llt{other.m_factorValid ? other.m_llt : std::nullopt}
369 , m_factorValid{other.m_factorValid}
373 m_logDeterminant = other.m_logDeterminant;
379 : m_moments{std::move(other.m_moments)}
380 , m_space{std::move(other.m_space)}
381 , m_llt{other.m_factorValid ? std::move(other.m_llt) : std::nullopt}
382 , m_factorValid{other.m_factorValid}
386 m_logDeterminant = other.m_logDeterminant;
388 other.m_stamp.touchShape();
389 other.m_factorValid =
false;
398 m_stamp.touchShape();
399 m_moments = other.m_moments;
400 m_space = other.m_space;
401 if (other.m_factorValid)
404 m_logDeterminant = other.m_logDeterminant;
406 m_factorValid = other.m_factorValid;
416 m_stamp.touchShape();
417 other.m_stamp.touchShape();
418 m_moments = std::move(other.m_moments);
419 m_space = std::move(other.m_space);
420 if (other.m_factorValid)
422 m_llt = std::move(other.m_llt);
423 m_logDeterminant = other.m_logDeterminant;
425 m_factorValid = other.m_factorValid;
426 other.m_factorValid =
false;
451 requires internal::SizedByLength<Space_>
453 m_stamp.touchShape();
454 fitValueDimension(n);
455 m_factorValid =
false;
466 template <
typename Path,
typename MeanDerived,
typename CovDerived,
typename CrossDerived>
470 Eigen::MatrixBase<MeanDerived>
const &
mean,
471 Eigen::MatrixBase<CovDerived>
const & cov,
472 Eigen::MatrixBase<CrossDerived>
const & cross
475 m_stamp.touchShape();
477 auto const rows = internal::growBlock<Path>(m_space, element,
mean.size());
479 cov.rows() == rows.differenceCount && cov.cols() == rows.differenceCount
480 && cross.rows() == rows.differenceCount
482 internal::insertRows(m_moments.mean(), rows.start,
mean);
483 internal::insertSymmetric(m_moments.covarianceLower(), rows.differenceStart, cov, cross);
485 m_factorValid =
false;
489 template <
typename Path,
typename MeanDerived,
typename CovDerived>
491 insert(
Index element, Eigen::MatrixBase<MeanDerived>
const &
mean, Eigen::MatrixBase<CovDerived>
const & cov)
493 using Cross = internal::MatrixOf<
497 CovDerived::MaxRowsAtCompileTime,
498 MomentsType::MatrixType::MaxColsAtCompileTime>;
506 template <
typename Path>
510 m_stamp.touchShape();
511 auto const rows = internal::shrinkBlock<Path>(m_space, element, count);
512 internal::eraseRows(m_moments.mean(), rows.start, rows.count);
513 internal::eraseSymmetric(m_moments.covarianceLower(), rows.differenceStart, rows.differenceCount);
514 m_factorValid =
false;
518 [[nodiscard]] Space_
const &
525 [[nodiscard]] MomentsType
const &
532 [[nodiscard]] VectorType
const &
535 return m_moments.mean();
542 [[nodiscard]] MatrixType
const &
545 return m_moments.covarianceLower();
552 template <
typename Path>
562 template <
typename Path>
573 template <
typename Path>
577 return internal::lowerSymmetricSelection<DifferenceDimensionAtCompileTime, MaxDifferenceDimensionAtCompileTime>(
584 template <
typename Path>
588 return internal::lowerSymmetricSelection<DifferenceDimensionAtCompileTime, MaxDifferenceDimensionAtCompileTime>(
599 template <
typename RowPath,
typename ColPath>
603 return internal::lowerSymmetricCross<DifferenceDimensionAtCompileTime, MaxDifferenceDimensionAtCompileTime>(
613 template <MatrixExpression Mean>
617 m_stamp.touchValues();
619 m_moments.mean() = std::forward<Mean>(
mean);
628 template <CovarianceExpression Cov>
632 m_stamp.touchValues();
634 m_moments.covarianceLower() = std::forward<Cov>(cov);
635 m_factorValid =
false;
655 m_stamp.touchValues();
656 EMRYS_FILTER_ASSERT(probability > 0 && probability < 1 &&
"the probability must be in (0, 1)");
661 m_moments.covarianceLower() *=
663 m_factorValid =
false;
673 [[nodiscard]] std::expected<MatrixType, GaussianError>
683 [[nodiscard]] std::expected<MatrixType, GaussianError>
686 if (
auto factorized =
factorize(); !factorized)
688 return std::unexpected{factorized.error()};
691 internal::solveInPlace(*m_llt, result);
698 [[nodiscard]] std::expected<MatrixType, GaussianError>
701 if (
auto factorized =
factorize(); !factorized)
703 return std::unexpected{factorized.error()};
714 [[nodiscard]] std::expected<MatrixType, GaussianError>
717 if (
auto factorized =
factorize(); !factorized)
719 return std::unexpected{factorized.error()};
722 internal::solveInPlace(m_llt->matrixL(), result);
731 template <
typename Path>
735 return internal::tagMarginal<GaussianMarginal, Path>(*
this);
739 template <
typename Path>
748 template <typename Path>
752 return internal::elementMarginal<GaussianMarginal, Path>(*
this, element);
756 template <
typename Path>
772 template <
typename GivenPath,
typename Derived>
776 return internal::tagConditionalGaussian<false, GivenPath>(*
this, values);
791 template <
typename GivenPath,
typename Derived>
795 return internal::tagConditionalGaussian<true, GivenPath>(*
this, values);
814 [[nodiscard]] std::expected<void, GaussianError>
821 return factorizeCovariance(m_moments.covarianceLower());
842 template <
typename R>
843 [[nodiscard]] std::expected<void, GaussianError>
848 "a density takes a repair, such as roots::ridge(), not axes: its factor is the Cholesky factor "
849 "(decisions.md [covariance-repair])"
851 auto result = std::as_const(*this).factorize();
856 auto repaired = repair.repaired(m_moments.covarianceLower());
859 return std::unexpected{repaired.error()};
861 result = factorizeCovariance(*repaired);
864 m_stamp.touchValues();
865 m_moments.covarianceLower() = std::move(*repaired);
878 [[nodiscard]] std::expected<DensityType, GaussianError>
883 return std::unexpected{result.error()};
897 template <typename R>
901 if (
auto result =
factorize(repair); !result)
903 return std::unexpected{result.error()};
909 template <
typename R>
916 friend struct internal::FactorizedGaussian;
920 friend class internal::ValuesCheck;
924 friend class internal::ShapeCheck;
926 friend struct internal::GaussianAccess;
929 [[nodiscard]] std::expected<void, GaussianError>
930 factorizeCovariance(
MatrixType const & cov)
const
934 m_llt.emplace(cov.rows());
937 if (m_llt->info() != Eigen::Success)
943 Scalar
const logDet = Scalar(2) * m_llt->matrixLLT().diagonal().array().log().sum();
948 m_logDeterminant = logDet;
949 m_factorValid =
true;
959 internal::fitValueDimension(m_space, m_moments.valueDimension());
962 &&
"the covariance must have a row per difference coordinate of the space"
969 fitValueDimension(Index n)
971 internal::fitValueDimension(m_space, n);
972 m_moments.resize(m_space);
979 m_moments.mean().setZero();
980 m_moments.covarianceLower().setZero();
983 MomentsType m_moments;
990 mutable std::optional<Eigen::LLT<MatrixType>> m_llt;
992 mutable Scalar m_logDeterminant{};
993 mutable bool m_factorValid{
false};
994 internal::Stamp m_stamp;
1009template <Space Space_, RealScalar Scalar_ =
double>
1042 return source().valueDimension();
1049 return source().differenceDimension();
1053 [[nodiscard]] Space_
const &
1056 return source().space();
1063 return source().moments();
1070 return source().mean();
1077 return source().covarianceLower();
1081 template <
typename Path>
1089 template <
typename Path>
1093 return source().template
mean<Path>(element);
1097 template <
typename Path>
1105 template <
typename Path>
1113 template <
typename RowPath,
typename ColPath>
1124 auto const &
llt = source().m_llt;
1132 return source().m_logDeterminant;
1143 return internal::FactorizedGaussian::logPeak(source());
1154 template <
typename Derived>
1158 static_assert(Derived::IsVectorAtCompileTime,
"squaredMahalanobisDistance() expects a vector");
1160 return internal::FactorizedGaussian::squaredMahalanobisDistance(source(), x);
1174 template <
typename Derived>
1201 template <
typename Derived>
1205 EMRYS_FILTER_ASSERT(probability > 0 && probability < 1 &&
"the probability must be in (0, 1)");
1221 template <
typename Derived>
1234 template <
typename Derived>
1236 density(Eigen::MatrixBase<Derived>
const & x)
const
1244 template <
typename Path>
1248 return internal::tagMarginal<GaussianMarginal, Path>(*
this);
1252 template <
typename Path>
1256 return internal::elementMarginal<GaussianMarginal, Path>(*
this, element);
1262 template <
typename GivenPath,
typename Derived>
1266 return internal::tagConditionalGaussian<false, GivenPath>(*
this, values);
1272 template <
typename GivenPath,
typename Derived>
1276 return internal::tagConditionalGaussian<true, GivenPath>(*
this, values);
1294 return m_check.get();
1297 internal::ValuesCheck<ParamsType> m_check;
1322template <
typename Source,
typename Selection,
typename SubSpace>
1353 using ResolvedType =
typename internal::SelectionHolder<Source, Selection, SubSpace>::ResolvedType;
1363 requires(!internal::isSharedSelection<Selection>)
1365 , m_selection{source, selection, std::move(
space)}
1370 requires(internal::isSharedSelection<Selection>)
1372 , m_selection{shared}
1390 [[nodiscard]] SubSpace
const &
1393 return m_selection.get().space;
1400 return m_selection.get().indices;
1407 return m_selection.get().differenceIndices;
1414 return source().mean()(
indices());
1444 [[nodiscard]] std::expected<MatrixType, GaussianError>
1455 [[nodiscard]] std::expected<MatrixType, GaussianError>
1458 auto const llt = factorization();
1461 return std::unexpected{llt.error()};
1464 internal::solveInPlace(*llt, result);
1471 [[nodiscard]] std::expected<MatrixType, GaussianError>
1474 auto const llt = factorization();
1477 return std::unexpected{llt.error()};
1486 [[nodiscard]] std::expected<MatrixType, GaussianError>
1489 auto const llt = factorization();
1492 return std::unexpected{llt.error()};
1495 internal::solveInPlace(llt->matrixL(), result);
1502 [[nodiscard]] Source
const &
1505 return m_holder.get();
1512 [[nodiscard]] std::expected<Eigen::LLT<MatrixType>,
GaussianError>
1513 factorization()
const
1516 if (llt.info() != Eigen::Success)
1521 if (!
allFinite(llt.matrixLLT().diagonal()))
1528 internal::HolderFor<Source> m_holder;
1529 internal::SelectionHolder<Source, Selection, SubSpace> m_selection;
1538template <
template <
typename,
typename,
typename>
typename Marginal,
typename Path,
typename Source>
1540tagMarginal(Source
const & source)
1542 auto const selection =
1544 auto space = pathSubspace<Path>(source.space());
1545 return Marginal<Source, std::remove_cvref_t<
decltype(selection)>,
decltype(space)>{
1556template <
template <
typename,
typename,
typename>
typename Marginal,
typename Path,
typename Source>
1558elementMarginal(Source
const & source, Eigen::Index element)
1560 auto const selection =
1561 Coordinates{asIndexSet(
idx<Path>(source.space(), element)), asIndexSet(
diffIdx<Path>(source.space(), element))};
1562 auto space = pathElementSubspace<Path>(source.space());
1563 return Marginal<Source, std::remove_cvref_t<
decltype(selection)>,
decltype(space)>{
1574#ifndef EMRYS_FILTER_NO_IO
1583template <
typename Space_,
typename Scalar_>
1585describe(std::ostream & stream, GaussianParams<Space_, Scalar_>
const & gaussian, Indent indent)
1587 stream << indent <<
"Gaussian";
1588 printDimension(stream, gaussian.valueDimension(), gaussian.differenceDimension());
1589 printMoments(stream, indent.deeper(), gaussian.moments());
The chi-squared distribution's CDF and quantile, with no dependency outside Eigen,...
Spaces built from other spaces (decisions.md [sizes-and-dimensions], [several-resizable-blocks]): Com...
How a function that stores a matrix argument takes it (decisions.md [forwarded-matrices]): the concep...
What marginals and conditionals are built from: resolving Eigen index sets against a space; the share...
The macros every header includes first.
Mathematical constants and scalar functions that work in every scalar type the library accepts,...
Moments: the mean and covariance that a Gaussian carries and that a mixture or a sample set is moment...
Printing: operator<< and std::formatter for every distribution, every filter (which prints its name a...
Square roots of a covariance (decisions.md [sigma-point-axes]): the Axes and Repair concepts that a r...
Solving by a factorization of a small matrix: transposedSolve() gives , the shape of a Kalman gain an...
The Space concept and what every continuous space shares (decisions.md [sizes-and-dimensions]): the t...
What parameters and their views share (decisions.md [view-stamp], [params-and-density]): Stamp,...
Subspaces, and growing and shrinking blocks (decisions.md [several-resizable-blocks]).
Tag paths and the index utilities (decisions.md [tag-addressing], [index-names]): TagPath and TagPath...
Read-only view of a subset of the entries of a GaussianParams or Gaussian.
Definition Gaussian.h:1324
SubSpace SpaceType
Definition Gaussian.h:1329
typename ParamsType::VectorType VectorType
Definition Gaussian.h:1346
static constexpr int MaxValueDimensionAtCompileTime
Definition Gaussian.h:1335
static constexpr bool IsInfoForm
Definition Gaussian.h:1349
ParamsType copy() const
Definition Gaussian.h:1435
Index differenceDimension() const
Definition Gaussian.h:1384
static constexpr bool IsSqrtForm
Definition Gaussian.h:1350
std::expected< MatrixType, GaussianError > covariance() const
Definition Gaussian.h:1445
GaussianParams< SubSpace, Scalar > ParamsType
Definition Gaussian.h:1344
Eigen::Index Index
Definition Gaussian.h:1328
std::expected< MatrixType, GaussianError > precisionRoot() const
Definition Gaussian.h:1487
std::expected< MatrixType, GaussianError > precision() const
Definition Gaussian.h:1456
decltype(ResolvedType::differenceIndices) DifferenceIndexVectorType
Definition Gaussian.h:1359
static constexpr int ValueDimensionAtCompileTime
Definition Gaussian.h:1332
typename ParamsType::MomentsType MomentsType
Definition Gaussian.h:1345
static constexpr int DifferenceDimensionAtCompileTime
Definition Gaussian.h:1338
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Gaussian.h:1341
IndexVectorType const & indices() const
Definition Gaussian.h:1398
auto mean() const
Definition Gaussian.h:1412
Index valueDimension() const
Definition Gaussian.h:1377
typename Type::Scalar Scalar
Definition Gaussian.h:1327
auto covarianceLower() const
Definition Gaussian.h:1421
GaussianMarginal(Source const &source, Selection shared)
Selects the coordinates of source that shared resolved, borrowing them.
Definition Gaussian.h:1369
DifferenceIndexVectorType const & differenceIndices() const
Definition Gaussian.h:1405
typename ParamsType::MatrixType MatrixType
Definition Gaussian.h:1347
MomentsType moments() const
Definition Gaussian.h:1428
decltype(ResolvedType::indices) IndexVectorType
Definition Gaussian.h:1356
typename internal::SelectionHolder< Type, Selection, SubSpace >::ResolvedType ResolvedType
Definition Gaussian.h:1353
std::expected< MatrixType, GaussianError > covarianceRoot() const
Definition Gaussian.h:1472
SubSpace const & space() const
Definition Gaussian.h:1391
GaussianMarginal(Source const &source, Selection const &selection, SubSpace space)
Selects selection's coordinates, which form space.
Definition Gaussian.h:1362
A Gaussian distribution: a mean and covariance in a space.
Definition Gaussian.h:259
typename MomentsType::Index Index
Definition Gaussian.h:264
auto mean() const
Definition Gaussian.h:554
void setMean(Mean &&mean)
Sets the mean, normalized by the space, moving mean if it is an rvalue of the stored type.
Definition Gaussian.h:615
Moments< Space_, Scalar_ > MomentsType
Definition Gaussian.h:262
GaussianParams(Space_ space, MomentsType moments)
Constructs a Gaussian from moments in space, which is resized to fit them if it has one resizable blo...
Definition Gaussian.h:348
std::expected< MatrixType, GaussianError > covarianceRoot() const
Definition Gaussian.h:699
GaussianParams(MomentsType moments)
Constructs a Gaussian from moments, in a default space resized to fit them.
Definition Gaussian.h:306
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Gaussian.h:281
static constexpr bool IsParams
Definition Gaussian.h:283
static constexpr int DifferenceDimensionAtCompileTime
Definition Gaussian.h:278
static constexpr bool IsInfoForm
Definition Gaussian.h:284
void toDensity(R const &) &&=delete
A density of a temporary would outlive it.
GaussianParams(Space_ space, Mean &&mean, Cov &&cov)
GaussianParams(mean, cov) in space.
Definition Gaussian.h:357
typename MomentsType::MatrixType MatrixType
Definition Gaussian.h:267
GaussianParams(Space_ space, Index n)
Constructs a Gaussian with n value coordinates in space, with a zero mean and covariance.
Definition Gaussian.h:338
GaussianParams(Mean &&mean, Cov &&cov)
Constructs a Gaussian from mean and cov, moving either one passed as an rvalue of the stored type.
Definition Gaussian.h:319
void setCovariance(Cov &&cov)
Sets the covariance, moving cov if it is an rvalue of the stored type, and invalidates the factorizat...
Definition Gaussian.h:630
void resize(Index n)
Resizes mean, covariance, and factorization, and the space's resizable block, to n value coordinates.
Definition Gaussian.h:450
typename MomentsType::VectorType VectorType
Definition Gaussian.h:265
static constexpr int ValueDimensionAtCompileTime
Definition Gaussian.h:272
void insert(Index element, Eigen::MatrixBase< MeanDerived > const &mean, Eigen::MatrixBase< CovDerived > const &cov)
insert() of entries independent of the existing ones.
Definition Gaussian.h:491
Gaussian< Space_, Scalar_ > DensityType
Definition Gaussian.h:269
auto covariance() const
Definition Gaussian.h:601
GaussianParams & operator=(GaussianParams &&other) noexcept
Moves the data and any valid factorization; a view of either object may not be read afterward.
Definition Gaussian.h:414
void erase(Index element, Index count=1)
Removes count elements of the resizable block named by the tag path Path, starting at its element ele...
Definition Gaussian.h:508
typename MomentsType::DifferenceVectorType DifferenceVectorType
a difference, such as a residual
Definition Gaussian.h:266
std::expected< void, GaussianError > factorize() const
Definition Gaussian.h:815
std::expected< MatrixType, GaussianError > precision() const
Definition Gaussian.h:684
GaussianParams(GaussianParams &&other) noexcept
Moves the data and any valid factorization; a view of other may not be read afterward.
Definition Gaussian.h:378
auto mean(Index element) const
mean<Path>() of repetition element of the repeated block Path starts at (see marginal<Path>(element))...
Definition Gaussian.h:564
static constexpr bool IsSqrtForm
Definition Gaussian.h:285
std::expected< MatrixType, GaussianError > precisionRoot() const
Definition Gaussian.h:715
MatrixType const & covarianceLower() const
Definition Gaussian.h:543
auto covariance(Index element) const
covariance<Path>() of repetition element of the repeated block Path starts at.
Definition Gaussian.h:586
auto conditional(Eigen::MatrixBase< Derived > const &values) const
Definition Gaussian.h:774
GaussianParams()
Constructs a Gaussian in a default space, with a zero mean and covariance, which factorize() rejects ...
Definition Gaussian.h:290
GaussianParams(Space_ space)
Constructs a Gaussian in space, with a zero mean and covariance.
Definition Gaussian.h:326
GaussianParams & operator=(GaussianParams const &other)
Copies the data and any valid factorization; a view of this object may not be read afterward.
Definition Gaussian.h:396
auto covariance() const
Definition Gaussian.h:575
typename MomentsType::Scalar Scalar
Definition Gaussian.h:263
std::expected< void, GaussianError > factorize(R const &repair)
factorize(), repairing a covariance that is not positive definite by repair, a Repair: g....
Definition Gaussian.h:844
Index differenceDimension() const
Definition Gaussian.h:439
Space_ SpaceType
Definition Gaussian.h:268
void marginal() const &&=delete
A marginal of a temporary would outlive it; copy the marginal of a named Gaussian instead.
void insert(Index element, Eigen::MatrixBase< MeanDerived > const &mean, Eigen::MatrixBase< CovDerived > const &cov, Eigen::MatrixBase< CrossDerived > const &cross)
Inserts the entries mean into the resizable block named by the tag path Path, before its element elem...
Definition Gaussian.h:468
std::expected< DensityType, GaussianError > toDensity() const &
Factorizes the covariance if needed (see factorize()), which writes the cache and so is not safe on o...
Definition Gaussian.h:879
MomentsType const & moments() const
Definition Gaussian.h:526
Index valueDimension() const
Definition Gaussian.h:432
VectorType const & mean() const
Definition Gaussian.h:533
Space_ const & space() const
Definition Gaussian.h:519
void marginal(Index element) const &&=delete
A marginal of a temporary would outlive it.
static constexpr int MaxValueDimensionAtCompileTime
Definition Gaussian.h:275
auto marginal() const &
Definition Gaussian.h:733
auto conditionalWithEvidence(Eigen::MatrixBase< Derived > const &values) const
Definition Gaussian.h:793
void scaleToConfidence(Scalar probability)
Scales the covariance so that its one-standard-deviation ellipsoid is the probability confidence regi...
Definition Gaussian.h:653
GaussianParams(Index n)
Constructs a Gaussian with n value coordinates, with a zero mean and covariance.
Definition Gaussian.h:298
void toDensity() const &&=delete
A density of a temporary would outlive it.
GaussianParams(GaussianParams const &other)
Copies the data and any valid factorization.
Definition Gaussian.h:365
std::expected< MatrixType, GaussianError > covariance() const
Definition Gaussian.h:674
The density of a Gaussian whose covariance has been factorized: a read-only view.
Definition Gaussian.h:1011
Index valueDimension() const
Definition Gaussian.h:1040
typename ParamsType::Index Index
Definition Gaussian.h:1018
Index differenceDimension() const
Definition Gaussian.h:1047
typename ParamsType::Scalar Scalar
Definition Gaussian.h:1017
auto covariance() const
Definition Gaussian.h:1099
Scalar density(Eigen::MatrixBase< Derived > const &x) const
Definition Gaussian.h:1236
typename ParamsType::MomentsType MomentsType
Definition Gaussian.h:1016
VectorType const & mean() const
Definition Gaussian.h:1068
static constexpr int DifferenceDimensionAtCompileTime
Definition Gaussian.h:1031
static constexpr bool IsParams
Definition Gaussian.h:1036
LltType const & llt() const
Definition Gaussian.h:1122
Space_ const & space() const
Definition Gaussian.h:1054
Eigen::LLT< MatrixType > LltType
Definition Gaussian.h:1022
GaussianParams< Space_, Scalar_ > ParamsType
Definition Gaussian.h:1014
static constexpr int ValueDimensionAtCompileTime
Definition Gaussian.h:1025
auto conditional(Eigen::MatrixBase< Derived > const &values) const
Definition Gaussian.h:1264
Space_ SpaceType
Definition Gaussian.h:1015
Scalar confidence(Eigen::MatrixBase< Derived > const &x) const
Definition Gaussian.h:1176
auto marginal() const
marginal() of the subspace named by the path Path; see GaussianParams::marginal().
Definition Gaussian.h:1246
MatrixType const & covarianceLower() const
Definition Gaussian.h:1075
Scalar squaredMahalanobisDistance(Eigen::MatrixBase< Derived > const &x) const
Definition Gaussian.h:1156
typename ParamsType::VectorType VectorType
Definition Gaussian.h:1019
auto mean(Index element) const
mean<Path>() of one repetition; see GaussianParams::mean<Path>(element).
Definition Gaussian.h:1091
auto mean() const
Definition Gaussian.h:1083
Scalar logDensity(Eigen::MatrixBase< Derived > const &x) const
Definition Gaussian.h:1223
auto covariance(Index element) const
covariance<Path>() of one repetition; see GaussianParams::covariance<Path>(element).
Definition Gaussian.h:1107
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Gaussian.h:1034
auto marginal(Index element) const
marginal() of one repetition of a repeated block; see GaussianParams::marginal(element).
Definition Gaussian.h:1254
auto conditionalWithEvidence(Eigen::MatrixBase< Derived > const &values) const
Definition Gaussian.h:1274
auto covariance() const
Definition Gaussian.h:1115
typename ParamsType::DifferenceVectorType DifferenceVectorType
Definition Gaussian.h:1020
typename ParamsType::MatrixType MatrixType
Definition Gaussian.h:1021
Scalar logPeak() const
Definition Gaussian.h:1141
bool contains(Eigen::MatrixBase< Derived > const &x, Scalar probability) const
Definition Gaussian.h:1203
MomentsType const & moments() const
Definition Gaussian.h:1061
static constexpr int MaxValueDimensionAtCompileTime
Definition Gaussian.h:1028
Scalar logDeterminant() const
Definition Gaussian.h:1130
First and second moments (mean and covariance) of a distribution.
Definition Moments.h:55
Scalar_ Scalar
Definition Moments.h:58
Eigen::Index Index
Definition Moments.h:59
DifferenceMatrix< Space_, Space_, Scalar > MatrixType
Definition Moments.h:76
DifferenceVector< Space_, Scalar > DifferenceVectorType
Definition Moments.h:75
ValueVector< Space_, Scalar > VectorType
Definition Moments.h:74
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Moments.h:72
static constexpr int DifferenceDimensionAtCompileTime
Definition Moments.h:69
static constexpr int MaxValueDimensionAtCompileTime
Definition Moments.h:66
static constexpr int ValueDimensionAtCompileTime
Definition Moments.h:63
A repair: what turns a covariance whose root failed into one to retry (decisions.md [sigma-point-axes...
Definition Roots.h:321
Scalar chiSquaredQuantile(std::type_identity_t< Scalar > const &probability, std::type_identity_t< Scalar > const &dof)
Definition ChiSquared.h:148
Scalar chiSquaredCdf(std::type_identity_t< Scalar > const &x, std::type_identity_t< Scalar > const &dof)
Definition ChiSquared.h:123
bool allFinite(Eigen::DenseBase< Derived > const &x)
Definition Math.h:354
#define EMRYS_FILTER_NO_UNIQUE_ADDRESS
Assertion used for programmer errors (misuse of the API).
Definition Macros.h:38
bool isFinite(T const &x)
Definition Math.h:305
Scalar log2Pi()
Definition Math.h:177
@ NotPositiveDefinite
the covariance is not positive definite, so it has no Cholesky factor or inverse
Definition NumericError.h:31
@ NonFinite
a matrix, a factor, or a log-weight holds NaN or infinity (+inf, for a log-weight)
Definition NumericError.h:33
NumericError GaussianError
Reasons a Gaussian, in any of its forms (decisions.md [gaussian-forms]), can fail to give a density,...
Definition NumericError.h:45
GivenBlock< Space, P, Value > given(Space const &space, Value const &value)
Definition Measurement.h:917
constexpr Residual< Inner > residual(Inner inner={})
Definition Resampling.h:544
Eigen::Index valueDimension(S const &space)
Definition Space.h:272
constexpr auto diffIdx()
idx<S, Path>() of the differences of the subspace named by Path: its rows of a covariance.
Definition TagPath.h:669
auto minus(S const &space, Eigen::MatrixBase< ADerived > const &a, Eigen::MatrixBase< BDerived > const &b)
Definition Space.h:715
Eigen::Index differenceDimension(S const &space)
Definition Space.h:284
constexpr int valueDimensionAtCompileTime
The value dimension of Space at compile time, or Eigen::Dynamic if it is resizable.
Definition Space.h:251
constexpr int maxValueDimensionAtCompileTime
The largest valid value dimension of Space, or Eigen::Dynamic if it is unbounded.
Definition Space.h:258
void normalizeValues(S const &space, Eigen::MatrixBase< Derived > const &block)
Brings the values in block (rows are space's coordinates, columns samples) into canonical form,...
Definition Space.h:331
constexpr auto idx()
Definition TagPath.h:658
The result of GaussianParams::conditionalWithEvidence(): the conditional distribution and the log-evi...
Definition Gaussian.h:56
Scalar_ logEvidence
, the given values' log-density under their marginal
Definition Gaussian.h:58
GaussianParams< Space_, Scalar_ > conditional
the distribution of the other blocks given the values
Definition Gaussian.h:57