带有参数的自定义PDF,并带有Scipy的支持
我想使用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)
我会遇到以下问题:
- “ _get_support()采用1个位置参数,但给出了4个。 我不确定如何处理它
- 支持不应取决于参数,因此,如果我评论_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:
- " _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
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_get_support
有望接收与PDF相同的形状参数,此处l1 l2,l3
。IIUC,如果支撑是实际的半轴,并且不依赖于L1,L2,L3,则您无需明确定义
get_support
。只是省略它。PS:您的片段还有很多其他问题。
k
在PDF中不确定; Z方法不会返回任何内容。编辑:喜欢:
_get_support
is expected to receive the same shape arguments as the PDF, herel1 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: