简体   繁体   English

从另一个 class 的方法动态创建 scipy.stats 分布

[英]Dynamically create scipy.stats distribution from methods of another class

I have created a class which represents a statistical simulation.我创建了一个代表统计模拟的 class。 One aspect of that is a distribution of p -values, and the class contains methods characterizing that distribution:其中一个方面是p值的分布,并且 class 包含表征该分布的方法:

class Simulation:
    ...
    def pdf_p(self, p):
        ...
    def cdf_p(self, p):
        ...
    def ppf_p(self, P):
        ...
    def rvs_p(self, size):
        ...

I would now like to expose that distribution also as a scipy.stats -style distribution object.我现在想将该发行版也公开为scipy.stats风格的发行版 object。 To do so, the __init__ method of my class contains a statement为此,我的 class 的__init__方法包含一条语句

self.p = PValueDist(self.pdf_p, self.cdf_p, self.ppf_p, self.rvs_p)

where the class PValueDist is defined as其中 class PValueDist定义为

from scipy.stats import rv_continuous

class PValueDist (rv_continuous):
    def __init__(self, pdf, cdf, ppf, rvs):
        self._pdf = pdf
        self._cdf = cdf
        self._ppf = ppf
        self._rvs = rvs
        super().__init__(self)

This seems to work, but I'm wondering whether it is the right or canonical way to do it?这似乎可行,但我想知道这是否是正确规范的方式?

Of course I could also make Simulation a subclass of rv_continuous and rename my methods.当然,我也可以使Simulation成为rv_continuous的子类并重命名我的方法。 However, the simulation comprises several different distributions, and identifying the simulation with one of them doesn't seem semantically correct.然而,模拟包含几个不同的分布,用其中一个来识别模拟在语义上似乎不正确。

Seems OK.似乎还可以。 Several things to watch out for: 1) scipy.stats distributions are instances, so don't forget to create one, 2) if the support is not a default ((0, inf) IIRC), set them in the__init__.需要注意的几件事:1)scipy.stats 分发是实例,所以不要忘记创建一个,2)如果支持不是默认的((0,inf)IIRC),请将它们设置在__init__中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM