aboutsummaryrefslogtreecommitdiff
path: root/src/estimate.rs
blob: 8a79d27a8196d7dda68560ad06a4a72008dbd897 (plain)
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
use std::fmt;

use crate::stats::Distribution;

#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize, Debug)]
pub enum Statistic {
    Mean,
    Median,
    MedianAbsDev,
    Slope,
    StdDev,
    Typical,
}

impl fmt::Display for Statistic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Statistic::Mean => f.pad("mean"),
            Statistic::Median => f.pad("median"),
            Statistic::MedianAbsDev => f.pad("MAD"),
            Statistic::Slope => f.pad("slope"),
            Statistic::StdDev => f.pad("SD"),
            Statistic::Typical => f.pad("typical"),
        }
    }
}

#[derive(Clone, PartialEq, Deserialize, Serialize, Debug)]
pub struct ConfidenceInterval {
    pub confidence_level: f64,
    pub lower_bound: f64,
    pub upper_bound: f64,
}

#[derive(Clone, PartialEq, Deserialize, Serialize, Debug)]
pub struct Estimate {
    /// The confidence interval for this estimate
    pub confidence_interval: ConfidenceInterval,
    ///
    pub point_estimate: f64,
    /// The standard error of this estimate
    pub standard_error: f64,
}

pub fn build_estimates(
    distributions: &Distributions,
    points: &PointEstimates,
    cl: f64,
) -> Estimates {
    let to_estimate = |point_estimate, distribution: &Distribution<f64>| {
        let (lb, ub) = distribution.confidence_interval(cl);

        Estimate {
            confidence_interval: ConfidenceInterval {
                confidence_level: cl,
                lower_bound: lb,
                upper_bound: ub,
            },
            point_estimate,
            standard_error: distribution.std_dev(None),
        }
    };

    Estimates {
        mean: to_estimate(points.mean, &distributions.mean),
        median: to_estimate(points.median, &distributions.median),
        median_abs_dev: to_estimate(points.median_abs_dev, &distributions.median_abs_dev),
        slope: None,
        std_dev: to_estimate(points.std_dev, &distributions.std_dev),
    }
}

pub fn build_change_estimates(
    distributions: &ChangeDistributions,
    points: &ChangePointEstimates,
    cl: f64,
) -> ChangeEstimates {
    let to_estimate = |point_estimate, distribution: &Distribution<f64>| {
        let (lb, ub) = distribution.confidence_interval(cl);

        Estimate {
            confidence_interval: ConfidenceInterval {
                confidence_level: cl,
                lower_bound: lb,
                upper_bound: ub,
            },
            point_estimate,
            standard_error: distribution.std_dev(None),
        }
    };

    ChangeEstimates {
        mean: to_estimate(points.mean, &distributions.mean),
        median: to_estimate(points.median, &distributions.median),
    }
}

pub struct PointEstimates {
    pub mean: f64,
    pub median: f64,
    pub median_abs_dev: f64,
    pub std_dev: f64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Estimates {
    pub mean: Estimate,
    pub median: Estimate,
    pub median_abs_dev: Estimate,
    pub slope: Option<Estimate>,
    pub std_dev: Estimate,
}
impl Estimates {
    pub fn typical(&self) -> &Estimate {
        self.slope.as_ref().unwrap_or(&self.mean)
    }
    pub fn get(&self, stat: Statistic) -> Option<&Estimate> {
        match stat {
            Statistic::Mean => Some(&self.mean),
            Statistic::Median => Some(&self.median),
            Statistic::MedianAbsDev => Some(&self.median_abs_dev),
            Statistic::Slope => self.slope.as_ref(),
            Statistic::StdDev => Some(&self.std_dev),
            Statistic::Typical => Some(self.typical()),
        }
    }
}

pub struct Distributions {
    pub mean: Distribution<f64>,
    pub median: Distribution<f64>,
    pub median_abs_dev: Distribution<f64>,
    pub slope: Option<Distribution<f64>>,
    pub std_dev: Distribution<f64>,
}
impl Distributions {
    pub fn typical(&self) -> &Distribution<f64> {
        self.slope.as_ref().unwrap_or(&self.mean)
    }
    pub fn get(&self, stat: Statistic) -> Option<&Distribution<f64>> {
        match stat {
            Statistic::Mean => Some(&self.mean),
            Statistic::Median => Some(&self.median),
            Statistic::MedianAbsDev => Some(&self.median_abs_dev),
            Statistic::Slope => self.slope.as_ref(),
            Statistic::StdDev => Some(&self.std_dev),
            Statistic::Typical => Some(self.typical()),
        }
    }
}

pub struct ChangePointEstimates {
    pub mean: f64,
    pub median: f64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChangeEstimates {
    pub mean: Estimate,
    pub median: Estimate,
}
impl ChangeEstimates {
    pub fn get(&self, stat: Statistic) -> &Estimate {
        match stat {
            Statistic::Mean => &self.mean,
            Statistic::Median => &self.median,
            _ => panic!("Unexpected statistic"),
        }
    }
}

pub struct ChangeDistributions {
    pub mean: Distribution<f64>,
    pub median: Distribution<f64>,
}
impl ChangeDistributions {
    pub fn get(&self, stat: Statistic) -> &Distribution<f64> {
        match stat {
            Statistic::Mean => &self.mean,
            Statistic::Median => &self.median,
            _ => panic!("Unexpected statistic"),
        }
    }
}