strafe_trait/statistic.rs
1use std::fmt::Debug;
2
3use strafe_type::Alpha64;
4
5use crate::{Assumption, Conclusion};
6
7#[derive(Debug)]
8pub enum RejectionStatus {
9 FailToReject(Box<dyn Conclusion>),
10 RejectInFavorOf(Box<dyn Conclusion>),
11}
12
13impl RejectionStatus {
14 pub fn unwrap(self) -> Box<dyn Conclusion> {
15 match self {
16 RejectionStatus::FailToReject(c) => c,
17 RejectionStatus::RejectInFavorOf(c) => c,
18 }
19 }
20
21 pub fn is<C: 'static + Conclusion>(self) -> bool {
22 self.unwrap().is::<C>()
23 }
24}
25
26/// The general form of a statistical evaluation
27pub trait StatisticalTest {
28 /// The data to feed into the testing function
29 type Input;
30 /// The response from the testing function
31 type Output;
32 /// Provide the required assumptions for the statistic to be calculated
33 fn assumptions() -> Vec<Box<dyn Assumption>>;
34 /// The hypotheses that is being tested
35 fn null_hypotheses() -> Vec<Box<dyn Conclusion>>;
36 /// The hypotheses that could be true if the null hypotheses is not
37 fn alternate_hypotheses() -> Vec<Box<dyn Conclusion>>;
38 /// The actual testing function
39 fn test(&mut self, input: &Self::Input) -> Self::Output;
40}
41
42/// The general form of a statistical conclusion
43pub trait Statistic {
44 /// Get the alpha level associated with this statistical run
45 fn alpha(&self) -> Alpha64;
46 /// Get the result of the statistic
47 fn statistic(&self) -> f64;
48 /// Get the p-value of the result (usually, p < alpha
49 /// means the model assumption is rejected)
50 fn probability_value(&self) -> f64;
51 /// A conclusion on whether the null hypothesis was passed or not
52 fn conclusion(&self) -> RejectionStatus;
53}
54
55/// The general form of a statistical estimation
56pub trait StatisticalEstimator {
57 /// The data to feed into the estimation function
58 type Input;
59 /// The response from the estimation function
60 type Output;
61 /// Provide the required assumptions for the statistic to be calculated
62 fn assumptions() -> Vec<Box<dyn Assumption>>;
63 /// The actual estimation function
64 fn estimate(&self, input: &Self::Input) -> Self::Output;
65}
66
67/// The general form of a statistic that generates an estimate
68pub trait StatisticalEstimate<E, CI> {
69 /// Get the alpha level associated with this statistical run
70 fn alpha(&self) -> Alpha64;
71 /// Get the estimated value of the statistic
72 fn estimate(&self) -> E;
73 /// Get the confidence interval of the estimate
74 fn confidence_interval(&self) -> CI;
75}