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

Std axis #486

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0c83a76
Added function signature.
LukeMathWalker Apr 19, 2018
dae4c72
Ignoring stuff
LukeMathWalker Apr 19, 2018
548e431
Added implementation detail in the documentation.
LukeMathWalker Apr 19, 2018
2049e82
Initialized the accumulators.
LukeMathWalker Apr 19, 2018
ee88de5
Fixing the initialization.
LukeMathWalker Apr 19, 2018
e9b19eb
Completed first implementation.
LukeMathWalker Apr 19, 2018
65343f6
Added a test for var_axis
LukeMathWalker Apr 24, 2018
371490c
remove changes
LukeMathWalker Apr 24, 2018
8beb7fd
Included changes proposed by jturner314
LukeMathWalker May 1, 2018
9db3840
Added panic note to the docs. Removed a print in the test.
LukeMathWalker May 1, 2018
2f502df
Changed bound to accept only Float values. Refactored initialization …
LukeMathWalker May 8, 2018
7482baa
Restoring trailing white spaces.
LukeMathWalker May 8, 2018
6383f45
Restoring white spaces
LukeMathWalker May 8, 2018
e552f9a
Restoring white spaces
LukeMathWalker May 8, 2018
bb7a22b
Made all required edits to remove ScalarOperand.
LukeMathWalker May 24, 2018
cdcd25d
Added function signature.
LukeMathWalker Apr 19, 2018
4b507e1
Ignoring stuff
LukeMathWalker Apr 19, 2018
fe017f6
Added implementation detail in the documentation.
LukeMathWalker Apr 19, 2018
c2bfe85
Initialized the accumulators.
LukeMathWalker Apr 19, 2018
2fabc23
Fixing the initialization.
LukeMathWalker Apr 19, 2018
0b7b289
Completed first implementation.
LukeMathWalker Apr 19, 2018
34387e3
Added a test for var_axis
LukeMathWalker Apr 24, 2018
217ba74
remove changes
LukeMathWalker Apr 24, 2018
3fc4228
Included changes proposed by jturner314
LukeMathWalker May 1, 2018
7389720
Added panic note to the docs. Removed a print in the test.
LukeMathWalker May 1, 2018
d5956b8
Changed bound to accept only Float values. Refactored initialization …
LukeMathWalker May 8, 2018
d078f76
Restoring trailing white spaces.
LukeMathWalker May 8, 2018
ef0c02d
Restoring white spaces
LukeMathWalker May 8, 2018
2279f49
Restoring white spaces
LukeMathWalker May 8, 2018
58091e4
Made all required edits to remove ScalarOperand.
LukeMathWalker May 24, 2018
7e52346
Merge branch 'master' of github.com:LukeMathWalker/ndarray
LukeMathWalker May 24, 2018
1ef804f
Merge
LukeMathWalker Sep 1, 2018
ee7a67a
Merge remote-tracking branch 'upstream/master'
LukeMathWalker Sep 17, 2018
a36ecc2
Added stub for standard deviation.
LukeMathWalker Sep 17, 2018
c76296c
Implemented std_axis.
LukeMathWalker Sep 17, 2018
fc4eba5
Fixed docs.
LukeMathWalker Sep 17, 2018
3b713f3
Added two tests for std_axis.
LukeMathWalker Sep 17, 2018
4f00e79
Added test for std_axis with random array.
LukeMathWalker Sep 17, 2018
c1dcdf4
Fixed tests.
LukeMathWalker Sep 17, 2018
930164c
Fixed doc tests.
LukeMathWalker Sep 17, 2018
af6be60
Formatting.
LukeMathWalker Sep 17, 2018
989e584
Using mapv_into to avoid extra allocation in std_axis.
LukeMathWalker Sep 18, 2018
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
50 changes: 48 additions & 2 deletions src/numeric/impl_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,53 @@ impl<A, S, D> ArrayBase<S, D>
}
}

