Skip to content

Commit 075e352

Browse files
authored
Merge branch 'master' into fix-mode-gamma
2 parents 88c40e4 + 2e3c453 commit 075e352

File tree

11 files changed

+93
-32
lines changed

11 files changed

+93
-32
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,55 @@ on:
66
pull_request:
77
branches: [ master ]
88

9+
env:
10+
CARGO_INCREMENTAL: 0
11+
RUSTFLAGS: "-Dwarnings"
12+
913
jobs:
14+
clippy:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Install Rust stable with clippy
19+
uses: dtolnay/rust-toolchain@stable
20+
with:
21+
components: clippy
22+
23+
- name: Run cargo clippy
24+
run: cargo clippy --all-targets
25+
26+
fmt:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v4
30+
- name: Install Rust nightly with rustfmt
31+
uses: dtolnay/rust-toolchain@nightly
32+
with:
33+
components: rustfmt
34+
35+
- name: Run rustfmt --check
36+
run: cargo fmt -- --check
37+
1038
test:
11-
name: Test
39+
needs: [clippy, fmt]
1240
runs-on: ${{ matrix.os }}
1341
strategy:
14-
fail-fast: false
1542
matrix:
16-
include:
17-
- os: ubuntu-latest
18-
target: x86_64-unknown-linux-gnu
19-
toolchain: stable
20-
- os: ubuntu-latest
21-
target: x86_64-unknown-linux-gnu
22-
toolchain: nightly
43+
os: [ubuntu-latest, macos-latest, windows-latest]
44+
toolchain: [stable, nightly]
45+
2346
steps:
24-
- uses: actions/checkout@v2
25-
- name: Install toolchain
26-
uses: actions-rs/toolchain@v1
47+
- uses: actions/checkout@v4
48+
- name: Install Rust ${{ matrix.toolchain }}
49+
uses: dtolnay/rust-toolchain@master
2750
with:
28-
profile: minimal
29-
target: ${{ matrix.target }}
3051
toolchain: ${{ matrix.toolchain }}
31-
override: true
32-
- name: Test nightly feature (if possible)
33-
if: ${{ matrix.toolchain == 'nightly' }}
34-
run: |
35-
cargo test --target ${{ matrix.target }} --features=nightly
36-
cargo test --target ${{ matrix.target }} --benches --features=nightly
52+
53+
- name: Test nightly feature
54+
if: matrix.toolchain == 'nightly'
55+
run: cargo test --all-targets --features=nightly
56+
3757
- name: Test default features
38-
run: |
39-
cargo test --target ${{ matrix.target }}
58+
if: matrix.toolchain != 'nightly'
59+
run: cargo test --all-targets
60+

src/distribution/bernoulli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl Discrete<u64, f64> for Bernoulli {
257257
}
258258

259259
#[rustfmt::skip]
260-
#[cfg(all(test, feature = "nightly"))]
260+
#[cfg(test)]
261261
mod testing {
262262
use std::fmt::Debug;
263263
use crate::distribution::DiscreteCDF;

src/distribution/cauchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ mod tests {
432432
test_almost(0.0, 0.1, 0.9936346508990272, 1e-16, sf(-5.0));
433433
test_almost(0.0, 0.1, 0.9682744825694465, 1e-16, sf(-1.0));
434434
test_case(0.0, 0.1, 0.5, sf(0.0));
435-
test_case(0.0, 0.1, 0.03172551743055352, sf(1.0));
435+
test_almost(0.0, 0.1, 0.03172551743055352, 1e-16, sf(1.0));
436436
test_case(0.0, 0.1, 0.006365349100972806, sf(5.0));
437437
test_almost(0.0, 1.0, 0.9371670418109989, 1e-16, sf(-5.0));
438438
test_case(0.0, 1.0, 0.75, sf(-1.0));

src/distribution/dirac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl Mode<Option<f64>> for Dirac {
185185
}
186186

187187
#[rustfmt::skip]
188-
#[cfg(all(test, feature = "nightly"))]
188+
#[cfg(test)]
189189
mod tests {
190190
use crate::statistics::*;
191191
use crate::distribution::{ContinuousCDF, Continuous, Dirac};

src/distribution/dirichlet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn is_valid_alpha(a: &[f64]) -> bool {
301301
}
302302

303303
#[rustfmt::skip]
304-
#[cfg(all(test, feature = "nightly"))]
304+
#[cfg(test)]
305305
mod tests {
306306
use super::*;
307307
use nalgebra::{DVector};

src/distribution/discrete_uniform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Discrete<i64, f64> for DiscreteUniform {
248248
}
249249

250250
#[rustfmt::skip]
251-
#[cfg(all(test, feature = "nightly"))]
251+
#[cfg(test)]
252252
mod tests {
253253
use std::fmt::Debug;
254254
use crate::statistics::*;

src/distribution/empirical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl ContinuousCDF<f64, f64> for Empirical {
206206
}
207207
}
208208

209-
#[cfg(all(test, feature = "nightly"))]
209+
#[cfg(test)]
210210
mod tests {
211211
use super::*;
212212
#[test]

src/distribution/laplace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl Continuous<f64, f64> for Laplace {
291291
}
292292
}
293293

294-
#[cfg(all(test, feature = "nightly"))]
294+
#[cfg(test)]
295295
mod tests {
296296
use super::*;
297297
use rand::thread_rng;

src/distribution/multivariate_normal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Continuous<Vec<f64>, f64> for MultivariateNormal {
242242
}
243243

244244
#[rustfmt::skip]
245-
#[cfg(all(test, feature = "nightly"))]
245+
#[cfg(test)]
246246
mod tests {
247247
use crate::distribution::{Continuous, MultivariateNormal};
248248
use crate::statistics::*;

src/distribution/normal.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ impl Normal {
5151
Ok(Normal { mean, std_dev })
5252
}
5353
}
54+
55+
/// Constructs a new standard normal distribution with a mean of 0
56+
/// and a standard deviation of 1.
57+
///
58+
///
59+
/// # Examples
60+
///
61+
/// ```
62+
/// use statrs::distribution::Normal;
63+
///
64+
/// let mut result = Normal::standard();
65+
/// ```
66+
pub fn standard() -> Normal {
67+
Normal {
68+
mean: 0.0,
69+
std_dev: 1.0,
70+
}
71+
}
5472
}
5573

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

313+
314+
impl std::default::Default for Normal {
315+
/// Returns the standard normal distribution with a mean of 0
316+
/// and a standard deviation of 1.
317+
fn default() -> Self {
318+
Self::standard()
319+
}
320+
}
321+
295322
#[rustfmt::skip]
296323
#[cfg(all(test, feature = "nightly"))]
297324
mod tests {
@@ -512,4 +539,17 @@ mod tests {
512539
test_almost(5.0, 2.0, 10.0, 1e-14, inverse_cdf(0.9937903346742238648330218954258077788721022530769078));
513540
test_case(5.0, 2.0, f64::INFINITY, inverse_cdf(1.0));
514541
}
542+
543+
#[test]
544+
fn test_default() {
545+
let n = Normal::default();
546+
547+
let n_mean = n.mean().unwrap();
548+
let n_std = n.std_dev().unwrap();
549+
550+
// Check that the mean of the distribution is close to 0
551+
assert_almost_eq!(n_mean, 0.0, 1e-15);
552+
// Check that the standard deviation of the distribution is close to 1
553+
assert_almost_eq!(n_std, 1.0, 1e-15);
554+
}
515555
}

0 commit comments

Comments
 (0)