I am trying to estimate the entropy of Random Variables (RVs), which involves a calculation of step: p_X * log(p_X). For example,
import numpy as np X = np.random.rand(100) binX = np.histogram(X, 10)[0] #create histogram with 10 bins p_X = binX / np.sum(binX) ent_X = -1 * np.sum(p_X * np.log(p_X)) Sometimes p_X shall be zero which mathematically make the whole term as zero. But python makes p_X * np.log(p_X) as NaN and makes the whole summation as NaN. Is there any way to manage (without any explicit checking for NaN) making p_X * np.log(p_X) to give zero whenever p_X is zero? Any insight and correction is appreciated and Thanks in advance:)
scipy.special.xlogy?