Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
lib.rs - source
[go: Go Back, main page]

criterion_stats/
lib.rs

1//! [Criterion]'s statistics library.
2//!
3//! [Criterion]: https://github.com/bheisler/criterion.rs
4//!
5//! **WARNING** This library is criterion's implementation detail and there no plans to stabilize
6//! it. In other words, the API may break at any time without notice.
7
8#![deny(missing_docs)]
9#![deny(warnings)]
10#![cfg_attr(
11    feature = "cargo-clippy",
12    allow(
13        clippy::used_underscore_binding,
14        clippy::just_underscores_and_digits,
15        clippy::transmute_ptr_to_ptr
16    )
17)]
18
19extern crate cast;
20extern crate num_cpus;
21extern crate num_traits;
22extern crate rand;
23extern crate thread_scoped;
24
25#[cfg(test)]
26#[macro_use]
27extern crate approx;
28#[cfg(test)]
29#[macro_use]
30extern crate quickcheck;
31
32#[cfg(test)]
33mod test;
34
35pub mod bivariate;
36pub mod tuple;
37pub mod univariate;
38
39mod float;
40
41use std::mem;
42use std::ops::Deref;
43
44use float::Float;
45use univariate::Sample;
46
47/// The bootstrap distribution of some parameter
48pub struct Distribution<A>(Box<[A]>);
49
50impl<A> Distribution<A>
51where
52    A: Float,
53{
54    /// Create a distribution from the given values
55    pub fn from(values: Box<[A]>) -> Distribution<A> {
56        Distribution(values)
57    }
58
59    /// Computes the confidence interval of the population parameter using percentiles
60    ///
61    /// # Panics
62    ///
63    /// Panics if the `confidence_level` is not in the `(0, 1)` range.
64    pub fn confidence_interval(&self, confidence_level: A) -> (A, A)
65    where
66        usize: cast::From<A, Output = Result<usize, cast::Error>>,
67    {
68        let _0 = A::cast(0);
69        let _1 = A::cast(1);
70        let _50 = A::cast(50);
71
72        assert!(confidence_level > _0 && confidence_level < _1);
73
74        let percentiles = self.percentiles();
75
76        // FIXME(privacy) this should use the `at_unchecked()` method
77        (
78            percentiles.at(_50 * (_1 - confidence_level)),
79            percentiles.at(_50 * (_1 + confidence_level)),
80        )
81    }
82
83    /// Computes the "likelihood" of seeing the value `t` or "more extreme" values in the
84    /// distribution.
85    pub fn p_value(&self, t: A, tails: &Tails) -> A {
86        use std::cmp;
87
88        let n = self.0.len();
89        let hits = self.0.iter().filter(|&&x| x < t).count();
90
91        let tails = A::cast(match *tails {
92            Tails::One => 1,
93            Tails::Two => 2,
94        });
95
96        A::cast(cmp::min(hits, n - hits)) / A::cast(n) * tails
97    }
98}
99
100impl<A> Deref for Distribution<A> {
101    type Target = Sample<A>;
102
103    fn deref(&self) -> &Sample<A> {
104        let slice: &[_] = &self.0;
105
106        unsafe { mem::transmute(slice) }
107    }
108}
109
110/// Number of tails for significance testing
111pub enum Tails {
112    /// One tailed test
113    One,
114    /// Two tailed test
115    Two,
116}
117
118fn dot<A>(xs: &[A], ys: &[A]) -> A
119where
120    A: Float,
121{
122    xs.iter()
123        .zip(ys)
124        .fold(A::cast(0), |acc, (&x, &y)| acc + x * y)
125}
126
127fn sum<A>(xs: &[A]) -> A
128where
129    A: Float,
130{
131    use std::ops::Add;
132
133    xs.iter().cloned().fold(A::cast(0), Add::add)
134}