子类化 scipy 的连续分布 [英] Subclassing scipy's continuous distributions
问题描述
我正在研究如何创建一个新的 scipy.stats.rv_continuous
子类.我的分布取决于位置"和形状"参数,但是 scipy.stats.distributions
中 _pdf
的每个示例都假定形状 &位置参数可以简单地应用于 X 轴,但某些分布并非如此.
I'm trying to figure out how to create a new scipy.stats.rv_continuous
subclass. My distribution depends on "location" and "shape" parameters, but every example of a _pdf
in scipy.stats.distributions
assumes that the shape & location parameters can simply be applied to the X-axis, which is not the case for some distributions.
例如,我正在使用的一个分布是对数正态分布的修改版本,其中 X 轴位置明确取决于分布的宽度,即:
For example, one distribution I'm working with is a modified version of the lognormal in which the X-axis location explicitly depends on the width of the distribution, i.e.:
def _pdf(self, x, x0, s):
Px = exp(-(log(x/x0)+s**2/2.)**2 / (2*s**2))
return Px / (s*x0*sqrt(2*pi))
我希望能够对 x0
使用 loc
,对 s
使用 scale
.有没有办法做到这一点,或者有没有更好的方法来子类rv_continuous
?
I'd like to be able to use loc
for x0
and scale
for s
. Is there any way to do this, or is there a better way to subclass rv_continuous
?
(请注意,简单地使用我定义的 PDF 会导致其他 rv_continuous
方法出现问题,例如 .fit
,因为 loc
> 和 scale
仍然被视为自由参数",即使它们不应该)
(note that simply using the PDF as I've defined it leads to problems in other rv_continuous
methods, e.g. .fit
, since loc
and scale
are still treated as "free parameters" even though they should not be)
推荐答案
你真的不应该试图改变 loc
和 scale
的含义.它们是标准的、定义明确的位置和比例参数,例如在 scipy.stats 教程,在维基百科这里和这里,还有这里.
You really shouldn't try to change the meaning of loc
and scale
. They are the standard, well-defined location and scale parameters as explained, for example, in the scipy.stats tutorial, on wikipedia here and here, and also here.
正如我在评论中提到的,您的公式中似乎缺少一个 1/x0
因子.没有它,PDF 从 0 到无穷大的积分是 x0
,而不是 1.
As I mentioned in a comment, it looks like you are missing a factor of 1/x0
in your formula. Without it, the integral from 0 to infinity of your PDF is x0
, not 1.
经过修正,很明显x0
实际上是比例参数.s
是一个形状参数.像许多其他仅定义在正实轴上的分布(例如伽马或对数正态分布)一样,您可以简单地忽略位置参数——其默认值为 0.(如果您使用 fit
方法,请务必使用参数 floc=0
,以防止该方法将 loc
视为自由参数.)但是,我不确定您所说的"X 轴位置明确取决于分布的宽度"--what 的 X 轴位置?
With the correction, it is clear that x0
is actually the scale parameter. s
is a shape parameter. Like many other distributions only defined on the positive real axis (e.g. gamma or log-normal), you can simply ignore the location parameter--its default value is 0. (If you use the fit
method, be sure to use the argument floc=0
, to prevent the method from treating loc
as a free parameter.) However, I'm not sure what you mean by "the X-axis location explicitly depends on the width of the distribution"--the X-axis location of what?
这篇关于子类化 scipy 的连续分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!