Skip to content

Commit

Permalink
Merge f74619e into d7f2e07
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon committed Feb 28, 2019
2 parents d7f2e07 + f74619e commit f9f42f6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/scalarstats.jl
Expand Up @@ -35,12 +35,12 @@ function genmean(a, p::Real)
if p == 0
return geomean(a)
end

# At least one of `x` or `p` must not be an int to avoid domain errors when `p` is a negative int.
# We choose `x` in order to exploit exponentiation by squaring when `p` is an int.
r = mean(a) do x
float(x)^p
end
end
return r^inv(p)
end

Expand Down Expand Up @@ -435,8 +435,9 @@ zscore(X::AbstractArray{<:Real}, dim::Int) = ((μ, σ) = mean_and_std(X, dim); z
"""
entropy(p, [b])
Compute the entropy of a collection of probabilities `p`, optionally specifying a real number
`b` such that the entropy is scaled by `1/log(b)`.
Compute the entropy of a collection of probabilities `p`,
optionally specifying a real number `b` such that the entropy is scaled by `1/log(b)`.
Elements with probability 0 or 1 add 0 to the entropy.
"""
entropy(p) = -sum(pᵢ -> iszero(pᵢ) ? zero(pᵢ) : pᵢ * log(pᵢ), p)

Expand Down
1 change: 0 additions & 1 deletion test/scalarstats.jl
Expand Up @@ -119,7 +119,6 @@ it = (xᵢ for xᵢ in x)

@test entropy([0.5, 0.5],2) 1.0
@test entropy([0.2, 0.3, 0.5], 2) 1.4854752972273344
@test entropy([0.2, 0.3, 0.5], 2) 1.4854752972273344
@test entropy([1.0, 0.0]) 0.0

##### Renyi entropies
Expand Down

0 comments on commit f9f42f6

Please sign in to comment.