summaryrefslogtreecommitdiff
path: root/rand/rand_distr/src/cauchy.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2020-04-04 14:39:19 -0700
committerDaniel Mueller <deso@posteo.net>2020-04-04 14:39:19 -0700
commitd0d9683df8398696147e7ee1fcffb2e4e957008c (patch)
tree4baa76712a76f4d072ee3936c07956580b230820 /rand/rand_distr/src/cauchy.rs
parent203e691f46d591a2cc8acdfd850fa9f5b0fb8a98 (diff)
downloadnitrocli-d0d9683df8398696147e7ee1fcffb2e4e957008c.tar.gz
nitrocli-d0d9683df8398696147e7ee1fcffb2e4e957008c.tar.bz2
Remove vendored dependencies
While it appears that by now we actually can get successful builds without Cargo insisting on Internet access by virtue of using the --frozen flag, maintaining vendored dependencies is somewhat of a pain point. This state will also get worse with upcoming changes that replace argparse in favor of structopt and pull in a slew of new dependencies by doing so. Then there is also the repository structure aspect, which is non-standard due to the way we vendor dependencies and a potential source of confusion. In order to fix these problems, this change removes all the vendored dependencies we have. Delete subrepo argparse/:argparse Delete subrepo base32/:base32 Delete subrepo cc/:cc Delete subrepo cfg-if/:cfg-if Delete subrepo getrandom/:getrandom Delete subrepo lazy-static/:lazy-static Delete subrepo libc/:libc Delete subrepo nitrokey-sys/:nitrokey-sys Delete subrepo nitrokey/:nitrokey Delete subrepo rand/:rand
Diffstat (limited to 'rand/rand_distr/src/cauchy.rs')
-rw-r--r--rand/rand_distr/src/cauchy.rs120
1 files changed, 0 insertions, 120 deletions
diff --git a/rand/rand_distr/src/cauchy.rs b/rand/rand_distr/src/cauchy.rs
deleted file mode 100644
index 6b0e7c6..0000000
--- a/rand/rand_distr/src/cauchy.rs
+++ /dev/null
@@ -1,120 +0,0 @@
-// Copyright 2018 Developers of the Rand project.
-// Copyright 2016-2017 The Rust Project Developers.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! The Cauchy distribution.
-
-use rand::Rng;
-use crate::{Distribution, Standard};
-use crate::utils::Float;
-
-/// The Cauchy distribution `Cauchy(median, scale)`.
-///
-/// This distribution has a density function:
-/// `f(x) = 1 / (pi * scale * (1 + ((x - median) / scale)^2))`
-///
-/// # Example
-///
-/// ```
-/// use rand_distr::{Cauchy, Distribution};
-///
-/// let cau = Cauchy::new(2.0, 5.0).unwrap();
-/// let v = cau.sample(&mut rand::thread_rng());
-/// println!("{} is from a Cauchy(2, 5) distribution", v);
-/// ```
-#[derive(Clone, Copy, Debug)]
-pub struct Cauchy<N> {
- median: N,
- scale: N,
-}
-
-/// Error type returned from `Cauchy::new`.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum Error {
- /// `scale <= 0` or `nan`.
- ScaleTooSmall,
-}
-
-impl<N: Float> Cauchy<N>
-where Standard: Distribution<N>
-{
- /// Construct a new `Cauchy` with the given shape parameters
- /// `median` the peak location and `scale` the scale factor.
- pub fn new(median: N, scale: N) -> Result<Cauchy<N>, Error> {
- if !(scale > N::from(0.0)) {
- return Err(Error::ScaleTooSmall);
- }
- Ok(Cauchy {
- median,
- scale
- })
- }
-}
-
-impl<N: Float> Distribution<N> for Cauchy<N>
-where Standard: Distribution<N>
-{
- fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> N {
- // sample from [0, 1)
- let x = Standard.sample(rng);
- // get standard cauchy random number
- // note that π/2 is not exactly representable, even if x=0.5 the result is finite
- let comp_dev = (N::pi() * x).tan();
- // shift and scale according to parameters
- self.median + self.scale * comp_dev
- }
-}
-
-#[cfg(test)]
-mod test {
- use crate::Distribution;
- use super::Cauchy;
-
- fn median(mut numbers: &mut [f64]) -> f64 {
- sort(&mut numbers);
- let mid = numbers.len() / 2;
- numbers[mid]
- }
-
- fn sort(numbers: &mut [f64]) {
- numbers.sort_by(|a, b| a.partial_cmp(b).unwrap());
- }
-
- #[test]
- fn test_cauchy_averages() {
- // NOTE: given that the variance and mean are undefined,
- // this test does not have any rigorous statistical meaning.
- let cauchy = Cauchy::new(10.0, 5.0).unwrap();
- let mut rng = crate::test::rng(123);
- let mut numbers: [f64; 1000] = [0.0; 1000];
- let mut sum = 0.0;
- for i in 0..1000 {
- numbers[i] = cauchy.sample(&mut rng);
- sum += numbers[i];
- }
- let median = median(&mut numbers);
- println!("Cauchy median: {}", median);
- assert!((median - 10.0).abs() < 0.4); // not 100% certain, but probable enough
- let mean = sum / 1000.0;
- println!("Cauchy mean: {}", mean);
- // for a Cauchy distribution the mean should not converge
- assert!((mean - 10.0).abs() > 0.4); // not 100% certain, but probable enough
- }
-
- #[test]
- #[should_panic]
- fn test_cauchy_invalid_scale_zero() {
- Cauchy::new(0.0, 0.0).unwrap();
- }
-
- #[test]
- #[should_panic]
- fn test_cauchy_invalid_scale_neg() {
- Cauchy::new(0.0, -10.0).unwrap();
- }
-}