Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rustfmt happy #219

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use_small_heuristics = "Default"
# TODO: single line functions only where short, please?
# https://github.com/rust-lang/rustfmt/issues/3358
fn_single_line = false
fn_params_layout = "Compressed"
overflow_delimited_expr = true
where_single_line = true

# enum_discrim_align_threshold = 20
# struct_field_align_threshold = 20
Expand All @@ -23,7 +20,6 @@ where_single_line = true
edition = "2021"

# Misc:
inline_attribute_width = 80
blank_lines_upper_bound = 2
reorder_impl_items = true
# report_todo = "Unnumbered"
Expand Down
5 changes: 4 additions & 1 deletion src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl DiscreteCDF<u64, f64> for Bernoulli {
self.b.cdf(x)
}

/// Calculates the survival function for the
/// Calculates the survival function for the
/// bernoulli distribution at `x`.
///
/// # Formula
Expand Down Expand Up @@ -158,6 +158,7 @@ impl Distribution<f64> for Bernoulli {
fn mean(&self) -> Option<f64> {
self.b.mean()
}

/// Returns the variance of the bernoulli
/// distribution
///
Expand All @@ -169,6 +170,7 @@ impl Distribution<f64> for Bernoulli {
fn variance(&self) -> Option<f64> {
self.b.variance()
}

/// Returns the entropy of the bernoulli
/// distribution
///
Expand All @@ -181,6 +183,7 @@ impl Distribution<f64> for Bernoulli {
fn entropy(&self) -> Option<f64> {
self.b.entropy()
}

/// Returns the skewness of the bernoulli
/// distribution
///
Expand Down
5 changes: 4 additions & 1 deletion src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl ContinuousCDF<f64, f64> for Beta {
} else if ulps_eq!(self.shape_a, 1.0) && ulps_eq!(self.shape_b, 1.0) {
1. - x
} else {
beta::beta_reg(self.shape_b, self.shape_a, 1.0 - x)
beta::beta_reg(self.shape_b, self.shape_a, 1.0 - x)
}
}
}
Expand Down Expand Up @@ -208,6 +208,7 @@ impl Distribution<f64> for Beta {
};
Some(mean)
}

/// Returns the variance of the beta distribution
///
/// # Remarks
Expand All @@ -230,6 +231,7 @@ impl Distribution<f64> for Beta {
};
Some(var)
}

/// Returns the entropy of the beta distribution
///
/// # Formula
Expand All @@ -251,6 +253,7 @@ impl Distribution<f64> for Beta {
};
Some(entr)
}

/// Returns the skewness of the Beta distribution
///
/// # Formula
Expand Down
3 changes: 3 additions & 0 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Distribution<f64> for Binomial {
fn mean(&self) -> Option<f64> {
Some(self.p * self.n as f64)
}

/// Returns the variance of the binomial distribution
///
/// # Formula
Expand All @@ -191,6 +192,7 @@ impl Distribution<f64> for Binomial {
fn variance(&self) -> Option<f64> {
Some(self.p * (1.0 - self.p) * self.n as f64)
}

/// Returns the entropy of the binomial distribution
///
/// # Formula
Expand All @@ -209,6 +211,7 @@ impl Distribution<f64> for Binomial {
};
Some(entr)
}

/// Returns the skewness of the binomial distribution
///
/// # Formula
Expand Down
8 changes: 5 additions & 3 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::f64;
/// # Examples
///
/// ```
///
///
/// use statrs::distribution::{Categorical, Discrete};
/// use statrs::statistics::Distribution;
/// use statrs::prec;
Expand All @@ -25,7 +25,7 @@ use std::f64;
pub struct Categorical {
norm_pmf: Vec<f64>,
cdf: Vec<f64>,
sf: Vec<f64>
sf: Vec<f64>,
}

impl Categorical {
Expand Down Expand Up @@ -194,6 +194,7 @@ impl Distribution<f64> for Categorical {
.fold(0.0, |acc, (idx, &val)| acc + idx as f64 * val),
)
}

/// Returns the variance of the categorical distribution
///
/// # Formula
Expand All @@ -217,6 +218,7 @@ impl Distribution<f64> for Categorical {
});
Some(var)
}

