-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tjlane
committed
Aug 11, 2024
1 parent
d35bb1a
commit 537eb2f
Showing
10 changed files
with
25 additions
and
661 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Empty file.
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,26 +1,21 @@ | ||
import numpy as np | ||
from scipy.stats import differential_entropy | ||
|
||
from . import mask | ||
|
||
|
||
def negentropy(X): | ||
def negentropy(samples: np.ndarray, tolerance: float = 1e-4) -> float: | ||
""" | ||
Return negentropy (float) of X (numpy array) | ||
""" | ||
|
||
# negetropy is the difference between the entropy of samples x | ||
# and a Gaussian with same variance | ||
# http://gregorygundersen.com/blog/2020/09/01/gaussian-entropy/ | ||
|
||
std = np.std(X) | ||
# neg_e = np.log(std*np.sqrt(2*np.pi*np.exp(1))) - differential_entropy(X) | ||
neg_e = 0.5 * np.log(2.0 * np.pi * std**2) + 0.5 - differential_entropy(X) | ||
# assert neg_e >= 0.0 + 1e-8 | ||
|
||
return neg_e | ||
|
||
The negetropy is defined as the difference between the entropy of a distribution | ||
and a Gaussian with same variance. | ||
citation: | ||
http://gregorygundersen.com/blog/2020/09/01/gaussian-entropy/ | ||
""" | ||
|
||
std = np.std(samples.flatten()) | ||
neg_e = 0.5 * np.log(2.0 * np.pi * std**2) + 0.5 - differential_entropy(samples.flatten()) | ||
if not neg_e >= -tolerance: | ||
raise ValueError(f"negentropy is a relatively big negative number {neg_e} that exceeds the tolerance {tolerance} -- something may have gone wrong") | ||
|
||
return neg_e |
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ authors = [ | |
{ name = "Thomas Lane", email = "[email protected]" } | ||
] | ||
dependencies = [ | ||
"numpy", | ||
"scipy", | ||
"skimage", | ||
"reciprocalspaceship", | ||
] | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
|
||
import numpy as np | ||
from meteor import validate | ||
|
||
def test_negentropy_gaussian() -> None: | ||
n_samples = 100 | ||
samples = np.random.normal(size=n_samples) | ||
negentropy = validate.negentropy(samples) | ||
|
||
# negentropy should be small for a Gaussian sample | ||
assert np.abs(negentropy) < 1e-5 |