Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Bernoulli Distribution

From Leeroopedia
Revision as of 14:31, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Avhz_RustQuant_Bernoulli_Distribution.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Mathematics, Statistics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete implementation of the Bernoulli probability distribution provided by the RustQuant library.

Description

The Bernoulli struct models the Bernoulli distribution, denoted X ~ Bern(p). This is a discrete probability distribution that takes the value 1 with probability p and the value 0 with probability q = 1 - p. It is the simplest discrete distribution and serves as a building block for the Binomial distribution.

The struct contains a single field:

  • p (f64) -- the probability of success (k = 1), constrained to [0, 1].

The implementation provides the full Distribution trait interface including: characteristic function (cf), probability mass function (pmf), cumulative distribution function (cdf), inverse CDF (quantile function), statistical moments (mean, median, mode, variance, skewness, kurtosis), entropy, moment generating function (mgf), and random sampling. A Default implementation sets p = 0.5.

The pdf method delegates to pmf since the Bernoulli distribution is discrete. Random sampling is performed using the rand_distr crate internally.

Usage

Use this distribution when modeling binary outcomes (success/failure, yes/no, true/false) with a fixed probability of success. Common applications in quantitative finance include modeling default/no-default events, barrier breaches, and binary option payoffs.

Code Reference

Source Location

Signature

pub struct Bernoulli {
    p: f64,
}

impl Bernoulli {
    pub fn new(probability: f64) -> Bernoulli
}

impl Distribution for Bernoulli {
    fn cf(&self, t: f64) -> Complex<f64>;
    fn pdf(&self, x: f64) -> f64;
    fn pmf(&self, k: f64) -> f64;
    fn cdf(&self, k: f64) -> f64;
    fn inv_cdf(&self, p: f64) -> f64;
    fn mean(&self) -> f64;
    fn median(&self) -> f64;
    fn mode(&self) -> f64;
    fn variance(&self) -> f64;
    fn skewness(&self) -> f64;
    fn kurtosis(&self) -> f64;
    fn entropy(&self) -> f64;
    fn mgf(&self, t: f64) -> f64;
    fn sample(&self, n: usize) -> Result<Vec<f64>, RustQuantError>;
}

Import

use RustQuant::math::distributions::Bernoulli;
use RustQuant::math::distributions::Distribution;

I/O Contract

Inputs

Name Type Required Description
probability f64 Yes Probability of success (k = 1). Must be in [0, 1].

Outputs

Name Type Description
Bernoulli struct A new Bernoulli distribution instance with the given probability.
mean() f64 Returns p.
variance() f64 Returns p * (1 - p).
sample(n) Result<Vec<f64>, RustQuantError> A vector of n random variates, each 0.0 or 1.0.

Usage Examples

use RustQuant::math::distributions::{Bernoulli, Distribution};

// Create a Bernoulli distribution with p = 0.5
let bernoulli = Bernoulli::new(0.5);

// Statistical moments
assert_eq!(bernoulli.mean(), 0.5);
assert_eq!(bernoulli.variance(), 0.25);
assert_eq!(bernoulli.skewness(), 0.0);
assert_eq!(bernoulli.kurtosis(), -2.0);

// Probability mass function
assert_eq!(bernoulli.pmf(1.0), 0.5);
assert_eq!(bernoulli.pmf(0.0), 0.5);

// Cumulative distribution function
assert_eq!(bernoulli.cdf(0.0), 0.5);
assert_eq!(bernoulli.cdf(1.0), 1.0);

// Generate 100 random samples
let sample = bernoulli.sample(100).expect("Bernoulli sampled.");

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment