use super::estimate::Bounds;
use crate::{CategoryValues, Error, NumericValues};
const DEFAULT_LEVEL: f64 = 0.95;
const DEFAULT_RESAMPLES: usize = 1_000;
const MAX_RESAMPLES: usize = 1_000_000;
#[derive(Debug, Clone)]
pub struct BarInput {
pub(crate) source: BarSource,
}
#[derive(Debug, Clone)]
pub(crate) enum BarSource {
Samples {
values: NumericValues,
error_bar: ErrorBar,
},
Estimates {
values: NumericValues,
bounds: Option<Bounds>,
},
}
impl BarInput {
#[must_use]
pub fn samples(values: NumericValues) -> BarSamples {
BarSamples {
values,
error_bar: ErrorBar::confidence_interval().into(),
}
}
#[must_use]
pub fn estimates(values: NumericValues) -> Self {
Self {
source: BarSource::Estimates {
values,
bounds: None,
},
}
}
#[must_use]
pub fn estimates_with_error_bars(values: NumericValues, bounds: Bounds) -> Self {
Self {
source: BarSource::Estimates {
values,
bounds: Some(bounds),
},
}
}
pub(crate) const fn values(&self) -> &NumericValues {
match &self.source {
BarSource::Samples { values, .. } | BarSource::Estimates { values, .. } => values,
}
}
pub(crate) const fn bounds(&self) -> Option<&Bounds> {
match &self.source {
BarSource::Samples { .. } | BarSource::Estimates { bounds: None, .. } => None,
BarSource::Estimates {
bounds: Some(bounds),
..
} => Some(bounds),
}
}
pub(crate) const fn error_bar(&self) -> Option<&ErrorBar> {
match &self.source {
BarSource::Samples { error_bar, .. } => Some(error_bar),
BarSource::Estimates { .. } => None,
}
}
pub(crate) const fn is_samples(&self) -> bool {
matches!(&self.source, BarSource::Samples { .. })
}
}
#[derive(Debug, Clone)]
pub struct BarSamples {
values: NumericValues,
error_bar: ErrorBar,
}
impl BarSamples {
#[must_use]
pub fn error_bar(mut self, error_bar: impl Into<ErrorBar>) -> Self {
self.error_bar = error_bar.into();
self
}
}
impl From<BarSamples> for BarInput {
fn from(samples: BarSamples) -> Self {
Self {
source: BarSource::Samples {
values: samples.values,
error_bar: samples.error_bar,
},
}
}
}
#[derive(Debug, Clone)]
pub struct ErrorBar(pub(crate) ErrorBarKind);
#[derive(Debug, Clone)]
pub(crate) enum ErrorBarKind {
Hidden,
StandardDeviation(StandardDeviation),
PercentileInterval(PercentileInterval),
StandardError(StandardError),
ConfidenceInterval(ConfidenceInterval),
}
impl ErrorBar {
#[must_use]
pub const fn hidden() -> Self {
Self(ErrorBarKind::Hidden)
}
#[must_use]
pub const fn standard_deviation() -> StandardDeviation {
StandardDeviation { scale: 1.0 }
}
#[must_use]
pub const fn percentile_interval() -> PercentileInterval {
PercentileInterval {
level: DEFAULT_LEVEL,
}
}
#[must_use]
pub const fn standard_error() -> StandardError {
StandardError { scale: 1.0 }
}
#[must_use]
pub const fn confidence_interval() -> ConfidenceInterval {
ConfidenceInterval {
level: DEFAULT_LEVEL,
algorithm: ConfidenceIntervalAlgorithm::Bootstrap(Bootstrap::new()),
}
}
pub(crate) const fn kind(&self) -> &ErrorBarKind {
&self.0
}
}
impl Default for ErrorBar {
fn default() -> Self {
Self::confidence_interval().into()
}
}
#[derive(Debug, Clone, Copy)]
pub struct StandardDeviation {
pub(crate) scale: f64,
}
impl StandardDeviation {
#[must_use]
pub const fn get_scale(&self) -> f64 {
self.scale
}
pub fn scale(mut self, scale: f64) -> Result<Self, Error> {
validate_positive_finite(scale, "standard-deviation scale")?;
self.scale = scale;
Ok(self)
}
}
impl From<StandardDeviation> for ErrorBar {
fn from(spec: StandardDeviation) -> Self {
Self(ErrorBarKind::StandardDeviation(spec))
}
}
#[derive(Debug, Clone, Copy)]
pub struct PercentileInterval {
pub(crate) level: f64,
}
impl PercentileInterval {
#[must_use]
pub const fn get_level(&self) -> f64 {
self.level
}
pub fn level(mut self, level: f64) -> Result<Self, Error> {
validate_level(level)?;
self.level = level;
Ok(self)
}
}
impl From<PercentileInterval> for ErrorBar {
fn from(spec: PercentileInterval) -> Self {
Self(ErrorBarKind::PercentileInterval(spec))
}
}
#[derive(Debug, Clone, Copy)]
pub struct StandardError {
pub(crate) scale: f64,
}
impl StandardError {
#[must_use]
pub const fn get_scale(&self) -> f64 {
self.scale
}
pub fn scale(mut self, scale: f64) -> Result<Self, Error> {
validate_positive_finite(scale, "standard-error scale")?;
self.scale = scale;
Ok(self)
}
}
impl From<StandardError> for ErrorBar {
fn from(spec: StandardError) -> Self {
Self(ErrorBarKind::StandardError(spec))
}
}
#[derive(Debug, Clone)]
pub struct ConfidenceInterval {
pub(crate) level: f64,
pub(crate) algorithm: ConfidenceIntervalAlgorithm,
}
impl ConfidenceInterval {
#[must_use]
pub const fn get_level(&self) -> f64 {
self.level
}
#[must_use]
pub const fn get_bootstrap(&self) -> Option<&Bootstrap> {
match &self.algorithm {
ConfidenceIntervalAlgorithm::Bootstrap(bootstrap) => Some(bootstrap),
ConfidenceIntervalAlgorithm::StudentT => None,
}
}
pub fn level(mut self, level: f64) -> Result<Self, Error> {
validate_level(level)?;
self.level = level;
Ok(self)
}
#[must_use]
pub fn bootstrap(mut self, bootstrap: Bootstrap) -> Self {
self.algorithm = ConfidenceIntervalAlgorithm::Bootstrap(bootstrap);
self
}
#[must_use]
pub fn student_t(mut self) -> Self {
self.algorithm = ConfidenceIntervalAlgorithm::StudentT;
self
}
}
impl Default for ConfidenceInterval {
fn default() -> Self {
ErrorBar::confidence_interval()
}
}
impl From<ConfidenceInterval> for ErrorBar {
fn from(spec: ConfidenceInterval) -> Self {
Self(ErrorBarKind::ConfidenceInterval(spec))
}
}
#[derive(Debug, Clone)]
pub(crate) enum ConfidenceIntervalAlgorithm {
Bootstrap(Bootstrap),
StudentT,
}
#[derive(Debug, Clone)]
pub struct Bootstrap {
pub(crate) resamples: usize,
pub(crate) seed: u64,
pub(crate) units: Option<CategoryValues>,
}
impl Bootstrap {
#[must_use]
pub const fn new() -> Self {
Self {
resamples: DEFAULT_RESAMPLES,
seed: 0,
units: None,
}
}
#[must_use]
pub const fn get_resamples(&self) -> usize {
self.resamples
}
#[must_use]
pub const fn get_seed(&self) -> u64 {
self.seed
}
pub fn resamples(mut self, resamples: usize) -> Result<Self, Error> {
if !(1..=MAX_RESAMPLES).contains(&resamples) {
return Err(Error::Data(
format!(
"bootstrap resamples must be between 1 and {MAX_RESAMPLES}, got {resamples}"
)
.into(),
));
}
self.resamples = resamples;
Ok(self)
}
#[must_use]
pub const fn seed(mut self, seed: u64) -> Self {
self.seed = seed;
self
}
#[must_use]
pub fn units(mut self, units: CategoryValues) -> Self {
self.units = Some(units);
self
}
}
impl Default for Bootstrap {
fn default() -> Self {
Self::new()
}
}
fn validate_level(level: f64) -> Result<(), Error> {
if !level.is_finite() || level <= 0.0 || level >= 1.0 {
return Err(Error::Data(
format!("error-bar level must be finite and strictly between 0 and 1, got {level}")
.into(),
));
}
Ok(())
}
fn validate_positive_finite(value: f64, role: &str) -> Result<(), Error> {
if !value.is_finite() || value <= 0.0 {
return Err(Error::Data(
format!("{role} must be finite and greater than zero, got {value}").into(),
));
}
Ok(())
}