/rust/registry/src/index.crates.io-1949cf8c6b5b557f/criterion-0.5.1/src/estimate.rs
Line | Count | Source |
1 | | use std::fmt; |
2 | | |
3 | | use crate::stats::Distribution; |
4 | | |
5 | | #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize, Debug)] |
6 | | pub enum Statistic { |
7 | | Mean, |
8 | | Median, |
9 | | MedianAbsDev, |
10 | | Slope, |
11 | | StdDev, |
12 | | Typical, |
13 | | } |
14 | | |
15 | | impl fmt::Display for Statistic { |
16 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
17 | 0 | match *self { |
18 | 0 | Statistic::Mean => f.pad("mean"), |
19 | 0 | Statistic::Median => f.pad("median"), |
20 | 0 | Statistic::MedianAbsDev => f.pad("MAD"), |
21 | 0 | Statistic::Slope => f.pad("slope"), |
22 | 0 | Statistic::StdDev => f.pad("SD"), |
23 | 0 | Statistic::Typical => f.pad("typical"), |
24 | | } |
25 | 0 | } |
26 | | } |
27 | | |
28 | | #[derive(Clone, PartialEq, Deserialize, Serialize, Debug)] |
29 | | pub struct ConfidenceInterval { |
30 | | pub confidence_level: f64, |
31 | | pub lower_bound: f64, |
32 | | pub upper_bound: f64, |
33 | | } |
34 | | |
35 | | #[derive(Clone, PartialEq, Deserialize, Serialize, Debug)] |
36 | | pub struct Estimate { |
37 | | /// The confidence interval for this estimate |
38 | | pub confidence_interval: ConfidenceInterval, |
39 | | /// |
40 | | pub point_estimate: f64, |
41 | | /// The standard error of this estimate |
42 | | pub standard_error: f64, |
43 | | } |
44 | | |
45 | 0 | pub fn build_estimates( |
46 | 0 | distributions: &Distributions, |
47 | 0 | points: &PointEstimates, |
48 | 0 | cl: f64, |
49 | 0 | ) -> Estimates { |
50 | 0 | let to_estimate = |point_estimate, distribution: &Distribution<f64>| { |
51 | 0 | let (lb, ub) = distribution.confidence_interval(cl); |
52 | | |
53 | 0 | Estimate { |
54 | 0 | confidence_interval: ConfidenceInterval { |
55 | 0 | confidence_level: cl, |
56 | 0 | lower_bound: lb, |
57 | 0 | upper_bound: ub, |
58 | 0 | }, |
59 | 0 | point_estimate, |
60 | 0 | standard_error: distribution.std_dev(None), |
61 | 0 | } |
62 | 0 | }; |
63 | | |
64 | 0 | Estimates { |
65 | 0 | mean: to_estimate(points.mean, &distributions.mean), |
66 | 0 | median: to_estimate(points.median, &distributions.median), |
67 | 0 | median_abs_dev: to_estimate(points.median_abs_dev, &distributions.median_abs_dev), |
68 | 0 | slope: None, |
69 | 0 | std_dev: to_estimate(points.std_dev, &distributions.std_dev), |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | pub fn build_change_estimates( |
74 | 0 | distributions: &ChangeDistributions, |
75 | 0 | points: &ChangePointEstimates, |
76 | 0 | cl: f64, |
77 | 0 | ) -> ChangeEstimates { |
78 | 0 | let to_estimate = |point_estimate, distribution: &Distribution<f64>| { |
79 | 0 | let (lb, ub) = distribution.confidence_interval(cl); |
80 | | |
81 | 0 | Estimate { |
82 | 0 | confidence_interval: ConfidenceInterval { |
83 | 0 | confidence_level: cl, |
84 | 0 | lower_bound: lb, |
85 | 0 | upper_bound: ub, |
86 | 0 | }, |
87 | 0 | point_estimate, |
88 | 0 | standard_error: distribution.std_dev(None), |
89 | 0 | } |
90 | 0 | }; |
91 | | |
92 | 0 | ChangeEstimates { |
93 | 0 | mean: to_estimate(points.mean, &distributions.mean), |
94 | 0 | median: to_estimate(points.median, &distributions.median), |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | pub struct PointEstimates { |
99 | | pub mean: f64, |
100 | | pub median: f64, |
101 | | pub median_abs_dev: f64, |
102 | | pub std_dev: f64, |
103 | | } |
104 | | |
105 | | #[derive(Debug, Serialize, Deserialize, Clone)] |
106 | | pub struct Estimates { |
107 | | pub mean: Estimate, |
108 | | pub median: Estimate, |
109 | | pub median_abs_dev: Estimate, |
110 | | pub slope: Option<Estimate>, |
111 | | pub std_dev: Estimate, |
112 | | } |
113 | | impl Estimates { |
114 | 0 | pub fn typical(&self) -> &Estimate { |
115 | 0 | self.slope.as_ref().unwrap_or(&self.mean) |
116 | 0 | } |
117 | 0 | pub fn get(&self, stat: Statistic) -> Option<&Estimate> { |
118 | 0 | match stat { |
119 | 0 | Statistic::Mean => Some(&self.mean), |
120 | 0 | Statistic::Median => Some(&self.median), |
121 | 0 | Statistic::MedianAbsDev => Some(&self.median_abs_dev), |
122 | 0 | Statistic::Slope => self.slope.as_ref(), |
123 | 0 | Statistic::StdDev => Some(&self.std_dev), |
124 | 0 | Statistic::Typical => Some(self.typical()), |
125 | | } |
126 | 0 | } |
127 | | } |
128 | | |
129 | | pub struct Distributions { |
130 | | pub mean: Distribution<f64>, |
131 | | pub median: Distribution<f64>, |
132 | | pub median_abs_dev: Distribution<f64>, |
133 | | pub slope: Option<Distribution<f64>>, |
134 | | pub std_dev: Distribution<f64>, |
135 | | } |
136 | | impl Distributions { |
137 | 0 | pub fn typical(&self) -> &Distribution<f64> { |
138 | 0 | self.slope.as_ref().unwrap_or(&self.mean) |
139 | 0 | } |
140 | 0 | pub fn get(&self, stat: Statistic) -> Option<&Distribution<f64>> { |
141 | 0 | match stat { |
142 | 0 | Statistic::Mean => Some(&self.mean), |
143 | 0 | Statistic::Median => Some(&self.median), |
144 | 0 | Statistic::MedianAbsDev => Some(&self.median_abs_dev), |
145 | 0 | Statistic::Slope => self.slope.as_ref(), |
146 | 0 | Statistic::StdDev => Some(&self.std_dev), |
147 | 0 | Statistic::Typical => Some(self.typical()), |
148 | | } |
149 | 0 | } |
150 | | } |
151 | | |
152 | | pub struct ChangePointEstimates { |
153 | | pub mean: f64, |
154 | | pub median: f64, |
155 | | } |
156 | | |
157 | | #[derive(Debug, Serialize, Deserialize, Clone)] |
158 | | pub struct ChangeEstimates { |
159 | | pub mean: Estimate, |
160 | | pub median: Estimate, |
161 | | } |
162 | | impl ChangeEstimates { |
163 | 0 | pub fn get(&self, stat: Statistic) -> &Estimate { |
164 | 0 | match stat { |
165 | 0 | Statistic::Mean => &self.mean, |
166 | 0 | Statistic::Median => &self.median, |
167 | 0 | _ => panic!("Unexpected statistic"), |
168 | | } |
169 | 0 | } |
170 | | } |
171 | | |
172 | | pub struct ChangeDistributions { |
173 | | pub mean: Distribution<f64>, |
174 | | pub median: Distribution<f64>, |
175 | | } |
176 | | impl ChangeDistributions { |
177 | 0 | pub fn get(&self, stat: Statistic) -> &Distribution<f64> { |
178 | 0 | match stat { |
179 | 0 | Statistic::Mean => &self.mean, |
180 | 0 | Statistic::Median => &self.median, |
181 | 0 | _ => panic!("Unexpected statistic"), |
182 | | } |
183 | 0 | } |
184 | | } |