numpy.random.normal(-2.5,0.1,1000) is a sample from the normal distribution. These are just 1000 numbers in random order. The documentation for entropy says:
pk[i] is the (possibly unnormalized) probability of event i .
So, to get the nickname for the result, you need the numbers to be βalignedβ so that the same indices correspond to the same positions in the distribution. In your example, t1[0] not related to t2[0] . Your example does not contain any direct information about how likely each value is, what you need for the KL divergence; it just gives you some actual values ββthat were taken from the distribution.
The easiest way to get alignment values ββis to evaluate the probability density function of the distribution for some fixed set of values. To do this, you need to use scipy.stats.norm (which leads to the fact that the distribution object can be controlled in various ways) instead of np.random.normal (which returns only selective values). Here is an example:
t1 = stats.norm(-2.5, 0.1) t2 = stats.norm(-2.5, 0.1) t3 = stats.norm(-2.4, 0.1) t4 = stats.norm(-2.3, 0.1) # domain to evaluate PDF on x = np.linspace(-5, 5, 100)
Then:
>>> stats.entropy(t1.pdf(x), t2.pdf(x)) -0.0 >>> stats.entropy(t1.pdf(x), t3.pdf(x)) 0.49999995020647586 >>> stats.entropy(t1.pdf(x), t4.pdf(x)) 1.999999900414918
You can see that as the distributions move farther apart, their divergence KL increases. (In fact, using your second example will give the KL divergence of inf , because they overlap so little.)
source share