Skip to main content

MultivariateDensity

Struct MultivariateDensity 

Source
pub struct MultivariateDensity<T, D>(/* private fields */)
where
    T: Scalar,
    D: Dim,
    DefaultAllocator: Allocator<D>;
Expand description

A D-dimensional distribution where each dimension is independent with potentially different univariate distributions. This is a product distribution:

  • Each marginal follows one of the available univariate distributions (Normal, Uniform, Cosine, etc.)
  • The joint density is the product of marginals: f(x₁, …, xₐ) = f₁(x₁) × … × fₐ(xₐ)

§Construction & Examples

Create a mixed 3D distribution (Normal × Uniform × Constant):

let marginals = SVector::from([
    NormalDensity::new(0.0, 1.0, None, None).unwrap().into(),
    UniformDensity::new(-1.0, 1.0).unwrap().into(),
    ConstantDensity::new(2.0).into(),
]);
let _dist = MultivariateDensity::<f64, Const<3>>::new(marginals);

Create a 5D distribution with mixed univariates:

let mvpdf = MultivariateDensity::<f64, Const<5>>::new(SVector::from([
   ConstantDensity::new(1.0).into(),
   CosineDensity::new(0.1, 0.2).unwrap().into(),
   LogUniformDensity::new(0.1, 0.5).unwrap().into(),
   NormalDensity::new(0.1, 0.25, Some(-0.5), Some(1.5)).unwrap().into(),
   UniformDensity::new(1.0, 2.0).unwrap().into(),
]));

Evaluate density at a point:

let marginals = SVector::from([
    NormalDensity::new(0.0, 1.0, None, None).unwrap().into(),
    UniformDensity::new(-1.0, 1.0).unwrap().into(),
]);
let dist = MultivariateDensity::<f64, U2>::new(marginals);
let sample = SVector::from([0.0, 0.5]);
// Use the Density trait to evaluate - see crate::Density for usage patterns
if let Some(dens) = (&dist).density::<U1, U2>(&sample.as_view()) {
    println!("Joint density: {}", dens);
}

Sample from the distribution:

let marginals = SVector::from([
    NormalDensity::new(0.0, 1.0, None, None).unwrap().into(),
    UniformDensity::new(-1.0, 1.0).unwrap().into(),
]);
let dist = MultivariateDensity::<f64, U2>::new(marginals);
let mut rng = StdRng::seed_from_u64(42);
if let Some(sample) = (&dist).sample(&mut rng, &SamplingMode::default()) {
    println!("Generated sample: {:?}", sample);
}

Implementations§

Source§

impl<T, D> MultivariateDensity<T, D>

Source

pub fn new(domains: OVector<UnivariateDensity<T>, D>) -> Self

Create a new MultivariateDensity from a vector of UnivariateDensitys.

Source

pub fn marginals(&self) -> &OVector<UnivariateDensity<T>, D>

Return a reference to the underlying vector of UnivariateDensitys.

Trait Implementations§

Source§

impl<T, D> Clone for MultivariateDensity<T, D>

Source§

fn clone(&self) -> MultivariateDensity<T, D>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, D> Debug for MultivariateDensity<T, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, D> Density<T, D> for MultivariateDensity<T, D>

Source§

fn density<RStride: Dim, CStride: Dim>( &self, sample: &VectorView<'_, T, D, RStride, CStride>, ) -> Option<T>

Calculates, or estimates, a density value for a sample. Returns None if the sample is outside of the function domain. Read more
Source§

fn domain(&self) -> Domain<T, D>

Returns the underlying function Domain.
Source§

fn mean(&self) -> OVector<T, D>

Returns the mean of the distribution.
Source§

fn sample( &self, rng: &mut impl RngExt, mode: &SamplingMode, ) -> Option<OVector<T, D>>

Draw a random sample from the probability density distribution using the provided random number generator and sampling mode. Read more
Source§

fn sample_iter( &self, rng: &mut impl RngExt, ) -> impl Iterator<Item = Option<OVector<T, D>>>

Returns an iterator that yields random samples from the distribution according to the specified sampling mode. Read more
Source§

fn variance(&self) -> OVector<T, D>

Returns the variance of the distribution.
Source§

fn ndims(&self) -> usize

Returns the number of dimensions of the distribution.
Source§

impl<'de, T, D> Deserialize<'de> for MultivariateDensity<T, D>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> From<MultivariateDensity<T, Const<1>>> for UnivariateDensity<T>
where T: RealField,

Source§

fn from(mv: MultivariateDensity<T, U1>) -> Self

Convert a 1D MultivariateDensity to a UnivariateDensity by extracting its single marginal.

Source§

impl From<MultivariateDensity<f64, Dyn>> for PyMultivariate

Source§

fn from(dist: MultivariateDensity<f64, Dyn>) -> Self

Convert a MultivariateDensity with runtime dimensions to a PyMultivariate.

Source§

impl<'__deriveMoreLifetime, T, D> IntoIterator for &'__deriveMoreLifetime MultivariateDensity<T, D>
where &'__deriveMoreLifetime OVector<UnivariateDensity<T>, D>: IntoIterator, T: Scalar, D: Dim, DefaultAllocator: Allocator<D>,

Source§

type Item = <&'__deriveMoreLifetime Matrix<UnivariateDensity<T>, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<UnivariateDensity<T>>> as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = <&'__deriveMoreLifetime Matrix<UnivariateDensity<T>, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<UnivariateDensity<T>>> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, D> IntoIterator for MultivariateDensity<T, D>

Source§

type Item = <Matrix<UnivariateDensity<T>, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<UnivariateDensity<T>>> as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = <Matrix<UnivariateDensity<T>, D, Const<1>, <DefaultAllocator as Allocator<D>>::Buffer<UnivariateDensity<T>>> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, D> Serialize for MultivariateDensity<T, D>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T, D> !Freeze for MultivariateDensity<T, D>

§

impl<T, D> !RefUnwindSafe for MultivariateDensity<T, D>

§

impl<T, D> !Send for MultivariateDensity<T, D>

§

impl<T, D> !Sync for MultivariateDensity<T, D>

§

impl<T, D> !Unpin for MultivariateDensity<T, D>

§

impl<T, D> !UnsafeUnpin for MultivariateDensity<T, D>

§

impl<T, D> !UnwindSafe for MultivariateDensity<T, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<'py, T, I> IntoPyDict<'py> for I
where T: PyDictItem<'py>, I: IntoIterator<Item = T>,

Source§

fn into_py_dict(self, py: Python<'py>) -> Result<Bound<'py, PyDict>, PyErr>

Converts self into a PyDict object pointer. Whether pointer owned or borrowed depends on implementation.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Ungil for T
where T: Send,