Skip to content

Commit ad3af92

Browse files
authored
Merge pull request #76 from JuliaDynamics/tsallis_entropy
Tsallis entropy
2 parents 02fbdcc + a612c0a commit ad3af92

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# CHANGELOG
2+
23
Changelog is kept with respect to version 0.11 of Entropies.jl.
34

5+
## 1.3
6+
* Introduce Tsallis entropy.
7+
8+
## 1.2
9+
* Added dispersion entropy.
10+
411
## 1.1
512
* Introduce convenience function `permentropy`.
613
* Several type instabilities fixed.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Entropies"
22
uuid = "ed8fcbec-b94c-44b6-89df-898894ad9591"
33
authors = "Kristian Agasøster Haaga <[email protected]>, George Datseries <[email protected]>"
44
repo = "https://github.com/juliadynamics/Entropies.jl.git"
5-
version = "1.2.1"
5+
version = "1.3.0"
66

77
[deps]
88
DelayEmbeddings = "5732040d-69e3-5649-938a-b6b4f237613f"

docs/src/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The main **API** of this package is contained in two functions:
1313
* [`probabilities`](@ref) which computes probability distributions of given datasets
1414
* [`genentropy`](@ref) which uses the output of [`probabilities`](@ref), or a set of
1515
pre-computed [`Probabilities`](@ref), to calculate entropies.
16+
* [`tsallisentropy`](@ref) which uses the output of [`probabilities`](@ref), or a set of
17+
pre-computed [`Probabilities`](@ref), to calculate Tsallis entropies.
1618

1719
These functions dispatch on subtypes of [`ProbabilitiesEstimator`](@ref), which are:
1820

@@ -34,6 +36,7 @@ ProbabilitiesEstimator
3436

3537
```@docs
3638
Entropies.genentropy
39+
Entropies.tsallisentropy
3740
```
3841

3942
## Fast histograms

src/Entropies.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ module Entropies
88
include("wavelet/wavelet.jl")
99
include("nearest_neighbors/nearest_neighbors.jl")
1010
include("dispersion/dispersion_entropy.jl")
11+
12+
include("tsallis/tsallis.jl")
1113
end

src/tsallis/tsallis.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export tsallisentropy
2+
3+
"""
4+
tsallisentropy(x::Probabilities; k = 1, q = 0)
5+
6+
Compute the Tsallis entropy of `x` (Tsallis, 1998)[^Tsallis1988].
7+
8+
```math
9+
S_q(\\bf p) = \\dfrac{k}{q - 1}\\left(1 - \\sum_{p_i \\in \\bf p} \\right)
10+
```
11+
12+
[^Tsallis1988]: Tsallis, C. (1988). Possible generalization of Boltzmann-Gibbs statistics. Journal of statistical physics, 52(1), 479-487.
13+
"""
14+
function tsallisentropy(prob::Probabilities; k = 1, q = 0)
15+
haszero = any(iszero, prob)
16+
p = if haszero
17+
i0 = findall(iszero, prob.p)
18+
# As for genentropy, we copy because if someone initialized Probabilities with 0s,
19+
# they'd probably want to index the zeros as well.
20+
deleteat!(copy(prob.p), i0)
21+
else
22+
prob.p
23+
end
24+
25+
return k/(q-1)*(1 - sum(prob .^ q))
26+
end

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,9 @@ end
376376
@test typeof(de) <: Real
377377
@test de >= 0.0
378378
end
379+
380+
@testset "Tsallis" begin
381+
p = Probabilities(repeat([1/5], 5))
382+
@assert round(tsallisentropy(p, q = -1/2, k = 1), digits = 2) 6.79
383+
end
379384
end

0 commit comments

Comments
 (0)