Implementation:Avhz RustQuant Uniform Distribution
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Statistics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete implementation of the Uniform probability distribution provided by the RustQuant library.
Description
The Uniform struct models the Uniform distribution, denoted X ~ Uni(a, b). This distribution assigns equal probability to all values within the interval [a, b]. The implementation supports both continuous and discrete variants through the DistributionClass enum.
The struct contains three fields:
- a (f64) -- the lower bound of the distribution.
- b (f64) -- the upper bound of the distribution.
- class (DistributionClass) -- either Discrete or Continuous.
When constructed with DistributionClass::Discrete, the bounds are rounded to the nearest integer. The implementation provides the full Distribution trait interface with class-aware behavior: the PDF, PMF, CDF, variance, kurtosis, entropy, MGF, and characteristic function all compute different formulas depending on whether the distribution is discrete or continuous.
The continuous PDF returns 1/(b-a) within [a,b] and 0 otherwise. The discrete PMF returns 1/(b-a+1) within [a,b] and 0 otherwise. The inverse CDF is only implemented for the continuous case (the discrete case uses todo!()). The mode for the discrete case is also unimplemented.
Random sampling delegates to the rand_distr crate.
Usage
Use this distribution when all outcomes in a range are equally likely. In quantitative finance, the continuous uniform distribution is used for random number generation (the basis of Monte Carlo simulation), probability integral transforms, and copula modeling. The discrete variant models equiprobable discrete outcomes.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/distributions/uniform.rs
- Lines: 1-423
Signature
pub struct Uniform {
a: f64,
b: f64,
class: DistributionClass,
}
impl Uniform {
pub fn new(a: f64, b: f64, class: DistributionClass) -> Self
}
impl Distribution for Uniform {
fn cf(&self, t: f64) -> Complex<f64>;
fn pdf(&self, x: f64) -> f64;
fn pmf(&self, x: f64) -> f64;
fn cdf(&self, x: f64) -> f64;
fn inv_cdf(&self, p: f64) -> f64; // Continuous only
fn mean(&self) -> f64;
fn median(&self) -> f64;
fn mode(&self) -> f64; // Continuous only
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::Uniform;
use RustQuant::math::distributions::Distribution;
use RustQuant::math::DistributionClass;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| a | f64 | Yes | Lower bound of the distribution. Must be <= b. |
| b | f64 | Yes | Upper bound of the distribution. |
| class | DistributionClass | Yes | Either Discrete or Continuous. |
Outputs
| Name | Type | Description |
|---|---|---|
| Uniform | struct | A new Uniform distribution instance. |
| mean() | f64 | Returns (a + b) / 2. |
| variance() | f64 | Returns (b - a)^2 / 12 (continuous) or (b - a + 1)^2 / 12 (discrete). |
| skewness() | f64 | Always returns 0.0. |
| sample(n) | Result<Vec<f64>, RustQuantError> | A vector of n random variates from the Uniform distribution. |
Usage Examples
use RustQuant::math::{Uniform, DistributionClass, distributions::Distribution};
// Create a continuous Uniform distribution on [0, 1]
let dist = Uniform::new(0.0, 1.0, DistributionClass::Continuous);
// Statistical moments
assert_eq!(dist.mean(), 0.5);
assert_eq!(dist.skewness(), 0.0);
// PDF: constant within the interval
let pdf = dist.pdf(0.5); // 1.0
// CDF: linear interpolation
let cdf = dist.cdf(0.5); // 0.5
// Inverse CDF
let inv = dist.inv_cdf(0.5); // 0.5
// Discrete Uniform distribution on {0, 1}
let disc = Uniform::new(0.0, 1.0, DistributionClass::Discrete);
let pmf = disc.pmf(0.5); // 0.5
// Generate 1000 random samples
let sample = dist.sample(1000).unwrap();