Skip to content

Commit

Permalink
Merge pull request #3 from mileslucas/ml/allnan
Browse files Browse the repository at this point in the history
fix case where no input is finite
  • Loading branch information
mileslucas authored Mar 11, 2024
2 parents 574ebd7 + e992c01 commit ae11a28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
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 <[email protected]> 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

2 comments on commit ae11a28

@mileslucas
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/102643

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.1 -m "<description of version>" ae11a28522175182f227f5495497e1d261dfc51d
git push origin v1.0.1

Please sign in to comment.