<p>我正在尝试为我从数据生成/估计的某些pdf计算<code>E[f(x)]</code>。在</p>
<p>文件中说:</p>
<blockquote>
<p>Subclassing</p>
<p>New random variables can be defined by subclassing rv_continuous class
and re-defining at least the _pdf or the _cdf method (normalized to
location 0 and scale 1) which will be given clean arguments (in
between a and b) and passing the argument check method.</p>
<p>If positive argument checking is not correct for your RV then you will
also need to re-define the _argcheck method.</p>
</blockquote>
<p>因此,我子类化并定义了\u pdf,但每当我尝试调用:</p>
<p><code>print my_continuous_rv.expect(lambda x: x)</code></p>
<p>西皮冲我大喊大叫:</p>
<pre><code>AttributeError: 'your_continuous_rv' object has no attribute 'a'
</code></pre>
<p>这是有意义的,因为我猜它是在试图求出积分的下限,因为它也会打印出错误:</p>
^{pr2}$
<p>我尝试将属性self.a和self.b定义为(我认为这是定义rv的限制/间隔):</p>
<pre><code>self.a = float("-inf")
self.b = float("inf")
</code></pre>
<p>然而,当我这么做的时候,它会抱怨说:</p>
<pre><code>if N > self.numargs:
AttributeError: 'your_continuous_rv' object has no attribute 'numargs'
</code></pre>
<p>我不太确定numargs应该是什么,但是在检查了scipy在github上的代码后,发现有以下代码行:</p>
<pre><code>if not hasattr(self, 'numargs'):
# allows more general subclassing with *args
self.numargs = len(shapes)
</code></pre>
<p>这就是我的函数要取的随机变量的形状。在</p>
<p>目前我只做了一个非常简单的随机变量,一个浮点数作为它的可能值。所以我决定将numargs硬编码为1。但这只会让夏比更大声地喊叫。在</p>
<p>因此,归根结底,我认为从文档中我不清楚当我对它进行子类化时我必须做些什么,因为我做了他们说的,重写了pdf,但在完成之后,它要求我输入self.a,我硬编码了它,然后它要求我输入numargs,在这一点上,我想我的结论是,我真的不知道他们要我如何子类,房车连续。有人知道吗?我可以从我想要拟合的数据中生成我想要的pdf,然后从pdf中得到期望值和类似的东西,我还需要在rv_continuous中初始化什么才能使它实际工作?在</p>