Skip to content

Commit

Permalink
Add histvar, fix histstd
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Jun 28, 2024
1 parent ba90c2d commit c8d39ef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NaNStatistics"
uuid = "b946abbf-3ea7-4610-9019-9858bfdeaf2d"
authors = ["C. Brenhin Keller"]
version = "0.6.37"
version = "0.6.38"

[deps]
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Expand Down
37 changes: 34 additions & 3 deletions src/Histograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function histcounts(x, xedges::AbstractRange; T=Int64)
end
histcounts(x, xmin::Number, xmax::Number, nbins::Integer; T=Int64) = histcounts(x, range(xmin, xmax, length=nbins+1); T=T)


"""
```julia
histmean(counts, bincenters)
Expand Down Expand Up @@ -67,9 +68,10 @@ function histmean(counts, bincenters)
end
export histmean


"""
```julia
histstd(counts, bincenters; corrected::Bool=true)
histvar(counts, bincenters; corrected::Bool=true)
```
Estimate the standard deviation of the data represented by a histogram,
specified as `counts` in equally spaced bins centered at `bincenters`.
Expand All @@ -88,11 +90,11 @@ julia> counts = histcounts(randn(10000), binedges);
julia> bincenters = (binedges[1:end-1] + binedges[2:end])/2
-9.995:0.01:9.995
t
julia> histstd(counts, bincenters)
julia> histvar(counts, bincenters)
0.9991854064196424
```
"""
function histstd(counts, bincenters; corrected::Bool=true)
function histvar(counts, bincenters; corrected::Bool=true)
N = ∅ₙ = zero(eltype(counts))
Σ == zero(Base.promote_op(*, eltype(counts), eltype(bincenters)))
@inbounds @simd ivdep for i in eachindex(counts, bincenters)
Expand All @@ -109,6 +111,35 @@ function histstd(counts, bincenters; corrected::Bool=true)
end
return Σ / max(N-corrected, ∅ₙ)
end
export histvar


"""
```julia
histstd(counts, bincenters; corrected::Bool=true)
```
Estimate the standard deviation of the data represented by a histogram,
specified as `counts` in equally spaced bins centered at `bincenters`.
If `counts` have been normalized, or represent an analytical estimate of a PDF
rather than a histogram representing counts of a dataset, Bessel's correction
to the standard deviation should likely not be performed - i.e., set the
`corrected` keyword argument to `false`.
## Examples
```julia
julia> binedges = -10:0.01:10;
julia> counts = histcounts(randn(10000), binedges);
julia> bincenters = (binedges[1:end-1] + binedges[2:end])/2
-9.995:0.01:9.995
t
julia> histstd(counts, bincenters)
0.999592620230683
```
"""
histstd(counts, bincenters; corrected::Bool=true) = sqrt(histvar(counts, bincenters; corrected))
export histstd


Expand Down
14 changes: 11 additions & 3 deletions test/testHistograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,23 @@

## --- Statistics on histograms

a = randn(10000)
a = randn(10000) * 1.2
binedges = -10:0.1:10
bincenters = (binedges[1:end-1] + binedges[2:end])/2
h = histcounts(a, binedges)
@test histmean(h, bincenters) nanmean(a) atol = 0.1
@test histstd(h, bincenters) nanstd(a) atol = 0.1
@test histmean(h, bincenters) nanmean(a) atol = 0.02
@test histvar(h, bincenters) nanvar(a) atol = 0.02
@test histstd(h, bincenters) nanstd(a) atol = 0.02

n = pdf.(Normal(0,1), bincenters)
@test histmean(n, bincenters) 0 atol = 1e-6
@test histstd(n, bincenters, corrected=false) 1 atol = 1e-6

binedges = -20:0.1:20
bincenters = (binedges[1:end-1] + binedges[2:end])/2
n = pdf.(Normal(1,2), bincenters)
@test histmean(n, bincenters) 1 atol = 1e-6
@test histvar(n, bincenters, corrected=false) 4 atol = 1e-3
@test histstd(n, bincenters, corrected=false) 2 atol = 2e-6

## --- End of File

0 comments on commit c8d39ef

Please sign in to comment.