I was experimenting with tensorflow-probability (tfp). I wanted to check, if the density of a Normal-distribution in tfp does integrate (sum) to one. I thought the following calculation inside the print should give me rougly 1, but instead I got 714.2143.
import tensorflow_probability as tfp
tfd = tfp.distributions
x = np.linspace(-7., 7., int(1e4), dtype=np.float64)
print(tf.reduce_sum( np.array( [tfd.Normal(loc=0, scale=1).prob(y) for y in x] )))
Output: tf.Tensor(714.2143, shape=(), dtype=float32)
What am I missing here?
If you want to calculate to area under the curve, which is integrating the pdf here, you need to divide by number of samples and multiply the length of support:
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
tfd = tfp.distributions
num_samples = 1000
min_val = -7
max_val = 7
x = np.linspace(min_val, max_val,
num_samples,
dtype=np.float64)
dist = tfd.Normal(loc=0, scale=1)
normalized_vals = np.array([dist.prob(y) for y in x])/ num_samples * (max_val-(min_val))
print(tf.reduce_sum(normalized_vals)) # 0.99899995