Scipy中的分布缩放和平移在Python中实现

huangapple go评论77阅读模式
英文:

Scaling and Shifting Scipy Distributions in Python

问题

我正在使用 scipy.stats.beta 来生成Beta分布的概率密度函数。

您可以在标准区间[0,1]中看到预期的结果:
Scipy中的分布缩放和平移在Python中实现

我想在区间[-1,1]上创建分布。对于 a, b = 0.5, 0.5,人们会期望“U”形曲线以均值为0分布在[-1,1]。然而,当 std = 1mean = 0 时,我得到了以下结果:
Scipy中的分布缩放和平移在Python中实现

现在,如果我设置 mean = -1std = 2,那么我会接近所期望的结果:
Scipy中的分布缩放和平移在Python中实现

为什么概率密度函数在使用标准差和均值输入时没有按预期进行缩放/居中?我可能没有理解分布函数的控制。

from scipy.stats import beta
import matplotlib.pyplot as plt

fig,ax = plt.subplots(nrows=1, ncols=1)

#x = np.linspace(beta.ppf(0.01, a, b),beta.ppf(0.99, a, b), 100)
x = np.arange(-1.0,1.0,0.01)

a, b = 0.5,0.5 #2.31, 0.627
mean = -1
std = 2
#mean, var, skew, kurt = beta.stats(a, b, moments='mvsk')
pdf = beta.pdf(x, a, b, loc=mean, scale=std)

ax.plot(x, pdf,'r-', lw=5, alpha=0.6, label='beta pdf')

mean = np.nanmean(pdf[np.isfinite(pdf)]) 
std = np.nanstd(pdf[np.isfinite(pdf)])
英文:

I am working with scipy.stats.beta to generate a Beta distribution probability density function.

You can see the expected result for the standard interval of [0,1] here:
Scipy中的分布缩放和平移在Python中实现

I would like to create the distribution on the interval [-1,1] instead. For a,b = 0.5, 0.5 one would expect the "u" shaped curve to distribute with the mean at 0 and ending at [-1,1]. However, with std = 1 and mean = 0 I get the following:
Scipy中的分布缩放和平移在Python中实现

Now, if I set mean = -1 and std = 2 then I get close to what the desired result is:
Scipy中的分布缩放和平移在Python中实现

Why is the PDF not scaling / centering as expected using the standard deviation and mean inputs? I must not be understanding the controls for the distribution function.

from scipy.stats import beta
import matplotlib.pyplot as plt

fig,ax = plt.subplots(nrows=1, ncols=1)

#x = np.linspace(beta.ppf(0.01, a, b),beta.ppf(0.99, a, b), 100)
x = np.arange(-1.0,1.0,0.01)

a, b = 0.5,0.5 #2.31, 0.627
mean = -1
std = 2
#mean, var, skew, kurt = beta.stats(a, b, moments='mvsk')
pdf = beta.pdf(x, a, b, loc=mean, scale=std)

ax.plot(x, pdf,'r-', lw=5, alpha=0.6, label='beta pdf')

mean = np.nanmean(pdf[np.isfinite(pdf)]) 
std = np.nanstd(pdf[np.isfinite(pdf)])

答案1

得分: 1

文档中:

上面的概率密度定义采用了“标准化”形式。要进行偏移和/或缩放分布,请使用loc和scale参数。具体来说,beta.pdf(x, a, b, loc, scale)y = (x - loc) / scale 完全等价。

所以当您使用 scale = 1loc = 0 时,您实际上得到的是分布 beta.pdf(x, a, b),您可以验证这一点。

您得到第一个图形的原因是,您试图在包含零的 x 域上绘制Beta分布,而 Beta(0, 0.5, 0.5) 趋向于无穷大。

请注意,您在图的右侧看不到分布爆炸,因为您的 x.max() == 0.99

英文:

From the docs:

> The probability density above is defined in the “standardized” form. To shift and/or scale the distribution use the loc and scale parameters. Specifically, beta.pdf(x, a, b, loc, scale) is identically equivalent to beta.pdf(y, a, b) / scale with y = (x - loc) / scale.

So when you are using scale = 1 and loc = 0, you are simply getting the distribution beta.pdf(x, a, b), which you can really verify.

The reason why you get the first plot is that you are trying to plot a Beta distribution over an x domain that contains zero and Beta(0, 0.5, 0.5) tends to Infinity.

Note that you don't see the distribution exploding at the right side of your plot because your x.max() == 0.99

答案2

得分: 1

locscale参数 不是 分布的均值和标准差。它们是平移和缩放参数;参见,例如“位置-尺度家族”。对于贝塔分布,loc是支持区间的左端,scale是其长度。

英文:

The parameters loc and scale are not the mean and standard deviation of the distribution. They are translation and scaling parameters; see, e.g. "Location-scale family". For the beta distribution, loc is the left end of the support interval, and scale is its length.

huangapple
  • 本文由 发表于 2023年6月16日 12:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定