我试图定义一个自定义的分布与pdf给定通过scipy.stats公司在
import numpy as np
from scipy.stats import rv_continuous
class CustomDistribution(rv_continuous):
def __init__(self, pdf=None):
super(CustomDistribution, self).__init__()
self.custom_pdf = pdf
print "Initialized!"
def _pdf(self, x, *args):
if self.custom_pdf is None:
# print 'PDF is not overridden'
return super(CustomDistribution, self)._pdf(x, *args)
else:
# print 'PDF is overridden'
return self.custom_pdf(x)
def g(x, mu):
if x < 0:
return 0
else:
return mu * np.exp(- mu * x)
my_exp_dist = CustomDistribution(pdf=lambda x: g(x, .5))
print my_exp_dist.mean()
如您所见,我试图定义指数分布,参数mu=0.5,但输出如下。在
Initialized!
D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:
IntegrationWarning: The algorithm does not converge. Roundoff error is detected in the extrapolation table. It is assumed that the requested tolerance cannot be achieved, and that the returned result (if full_output = 1) is the best which can be obtained.
warnings.warn(msg, IntegrationWarning)D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:
IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
2.0576933609
If increasing the limit yields no improvement it is advised to analyze the integrand in order to determine the difficulties. If the position of a local difficulty can be determined (singularity, discontinuity) one will probably gain from splitting up the interval and calling the integrator on the subranges. Perhaps a special-purpose integrator should be used. warnings.warn(msg, IntegrationWarning)
我该怎么做才能改善这一点?在
注:计算精度问题在this GitHub issue中讨论。在
这似乎是你想要的。每次创建该类的实例时,都必须为lambda参数指定一个值。rv_continuous足够聪明,可以推断出你没有提供的物品,但你可以,当然,提供更多的定义,我在这里。在
相关问题 更多 >
编程相关推荐