Python:如何从非标准柯西分布中随机抽样,从而使用不同的参数?

Python: how to randomly sample from nonstandard Cauchy distribution, hence with different parameters?

我在看这里:numpy

而且我可以看到您可以使用命令 np.random.standard_cauchy() 指定一个数组,从标准 Cauchy 中采样。

我需要从可能具有 x_0 != 0gamma != 1 的 Cauchy 中采样,即可能不在原点,也不具有等于 1 的比例。

我该怎么做?

如果你有 scipy,你可以使用 scipy.stats.cauchy,它需要一个位置 (x0) 和一个比例 (gamma) 参数。它公开了 rvs 方法来抽取随机样本:

x = stats.cauchy.rvs(loc=100, scale=2.5, size=1000)  # draw 1000 samples

您可以避免依赖 SciPy,因为 Cauchy 分布是 location-scale family 的一部分。这意味着,如果您从 Cauchy(0, 1) 中抽取样本 x,只需将其移动 x_0 并与 gamma 相乘,x' = x_0 + gamma * x 将根据 Cauchy(x_0, gamma).