我正在试验张量流概率 (tfp)。我想检查 tfp 中正态分布的密度是否整合(求和)为一。我认为印刷品中的以下计算应该给我 rougly 1,但我得到的是 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] )))
输出:tf.Tensor(714.2143, shape=(), dtype=float32)
我在这里错过了什么?
如果你想计算曲线下的面积,也就是在这里整合pdf,你需要除以样本数并乘以支撑长度:
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