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
#![warn(missing_docs)] //! Computation with uncertain values. //! //! When working with values which are not exactly determined, such as sensor data, it //! can be difficult to handle uncertainties correctly. //! //! The [`Uncertain`] trait makes such computations as natural as regular computations: //! //! ``` //! use uncertain::{Uncertain, Distribution}; //! use rand_distr::Normal; //! //! // Some inputs about which we are not sure //! let x = Distribution::from(Normal::new(5.0, 2.0).unwrap()); //! let y = Distribution::from(Normal::new(7.0, 3.0).unwrap()); //! //! // Do some computations //! let distance = x.sub(y).map(|diff: f64| diff.abs()); //! //! // Ask a question about the result //! let is_it_far = distance.map(|dist| dist > 2.0); //! //! // Check how certain the answer is //! assert_eq!(is_it_far.pr(0.9), false); //! assert_eq!(is_it_far.pr(0.5), true); //! ``` //! //! This works by sampling a Bayesian network which is implicitly created by describing the computation //! on the uncertain type. The [`Uncertain`] trait only permits tests for simple boolean hypotheses. This //! is by design: using Wald's [sequential probability ratio test][sprt], evaluation typically //! takes less than `100` samples. //! //! # References //! //! The [`Uncertain`] trait exported from the library is an implementation of //! the paper [`Uncertain<T>`][paper]. //! //! [paper]: https://www.cs.utexas.edu/users/mckinley/papers/uncertainty-asplos-2014.pdf //! [sprt]: https://en.wikipedia.org/wiki/Sequential_probability_ratio_test use adapters::*; use num_traits::{identities, Float}; use rand_pcg::Pcg32; use reference::RefUncertain; mod adapters; mod boxed; mod dist; mod expectation; mod point; mod reference; mod sprt; pub use boxed::BoxedUncertain; pub use dist::Distribution; pub use point::PointMass; pub use expectation::ConvergenceError; pub(crate) type Rng = Pcg32; /// An interface for using uncertain values in computations. #[must_use = "uncertain values are lazy and do nothing unless queried"] pub trait Uncertain { /// The type of the contained value. type Value; /// Generate a random sample from the distribution of this /// uncertain value. This is similar to [`Distribution::sample`], /// with one important difference: /// /// If the type which implements `Uncertain` is either [`Copy`] or [`Clone`], or if /// its references implement `Uncertain`, then it must guarantee that it will return /// the same value if queried consecutively with the same epoch (but different rng state). /// /// This is important when a value is reused within a computation. Consider the following /// example: /// ```text /// x ~ Normal(0, 1) /// y ~ Normal(0, 1) /// a = x + y /// b = a + x /// /// Correct computation graph: Incorrect computation graph: /// x --+---------+ x (2nd sample) --+ /// | | | /// | (+) -> b x --+ (+) -> b /// | | | | /// (+) -> a --+ (+) -> a -----+ /// | | /// y --+ y --+ /// ``` /// /// If your type is either [`Copy`] or [`Clone`], it is recommended to implement /// [`Distribution`] instead of this trait since any such type /// automatically implements [`Into<Distribution>`] in a correct way. /// /// [`Distribution`]: rand::distributions::Distribution /// [`Distribution::sample`]: rand::distributions::Distribution::sample /// [`Into<Distribution>`]: Distribution fn sample(&self, rng: &mut Rng, epoch: usize) -> Self::Value; /// Determine if the probability of obtaining `true` form this uncertain /// value is at least `probability`. /// /// This function evaluates a statistical test by sampling the underlying /// uncertain value and determining if it is plausible that it has been /// generated from a [Bernoulli distribution][bernoulli] /// with a value of p of at least `probability`. (I.e. if hypothesis /// `H_0: p >= probability` is plausible.) /// /// The underlying implementation uses the [sequential probability ratio test][sprt], /// which takes the least number of samples necessary to establish or reject /// a hypothesis. In practice this means that usually only `O(10)` samples /// are required. /// /// [bernoulli]: https://en.wikipedia.org/wiki/Bernoulli_distribution /// [sprt]: https://en.wikipedia.org/wiki/Sequential_probability_ratio_test /// /// # Panics /// /// Panics if `probability <= 0 || probability >= 1`. /// /// # Examples /// /// Basic usage: test if some event is more likely than not. /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let x = Distribution::from(Bernoulli::new(0.8).unwrap()); /// assert_eq!(x.pr(0.5), true); /// /// let y = Distribution::from(Bernoulli::new(0.3).unwrap()); /// assert_eq!(y.pr(0.5), false); /// ``` fn pr(&self, probability: f32) -> bool where Self::Value: Into<bool>, { if probability <= 0.0 || probability >= 1.0 { panic!("Probability {:?} must be in (0, 1)", probability); } sprt::compute(self, probability) } /// Calculate the expectation of this uncertain value to the desired /// precision. This can be useful e.g. when displaying values in a user /// interface. /// /// If the expected value does not converge to within the desired precision, /// a [`ConvergenceError`](ConvergenceError) is returned which can be used /// to obtain the non converged expectation and estimated error. /// /// Note that this value should typically not be used in further computation or in /// comparisons. It is usually more expensive to calculate than [`pr`](Uncertain::pr) and /// calculations or comparisons using the resulting value can be miss-leading. /// /// # Panics /// /// Panics if `precision <= 0`. /// /// # Example /// /// If we have a [bimodal][multi-modal] distribution, the expected value can lead to confusing /// results: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let choice = Distribution::from(Bernoulli::new(0.6).unwrap()); /// let value = choice.map(|c| if c { 1.0 } else { -1.0 }).into_ref(); /// /// let bigger_eq_zero = (&value).map(|v| v >= 0.0); /// let bigger_eq_half = (&value).map(|v| v >= 0.5); /// /// assert_eq!(bigger_eq_zero.pr(0.5), true); /// assert_eq!(bigger_eq_half.pr(0.5), true); // this is true /// /// let expected_value = value.expect(0.1).unwrap(); /// assert_eq!(expected_value >= 0.0, true); /// assert_eq!(expected_value >= 0.5, false); // but this is not :o /// ``` /// /// # Details /// /// To take as few samples as possible, this method utilizes an online sampling /// strategy to compute estimates of the mean and variance of the distribution /// modeled by the uncertain value. /// /// To determine if the mean has converged to the desired precision, the /// variance of the estimate (i.e. `var(E(x))`) is computed, assuming /// the samples are [identically and independently distributed][iid]. /// /// The function returns if the [two sigma confidence interval][two-sigma] (i.e. `2 * sqrt(var(E(x)))`) /// is smaller than the desired precision and the returned estimate lies within /// plus/ minus precision of the true value with approximately `95%` probability. /// /// [iid]: https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables /// [multi-modal]: https://en.wikipedia.org/wiki/Multimodal_distribution /// [two-sigma]: https://en.wikipedia.org/wiki/68–95–99.7_rule fn expect(&self, precision: Self::Value) -> Result<Self::Value, ConvergenceError<Self::Value>> where Self::Value: Float, { if precision <= identities::zero() { panic!("Precision must be larger than 0"); } expectation::compute(self, precision) } /// Box this uncertain value, such that it's type becomes opaque. This is /// necessary when you want to mix different sources for uncertain values /// e.g. to return different distributions inside [`flat_map`](Self::flat_map). /// /// This boxes the underlying uncertain value using a trait object and should /// only be used if necessary. /// /// # Examples /// /// Basic example: /// /// ``` /// use uncertain::{Uncertain, Distribution, PointMass}; /// use rand_distr::{Bernoulli, StandardNormal}; /// /// let choice = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let value = choice.flat_map(|fixed| if fixed { /// PointMass::new(5.0).into_boxed() /// } else { /// Distribution::from(StandardNormal).into_boxed() /// }); /// assert!(value.map(|v| v > 0.25).pr(0.5)); /// ``` fn into_boxed(self) -> BoxedUncertain<Self::Value> where Self: 'static + Sized + Send, { BoxedUncertain::new(self) } /// Bundle this uncertain value with a cache, so it can be reused in a calculation. /// /// Uncertain values should normally not implement `Copy` or `Clone`, since the same value /// is only allowed to be sampled once for every epoch (see [`sample`](Self::sample)). /// This wrapper allows a value to be reused by caching the sample result for every epoch and /// implementing [`Uncertain`] for references. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(5.0, 2.0).unwrap()).into_ref(); /// let y = Distribution::from(Normal::new(10.0, 5.0).unwrap()); /// let a = y.add(&x); /// let b = a.add(&x); /// /// let bigger_than_twelve = b.map(|v| v > 12.0); /// assert!(bigger_than_twelve.pr(0.5)); /// ``` fn into_ref(self) -> RefUncertain<Self> where Self: Sized, Self::Value: Clone, { RefUncertain::new(self) } /// Takes an uncertain value and produces another which /// generates values by calling a closure. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(0.0, 1.0).unwrap()); /// let y = x.map(|x| 5.0 + x); /// let bigger_eq_four = y.map(|v| v >= 4.0); /// assert!(bigger_eq_four.pr(0.5)); /// ``` fn map<O, F>(self, func: F) -> Map<Self, F> where Self: Sized, F: Fn(Self::Value) -> O, { Map::new(self, func) } /// Takes an uncertain value and produces another which /// generates values by calling a closure to generate /// fresh uncertain types that can depend on the value /// contained in self. /// /// This is useful for cases where the distribution of /// an uncertain value depends on another. /// /// # Example /// /// Basic example: model two poker chip factories. The first of /// which produces chips with `N ~ Binomial(20, 0.3)` and /// the second of which produces chips with `M ~ Binomial(50, 0.5)`. /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::{Binomial, Bernoulli}; /// /// let is_first_factory = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let number_of_chips = is_first_factory /// .flat_map(|is_first| if is_first { /// Distribution::from(Binomial::new(20, 0.3).unwrap()) /// } else { /// Distribution::from(Binomial::new(50, 0.5).unwrap()) /// }); /// assert!(number_of_chips.map(|n| n < 25).pr(0.5)); /// ``` fn flat_map<O, F>(self, func: F) -> FlatMap<Self, F> where Self: Sized, O: Uncertain, F: Fn(Self::Value) -> O, { FlatMap::new(self, func) } /// Combine two uncertain values using a closure. The closure /// `func` receives `self` as the first, and `other` as the /// second argument. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let x = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let y = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let are_equal = x.join(y, |x, y| x == y); /// assert!(are_equal.pr(0.5)); /// ``` fn join<O, U, F>(self, other: U, func: F) -> Join<Self, U, F> where Self: Sized, U: Uncertain, F: Fn(Self::Value, U::Value) -> O, { Join::new(self, other, func) } /// Negate the boolean contained in self. This is a shorthand /// for `x.map(|b| !b)`. /// /// # Examples /// /// Inverting a Bernoulli distribution: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let x = Distribution::from(Bernoulli::new(0.1).unwrap()); /// assert!(x.not().pr(0.9)); /// ``` fn not(self) -> Not<Self> where Self: Sized, Self::Value: Into<bool>, { Not::new(self) } /// Combines two boolean values. This should be preferred over /// `x.join(y, |x, y| x && y)`, since it uses short-circuit logic /// to avoid sampling `y` if `x` is already false. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let x = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let y = Distribution::from(Bernoulli::new(0.5).unwrap()); /// let both = x.and(y); /// assert_eq!(both.pr(0.5), false); /// assert_eq!(both.not().pr(0.5), true); /// ``` fn and<U>(self, other: U) -> And<Self, U> where Self: Sized, Self::Value: Into<bool>, U: Uncertain, U::Value: Into<bool>, { And::new(self, other) } /// Combines two boolean values. This should be preferred over /// `x.join(y, |x, y| x || y)`, since it uses short-circuit logic /// to avoid sampling `y` if `x` is already true. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Bernoulli; /// /// let x = Distribution::from(Bernoulli::new(0.3).unwrap()); /// let y = Distribution::from(Bernoulli::new(0.3).unwrap()); /// let either = x.or(y); /// assert_eq!(either.pr(0.5), true); /// assert_eq!(either.not().pr(0.5), false); /// ``` fn or<U>(self, other: U) -> Or<Self, U> where Self: Sized, Self::Value: Into<bool>, U: Uncertain, U::Value: Into<bool>, { Or::new(self, other) } /// Add two uncertain values. This is a shorthand /// for `x.join(y, |x, y| x + y)`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(1.0, 1.0).unwrap()); /// let y = Distribution::from(Normal::new(4.0, 1.0).unwrap()); /// assert!(x.add(y).map(|sum| sum >= 5.0).pr(0.5)); /// ``` fn add<U>(self, other: U) -> Sum<Self, U> where Self: Sized, U: Uncertain, Self::Value: std::ops::Add<U::Value>, { Sum::new(self, other) } /// Subtract two uncertain values. This is a shorthand /// for `x.join(y, |x, y| x - y)`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(7.0, 1.0).unwrap()); /// let y = Distribution::from(Normal::new(2.0, 1.0).unwrap()); /// assert!(x.sub(y).map(|diff| diff >= 5.0).pr(0.5)); /// ``` fn sub<U>(self, other: U) -> Difference<Self, U> where Self: Sized, U: Uncertain, Self::Value: std::ops::Sub<U::Value>, { Difference::new(self, other) } /// Multiply two uncertain values. This is a shorthand /// for `x.join(y, |x, y| x * y)`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(4.0, 1.0).unwrap()); /// let y = Distribution::from(Normal::new(2.0, 1.0).unwrap()); /// assert!(x.mul(y).map(|prod| prod >= 4.0).pr(0.5)); /// ``` fn mul<U>(self, other: U) -> Product<Self, U> where Self: Sized, U: Uncertain, Self::Value: std::ops::Mul<U::Value>, { Product::new(self, other) } /// Divide two uncertain values. This is a shorthand /// for `x.join(y, |x, y| x / y)`. /// /// # Examples /// /// Basic usage: /// /// ``` /// use uncertain::{Uncertain, Distribution}; /// use rand_distr::Normal; /// /// let x = Distribution::from(Normal::new(100.0, 1.0).unwrap()); /// let y = Distribution::from(Normal::new(2.0, 1.0).unwrap()); /// assert!(x.div(y).map(|prod| prod <= 50.0).pr(0.5)); /// ``` fn div<U>(self, other: U) -> Ratio<Self, U> where Self: Sized, U: Uncertain, Self::Value: std::ops::Div<U::Value>, { Ratio::new(self, other) } }