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

fix case where no input is finite #3

Merged
merged 1 commit into from
Mar 11, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BiweightStats"
uuid = "5bf8a1e9-d5f8-4697-9608-80edd97af0ad"
authors = ["Miles Lucas <mdlucas@hawaii.edu> and contributors"]
version = "1.0.0"
version = "1.0.1"

[deps]
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand Down
10 changes: 7 additions & 3 deletions src/BiweightStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end
Creates an iterator based on the biweight transform.[^1] This iterator will first filter all input data so that only finite values remain. Then, the iteration will progress using a custom state, which includes a flag to indicate whether the value is within the cutoff, which is `c` times the median-absolute-deviation (MAD). The MAD is based on the deviation from `M`, which will default to the median of `X` if `M` is `nothing`.

!!! note "Advanced usage"

This transform iterator is used for the internal calculations in `BiweightStats.jl`, which is why it has a somewhat complicated iterator implementation.

# Examples
Expand Down Expand Up @@ -106,6 +106,10 @@ julia> (d, u2, flag), _ = iterate(bt, 10)
"""
function BiweightTransform(X; c=9, M=nothing)
data = filter(isfinite, X)
# if no valid input return NaN
if isempty(data)
return BiweightTransform(NaN, NaN, NaN)
end
if isnothing(M)
med = median(data)
else
Expand Down Expand Up @@ -312,7 +316,7 @@ Computes biweight midcovariance between the two vectors. If only one vector is p
```

!!! warning

`NaN` and `Inf` cannot be removed in the covariance calculation, so if they are present the returned value will be `NaN`. To prevent this, consider imputing values for the non-finite data.

# Examples
Expand Down Expand Up @@ -372,7 +376,7 @@ midcov(X; kwargs...) = midvar(X; kwargs...)
Computes the variance-covariance matrix using the biweight midcovariance. By default, each column is a separate variable, so an `(M, N)` matrix with `dims=1` will create an `(N, N)` covariance matrix. If `dims=2`, though, each row will become a variable, leading to an `(M, M)` covariance matrix.

!!! warning

`NaN` and `Inf` cannot be removed in the covariance calculation, so if they are present the returned value will be `NaN`. To prevent this, consider imputing values for the non-finite data.

# Examples
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ rng = StableRNG(1123)
val = location([1, 2, 3, v, 2])
@test val ≈ 2 atol = 1e-5
end

# All NaN
val = location([NaN, NaN, NaN])
@test isnan(val)
end

@testset "scale" begin
Expand Down Expand Up @@ -81,6 +85,10 @@ rng = StableRNG(1123)
val = midvar([1, 2, 3, v, 2])
@test val ≈ 0.55472 atol = 1e-5
end

# All NaN
val = midvar([NaN, NaN, NaN])
@test isnan(val)
end

@testset "midcov" begin
Expand Down
Loading