polsys / ennemi

Easy Nearest Neighbor Estimation of Mutual Information
https://polsys.github.io/ennemi/
MIT License
35 stars 12 forks source link

Is there an option to choose the unit of mutual info? #109

Closed yzhang511 closed 1 year ago

yzhang511 commented 2 years ago

Is there an option to choose the unit of mutual info, e.g. bits vs. nats? what's the units of the MI being computed by this package? i couldn't find it in the documentation.

thanks :))

polsys commented 2 years ago

Hi, Thank you for the question! I'll try to improve the documentation on this :)

ennemi returns results always in nats. To convert to bits, you can divide by log(2). E.g.

import numpy as np
from ennemi import estimate_entropy

data = np.arange(256)
print(f"{estimate_entropy(data, discrete=True)} nats")
print(f"{estimate_entropy(data, discrete=True) / np.log(2)} bits")

prints

5.545177444479562 nats
8.0 bits

I did consider adding a parameter for the unit, but there are too many parameters already 😄 It would make sense to include the above example in the docs (e.g. the What is entropy? page) to explain how to convert between the two. I'll do this once I have a bit more time.