/// Returns the entropy of the categorical distribution
///
/// # Formula
Expand Down Expand Up @@ -294,7 +296,7 @@ pub fn prob_mass_to_cdf(prob_mass: &[f64]) -> Vec<f64> {
cdf
}

/// Computes the sf from the given cumulative densities.
/// Computes the sf from the given cumulative densities.
/// Performs no parameter or bounds checking.
pub fn cdf_to_sf(cdf: &[f64]) -> Vec<f64> {
let max = *cdf.last().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl Distribution<f64> for Chi {
Some(mean)
}
}

/// Returns the variance of the chi distribution
///
/// # Remarks
Expand All @@ -203,6 +204,7 @@ impl Distribution<f64> for Chi {
let mean = self.mean()?;
Some(self.freedom - mean * mean)
}

/// Returns the entropy of the chi distribution
///
/// # Remarks
Expand All @@ -228,6 +230,7 @@ impl Distribution<f64> for Chi {
/ 2.0;
Some(entr)
}

/// Returns the skewness of the chi distribution
///
/// # Remarks
Expand Down
3 changes: 3 additions & 0 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl Distribution<f64> for ChiSquared {
fn mean(&self) -> Option<f64> {
self.g.mean()
}

/// Returns the variance of the chi-squared distribution
///
/// # Formula
Expand All @@ -189,6 +190,7 @@ impl Distribution<f64> for ChiSquared {
fn variance(&self) -> Option<f64> {
self.g.variance()
}

/// Returns the entropy of the chi-squared distribution
///
/// # Formula
Expand All @@ -202,6 +204,7 @@ impl Distribution<f64> for ChiSquared {
fn entropy(&self) -> Option<f64> {
self.g.entropy()
}

/// Returns the skewness of the chi-squared distribution
///
/// # Formula
Expand Down
5 changes: 3 additions & 2 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl ContinuousCDF<f64, f64> for Dirac {
/// dirac distribution at `x`
///
/// Where the value is 1 if x > `v`, 0 otherwise.
///
fn cdf(&self, x: f64) -> f64 {
if x < self.0 {
0.0
Expand All @@ -69,7 +68,6 @@ impl ContinuousCDF<f64, f64> for Dirac {
/// dirac distribution at `x`
///
/// Where the value is 0 if x > `v`, 1 otherwise.
///
fn sf(&self, x: f64) -> f64 {
if x < self.0 {
1.0
Expand Down Expand Up @@ -117,6 +115,7 @@ impl Distribution<f64> for Dirac {
fn mean(&self) -> Option<f64> {
Some(self.0)
}

/// Returns the variance of the dirac distribution
///
/// # Formula
Expand All @@ -129,6 +128,7 @@ impl Distribution<f64> for Dirac {
fn variance(&self) -> Option<f64> {
Some(0.0)
}

/// Returns the entropy of the dirac distribution
///
/// # Formula
Expand All @@ -141,6 +141,7 @@ impl Distribution<f64> for Dirac {
fn entropy(&self) -> Option<f64> {
Some(0.0)
}

/// Returns the skewness of the dirac distribution
///
/// # Formula
Expand Down
1 change: 1 addition & 0 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Dirichlet {
fn alpha_sum(&self) -> f64 {
self.alpha.fold(0.0, |acc, x| acc + x)
}

/// Returns the entropy of the dirichlet distribution
///
/// # Formula
Expand Down
5 changes: 4 additions & 1 deletion src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl DiscreteCDF<i64, f64> for DiscreteUniform {
}

fn sf(&self, x: i64) -> f64 {
//1. - self.cdf(x)
// 1. - self.cdf(x)
if x < self.min {
1.0
} else if x >= self.max {
Expand Down Expand Up @@ -137,6 +137,7 @@ impl Distribution<f64> for DiscreteUniform {
fn mean(&self) -> Option<f64> {
Some((self.min + self.max) as f64 / 2.0)
}

/// Returns the variance of the discrete uniform distribution
///
/// # Formula
Expand All @@ -148,6 +149,7 @@ impl Distribution<f64> for DiscreteUniform {
let diff = (self.max - self.min) as f64;
Some(((diff + 1.0) * (diff + 1.0) - 1.0) / 12.0)
}

/// Returns the entropy of the discrete uniform distribution
///
/// # Formula
Expand All @@ -159,6 +161,7 @@ impl Distribution<f64> for DiscreteUniform {
let diff = (self.max - self.min) as f64;
Some((diff + 1.0).ln())
}

/// Returns the skewness of the discrete uniform distribution
///
/// # Formula
Expand Down
12 changes: 8 additions & 4 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl Empirical {
///
/// let mut result = Empirical::new();
/// assert!(result.is_ok());
///
/// ```
pub fn new() -> Result<Empirical> {
Ok(Empirical {
Expand All @@ -65,13 +64,15 @@ impl Empirical {
data: BTreeMap::new(),
})
}

pub fn from_vec(src: Vec<f64>) -> Empirical {
let mut empirical = Empirical::new().unwrap();
for elt in src.into_iter() {
empirical.add(elt);
}
empirical
}

pub fn add(&mut self, data_point: f64) {
if !data_point.is_nan() {
self.sum += 1.;
Expand All @@ -89,6 +90,7 @@ impl Empirical {
*self.data.entry(NonNan(data_point)).or_insert(0) += 1;
}
}

pub fn remove(&mut self, data_point: f64) {
if !data_point.is_nan() {
if let (Some(val), Some((mean, var))) =
Expand All @@ -111,6 +113,7 @@ impl Empirical {
}
}
}

// Due to issues with rounding and floating-point accuracy the default
// implementation may be ill-behaved.
// Specialized inverse cdfs should be used whenever possible.
Expand Down Expand Up @@ -158,7 +161,7 @@ impl ::rand::distributions::Distribution<f64> for Empirical {
/// Panics if number of samples is zero
impl Max<f64> for Empirical {
fn max(&self) -> f64 {
self.data.keys().rev().map(|key| key.0) .next().unwrap()
self.data.keys().rev().map(|key| key.0).next().unwrap()
}
}

Expand All @@ -173,6 +176,7 @@ impl Distribution<f64> for Empirical {
fn mean(&self) -> Option<f64> {
self.mean_and_var.map(|(mean, _)| mean)
}

fn variance(&self) -> Option<f64> {
self.mean_and_var.map(|(_, var)| var / (self.sum - 1.))
}
Expand Down Expand Up @@ -256,8 +260,8 @@ mod tests {
let unchanged = empirical.clone();
empirical.add(2.0);
empirical.remove(2.0);
//because of rounding errors, this doesn't hold in general
//due to the mean and variance being calculated in a streaming way
// because of rounding errors, this doesn't hold in general
// due to the mean and variance being calculated in a streaming way
assert_eq!(unchanged, empirical);
}
}
3 changes: 3 additions & 0 deletions src/distribution/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Distribution<f64> for Erlang {
fn mean(&self) -> Option<f64> {
self.g.mean()
}

/// Returns the variance of the erlang distribution
///
/// # Formula
Expand All @@ -178,6 +179,7 @@ impl Distribution<f64> for Erlang {
fn variance(&self) -> Option<f64> {
self.g.variance()
}

/// Returns the entropy of the erlang distribution
///
/// # Formula
Expand All @@ -191,6 +193,7 @@ impl Distribution<f64> for Erlang {
fn entropy(&self) -> Option<f64> {
self.g.entropy()
}

/// Returns the skewness of the erlang distribution
///
/// # Formula
Expand Down
3 changes: 3 additions & 0 deletions src/distribution/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl Distribution<f64> for Exp {
fn mean(&self) -> Option<f64> {
Some(1.0 / self.rate)
}

/// Returns the variance of the exponential distribution
///
/// # Formula
Expand All @@ -164,6 +165,7 @@ impl Distribution<f64> for Exp {
fn variance(&self) -> Option<f64> {
Some(1.0 / (self.rate * self.rate))
}

/// Returns the entropy of the exponential distribution
///
/// # Formula
Expand All @@ -176,6 +178,7 @@ impl Distribution<f64> for Exp {
fn entropy(&self) -> Option<f64> {
Some(1.0 - self.rate.ln())
}

/// Returns the skewness of the exponential distribution
///
/// # Formula
Expand Down
Loading
Loading