/// Return standard deviation along `axis`.
///
/// The standard deviation is computed from the variance using
/// the [Welford one-pass algorithm](https://www.jstor.org/stable/1266577).
///
/// The parameter `ddof` specifies the "delta degrees of freedom". For
/// example, to calculate the population standard deviation, use `ddof = 0`,
/// or to calculate the sample standard deviation, use `ddof = 1`.
///
/// The standard deviation is defined as:
///
/// ```text
/// 1 n
/// stddev = sqrt ( ―――――――― ∑ (xᵢ - x̅)² )
/// n - ddof i=1
/// ```
///
/// where
///
/// ```text
/// 1 n
/// x̅ = ― ∑ xᵢ
/// 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.
///
/// # Example
///
/// ```
/// use ndarray::{aview1, arr2, Axis};
///
/// let a = arr2(&[[1., 2.],
/// [3., 4.],
/// [5., 6.]]);
/// let stddev = a.std_axis(Axis(0), 1.);
/// assert_eq!(stddev, aview1(&[2., 2.]));
/// ```
pub fn std_axis(&self, axis: Axis, ddof: A) -> Array<A, D::Smaller>
where
A: Float,
D: RemoveAxis,
{
self.var_axis(axis, ddof).mapv(|x| x.sqrt())
Copy link
Member

Choose a reason for hiding this comment

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

I'd change the .mapv into .mapv_into to avoid the extra allocation.

}

/// Return `true` if the arrays' elementwise differences are all within
/// the given absolute tolerance, `false` otherwise.
///
Expand All @@ -202,5 +249,4 @@ impl<A, S, D> ArrayBase<S, D>
}
}).is_done()
}
}

}
61 changes: 61 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,53 @@ fn var_axis() {
assert!(d.var_axis(Axis(0), 0.).all_close(&aview0(&1.8875), 1e-12));
}

#[test]
fn std_axis() {
let a = array![
[
[ 0.22935481, 0.08030619, 0.60827517, 0.73684379],
[ 0.90339851, 0.82859436, 0.64020362, 0.2774583 ],
[ 0.44485313, 0.63316367, 0.11005111, 0.08656246]
],
[
[ 0.28924665, 0.44082454, 0.59837736, 0.41014531],
[ 0.08382316, 0.43259439, 0.1428889 , 0.44830176],
[ 0.51529756, 0.70111616, 0.20799415, 0.91851457]
],
];
assert!(a.std_axis(Axis(0), 1.5).all_close(
&aview2(&[
[ 0.05989184, 0.36051836, 0.00989781, 0.32669847],
[ 0.81957535, 0.39599997, 0.49731472, 0.17084346],
[ 0.07044443, 0.06795249, 0.09794304, 0.83195211],
]),
1e-4,
));
assert!(a.std_axis(Axis(1), 1.7).all_close(
&aview2(&[
[ 0.42698655, 0.48139215, 0.36874991, 0.41458724],
[ 0.26769097, 0.18941435, 0.30555015, 0.35118674],
]),
1e-8,
));
assert!(a.std_axis(Axis(2), 2.3).all_close(
&aview2(&[
[ 0.41117907, 0.37130425, 0.35332388],
[ 0.16905862, 0.25304841, 0.39978276],
]),
1e-8,
));

let b = array![[100000., 1., 0.01]];
assert!(b.std_axis(Axis(0), 0.).all_close(&aview1(&[0., 0., 0.]), 1e-12));
assert!(
b.std_axis(Axis(1), 0.).all_close(&aview1(&[47140.214021552769]), 1e-6),
);

let c = array![[], []];
assert_eq!(c.std_axis(Axis(0), 0.), aview1(&[]));
}

#[test]
#[should_panic]
fn var_axis_bad_dof() {
Expand All @@ -759,6 +806,20 @@ fn var_axis_empty_axis() {
a.var_axis(Axis(1), 0.);
}

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

#[test]
#[should_panic]
fn std_axis_empty_axis() {
let a = array![[], []];
a.std_axis(Axis(1), 0.);
}

#[test]
fn iter_size_hint()
{
Expand Down