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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
use strafe_type::{FloatConstraint, LogProbability64, Positive64, Probability64, Real64};
use crate::{
distribution::tukey::{p::ptukey, q::qtukey},
traits::{Distribution, RNG},
};
mod p;
mod q;
/// # The Studentized Range Distribution
///
/// ## Description
///
/// Functions of the distribution of the studentized range, $ R/s $, where $ R $ is the range of a
/// standard normal sample and $ df*s^2 $ is independently distributed as chi-squared with df
/// degrees of freedom, see pchisq.
///
/// ## Arguments
///
/// * nmeans: sample size for range (same for each group).
/// * df: degrees of freedom for s (see below).
/// * nranges: number of groups whose maximum range is considered.
///
/// ## Details
///
/// If ng = nranges is greater than one, R is the maximum of ng groups of nmeans observations each.
///
/// ## Note
///
/// A Legendre 16-point formula is used for the integral of ptukey. The computations are relatively
/// expensive, especially for qtukey which uses a simple secant method for finding the inverse of
/// ptukey. qtukey will be accurate to the 4th decimal place.
///
/// ## Source
///
/// qtukey is in part adapted from Odeh and Evans (1974).
///
/// ## References
///
/// Copenhaver, Margaret Diponzio and Holland, Burt S. (1988). Computation of the distribution of
/// the maximum studentized range statistic with application to multiple significance testing of
/// simple effects. Journal of Statistical Computation and Simulation, 30, 1–15.
/// doi: 10.1080/00949658808811082.
///
/// Odeh, R. E. and Evans, J. O. (1974). Algorithm AS 70: Percentage Points of the Normal
/// Distribution. Applied Statistics, 23, 96–97. doi: 10.2307/2347061.
///
/// ## See Also
///
/// Distributions for standard distributions, including pnorm and qnorm for the corresponding
/// functions for the normal distribution.
///
/// ## Examples
///
/// ```rust
/// # use r2rs_base::traits::StatisticalSlice;
/// # use r2rs_nmath::{distribution::TukeyBuilder, traits::Distribution};
/// # use strafe_plot::prelude::{IntoDrawingArea, Line, Plot, PlotOptions, SVGBackend, BLACK};
/// # use strafe_type::FloatConstraint;
/// let tukey = TukeyBuilder::new().with_means(6).with_df(5).build();
/// let x = <[f64]>::sequence(-1.0, 8.0, 1000);
/// let y = x
/// .iter()
/// .map(|x| tukey.probability(x, true).unwrap())
/// .collect::<Vec<_>>();
///
/// let root = SVGBackend::new("prob.svg", (1024, 768)).into_drawing_area();
/// Plot::new()
/// .with_options(PlotOptions {
/// x_axis_label: "x".to_string(),
/// y_axis_label: "probability".to_string(),
/// ..Default::default()
/// })
/// .with_plottable(Line {
/// x,
/// y,
/// color: BLACK,
/// ..Default::default()
/// })
/// .plot(&root)
/// .unwrap();
/// # use std::fs::rename;
/// # drop(root);
/// # rename(
/// # format!("prob.svg"),
/// # format!("src/distribution/tukey/doctest_out/prob.svg"),
/// # )
/// # .unwrap();
/// ```
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = embed_doc_image::embed_image!("prob", "src/distribution/tukey/doctest_out/prob.svg")))]
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = "![Probability][prob]"))]
///
/// The precision may be not much more than about 8 digits
/// ```rust
/// # use r2rs_nmath::{distribution::TukeyBuilder, traits::Distribution};
/// # use strafe_type::FloatConstraint;
/// let tukey = TukeyBuilder::new().with_means(2).with_df(5).build();
/// let x = (0..=10).collect::<Vec<_>>();
/// let p = x
/// .iter()
/// .map(|x| tukey.probability(x, true).unwrap())
/// .collect::<Vec<_>>();
/// println!("{p:?}");
///
/// let df = (2..=11).collect::<Vec<_>>();
/// let q = df
/// .iter()
/// .map(|df| {
/// TukeyBuilder::new()
/// .with_means(2)
/// .with_df(df)
/// .build()
/// .quantile(0.95, true)
/// .unwrap()
/// })
/// .collect::<Vec<_>>();
/// println!("{q:?}");
///
/// let err = q
/// .iter()
/// .zip(df.iter())
/// .map(|(q, df)| {
/// 0.95 - TukeyBuilder::new()
/// .with_means(2)
/// .with_df(df)
/// .build()
/// .probability(q, true)
/// .unwrap()
/// })
/// .collect::<Vec<_>>();
/// println!("{err:?}");
/// # use std::{fs::File, io::Write};
/// # let mut f = File::create("src/distribution/tukey/doctest_out/err.md").unwrap();
/// # writeln!(f, "```output").unwrap();
/// # writeln!(f, "{p:?}").unwrap();
/// # writeln!(f, "{q:?}").unwrap();
/// # writeln!(f, "{err:?}").unwrap();
/// # writeln!(f, "```").unwrap();
/// ```
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = include_str!("doctest_out/err.md")))]
pub struct Tukey {
ranges: Positive64,
means: Positive64,
df: Positive64,
}
impl Distribution for Tukey {
fn density<R: Into<Real64>>(&self, _x: R) -> Real64 {
unimplemented!()
}
fn log_density<R: Into<Real64>>(&self, _x: R) -> Real64 {
unimplemented!()
}
fn probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> Probability64 {
let ret = ptukey(q, self.ranges, self.means, self.df, lower_tail, false);
ret.unwrap().into()
}
fn log_probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> LogProbability64 {
let ret = ptukey(q, self.ranges, self.means, self.df, lower_tail, true);
ret.unwrap().into()
}
fn quantile<P: Into<Probability64>>(&self, p: P, lower_tail: bool) -> Real64 {
qtukey(
p.into().unwrap(),
self.ranges,
self.means,
self.df,
lower_tail,
false,
)
}
fn log_quantile<LP: Into<LogProbability64>>(&self, p: LP, lower_tail: bool) -> Real64 {
qtukey(
p.into().unwrap(),
self.ranges,
self.means,
self.df,
lower_tail,
true,
)
}
fn random_sample<R: RNG>(&self, _rng: &mut R) -> Real64 {
unimplemented!()
}
}
pub struct TukeyBuilder {
ranges: Option<Positive64>,
means: Option<Positive64>,
df: Option<Positive64>,
}
impl TukeyBuilder {
pub fn new() -> Self {
Self {
ranges: None,
means: None,
df: None,
}
}
pub fn with_ranges<P: Into<Positive64>>(&mut self, ranges: P) -> &mut Self {
self.ranges = Some(ranges.into());
self
}
pub fn with_means<P: Into<Positive64>>(&mut self, means: P) -> &mut Self {
self.means = Some(means.into());
self
}
pub fn with_df<P: Into<Positive64>>(&mut self, df: P) -> &mut Self {
self.df = Some(df.into());
self
}
pub fn build(&self) -> Tukey {
let ranges = self.ranges.unwrap_or(1.0.into());
let means = self.means.unwrap_or(2.0.into());
let df = self.df.unwrap_or(2.0.into());
Tukey { ranges, means, df }
}
}
#[cfg(test)]
mod tests;
#[cfg(all(test, feature = "enable_proptest"))]
mod proptests;
#[cfg(all(test, feature = "enable_covtest"))]
mod covtests;