53template <Space Space_, RealScalar Scalar_ =
double,
typename Allocator_ = DefaultAllocator>
108 [[nodiscard]] Space_
const &
118 return m_particles.get();
125 return m_logTotalWeight;
132 return m_kernel.covarianceLower();
136 [[nodiscard]]
typename KernelType::DensityType
139 auto const density = m_kernel.toDensity();
155 template <ExecutionPolicy P>
159 MomentsType result = internal::weightedMomentsIn<MomentsType>(
160 std::forward<P>(policy),
166 result.covarianceLower() += m_kernel.covarianceLower().template selfadjointView<Eigen::Lower>().toDenseMatrix();
178 template <
typename Derived>
188 template <ExecutionPolicy P,
typename Derived>
190 logDensity(P && policy, Eigen::MatrixBase<Derived>
const & x)
const
192 static_assert(Derived::IsVectorAtCompileTime,
"logDensity() expects a vector");
194 auto const & llt = internal::FactorizedGaussian::llt(m_kernel);
197 std::forward<P>(policy),
201 for (
Index i = begin; i < begin + length; ++i)
204 llt.matrixL().solveInPlace(residual);
205 range.add(set.logWeight(i) -
Scalar(0.5) * residual.squaredNorm());
214 return sum.
value() - m_logTotalWeight + internal::FactorizedGaussian::logPeak(m_kernel);
223 template <
typename Derived>
225 density(Eigen::MatrixBase<Derived>
const & x)
const
238 template <MatrixExpression Bandw
idth>
239 [[nodiscard]]
static std::expected<KernelDensity, GaussianError>
244 if (
auto result =
kernel.factorize(); !result)
246 return std::unexpected{result.error()};
257 , m_kernel{std::move(
kernel)}
260 internal::ValuesCheck<ParamsType> m_particles;
281 std::uniform_random_bit_generator G,
287 EMRYS_FILTER_ASSERT(n >= 0);
289 auto const & particles = kde.
particles();
290 auto weights = internal::bufferOf<Scalar>(particles.get_allocator());
291 weights.resize(std::size_t(particles.size()));
292 for (Eigen::Index i = 0; i < particles.size(); ++i)
294 weights[std::size_t(i)] = Eigen::numext::exp(particles.logWeight(i) - kde.
logTotalWeight());
296 auto ancestors = internal::bufferOf<Eigen::Index>(particles.get_allocator());
297 ancestors.resize(std::size_t(n));
298 resampler(std::span<Scalar const>{weights}, std::span<Eigen::Index>{ancestors}, rng);
301 Normals
const offsets = internal::multiply(
302 internal::asTriangular<Eigen::Lower>(kde.
kernel().llt().matrixLLT()),
306 for (Eigen::Index j = 0; j < n; ++j)
308 samples.col(j) =
Emrys::plus(kde.
space(), particles.state(ancestors[std::size_t(j)]), offsets.col(j));
319template <
typename Scalar,
typename Space,
typename Allocator, std::uniform_random_bit_generator G>
324 auto const & particles = kde.
particles();
325 Eigen::Index
const picked = internal::drawIndex<Scalar>(
327 [&](Eigen::Index k) {
328 return Eigen::numext::exp(Scalar(particles.logWeight(k) - kde.logTotalWeight()));
332 Difference
const z = internal::standardNormalVector<Difference>(kde.
differenceDimension(), rng);
335 particles.state(picked),
336 Difference{internal::multiply(internal::asTriangular<Eigen::Lower>(kde.kernel().llt().matrixLLT()), z)}
353 std::uniform_random_bit_generator G,
354 typename R = resamplers::Multinomial>
355 requires Resampler<R, Scalar, G>
356[[nodiscard]] SamplesWithLogDensity<Space, Scalar>
361 R
const & resampler = {}
377 std::uniform_random_bit_generator G,
386 R
const & resampler = {}
389 auto result = SamplesWithLogDensity<Space, Scalar>{
drawSamples(kde, n, rng, resampler), {}};
390 result.logDensity.resize(n);
391 internal::forEachIndex(std::forward<P>(policy), n, [&](Eigen::Index j) {
392 result.logDensity(j) = kde.
logDensity(result.samples.col(j));
402template <
typename Scalar,
typename Space,
typename Allocator, std::uniform_random_bit_generator G>
The allocator conventions of decisions.md [allocation]: DefaultAllocator, Capacity,...
Execution policies (decisions.md [execution-policies]): execution::sequential, the ExecutionPolicy co...
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,...
Products of fixed-size matrices by Eigen's fixed-size product at every size: multiply() is by Eigen'...
The particle set: ParticleSet, the weighted samples, with resampling, upkeep of weights and particles...
The resamplers: function objects that draw ancestors in proportion to weights, which ParticleSet::res...
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...
What parameters and their views share (decisions.md [view-stamp], [params-and-density]): Stamp,...
Weighted statistics over points, all in internal, which the containers of weighted elements (Gaussian...
Log-weights: the WeightError a normalization can fail with (NumericError.h), and the streaming log-su...
A Gaussian distribution: a mean and covariance in a space.
Definition Gaussian.h:259
Gaussian kernel density estimate over a ParticleSet.
Definition KernelDensity.h:55
Scalar density(Eigen::MatrixBase< Derived > const &x) const
Definition KernelDensity.h:225
Scalar logDensity(P &&policy, Eigen::MatrixBase< Derived > const &x) const
logDensity() under policy: a log-sum-exp over the particles, reduced over chunks of them (see Executi...
Definition KernelDensity.h:190
typename ParamsType::DifferenceVectorType DifferenceVectorType
a difference
Definition KernelDensity.h:82
Space_ SpaceType
the particles' space
Definition KernelDensity.h:58
Scalar logTotalWeight() const
Definition KernelDensity.h:123
Allocator_ allocator_type
the particles' allocator
Definition KernelDensity.h:60
MomentsType moments() const
Definition KernelDensity.h:147
static constexpr int ValueDimensionAtCompileTime
Definition KernelDensity.h:63
Index size() const
Definition KernelDensity.h:88
static constexpr int DifferenceDimensionAtCompileTime
Definition KernelDensity.h:69
Scalar logDensity(Eigen::MatrixBase< Derived > const &x) const
Definition KernelDensity.h:180
typename ParamsType::MomentsType MomentsType
a mean and covariance together
Definition KernelDensity.h:78
typename ParamsType::Scalar Scalar
the scalar type
Definition KernelDensity.h:79
GaussianParams< Linear< DifferenceDimensionAtCompileTime, MaxDifferenceDimensionAtCompileTime >, Scalar_ > KernelType
The zero-mean kernel, in the linear tangent space of Space_.
Definition KernelDensity.h:75
typename ParamsType::MatrixType MatrixType
a matrix over differences
Definition KernelDensity.h:83
typename ParamsType::VectorType VectorType
a value, such as a state
Definition KernelDensity.h:81
ParticleSet< Space_, Scalar_, Allocator_ > ParamsType
the particles it reads
Definition KernelDensity.h:59
MatrixType const & bandwidth() const
Definition KernelDensity.h:130
typename ParamsType::Index Index
Eigen's index type.
Definition KernelDensity.h:80
ParamsType const & particles() const
Definition KernelDensity.h:116
Index differenceDimension() const
Definition KernelDensity.h:102
KernelType::DensityType kernel() const
Definition KernelDensity.h:137
typename ParamsType::SampleSetType SampleSetType
states as columns
Definition KernelDensity.h:84
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition KernelDensity.h:72
Index valueDimension() const
Definition KernelDensity.h:95
static constexpr int MaxValueDimensionAtCompileTime
Definition KernelDensity.h:66
MomentsType moments(P &&policy) const
moments() under policy: the particles' moments are reductions over them (see Execution policies).
Definition KernelDensity.h:157
Space_ const & space() const
Definition KernelDensity.h:109
Streaming log-sum-exp: accumulates without overflow.
Definition Weights.h:46
void add(Scalar x)
Adds x to the sum, rescaling the running sum if x is the new maximum.
Definition Weights.h:51
Scalar value() const
Definition Weights.h:133
ValueVector< Space_, Scalar > VectorType
a value, such as the mean
Definition Moments.h:74
A set of weighted samples: the empirical distribution.
Definition ParticleSet.h:279
typename MomentsType::Index Index
Definition ParticleSet.h:284
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition ParticleSet.h:317
Index size() const
Definition ParticleSet.h:606
static constexpr int ValueDimensionAtCompileTime
Definition ParticleSet.h:308
typename MomentsType::Scalar Scalar
Definition ParticleSet.h:283
static constexpr int MaxValueDimensionAtCompileTime
Definition ParticleSet.h:311
typename MomentsType::DifferenceVectorType DifferenceVectorType
Definition ParticleSet.h:286
Space_ const & space() const
Definition ParticleSet.h:652
Index differenceDimension() const
Definition ParticleSet.h:645
static constexpr int DifferenceDimensionAtCompileTime
Definition ParticleSet.h:314
typename MomentsType::VectorType VectorType
Definition ParticleSet.h:285
Moments< Space_, Scalar_ > MomentsType
Definition ParticleSet.h:282
Index valueDimension() const
Definition ParticleSet.h:638
typename MomentsType::MatrixType MatrixType
Definition ParticleSet.h:287
SampleSet< Space_, Scalar_ > SampleSetType
Definition ParticleSet.h:290
An execution policy: execution::sequential, a policy of the standard library such as std::execution::...
Definition Execution.h:459
Requirements on a resampler for scalar type Scalar and random generator G.
Definition Resampling.h:61
A type usable as the space of a distribution.
Definition Space.h:186
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
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
constexpr Sequential sequential
The default execution policy: plain loops on the calling thread, in index order (Sequential).
Definition Execution.h:128
auto plus(S const &space, Eigen::MatrixBase< XDerived > const &x, Eigen::MatrixBase< DDerived > const &d)
Definition Space.h:675
auto minus(S const &space, Eigen::MatrixBase< ADerived > const &a, Eigen::MatrixBase< BDerived > const &b)
Definition Space.h:715
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
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
Scalar logDensity
the log-density at sample
Definition Sampling.h:52
Samples in Space together with the log of the density they were drawn from, evaluated at each.
Definition Sampling.h:36
Draws each ancestor independently.
Definition Resampling.h:219