Skip to content

Commit

Permalink
Merge branch 'master' into fix-mode-gamma
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion authored May 27, 2024
2 parents 88c40e4 + 2e3c453 commit 075e352
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 32 deletions.
65 changes: 43 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,55 @@ on:
pull_request:
branches: [ master ]

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings"

jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable with clippy
uses: dtolnay/rust-toolchain@stable
with:
components: clippy

- name: Run cargo clippy
run: cargo clippy --all-targets

fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust nightly with rustfmt
uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt

- name: Run rustfmt --check
run: cargo fmt -- --check

test:
name: Test
needs: [clippy, fmt]
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
toolchain: stable
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
toolchain: nightly
os: [ubuntu-latest, macos-latest, windows-latest]
toolchain: [stable, nightly]

steps:
- uses: actions/checkout@v2
- name: Install toolchain
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- name: Install Rust ${{ matrix.toolchain }}
uses: dtolnay/rust-toolchain@master
with:
profile: minimal
target: ${{ matrix.target }}
toolchain: ${{ matrix.toolchain }}
override: true
- name: Test nightly feature (if possible)
if: ${{ matrix.toolchain == 'nightly' }}
run: |
cargo test --target ${{ matrix.target }} --features=nightly
cargo test --target ${{ matrix.target }} --benches --features=nightly

- name: Test nightly feature
if: matrix.toolchain == 'nightly'
run: cargo test --all-targets --features=nightly

- name: Test default features
run: |
cargo test --target ${{ matrix.target }}
if: matrix.toolchain != 'nightly'
run: cargo test --all-targets

2 changes: 1 addition & 1 deletion src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Discrete<u64, f64> for Bernoulli {
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod testing {
use std::fmt::Debug;
use crate::distribution::DiscreteCDF;
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ mod tests {
test_almost(0.0, 0.1, 0.9936346508990272, 1e-16, sf(-5.0));
test_almost(0.0, 0.1, 0.9682744825694465, 1e-16, sf(-1.0));
test_case(0.0, 0.1, 0.5, sf(0.0));
test_case(0.0, 0.1, 0.03172551743055352, sf(1.0));
test_almost(0.0, 0.1, 0.03172551743055352, 1e-16, sf(1.0));
test_case(0.0, 0.1, 0.006365349100972806, sf(5.0));
test_almost(0.0, 1.0, 0.9371670418109989, 1e-16, sf(-5.0));
test_case(0.0, 1.0, 0.75, sf(-1.0));
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Mode<Option<f64>> for Dirac {
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use crate::statistics::*;
use crate::distribution::{ContinuousCDF, Continuous, Dirac};
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn is_valid_alpha(a: &[f64]) -> bool {
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use super::*;
use nalgebra::{DVector};
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 @@ -248,7 +248,7 @@ impl Discrete<i64, f64> for DiscreteUniform {
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use std::fmt::Debug;
use crate::statistics::*;
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl ContinuousCDF<f64, f64> for Empirical {
}
}

#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl Continuous<f64, f64> for Laplace {
}
}

#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use super::*;
use rand::thread_rng;
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/multivariate_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl Continuous<Vec<f64>, f64> for MultivariateNormal {
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
#[cfg(test)]
mod tests {
use crate::distribution::{Continuous, MultivariateNormal};
use crate::statistics::*;
Expand Down
40 changes: 40 additions & 0 deletions src/distribution/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ impl Normal {
Ok(Normal { mean, std_dev })
}
}

/// Constructs a new standard normal distribution with a mean of 0
/// and a standard deviation of 1.
///
///
/// # Examples
///
/// ```
/// use statrs::distribution::Normal;
///
/// let mut result = Normal::standard();
/// ```
pub fn standard() -> Normal {
Normal {
mean: 0.0,
std_dev: 1.0,
}
}
}

impl ::rand::distributions::Distribution<f64> for Normal {
Expand Down Expand Up @@ -292,6 +310,15 @@ pub fn sample_unchecked<R: Rng + ?Sized>(rng: &mut R, mean: f64, std_dev: f64) -
mean + std_dev * ziggurat::sample_std_normal(rng)
}


impl std::default::Default for Normal {
/// Returns the standard normal distribution with a mean of 0
/// and a standard deviation of 1.
fn default() -> Self {
Self::standard()
}
}

#[rustfmt::skip]
#[cfg(all(test, feature = "nightly"))]
mod tests {
Expand Down Expand Up @@ -512,4 +539,17 @@ mod tests {
test_almost(5.0, 2.0, 10.0, 1e-14, inverse_cdf(0.9937903346742238648330218954258077788721022530769078));
test_case(5.0, 2.0, f64::INFINITY, inverse_cdf(1.0));
}

#[test]
fn test_default() {
let n = Normal::default();

let n_mean = n.mean().unwrap();
let n_std = n.std_dev().unwrap();

// Check that the mean of the distribution is close to 0
assert_almost_eq!(n_mean, 0.0, 1e-15);
// Check that the standard deviation of the distribution is close to 1
assert_almost_eq!(n_std, 1.0, 1e-15);
}
}
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod tests {
#[test]
fn test_sync_send() {
// Error types should implement Sync and Send
let _ = assert_sync::<StatsError>();
let _ = assert_send::<StatsError>();
assert_sync::<StatsError>();
assert_send::<StatsError>();
}
}

0 comments on commit 075e352

Please sign in to comment.