-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add the perplexity #33
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from .shannon import entropy, conditional_entropy, mutual_information | ||
from .total_correlation import total_correlation | ||
from .coinformation import coinformation | ||
from .perplexity import perplexity | ||
from .jsd import jensen_shannon_divergence | ||
from .common_info import common_information | ||
from .lattice import insert_join, insert_meet |
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,47 @@ | ||
""" | ||
A version of the entropy with signature common to the other multivariate | ||
measures. | ||
""" | ||
|
||
from .shannon import conditional_entropy, entropy | ||
from ..utils.misc import flatten | ||
|
||
def entropy2(dist, rvs=None, crvs=None, rv_names=None): | ||
""" | ||
Parameters | ||
---------- | ||
dist : Distribution | ||
The distribution from which the entropy is calculated. | ||
rvs : list, None | ||
The indexes of the random variable used to calculate the entropy. If | ||
None, then the entropy is calculated over all random variables. | ||
crvs : list, None | ||
The indexes of the random variables to condition on. If None, then no | ||
variables are condition on. | ||
rv_names : bool | ||
If `True`, then the elements of `rvs` are treated as random variable | ||
names. If `False`, then the elements of `rvs` are treated as random | ||
variable indexes. If `None`, then the value `True` is used if the | ||
distribution has specified names for its random variables. | ||
|
||
Returns | ||
------- | ||
H : float | ||
The entropy. | ||
""" | ||
if dist.is_joint(): | ||
if rvs is None: | ||
# Set to entropy of entire distribution | ||
rvs = list(range(dist.outcome_length())) | ||
rv_names = False | ||
else: | ||
# this will allow inputs of the form [0, 1, 2] or [[0, 1], [2]], | ||
# allowing uniform behavior with the mutual information like | ||
# measures. | ||
rvs = set(flatten(rvs)) | ||
if crvs is None: | ||
crvs = [] | ||
else: | ||
return entropy(dist) | ||
|
||
return conditional_entropy(dist, rvs, crvs, rv_names) |
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,49 @@ | ||
""" | ||
The perplexity of a distribution. | ||
""" | ||
|
||
from .shannon import conditional_entropy, entropy | ||
from ..utils.misc import flatten | ||
|
||
def perplexity(dist, rvs=None, crvs=None, rv_names=None): | ||
""" | ||
Parameters | ||
---------- | ||
dist : Distribution | ||
The distribution from which the perplexity is calculated. | ||
rvs : list, None | ||
The indexes of the random variable used to calculate the perplexity. | ||
If None, then the perpelxity is calculated over all random variables. | ||
crvs : list, None | ||
The indexes of the random variables to condition on. If None, then no | ||
variables are condition on. | ||
rv_names : bool | ||
If `True`, then the elements of `rvs` are treated as random variable | ||
names. If `False`, then the elements of `rvs` are treated as random | ||
variable indexes. If `None`, then the value `True` is used if the | ||
distribution has specified names for its random variables. | ||
|
||
Returns | ||
------- | ||
P : float | ||
The perplexity. | ||
""" | ||
|
||
base = dist.get_base(numerical=True) if dist.is_log() else 2 | ||
|
||
if dist.is_joint(): | ||
if rvs is None: | ||
# Set to entropy of entire distribution | ||
rvs = list(range(dist.outcome_length())) | ||
rv_names = False | ||
else: | ||
# this will allow inputs of the form [0, 1, 2] or [[0, 1], [2]], | ||
# allowing uniform behavior with the mutual information like | ||
# measures. | ||
rvs = set(flatten(rvs)) | ||
if crvs is None: | ||
crvs = [] | ||
else: | ||
return base**entropy(dist) | ||
|
||
return base**conditional_entropy(dist, rvs, crvs, rv_names) |
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,44 @@ | ||
from __future__ import division | ||
|
||
from nose.tools import * | ||
|
||
from dit import (ScalarDistribution as SD, | ||
Distribution as D) | ||
from dit.algorithms import perplexity as P | ||
from six.moves import range | ||
|
||
|
||
def test_p1(): | ||
for i in range(2, 10): | ||
assert_almost_equal(P(SD([1/i]*i)), i) | ||
|
||
def test_p2(): | ||
for i in range(2, 10): | ||
d = SD([1/i]*i) | ||
d.set_base(i) | ||
assert_almost_equal(P(d), i) | ||
|
||
def test_p3(): | ||
for i in range(2, 10): | ||
d = D([str(_) for _ in range(i)], [1/i]*i) | ||
assert_almost_equal(P(d), i) | ||
|
||
def test_p4(): | ||
for i in range(2, 10): | ||
d = D([str(_) for _ in range(i)], [1/i]*i) | ||
d.set_base(i) | ||
assert_almost_equal(P(d), i) | ||
|
||
def test_p5(): | ||
d = D(['00', '01', '10', '11'], [1/4]*4) | ||
assert_almost_equal(P(d), 4) | ||
assert_almost_equal(P(d, [0]), 2) | ||
assert_almost_equal(P(d, [1]), 2) | ||
assert_almost_equal(P(d, [0], [1]), 2) | ||
assert_almost_equal(P(d, [1], [0]), 2) | ||
|
||
def test_p6(): | ||
d = D(['00', '11'], [1/2]*2) | ||
assert_almost_equal(P(d), 2) | ||
assert_almost_equal(P(d, [0], [1]), 1) | ||
assert_almost_equal(P(d, [1], [0]), 1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!