Skip to content

Commit

Permalink
Merge branch 'master' into gumbel_issue189
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyasen1809 authored Sep 11, 2024
2 parents 69c249b + 1f8e00a commit c45da71
Show file tree
Hide file tree
Showing 45 changed files with 194 additions and 188 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ on:
branches: [ master ]
jobs:
coverage:
timeout-minutes: 20
strategy:
matrix:
toolchain: [nightly, nightly-2024-09-01]

name: Coverage
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
components: llvm-tools-preview

- uses: taiki-e/install-action@v2
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ jobs:
with:
components: clippy

- name: Run cargo clippy
- name: Run cargo clippy (default features)
run: cargo clippy --all-targets

- name: Run cargo clippy without default features
run: cargo clippy --no-default-features --all-targets

fmt:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -65,3 +62,15 @@ jobs:
- name: Test default features
run: cargo test

features:
needs: [clippy, fmt]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
- name: Check all possible feature sets
run: cargo hack check --feature-powerset --no-dev-deps
28 changes: 14 additions & 14 deletions Cargo.lock.MSRV

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
include = ["CHANGELOG.md", "LICENSE.md", "src/", "tests/"]

# When changing MSRV: Also update the README
rust-version = "1.66.0"
rust-version = "1.61.0"

[lib]
name = "statrs"
Expand All @@ -22,21 +22,26 @@ path = "src/lib.rs"
[[bench]]
name = "order_statistics"
harness = false
required-features = ["rand"]

[features]
default = ["nalgebra"]
default = ["nalgebra", "rand"]
nalgebra = ["dep:nalgebra"]
rand = ["dep:rand", "nalgebra?/rand"]

[dependencies]
rand = "0.8"
approx = "0.5.0"
num-traits = "0.2.14"

[dependencies.rand]
version = "0.8"
optional = true

[dependencies.nalgebra]
version = "0.32"
optional = true
default-features = false
features = ["rand", "std"]
features = ["std"]

[dev-dependencies]
criterion = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you'd like to modify where the data is downloaded, you can use the environmen

## Minimum supported Rust version (MSRV)

This crate requires a Rust version of 1.66.0 or higher. Increases in MSRV will be considered a semver non-breaking API change and require a version increase (PATCH until 1.0.0, MINOR after 1.0.0).
This crate requires a Rust version of 1.61.0 or higher. Increases in MSRV will be considered a semver non-breaking API change and require a version increase (PATCH until 1.0.0, MINOR after 1.0.0).

## Contributing

Expand Down
1 change: 0 additions & 1 deletion benches/order_statistics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate rand;
extern crate statrs;
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use rand::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Binomial, BinomialError, Discrete, DiscreteCDF};
use crate::statistics::*;
use rand::Rng;

