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

Fixes and updates #134

Merged
merged 8 commits into from
Apr 19, 2021
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
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ before_script:
script:
- cargo fmt --all -- --check
- cargo build
- cargo test
- >
if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
cargo test --features nightly
else
cargo test
fi

after_success: >
if [[ "$TRAVIS_OS_NAME" == "linux" ]] && [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ repository = "https://github.com/boxtown/statrs"
edition = "2018"

[lib]

name = "statrs"
path = "src/lib.rs"

[features]
nightly = []

[dependencies]
rand = "0.7.3"
nalgebra = "0.23.0"
rand = "0.8"
nalgebra = { version = "0.26", features = ["rand"] }
approx = "0.4.0"
num-traits = "0.2.14"
lazy_static = "1.4.0"
Expand Down
6 changes: 3 additions & 3 deletions benches/order_statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use statrs::statistics::*;

fn bench_order_statistic(c: &mut Criterion) {
let mut rng = thread_rng();
let to_random_owned = |data: &[f64]| -> Vec<f64> {
let to_random_owned = |data: &[f64]| -> Data<Vec<f64>> {
let mut rng = thread_rng();
let mut owned = data.to_vec();
owned.shuffle(&mut rng);
owned
Data::new(owned)
};
let k = black_box(rng.gen());
let tau = black_box(rng.gen_range(0.0, 1.0));
let tau = black_box(rng.gen_range(0.0..1.0));
let mut group = c.benchmark_group("order statistic");
let data: Vec<_> = (0..100).map(|x| x as f64).collect();
group.bench_function("order_statistic", |b| {
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl Continuous<f64, f64> for Beta {
}

#[rustfmt::skip]
#[cfg(test)]
#[cfg(all(test, feature = "nightly"))]
mod tests {
use super::*;
use crate::consts::ACC;
Expand Down
8 changes: 0 additions & 8 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,6 @@ mod tests {
assert_almost_eq!(expected, x, acc);
}

fn test_is_nan<F>(freedom: f64, eval: F)
where
F: Fn(Chi) -> f64,
{
let x = get_value(freedom, eval);
assert!(x.is_nan());
}

#[test]
fn test_create() {
create_case(1.0);
Expand Down
4 changes: 1 addition & 3 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::{prec, Result, StatsError};
use nalgebra::DMatrix;
use nalgebra::DVector;
use nalgebra::{
base::allocator::Allocator,
base::{dimension::DimName, MatrixN, VectorN},
DefaultAllocator, Dim, DimMin, U1,
base::allocator::Allocator, base::dimension::DimName, DefaultAllocator, Dim, DimMin, U1,
};
use rand::Rng;
use std::f64;
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl DiscreteUniform {

impl ::rand::distributions::Distribution<f64> for DiscreteUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
rng.gen_range(self.min, self.max + 1) as f64
rng.gen_range(self.min..=self.max) as f64
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/distribution/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub fn sample_unchecked<R: Rng + ?Sized>(rng: &mut R, shape: f64, rate: f64) ->
}
}

#[cfg(test)]
#[cfg(all(test, feature = "nightly"))]
mod tests {
use super::*;
use crate::consts::ACC;
Expand Down
2 changes: 2 additions & 0 deletions src/distribution/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod test {
use crate::consts::ACC;
use crate::distribution::{Continuous, ContinuousCDF, Discrete, DiscreteCDF};

#[cfg(feature = "nightly")]
#[macro_export]
macro_rules! testing_boiler {
($arg:ty, $dist:ty) => {
Expand Down Expand Up @@ -58,6 +59,7 @@ pub mod test {
assert_abs_diff_eq!(expected, x, epsilon = acc);
}

#[allow(dead_code)] // This is not used by all distributions.
fn test_none<F, T>(arg: $arg, eval: F)
where
F: Fn($dist) -> Option<T>,
Expand Down
11 changes: 2 additions & 9 deletions src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Laplace {

impl ::rand::distributions::Distribution<f64> for Laplace {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x: f64 = rng.gen_range(-0.5, 0.5);
let x: f64 = rng.gen_range(-0.5..0.5);
self.location - self.scale * x.signum() * (1. - 2. * x).ln()
}
}
Expand Down Expand Up @@ -280,12 +280,6 @@ mod tests {
n.unwrap()
}

fn create_case(location: f64, scale: f64) {
let n = try_create(location, scale);
assert_eq!(location, n.location);
assert_eq!(scale, n.scale);
}

fn bad_create_case(location: f64, scale: f64) {
let n = Laplace::new(location, scale);
assert!(n.is_err());
Expand All @@ -302,7 +296,6 @@ mod tests {

fn test_is_nan<F>(location: f64, scale: f64, eval: F)
where
F: Fn(laplace::Laplace) -> f64,
F: Fn(Laplace) -> f64,
{
let n = try_create(location, scale);
Expand Down Expand Up @@ -405,7 +398,7 @@ mod tests {
#[test]
fn test_max() {
test_case(0.0, 1.0, INF, |l| l.max());
}
}

#[test]
fn test_density() {
Expand Down
9 changes: 4 additions & 5 deletions src/distribution/multivariate_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::distribution::Normal;
use crate::statistics::{Max, MeanN, Min, Mode, VarianceN};
use crate::{Result, StatsError};
use nalgebra::{
base::allocator::Allocator,
base::{dimension::DimName, MatrixN, VectorN},
Cholesky, DefaultAllocator, Dim, DimMin, LU, U1,
base::allocator::Allocator, base::dimension::DimName, Cholesky, DefaultAllocator, Dim, DimMin,
LU, U1,
};
use nalgebra::{DMatrix, DVector};
use rand::Rng;
Expand Down Expand Up @@ -212,8 +211,8 @@ mod tests {
use core::fmt::Debug;
use nalgebra::base::allocator::Allocator;
use nalgebra::{
DefaultAllocator, Dim, DimMin, DimName, Matrix2, Matrix3, MatrixN, Vector2, Vector3,
VectorN, U1, U2,
DefaultAllocator, Dim, DimMin, DimName, Matrix2, Matrix3, Vector2, Vector3,
U1, U2,
};

fn try_create(mean: Vec<f64>, covariance: Vec<f64>) -> MultivariateNormal
Expand Down
Loading