带有参数的自定义PDF,并带有Scipy的支持

发布于 2025-01-21 19:43:52 字数 1174 浏览 0 评论 0原文

我想使用RV_Continouul类构建自定义PDF;这样的PDF应取决于几个参数,l1,l2,l3在下面的示例中。这是我的尝试:

k=1
class estimated_pdf(scipy.stats.rv_continuous):


    def _get_support(self, l1, l2, l3):
         self.a=0
         self.b=np.inf
    

    def _pdf(self, x, l1, l2, l3):
        return np.exp(-l1*x/k-l2*x**2/k-l3*x**3/k)


    def Z(self, l1, l2, l3):
        return quad(self._pdf, 0, np.inf, args=(l1, l2, l3))

    # edited
    def my_freeze(self, l1, l2, l3):
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3


p = estimated_pdf()
# p.__call__(l1=1, l2=2, l3=3)
p.my_freeze(l1=1, l2=2, l3=3)

我会遇到以下问题:

  1. “ _get_support()采用1个位置参数,但给出了4个。 我不确定如何处理它
  2. 支持不应取决于参数,因此,如果我评论_get_support部分, :“ _parse_args()缺少3所必需的位置参数:'l1','l2'和''和' l3'“

我不知道发生了什么,1。不开心,因为我给它l1,l2,l3参数看起来2。不开心,因为我没有给它。

编辑:我意识到使用__调用__freeze会将我的对象再次变为rv_continuul对象,而不是estectated_pdf对象,因此我添加了我自己的my_freeze 解决该问题的功能。 我还将l1,l2,l3参数添加到_Get_Support。目前,没有提出错误,但是通过检查支持,我知道这是完整的真实行,即dafault值,因此_get> _get_support仍然不起作用。

I want to build a custom pdf using the rv_continuous class; such a pdf should depend on a couple of parameters, l1, l2, l3 in the example below. This is my attempt:

k=1
class estimated_pdf(scipy.stats.rv_continuous):


    def _get_support(self, l1, l2, l3):
         self.a=0
         self.b=np.inf
    

    def _pdf(self, x, l1, l2, l3):
        return np.exp(-l1*x/k-l2*x**2/k-l3*x**3/k)


    def Z(self, l1, l2, l3):
        return quad(self._pdf, 0, np.inf, args=(l1, l2, l3))

    # edited
    def my_freeze(self, l1, l2, l3):
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3


p = estimated_pdf()
# p.__call__(l1=1, l2=2, l3=3)
p.my_freeze(l1=1, l2=2, l3=3)

I get the following problems:

  1. " _get_support() takes 1 positional argument but 4 were given "; the support should not depend on the parameters, so I'm not sure how to handle this
  2. if I comment out the _get_support part I get instead: " _parse_args() missing 3 required positional arguments: 'l1', 'l2', and 'l3' "

I have no clue what's happening, 1. is not happy because I gave it the l1, l2, l3 parameters while it looks like 2. is not happy becasue I didn't give it.

Edit: I realised that using __call__ or freeze would change my object to be again a rv_continuous object instead of a estimated_pdf object, so I added my own my_freeze function to solve that issue.
I also added the l1, l2, l3 parameters to the _get_support. Right now no errors are raised, but by checking the support, I get that it's the full real line, i.e. the dafault value, so _get_support still isn't working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-01-28 19:43:52

_get_support有望接收与PDF相同的形状参数,此处l1 l2,l3

IIUC,如果支撑是实际的半轴,并且不依赖于L1,L2,L3,则您无需明确定义get_support。只是省略它。

PS:您的片段还有很多其他问题。 k在PDF中不确定; Z方法不会返回任何内容。

编辑:喜欢:

In [8]: from numpy import exp

In [9]: class my_distr_gen(rv_continuous):
   ...:     def _get_support(self, l1, l2, l3):
   ...:         return 0, float('inf')
   ...:     def _pdf(self, x, l1, l2, l3):
   ...:         return exp(-l1*x - l2*x**2 - l3*x**3)  # NB: normalize!
   ...: 

In [10]: my_distr = my_distr_gen()

In [11]: my_distr.pdf(1, l1=1, l2=2, l3=3)
Out[11]: 0.0024787521766663585

In [12]: my_distr.pdf(-1, l1=1, l2=2, l3=3)
Out[12]: 0.0

_get_support is expected to receive the same shape arguments as the PDF, here l1 l2, l3.

IIUC, if the support is the real semi axis, and does not depend on l1, l2, l3, you do not need to explicitly define get_support. Just omit it.

PS : Your snippet has a bunch of other issues. k is undefined in the PDF; Z method does not return anything.

EDIT: Like so:

In [8]: from numpy import exp

In [9]: class my_distr_gen(rv_continuous):
   ...:     def _get_support(self, l1, l2, l3):
   ...:         return 0, float('inf')
   ...:     def _pdf(self, x, l1, l2, l3):
   ...:         return exp(-l1*x - l2*x**2 - l3*x**3)  # NB: normalize!
   ...: 

In [10]: my_distr = my_distr_gen()

In [11]: my_distr.pdf(1, l1=1, l2=2, l3=3)
Out[11]: 0.0024787521766663585

In [12]: my_distr.pdf(-1, l1=1, l2=2, l3=3)
Out[12]: 0.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文