/// Implements the
/// [Bernoulli](https://en.wikipedia.org/wiki/Bernoulli_distribution)
Expand Down Expand Up @@ -85,8 +84,9 @@ impl std::fmt::Display for Bernoulli {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Bernoulli {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.gen_bool(self.p()) as u8 as f64
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::{beta, gamma};
use crate::statistics::*;
use rand::Rng;

/// Implements the [Beta](https://en.wikipedia.org/wiki/Beta_distribution)
/// distribution
Expand Down Expand Up @@ -121,8 +120,9 @@ impl std::fmt::Display for Beta {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Beta {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
// Generated by sampling two gamma distributions and normalizing.
let x = super::gamma::sample_unchecked(rng, self.shape_a, 1.0);
let y = super::gamma::sample_unchecked(rng, self.shape_b, 1.0);
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::function::{beta, factorial};
use crate::statistics::*;
use rand::Rng;
use std::f64;

/// Implements the
Expand Down Expand Up @@ -110,8 +109,9 @@ impl std::fmt::Display for Binomial {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Binomial {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
(0..self.n).fold(0.0, |acc, _| {
let n: f64 = rng.gen();
if n < self.p {
Expand Down
7 changes: 4 additions & 3 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::statistics::*;
use rand::Rng;
use std::f64;

/// Implements the
Expand Down Expand Up @@ -124,8 +123,9 @@ impl std::fmt::Display for Categorical {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Categorical {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
sample_unchecked(rng, &self.cdf)
}
}
Expand Down Expand Up @@ -322,7 +322,8 @@ impl Discrete<u64, f64> for Categorical {

/// Draws a sample from the categorical distribution described by `cdf`
/// without doing any bounds checking
pub fn sample_unchecked<R: Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> f64 {
#[cfg(feature = "rand")]
pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> f64 {
let draw = rng.gen::<f64>() * cdf.last().unwrap();
cdf.iter()
.enumerate()
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::statistics::*;
use rand::Rng;
use std::f64;

/// Implements the [Cauchy](https://en.wikipedia.org/wiki/Cauchy_distribution)
Expand Down Expand Up @@ -111,8 +110,9 @@ impl std::fmt::Display for Cauchy {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Cauchy {
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
self.location + self.scale * (f64::consts::PI * (r.gen::<f64>() - 0.5)).tan()
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::gamma;
use crate::statistics::*;
use rand::Rng;
use std::f64;

/// Implements the [Chi](https://en.wikipedia.org/wiki/Chi_distribution)
Expand Down Expand Up @@ -94,8 +93,9 @@ impl std::fmt::Display for Chi {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Chi {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
(0..self.freedom as i64)
.fold(0.0, |acc, _| {
acc + super::normal::sample_unchecked(rng, 0.0, 1.0).powf(2.0)
Expand Down Expand Up @@ -381,7 +381,7 @@ mod tests {
fn test_large_dof_mean_not_nan() {
for i in 1..1000 {
let mean = Chi::new(i as f64).unwrap().mean().unwrap();
assert!(!mean.is_nan(), "Chi mean for {} dof was {}", i, mean);
assert!(!mean.is_nan(), "Chi mean for {i} dof was {mean}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF, Gamma, GammaError};
use crate::statistics::*;
use rand::Rng;
use std::f64;

/// Implements the
Expand Down Expand Up @@ -101,8 +100,9 @@ impl std::fmt::Display for ChiSquared {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for ChiSquared {
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
::rand::distributions::Distribution::sample(&self.g, r)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::ContinuousCDF;
use crate::statistics::*;
use rand::Rng;

/// Implements the [Dirac Delta](https://en.wikipedia.org/wiki/Dirac_delta_function#As_a_distribution)
/// distribution
Expand Down Expand Up @@ -69,8 +68,9 @@ impl std::fmt::Display for Dirac {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for Dirac {
fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, _: &mut R) -> f64 {
self.0
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::distribution::Continuous;
use crate::function::gamma;
use crate::prec;
use crate::statistics::*;
use nalgebra::{Const, Dim, Dyn, OMatrix, OVector};
use rand::Rng;
use nalgebra::{Dim, Dyn, OMatrix, OVector};
use std::f64;

/// Implements the
Expand Down Expand Up @@ -192,16 +191,17 @@ where
}
}

#[cfg(feature = "rand")]
impl<D> ::rand::distributions::Distribution<OVector<f64, D>> for Dirichlet<D>
where
D: Dim,
nalgebra::DefaultAllocator: nalgebra::allocator::Allocator<f64, D>,
{
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> OVector<f64, D> {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> OVector<f64, D> {
let mut sum = 0.0;
OVector::from_iterator_generic(
self.alpha.shape_generic().0,
Const::<1>,
nalgebra::Const::<1>,
self.alpha.iter().map(|&a| {
let sample = super::gamma::sample_unchecked(rng, a, 1.0);
sum += sample;
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::statistics::*;
use rand::Rng;

/// Implements the [Discrete
/// Uniform](https://en.wikipedia.org/wiki/Discrete_uniform_distribution)
Expand Down Expand Up @@ -75,8 +74,9 @@ impl std::fmt::Display for DiscreteUniform {
}
}

#[cfg(feature = "rand")]
impl ::rand::distributions::Distribution<f64> for DiscreteUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.gen_range(self.min..=self.max) as f64
}
}
Expand Down
Loading

0 comments on commit c45da71

Please sign in to comment.