72template <Space Space_, RealScalar Scalar_ =
double>
103 template <MatrixExpression Location,
typename ScaleDerived>
104 [[nodiscard]]
static std::expected<StudentT, StudentTError>
107 if (
auto const error = checkDegreesOfFreedom(
dof))
109 return std::unexpected{*error};
116 if (
auto result = gaussian.factorize(); !result)
118 return std::unexpected{
123 return StudentT{std::move(gaussian),
dof};
127 template <MatrixExpression Location,
typename ScaleDerived>
128 [[nodiscard]]
static std::expected<StudentT, StudentTError>
135 [[nodiscard]]
static std::expected<StudentT, StudentTError>
145 return m_gaussian.valueDimension();
152 return m_gaussian.differenceDimension();
156 [[nodiscard]] Space_
const &
159 return m_gaussian.space();
166 return m_gaussian.mean();
173 return m_gaussian.covarianceLower();
189 auto const density = m_gaussian.toDensity();
197 template <
typename Derived>
208 template <
typename Derived>
210 density(Eigen::MatrixBase<Derived>
const & x)
const
218 [[nodiscard]] std::expected<MomentsType, MomentsError>
234 return m_logNormalizer
240 template <
typename Path>
244 return internal::marginalOverPath<Path>(
space(), [&](
auto const & target,
auto subspace) {
245 return restricted(target, std::move(subspace));
250 template <
typename Path>
254 return internal::marginalOverElement<Path>(
space(), element, [&](
auto const & target,
auto subspace) {
255 return restricted(target, std::move(subspace));
271 template <
typename GivenPath,
typename Derived>
275 return internal::tagConditional<GivenPath>(
277 [&](
auto const & t,
auto const & g,
auto subspace,
auto const & givenSpace) {
278 return conditioned(t, g, values, std::move(subspace), givenSpace);
286 [[nodiscard]]
static std::optional<StudentTError>
301 template <
typename Target,
typename SubSpace>
302 [[nodiscard]] StudentT<SubSpace, Scalar>
303 restricted(Target
const & target, SubSpace subspace)
const
305 using Result = StudentT<SubSpace, Scalar>;
306 auto result = Result::create(
309 typename Result::MatrixType{internal::lowerSymmetricBlock(scale(), target.differences, target.differences)},
312 EMRYS_FILTER_ASSERT(result.has_value() &&
"a principal block of a positive definite scale should factorize");
313 return std::move(*result);
319 template <
typename Target,
typename Given,
typename Derived,
typename SubSpace,
typename GivenSpace>
320 [[nodiscard]] std::expected<StudentT<SubSpace, Scalar>,
StudentTError>
322 Target
const & target,
324 Eigen::MatrixBase<Derived>
const & values,
326 GivenSpace
const & givenSpace
329 using Result = StudentT<SubSpace, Scalar>;
331 internal::conditionMomentsWithEvidence(
location(),
scale(), target,
given, values, givenSpace, subspace);
337 Scalar const stretch = (m_dof + gaussian->squaredDistance) / (m_dof + givenSize);
338 return Result::create(
340 std::move(gaussian->moments.mean),
341 typename Result::MatrixType{stretch * gaussian->moments.cov},
351 : m_gaussian{std::move(gaussian)}
358 -
Scalar(0.5) * logDeterminant;
370template <
typename Space,
typename Scalar =
double>
374 Eigen::Array<Scalar, Eigen::Dynamic, 1>
382template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
383[[nodiscard]] StudentTDraws<Space, Scalar>
384drawStudentT(StudentT<Space, Scalar>
const & t, Eigen::Index n, G & rng)
386 EMRYS_FILTER_ASSERT(n >= 0);
388 Normals
const z = standardNormal<Normals>(t.differenceDimension(), n, rng);
389 auto chiSquared = std::chi_squared_distribution<Scalar>{t.dof()};
390 auto stretch = Eigen::Array<Scalar, Eigen::Dynamic, 1>{n};
391 for (Eigen::Index j = 0; j < n; ++j)
393 stretch(j) = Eigen::numext::sqrt(t.dof() / chiSquared(rng));
395 Normals offsets = multiply(asTriangular<Eigen::Lower>(t.scaleDensity().llt().matrixLLT()), z);
396 for (Eigen::Index j = 0; j < n; ++j)
398 offsets.col(j) *= stretch(j);
402 z.colwise().squaredNorm().transpose().array() * stretch.square()
407template <
typename Space,
typename Scalar =
double>
411 Scalar squaredDistance = 0;
417template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
418[[nodiscard]] StudentTDraw<Space, Scalar>
419drawOneStudentT(StudentT<Space, Scalar>
const & t, G & rng)
422 Difference
const z = standardNormalVector<Difference>(t.differenceDimension(), rng);
423 Scalar
const stretch = Eigen::numext::sqrt(t.dof() / std::chi_squared_distribution<Scalar>{t.dof()}(rng));
424 Difference offset = multiply(asTriangular<Eigen::Lower>(t.scaleDensity().llt().matrixLLT()), z);
428 z.squaredNorm() * stretch * stretch
440template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
444 auto draws = internal::drawStudentT(t, n, rng);
445 return std::move(draws.samples);
453template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
454[[nodiscard]]
typename Moments<Space, Scalar>::VectorType
457 return internal::drawOneStudentT(t, rng).sample;
469template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
470[[nodiscard]] SamplesWithLogDensity<Space, Scalar>
473 auto draws = internal::drawStudentT(t, n, rng);
478 for (Eigen::Index j = 0; j < n; ++j)
485 for (Eigen::Index j = 0; j < n; ++j)
487 result.logDensity(j) = t.
logDensity(result.samples.col(j));
498template <
typename Scalar,
typename Space, std::uniform_random_bit_generator G>
499[[nodiscard]] SampleWithLogDensity<Space, Scalar>
502 auto draw = internal::drawOneStudentT(t, rng);
510 result.logDensity = t.
logDensity(result.sample);
517#ifndef EMRYS_FILTER_NO_IO
527describe(std::ostream & stream,
StudentTError error, Indent indent)
533 stream <<
"the scale matrix is not positive definite";
536 stream <<
"the scale matrix is not finite";
539 stream <<
"the degrees of freedom are not positive";
542 stream <<
"the degrees of freedom are not finite";
How a function that stores a matrix argument takes it (decisions.md [forwarded-matrices]): the concep...
The Gaussian: its parameters GaussianParams, the Gaussian that evaluates the density,...
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...
Products of fixed-size matrices by Eigen's fixed-size product at every size: multiply() is by Eigen'...
Printing: operator<< and std::formatter for every distribution, every filter (which prints its name a...
SampleSet: a matrix whose columns are samples in a space, which the samplers return and the unscented...
Drawing samples: SamplesWithLogDensity and SampleWithLogDensity, the Gaussian draws drawSamples(),...
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...
Subspaces, and growing and shrinking blocks (decisions.md [several-resizable-blocks]).
A Gaussian distribution: a mean and covariance in a space.
Definition Gaussian.h:259
Moments< Space_, Scalar_ > MomentsType
Definition Gaussian.h:262
Gaussian< Space_, Scalar_ > DensityType
Definition Gaussian.h:269
MatrixType const & covarianceLower() const
Definition Gaussian.h:543
VectorType const & mean() const
Definition Gaussian.h:533
Space_ const & space() const
Definition Gaussian.h:519
Scalar logDeterminant() const
Definition Gaussian.h:1130
ValueVector< Space_, Scalar > VectorType
a value, such as the mean
Definition Moments.h:74
The multivariate Student-t distribution: a Gaussian with heavier tails.
Definition StudentT.h:74
Space_ const & space() const
Definition StudentT.h:157
GaussianParams< Space_, Scalar_ > ScaleGaussianType
, which holds the parameters
Definition StudentT.h:78
static std::expected< StudentT, StudentTError > create(Location &&location, Eigen::MatrixBase< ScaleDerived > const &scale, Scalar dof)
create() in the default space.
Definition StudentT.h:129
static constexpr int MaxDifferenceDimensionAtCompileTime
a bound on the length of a difference, or Eigen::Dynamic
Definition StudentT.h:97
MatrixType const & scale() const
Definition StudentT.h:171
static std::expected< StudentT, StudentTError > create(Space_ space, Location &&location, Eigen::MatrixBase< ScaleDerived > const &scale, Scalar dof)
Definition StudentT.h:105
typename ScaleGaussianType::MomentsType MomentsType
a mean and covariance together
Definition StudentT.h:80
auto marginal() const
marginal() of the subspace named by the tag path Path (see idx()), in that subspace.
Definition StudentT.h:242
typename MomentsType::VectorType VectorType
a value
Definition StudentT.h:84
Scalar logDensityAtSquaredDistance(Scalar squaredDistance) const
Definition StudentT.h:232
Space_ SpaceType
the space the values live in
Definition StudentT.h:83
ScaleGaussianType::DensityType scaleDensity() const
Definition StudentT.h:187
Scalar_ Scalar
the scalar type
Definition StudentT.h:81
VectorType const & location() const
Definition StudentT.h:164
static constexpr int MaxValueDimensionAtCompileTime
a bound on the length of a value, or Eigen::Dynamic
Definition StudentT.h:91
Eigen::Index Index
Eigen's index type.
Definition StudentT.h:82
Index differenceDimension() const
Definition StudentT.h:150
static constexpr int DifferenceDimensionAtCompileTime
the length of a difference and the rows of the scale matrix, or Eigen::Dynamic
Definition StudentT.h:94
typename MomentsType::MatrixType MatrixType
a matrix over differences
Definition StudentT.h:85
std::expected< MomentsType, MomentsError > moments() const
Definition StudentT.h:219
Scalar dof() const
Definition StudentT.h:178
auto conditional(Eigen::MatrixBase< Derived > const &values) const
Definition StudentT.h:273
Scalar density(Eigen::MatrixBase< Derived > const &x) const
Definition StudentT.h:210
static std::expected< StudentT, StudentTError > create(ScaleGaussianType const &gaussian, Scalar dof)
create() from the location and scale of gaussian, which keeps its space.
Definition StudentT.h:136
Scalar logDensity(Eigen::MatrixBase< Derived > const &x) const
Definition StudentT.h:199
static constexpr int ValueDimensionAtCompileTime
the length of a value, or Eigen::Dynamic
Definition StudentT.h:88
auto marginal(Index element) const
marginal() of one repetition of a repeated block; see GaussianParams::marginal(element).
Definition StudentT.h:252
Index valueDimension() const
Definition StudentT.h:143
Scalar pi()
Definition Math.h:145
Scalar logOnePlus(std::type_identity_t< Scalar > const &x)
Definition Math.h:247
Scalar logGamma(std::type_identity_t< Scalar > const &x)
Definition Math.h:216
bool isFinite(T const &x)
Definition Math.h:305
@ 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
SampleWithLogDensity< Space, Scalar > drawSampleWithLogDensity(Gamma< Space, Scalar > const &gamma, G &rng)
Definition Gamma.h:546
Eigen::Matrix< Scalar, valueDimensionAtCompileTime< Space >, Eigen::Dynamic,(maxValueDimensionAtCompileTime< Space >==1 ? Eigen::RowMajor :Eigen::ColMajor), maxValueDimensionAtCompileTime< Space >, Eigen::Dynamic > SampleSet
A matrix whose columns are samples in Space (see Spaces), with the space's size and storage bound.
Definition SampleSet.h:33
Eigen::Index drawSample(Categorical< Space, Scalar > const &distribution, G &rng)
Definition Categorical.h:1111
StudentTError
Reasons a location, scale matrix, and degrees of freedom fail to define a StudentT.
Definition StudentT.h:38
std::vector< Eigen::Index > drawSamples(Categorical< Space, Scalar > const &distribution, Eigen::Index n, G &rng)
Definition Categorical.h:1087
SamplesWithLogDensity< Space, Scalar > drawSamplesWithLogDensity(Gamma< Space, Scalar > const &gamma, Eigen::Index n, G &rng)
Definition Gamma.h:529
@ Infinite
the distribution has no finite mean or covariance, such as a Student-t with
Definition Moments.h:33
@ NotPositiveDegreesOfFreedom
the degrees of freedom are zero or negative
Definition StudentT.h:41
@ NotPositiveDefinite
the scale matrix is not positive definite
Definition StudentT.h:39
@ NonFiniteScale
the scale matrix is not finite
Definition StudentT.h:40
@ NonFiniteDegreesOfFreedom
the degrees of freedom are NaN or infinite
Definition StudentT.h:42
GivenBlock< Space, P, Value > given(Space const &space, Value const &value)
Definition Measurement.h:917
auto plus(S const &space, Eigen::MatrixBase< XDerived > const &x, Eigen::MatrixBase< DDerived > const &d)
Definition Space.h:675
ValueVector< DifferenceSpace< S >, Scalar > DifferenceVector
A difference in S as an Eigen column vector of Scalar, the type of minus() and of the offset plus() t...
Definition Space.h:650
constexpr bool isLinear
Whether S is linear: its values and its differences are both plain vectors.
Definition Space.h:321
One sample in Space, in a vector sized by the space, together with the log of the density it was draw...
Definition Sampling.h:50
Samples in Space together with the log of the density they were drawn from, evaluated at each.
Definition Sampling.h:36
Eigen::Array< Scalar, Eigen::Dynamic, 1 > logDensity
logDensity(i) is the log-density at samples.col(i)
Definition Sampling.h:38