Skip to content
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "NaNMath"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
repo = "https://github.com/JuliaMath/NaNMath.jl.git"
authors = ["Miles Lubin"]
version = "1.1.3"
version = "1.2.0"

[deps]
OpenLibm_jll = "05823500-19ac-5b8b-9628-191a04bc5112"
Expand Down
36 changes: 20 additions & 16 deletions src/NaNMath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,27 @@ function mean_count(x::AbstractArray{T}) where T<:AbstractFloat
end

"""
NaNMath.var(A)
NaNMath.var(A)

##### Args:
* `A`: A one dimensional array of floating point numbers
* `A`: An array of floating point numbers

##### Returns:
* Returns the sample variance of a vector A. The algorithm will return
an estimator of the generative distribution's variance under the
assumption that each entry of v is an IID drawn from that generative
distribution. This computation is equivalent to calculating \\
sum((v - mean(v)).^2) / (length(v) - 1). NaN values are ignored.
* Returns the sample variance of all elements in the array, ignoring
NaN's. The algorithm returns an estimator of the generative
distribution's variance under the assumption that each non-NaN entry
of `A` is an IID drawn from that generative distribution. This
computation is equivalent to calculating \\
`sum((A - mean(A)).^2) / (length(A) - 1)` \\
while skipping NaN values.

##### Examples:
```julia
using NaNMath
NaNMath.var([1., 2., NaN]) # result: 0.5
```
"""
function var(x::Vector{T}) where T<:AbstractFloat
function var(x::AbstractArray{T}) where T<:AbstractFloat
mean_val, n = mean_count(x)
if !isnan(mean_val)
sum_square = zero(eltype(x))
Expand All @@ -317,25 +319,27 @@ function var(x::Vector{T}) where T<:AbstractFloat
end

"""
NaNMath.std(A)
NaNMath.std(A)

##### Args:
* `A`: A one dimensional array of floating point numbers
* `A`: An array of floating point numbers

##### Returns:
* Returns the standard deviation of a vector A. The algorithm will return
an estimator of the generative distribution's standard deviation under the
assumption that each entry of v is an IID drawn from that generative
distribution. This computation is equivalent to calculating \\
sqrt(sum((v - mean(v)).^2) / (length(v) - 1)). NaN values are ignored.
* Returns the standard deviation of all elements in the array,
ignoring NaN's. The algorithm returns an estimator of the
generative distribution's standard deviation under the assumption
that each non-NaN entry of v is an IID drawn from that generative
distribution. This computation is equivalent to calculating \\
`sqrt(sum((A - mean(A)).^2) / (length(A) - 1))` \\
while skipping NaN values.

##### Examples:
```julia
using NaNMath
NaNMath.std([1., 2., NaN]) # result: 0.7071067811865476
```
"""
function std(x::Vector{T}) where T<:AbstractFloat
function std(x::AbstractArray{T}) where T<:AbstractFloat
return sqrt(var(x))
end

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ end
@test NaNMath.mean([1., 2., NaN]) == 1.5
@test NaNMath.mean([1. 2.; NaN 3.]) == 2.0
@test NaNMath.var([1., 2., NaN]) == 0.5
@test NaNMath.var([1. 2.; NaN 3.]) == 1.0
@test NaNMath.std([1., 2., NaN]) == 0.7071067811865476
@test NaNMath.std([1. 2.; NaN 3.]) == 1.0

@test NaNMath.median([1.]) == 1.
@test NaNMath.median([1., NaN]) == 1.
Expand Down
Loading