Skip to content

Adjust constraints on ddof for var_axis and std_axis #515

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

Merged
merged 3 commits into from
Nov 27, 2018
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
41 changes: 24 additions & 17 deletions src/numeric/impl_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

use std::ops::{Add, Div, Mul};
use libnum::{self, One, Zero, Float};
use libnum::{self, One, Zero, Float, FromPrimitive};
use itertools::free::enumerate;

use imp_prelude::*;
Expand Down Expand Up @@ -163,8 +163,11 @@ impl<A, S, D> ArrayBase<S, D>
/// n i=1
/// ```
///
/// **Panics** if `ddof` is greater than or equal to the length of the
/// axis, if `axis` is out of bounds, or if the length of the axis is zero.
/// and `n` is the length of the axis.
///
/// **Panics** if `ddof` is less than zero or greater than `n`, if `axis`
/// is out of bounds, or if `A::from_usize()` fails for any any of the
/// numbers in the range `0..=n`.
///
/// # Example
///
Expand All @@ -179,27 +182,28 @@ impl<A, S, D> ArrayBase<S, D>
/// ```
pub fn var_axis(&self, axis: Axis, ddof: A) -> Array<A, D::Smaller>
where
A: Float,
A: Float + FromPrimitive,
D: RemoveAxis,
{
let mut count = A::zero();
let zero = A::from_usize(0).expect("Converting 0 to `A` must not fail.");
let n = A::from_usize(self.len_of(axis)).expect("Converting length to `A` must not fail.");
assert!(
!(ddof < zero || ddof > n),
"`ddof` must not be less than zero or greater than the length of \
the axis",
);
let dof = n - ddof;
let mut mean = Array::<A, _>::zeros(self.dim.remove_axis(axis));
let mut sum_sq = Array::<A, _>::zeros(self.dim.remove_axis(axis));
for subview in self.axis_iter(axis) {
count = count + A::one();
for (i, subview) in self.axis_iter(axis).enumerate() {
let count = A::from_usize(i + 1).expect("Converting index to `A` must not fail.");
azip!(mut mean, mut sum_sq, x (subview) in {
let delta = x - *mean;
*mean = *mean + delta / count;
*sum_sq = (x - *mean).mul_add(delta, *sum_sq);
});
}
if ddof >= count {
panic!("`ddof` needs to be strictly smaller than the length \
of the axis you are computing the variance for!")
} else {
let dof = count - ddof;
sum_sq.mapv_into(|s| s / dof)
}
sum_sq.mapv_into(|s| s / dof)
}

/// Return standard deviation along `axis`.
Expand Down Expand Up @@ -227,8 +231,11 @@ impl<A, S, D> ArrayBase<S, D>
/// n i=1
/// ```
///
/// **Panics** if `ddof` is greater than or equal to the length of the
/// axis, if `axis` is out of bounds, or if the length of the axis is zero.
/// and `n` is the length of the axis.
///
/// **Panics** if `ddof` is less than zero or greater than `n`, if `axis`
/// is out of bounds, or if `A::from_usize()` fails for any any of the
/// numbers in the range `0..=n`.
///
/// # Example
///
Expand All @@ -243,7 +250,7 @@ impl<A, S, D> ArrayBase<S, D>
/// ```
pub fn std_axis(&self, axis: Axis, ddof: A) -> Array<A, D::Smaller>
where
A: Float,
A: Float + FromPrimitive,
D: RemoveAxis,
{
self.var_axis(axis, ddof).mapv_into(|x| x.sqrt())
Expand Down
31 changes: 24 additions & 7 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,16 +810,32 @@ fn std_axis() {

#[test]
#[should_panic]
fn var_axis_bad_dof() {
fn var_axis_negative_ddof() {
let a = array![1., 2., 3.];
a.var_axis(Axis(0), 4.);
a.var_axis(Axis(0), -1.);
}

#[test]
#[should_panic]
fn var_axis_too_large_ddof() {
let a = array![1., 2., 3.];
a.var_axis(Axis(0), 4.);
}

#[test]
fn var_axis_nan_ddof() {
let a = Array2::<f64>::zeros((2, 3));
let v = a.var_axis(Axis(1), ::std::f64::NAN);
assert_eq!(v.shape(), &[2]);
v.mapv(|x| assert!(x.is_nan()));
}

#[test]
fn var_axis_empty_axis() {
let a = array![[], []];
a.var_axis(Axis(1), 0.);
let a = Array2::<f64>::zeros((2, 0));
let v = a.var_axis(Axis(1), 0.);
assert_eq!(v.shape(), &[2]);
v.mapv(|x| assert!(x.is_nan()));
}

#[test]
Expand All @@ -830,10 +846,11 @@ fn std_axis_bad_dof() {
}

#[test]
#[should_panic]
fn std_axis_empty_axis() {
let a = array![[], []];
a.std_axis(Axis(1), 0.);
let a = Array2::<f64>::zeros((2, 0));
let v = a.std_axis(Axis(1), 0.);
assert_eq!(v.shape(), &[2]);
v.mapv(|x| assert!(x.is_nan()));
}

#[test]
Expand Down