-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from Autoplectic/entropy_triangle
Entropy triangle
- Loading branch information
Showing
8 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
The entropy triangle, from [Valverde-Albacete, Francisco Jose, and Carmen | ||
Pelaez-Moreno. "The Multivariate Entropy Triangle and Applications." Hybrid | ||
Artificial Intelligent Systems. Springer International Publishing, 2016. | ||
647-658]. | ||
""" | ||
|
||
from ..distribution import BaseDistribution | ||
from ..distconst import product_distribution, uniform_like | ||
from ..multivariate import entropy, residual_entropy | ||
|
||
class EntropyTriangle(object): | ||
""" | ||
BaseProfile | ||
Static Attributes | ||
----------------- | ||
left_label : str | ||
The label for the bottom axis when plotting. | ||
right_label : str | ||
The label for the right axis when plotting. | ||
bottom_label : str | ||
The label for the bottom axis when plotting. | ||
Attributes | ||
---------- | ||
dists : [Distribution] | ||
points : list of tuples | ||
Methods | ||
------- | ||
draw | ||
Plot the entropy triangle. | ||
""" | ||
|
||
left_label = "$\operatorname{R}[\mathrm{dist}]$" | ||
right_label = "$\operatorname{T}[\mathrm{dist}] + \operatorname{B}[\mathrm{dist}]$" | ||
bottom_label = "$\Delta \operatorname{H}_{\Pi_\overline{X}}$" | ||
|
||
def __init__(self, dists): | ||
""" | ||
Initialize the entropy triangle. | ||
Parameters | ||
---------- | ||
dists : [Distribution] or Distribution | ||
The list of distributions to plot on the entropy triangle. If a single distribution is provided, it alone will be computed. | ||
""" | ||
if isinstance(dists, BaseDistribution): | ||
self.dists = [dists] | ||
else: | ||
self.dists = dists | ||
self.points = [self._compute_point(dist) for dist in self.dists] | ||
|
||
@staticmethod | ||
def _compute_point(dist): | ||
""" | ||
Compute the deviation from uniformity, dependence, and independence of a | ||
distribution. | ||
Parameters | ||
---------- | ||
dist : Distribution | ||
The distribution to compute values for. | ||
""" | ||
H_U = entropy(uniform_like(dist)) | ||
H_P = entropy(product_distribution(dist)) | ||
|
||
Delta = H_U - H_P | ||
VI = residual_entropy(dist) | ||
M = H_P - VI | ||
|
||
return (Delta/H_U, M/H_U, VI/H_U) | ||
|
||
def draw(self, ax=None, setup=True, marker='o', color='k'): | ||
""" | ||
Plot the entropy triangle. | ||
Parameters | ||
---------- | ||
ax : Axis or None | ||
The matplotlib axis to plot on. If none is provided, one will be constructed. | ||
setup : bool | ||
If true, labels, tick marks, gridlines, and a boundary will be added to the plot. Defaults to True. | ||
marker : str | ||
The matplotlib marker shape to use. | ||
color : str | ||
The color of marker to use. | ||
""" | ||
import ternary | ||
|
||
if ax is None: | ||
fig, ax = ternary.figure() | ||
fig.set_size_inches(8, 8) | ||
else: | ||
ax = ternary.TernaryAxesSubplot(ax=ax) | ||
|
||
if setup: | ||
ax.boundary() | ||
ax.gridlines(multiple=0.1) | ||
|
||
fontsize = 20 | ||
ax.set_title("Entropy Triangle", fontsize=fontsize) | ||
ax.left_axis_label(self.left_label, fontsize=fontsize) | ||
ax.right_axis_label(self.right_label, fontsize=fontsize) | ||
ax.bottom_axis_label(self.bottom_label, fontsize=fontsize) | ||
|
||
ax.ticks(axis='lbr', multiple=0.1, linewidth=1) | ||
ax.clear_matplotlib_ticks() | ||
|
||
ax.scatter(self.points, marker=marker, color=color) | ||
|
||
return ax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
Cython | ||
cvxopt | ||
ipython | ||
matplotlib | ||
numdifftools | ||
python-ternary | ||
scipy >= 0.15.0 |