1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
//! Trait definitions
use crate::data::DataOrSuffStat;
use rand::Rng;
/// Random variable
///
/// Contains the minimal functionality that a random object must have to be
/// useful: a function defining the un-normalized density/mass at a point,
/// and functions to draw samples from the distribution.
pub trait Rv<X> {
/// Probability function
///
/// # Example
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::Rv;
///
/// let g = Gaussian::standard();
/// assert!(g.f(&0.0_f64) > g.f(&0.1_f64));
/// assert!(g.f(&0.0_f64) > g.f(&-0.1_f64));
/// ```
fn f(&self, x: &X) -> f64 {
self.ln_f(x).exp()
}
/// Probability function
///
/// # Example
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::Rv;
///
/// let g = Gaussian::standard();
/// assert!(g.ln_f(&0.0_f64) > g.ln_f(&0.1_f64));
/// assert!(g.ln_f(&0.0_f64) > g.ln_f(&-0.1_f64));
/// ```
fn ln_f(&self, x: &X) -> f64;
/// Single draw from the `Rv`
///
/// # Example
///
/// Flip a coin
///
/// ```
/// use rv::dist::Bernoulli;
/// use rv::traits::Rv;
///
/// let b = Bernoulli::uniform();
/// let mut rng = rand::thread_rng();
/// let x: bool = b.draw(&mut rng); // could be true, could be false.
/// ```
fn draw<R: Rng>(&self, rng: &mut R) -> X;
/// Multiple draws of the `Rv`
///
/// # Example
///
/// Flip a lot of coins
///
/// ```
/// use rv::dist::Bernoulli;
/// use rv::traits::Rv;
///
/// let b = Bernoulli::uniform();
/// let mut rng = rand::thread_rng();
/// let xs: Vec<bool> = b.sample(22, &mut rng);
///
/// assert_eq!(xs.len(), 22);
/// ```
///
/// Estimate Gaussian mean
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::Rv;
///
/// let gauss = Gaussian::standard();
/// let mut rng = rand::thread_rng();
/// let xs: Vec<f64> = gauss.sample(100_000, &mut rng);
///
/// assert::close(xs.iter().sum::<f64>()/100_000.0, 0.0, 1e-2);
/// ```
fn sample<R: Rng>(&self, n: usize, mut rng: &mut R) -> Vec<X> {
(0..n).map(|_| self.draw(&mut rng)).collect()
}
/// Create a never-ending iterator of samples
///
/// # Example
///
/// Estimate the mean of a Gamma distribution
///
/// ```
/// use rv::traits::Rv;
/// use rv::dist::Gamma;
///
/// let mut rng = rand::thread_rng();
///
/// let gamma = Gamma::new(2.0, 1.0).unwrap();
///
/// let n = 1_000_000_usize;
/// let mean = <Gamma as Rv<f64>>::sample_stream(&gamma, &mut rng)
/// .take(n)
/// .sum::<f64>() / n as f64;;
///
/// assert::close(mean, 2.0, 1e-2);
/// ```
fn sample_stream<'r, R: Rng>(
&'r self,
mut rng: &'r mut R,
) -> Box<dyn Iterator<Item = X> + 'r> {
Box::new(std::iter::repeat_with(move || self.draw(&mut rng)))
}
}
/// Identifies the support of the Rv
pub trait Support<X> {
/// Returns `true` if `x` is in the support of the `Rv`
///
/// # Example
///
/// ```
/// use rv::dist::Uniform;
/// use rv::traits::Support;
///
/// // Create uniform with support on the interval [0, 1]
/// let u = Uniform::new(0.0, 1.0).unwrap();
///
/// assert!(u.supports(&0.5_f64));
/// assert!(!u.supports(&-0.1_f64));
/// assert!(!u.supports(&1.1_f64));
/// ```
fn supports(&self, x: &X) -> bool;
}
/// Is a continuous probability distributions
///
/// This trait uses the `Rv<X>` and `Support<X>` implementations to implement
/// itself.
pub trait ContinuousDistr<X>: Rv<X> + Support<X> {
/// The value of the Probability Density Function (PDF) at `x`
///
/// # Example
///
/// Compute the Gaussian PDF, f(x)
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::ContinuousDistr;
///
/// let g = Gaussian::standard();
///
/// let f_mean = g.pdf(&0.0_f64);
/// let f_low = g.pdf(&-1.0_f64);
/// let f_high = g.pdf(&1.0_f64);
///
/// assert!(f_mean > f_low);
/// assert!(f_mean > f_high);
/// assert!((f_low - f_high).abs() < 1E-12);
/// ```
///
/// Returns 0 if x is not in support
///
/// ```
/// # use rv::traits::ContinuousDistr;
/// use rv::dist::Exponential;
///
/// let expon = Exponential::new(1.0).unwrap();
/// let f = expon.pdf(&-1.0_f64);
/// assert_eq!(f, 0.0);
/// ```
fn pdf(&self, x: &X) -> f64 {
self.ln_pdf(x).exp()
}
/// The value of the log Probability Density Function (PDF) at `x`
///
/// # Example
///
/// Compute the natural logarithm of the Gaussian PDF, ln(f(x))
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::ContinuousDistr;
///
/// let g = Gaussian::standard();
///
/// let lnf_mean = g.ln_pdf(&0.0_f64);
/// let lnf_low = g.ln_pdf(&-1.0_f64);
/// let lnf_high = g.ln_pdf(&1.0_f64);
///
/// assert!(lnf_mean > lnf_low);
/// assert!(lnf_mean > lnf_high);
/// assert!((lnf_low - lnf_high).abs() < 1E-12);
/// ```
///
/// Returns -inf if x is not in support
///
/// ```
/// # use rv::traits::ContinuousDistr;
/// use rv::dist::Exponential;
///
/// let expon = Exponential::new(1.0).unwrap();
/// let f = expon.ln_pdf(&-1.0_f64);
/// assert_eq!(f, std::f64::NEG_INFINITY);
/// ```
fn ln_pdf(&self, x: &X) -> f64 {
if self.supports(x) {
self.ln_f(x)
} else {
std::f64::NEG_INFINITY
}
}
}
/// Has a cumulative distribution function (CDF)
pub trait Cdf<X>: Rv<X> {
/// The value of the Cumulative Density Function at `x`
///
/// # Example
///
/// The proportion of probability in (-∞, μ) in N(μ, σ) is 50%
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::Cdf;
///
/// let g = Gaussian::new(1.0, 1.5).unwrap();
///
/// assert!((g.cdf(&1.0_f64) - 0.5).abs() < 1E-12);
/// ```
fn cdf(&self, x: &X) -> f64;
/// Survival function, `1 - CDF(x)`
fn sf(&self, x: &X) -> f64 {
1.0 - self.cdf(x)
}
}
/// Has an inverse-CDF / quantile function
pub trait InverseCdf<X>: Rv<X> + Support<X> {
/// The value of the `x` at the given probability in the CDF
///
/// # Example
///
/// The CDF identity: p = CDF(x) => x = CDF<sup>-1</sup>(p)
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::Cdf;
/// use rv::traits::InverseCdf;
///
/// let g = Gaussian::standard();
///
/// let x: f64 = 1.2;
/// let p: f64 = g.cdf(&x);
/// let y: f64 = g.invcdf(p);
///
/// // x and y should be about the same
/// assert!((x - y).abs() < 1E-12);
/// ```
fn invcdf(&self, p: f64) -> X;
/// Alias for `invcdf`
fn quantile(&self, p: f64) -> X {
self.invcdf(p)
}
/// Interval containing `p` proportion for the probability
///
/// # Example
///
/// Confidence interval
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::InverseCdf;
///
/// let g = Gaussian::new(100.0, 15.0).unwrap();
/// let ci: (f64, f64) = g.interval(0.68268949213708585); // one stddev
/// assert!( (ci.0 - 85.0).abs() < 1E-12);
/// assert!( (ci.1 - 115.0).abs() < 1E-12);
/// ```
fn interval(&self, p: f64) -> (X, X) {
let pt = (1.0 - p) / 2.0;
(self.quantile(pt), self.quantile(p + pt))
}
}
/// Is a discrete probability distribution
pub trait DiscreteDistr<X>: Rv<X> + Support<X> {
/// Probability mass function (PMF) at `x`
///
/// # Panics
///
/// If `x` is not supported
///
/// # Example
///
/// The probability of a fair coin coming up heads in 0.5
///
/// ```
/// use rv::dist::Bernoulli;
/// use rv::traits::DiscreteDistr;
///
/// // Fair coin (p = 0.5)
/// let b = Bernoulli::uniform();
///
/// assert!( (b.pmf(&true) - 0.5).abs() < 1E-12);
/// ```
fn pmf(&self, x: &X) -> f64 {
self.ln_pmf(x).exp()
}
/// Natural logarithm of the probability mass function (PMF)
///
/// # Example
///
/// The probability of a fair coin coming up heads in 0.5
///
/// ```
/// use rv::dist::Bernoulli;
/// use rv::traits::DiscreteDistr;
///
/// // Fair coin (p = 0.5)
/// let b = Bernoulli::uniform();
///
/// assert!( (b.ln_pmf(&true) - 0.5_f64.ln()).abs() < 1E-12);
/// ```
fn ln_pmf(&self, x: &X) -> f64 {
if self.supports(x) {
self.ln_f(x)
} else {
std::f64::NEG_INFINITY
}
}
}
/// Defines the distribution mean
pub trait Mean<X> {
/// Returns `None` if the mean is undefined
fn mean(&self) -> Option<X>;
}
/// Defines the distribution median
pub trait Median<X> {
/// Returns `None` if the median is undefined
fn median(&self) -> Option<X>;
}
/// Defines the distribution mode
pub trait Mode<X> {
/// Returns `None` if the mode is undefined or is not a single value
fn mode(&self) -> Option<X>;
}
/// Defines the distribution variance
pub trait Variance<X> {
/// Returns `None` if the variance is undefined
fn variance(&self) -> Option<X>;
}
/// Defines the distribution entropy
pub trait Entropy {
/// The entropy, *H(X)*
fn entropy(&self) -> f64;
}
pub trait Skewness {
fn skewness(&self) -> Option<f64>;
}
pub trait Kurtosis {
fn kurtosis(&self) -> Option<f64>;
}
/// KL divergences
pub trait KlDivergence {
/// The KL divergence, KL(P|Q) between this distribution, P, and another, Q
///
/// # Example
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::KlDivergence;
///
/// let g1 = Gaussian::new(1.0, 1.0).unwrap();
/// let g2 = Gaussian::new(-1.0, 2.0).unwrap();
///
/// let kl_self = g1.kl(&g1);
/// let kl_other = g1.kl(&g2);
///
/// // KL(P|P) = 0
/// assert!( kl_self < 1E-12 );
///
/// // KL(P|Q) > 0 if P ≠ Q
/// assert!( kl_self < kl_other );
/// ```
fn kl(&self, other: &Self) -> f64;
/// Symmetrised divergence, KL(P|Q) + KL(Q|P)
///
/// # Example
///
/// ```
/// use rv::dist::Gaussian;
/// use rv::traits::KlDivergence;
///
/// let g1 = Gaussian::new(1.0, 1.0).unwrap();
/// let g2 = Gaussian::new(-1.0, 2.0).unwrap();
///
/// let kl_12 = g1.kl(&g2);
/// let kl_21 = g2.kl(&g1);
///
/// let kl_sym = g1.kl_sym(&g2);
///
/// assert!( (kl_12 + kl_21 - kl_sym).abs() < 1E-10 );
/// ```
fn kl_sym(&self, other: &Self) -> f64 {
self.kl(other) + other.kl(self)
}
}
/// The data for this distribution can be summarized by a statistic
pub trait HasSuffStat<X>: Rv<X> {
type Stat: SuffStat<X>;
fn empty_suffstat(&self) -> Self::Stat;
/// Return the log likelihood for the data represented by the sufficient
/// statistic.
fn ln_f_stat(&self, stat: &Self::Stat) -> f64;
}
/// Is a [sufficient statistic](https://en.wikipedia.org/wiki/Sufficient_statistic) for a
/// distribution.
///
/// # Examples
///
/// Basic suffstat useage.
///
/// ```
/// use rv::data::BernoulliSuffStat;
/// use rv::traits::SuffStat;
///
/// // Bernoulli sufficient statistics are the number of observations, n, and
/// // the number of successes, k.
/// let mut stat = BernoulliSuffStat::new();
///
/// assert!(stat.n() == 0 && stat.k() == 0);
///
/// stat.observe(&true); // observe `true`
/// assert!(stat.n() == 1 && stat.k() == 1);
///
/// stat.observe(&false); // observe `false`
/// assert!(stat.n() == 2 && stat.k() == 1);
///
/// stat.forget_many(&vec![false, true]); // forget `true` and `false`
/// assert!(stat.n() == 0 && stat.k() == 0);
/// ```
///
/// Conjugate analysis of coin flips using Bernoulli with a Beta prior on the
/// success probability.
///
/// ```
/// use rv::traits::SuffStat;
/// use rv::traits::ConjugatePrior;
/// use rv::data::BernoulliSuffStat;
/// use rv::dist::{Bernoulli, Beta};
///
/// let flips = vec![true, false, false];
///
/// // Pack the data into a sufficient statistic that holds the number of
/// // trials and the number of successes
/// let mut stat = BernoulliSuffStat::new();
/// stat.observe_many(&flips);
///
/// let prior = Beta::jeffreys();
///
/// // If we observe more false than true, the posterior predictive
/// // probability of true decreases.
/// let pp_no_obs = prior.pp(&true, &(&BernoulliSuffStat::new()).into());
/// let pp_obs = prior.pp(&true, &(&flips).into());
///
/// assert!(pp_obs < pp_no_obs);
/// ```
pub trait SuffStat<X> {
/// Returns the number of observations
fn n(&self) -> usize;
/// Assimilate the datum `x` into the statistic
fn observe(&mut self, x: &X);
/// Remove the datum `x` from the statistic
fn forget(&mut self, x: &X);
/// Assimilate several observations
fn observe_many(&mut self, xs: &[X]) {
xs.iter().for_each(|x| self.observe(x));
}
/// Forget several observations
fn forget_many(&mut self, xs: &[X]) {
xs.iter().for_each(|x| self.forget(x));
}
}
/// A prior on `Fx` that induces a posterior that is the same form as the prior
///
/// # Example
///
/// Conjugate analysis of coin flips using Bernoulli with a Beta prior on the
/// success probability.
///
/// ```
/// use rv::traits::ConjugatePrior;
/// use rv::dist::{Bernoulli, Beta};
///
/// let flips = vec![true, false, false];
/// let prior = Beta::jeffreys();
///
/// // If we observe more false than true, the posterior predictive
/// // probability of true decreases.
/// let pp_no_obs = prior.pp(&true, &(&vec![]).into());
/// let pp_obs = prior.pp(&true, &(&flips).into());
///
/// assert!(pp_obs < pp_no_obs);
/// ```
///
/// Use a cache to speed up repeated computations.
///
/// ```
/// # use rv::traits::ConjugatePrior;
/// use rv::traits::{Rv, SuffStat};
/// use rv::dist::{Categorical, SymmetricDirichlet};
/// use rv::data::{CategoricalSuffStat, DataOrSuffStat};
/// use std::time::Instant;
///
/// let ncats = 10;
/// let symdir = SymmetricDirichlet::jeffreys(ncats).unwrap();
/// let mut suffstat = CategoricalSuffStat::new(ncats);
/// let mut rng = rand::thread_rng();
///
/// Categorical::new(&vec![1.0, 1.0, 5.0, 1.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0])
/// .unwrap()
/// .sample_stream(&mut rng)
/// .take(1000)
/// .for_each(|x: u8| suffstat.observe(&x));
///
///
/// let stat = DataOrSuffStat::SuffStat(&suffstat);
///
/// // Get predictions from predictive distribution using the cache
/// let t_cache = {
/// let t_start = Instant::now();
/// let cache = symdir.ln_pp_cache(&stat);
/// // Argmax
/// let k_max = (0..ncats).fold((0, std::f64::NEG_INFINITY), |(ix, f), y| {
/// let f_r = symdir.ln_pp_with_cache(&cache, &y);
/// if f_r > f {
/// (y, f_r)
/// } else {
/// (ix, f)
/// }
///
/// });
///
/// assert_eq!(k_max.0, 2);
/// t_start.elapsed()
/// };
///
/// // Get predictions from predictive distribution w/o cache
/// let t_no_cache = {
/// let t_start = Instant::now();
/// // Argmax
/// let k_max = (0..ncats).fold((0, std::f64::NEG_INFINITY), |(ix, f), y| {
/// let f_r = symdir.ln_pp(&y, &stat);
/// if f_r > f {
/// (y, f_r)
/// } else {
/// (ix, f)
/// }
///
/// });
///
/// assert_eq!(k_max.0, 2);
/// t_start.elapsed()
/// };
///
/// // Using cache improves runtime
/// assert!(t_no_cache.as_nanos() > 2 * t_cache.as_nanos());
/// ```
pub trait ConjugatePrior<X, Fx>: Rv<Fx>
where
Fx: Rv<X> + HasSuffStat<X>,
{
/// Type of the posterior distribution
type Posterior: Rv<Fx>;
/// Type of the `ln_m` cache
type LnMCache;
/// Type of the `ln_pp` cache
type LnPpCache;
/// Computes the posterior distribution from the data
fn posterior(&self, x: &DataOrSuffStat<X, Fx>) -> Self::Posterior;
/// Compute the cache for the log marginal likelihood.
fn ln_m_cache(&self) -> Self::LnMCache;
/// Log marginal likelihood with supplied cache.
fn ln_m_with_cache(
&self,
cache: &Self::LnMCache,
x: &DataOrSuffStat<X, Fx>,
) -> f64;
/// The log marginal likelihood
fn ln_m(&self, x: &DataOrSuffStat<X, Fx>) -> f64 {
let cache = self.ln_m_cache();
self.ln_m_with_cache(&cache, x)
}
/// Compute the cache for the Log posterior predictive of y given x.
///
/// The cache should encompass all information about `x`.
fn ln_pp_cache(&self, x: &DataOrSuffStat<X, Fx>) -> Self::LnPpCache;
/// Log posterior predictive of y given x with supplied ln(norm)
fn ln_pp_with_cache(&self, cache: &Self::LnPpCache, y: &X) -> f64;
/// Log posterior predictive of y given x
fn ln_pp(&self, y: &X, x: &DataOrSuffStat<X, Fx>) -> f64 {
let cache = self.ln_pp_cache(x);
self.ln_pp_with_cache(&cache, y)
}
/// Marginal likelihood of x
fn m(&self, x: &DataOrSuffStat<X, Fx>) -> f64 {
self.ln_m(x).exp()
}
/// Posterior Predictive distribution
fn pp(&self, y: &X, x: &DataOrSuffStat<X, Fx>) -> f64 {
self.ln_pp(y, x).exp()
}
}
/// Get the quad bounds of a univariate real distribution
pub trait QuadBounds {
fn quad_bounds(&self) -> (f64, f64);
}