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

Add a way to easily get the standard normal distribution. #228

Merged
merged 6 commits into from
May 25, 2024
Merged
Changes from 5 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
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 @@ -288,6 +306,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 @@ -508,4 +535,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();

n_mean = n.mean().unwrap();
n_std = n.std_dev().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines need a let:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops not sure how I missed this one (cargo test ran??). Fixed.

Copy link
Contributor

@FreezyLemon FreezyLemon May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you scroll up, you'll see that the entire tests module is only run when the nightly feature flag is enabled (which requires a nightly compiler). So you'd need to run cargo +nightly test -F nightly, which is easy to miss


// 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);
}
}