定义样本空间(用于运行简单的统计模拟)的简单API
项目描述
…是一个非常轻量级的Python API,用于模拟样本空间、事件、随机变量和(条件)分布。
示例
查看iPython笔记本或阅读以下内容
from sample_space import *
class NCoinTosses(Experiment):
def __init__(self, n, p):
self.n = n
self.p = p
def rerun(self):
self.tosses = [Bern(self.p) for _ in range(self.n)]
def heads(self):
return sum(self.tosses)
def there_are_at_least_two_heads(self):
return self.heads() >= 2
def first_toss_heads(self):
return self.tosses[0]
space = SampleSpace(NCoinTosses(10, 0.5), iters=20000)
# ask for probability of any truthy method
print(' P(#H>=2):', space.probability_that('there_are_at_least_two_heads'))
# alias for the above, if it's more grammatical
print(' P(H1):', space.probability_of('first_toss_heads'))
# change the number of iterations
print(' P(H1), 1K iters:', space.probability_of('first_toss_heads', iters=1000))
# ask for probabilities of functions of random variables
print(' P(#H>5):', space.probability_that(['heads', is_greater_than(5)]))
# ask for conditional probabilities
print(' P(#H>5|H1):', space.probability_that(['heads', is_greater_than(5)], given=['first_toss_heads']))
print(' P(H1|#H>5):', space.probability_of('first_toss_heads', given=[['heads', is_greater_than(5)]]))
print(' P(#H>5|H1,H>=2):', space.probability_that(['heads', is_greater_than(5)],
given=['first_toss_heads', 'there_are_at_least_two_heads']))
# ask for expectations, variances, and moments, conditionally or absolutely
print(' E(#H):', space.expected_value_of('heads'))
print(' E(#H|H1):', space.expected_value_of('heads', given=['first_toss_heads']))
print(' Var(#H):', space.variance_of('heads'))
print(' Var(#H|H1):', space.variance_of('heads', given=['first_toss_heads']))
print('1st moment of #H:', space.nth_moment_of('heads', 1))
print('2nd moment of #H:', space.nth_moment_of('heads', 2))
print('3rd moment of #H:', space.nth_moment_of('heads', 3))
print('4th moment of #H:', space.nth_moment_of('heads', 4))
print(' Skewness of #H:', space.nth_moment_of('heads', 3, central=True, normalized=True), '(using nth_moment_of w/ central=True, normalized=True)')
print(' Skewness of #H:', space.skewness_of('heads'), '(using skewness_of)')
print(' Kurtosis of #H:', space.kurtosis_of('heads'))
# some plots
fig = plt.figure(figsize=(14,3))
# plot distribution histograms
fig.add_subplot(121)
space.plot_distribution_of('heads') # pass kwargs
plt.legend()
# plot conditional distribution histograms
fig.add_subplot(122)
space.plot_distribution_of('heads', given=['first_toss_heads'], bins=10) # can pass kwargs
plt.legend()
plt.show()
这将输出(加上一些图表)
P(#H>=2): 0.98975 P(H1): 0.502 P(H1), 1K iters: 0.48 P(#H>5): 0.37665 P(#H>5|H1): 0.5076294006183305 P(H1|#H>5): 0.6580109757729888 P(#H>5|H1,H>=2): 0.49361831442463533 E(#H): 4.9983 E(#H|H1): 5.48924623116 Var(#H): 2.4486457975 Var(#H|H1): 2.31806506582 1st moment of #H: 4.99245 2nd moment of #H: 27.5097 3rd moment of #H: 163.13055 4th moment of #H: 1015.54155 Skewness of #H: -0.00454435802967 (using nth_moment_of w/ central=True, normalized=True) Skewness of #H: 0.00414054522343 (using skewness_of) Kurtosis of #H: 2.78225928171
为什么?
主要是为了避免在统计模拟中的错误/减少模板代码,以检查家庭作业的解决方案。但也可以更好地理解概率论。
样本空间是概率论中的一个核心概念。它们封装了重复进行带有随机结果的实验的想法。几乎所有重要的统计量——事件的可能性,或随机变量的任何时刻——都是相对于样本空间定义的。因此,如果您试图编程有意义的模拟(并且如果您更关心表达性而不是性能),您最好通过显式定义一个来组织您的代码。
安装/使用
首先运行
pip install sample_space
导入库。然后定义一个响应rerun(self)的Experiment子类。 rerun应执行一个随机实验并将一个或多个基本结果存储为实例变量。如果您想定义更复杂的事件或随机变量,您可以将它们表示为实例方法。
然后,使用您自己的Experiment实例初始化一个SampleSpace。您可以查询样本空间以获取事件probability_that/probability_of的概率,或者查询其distribution_of、expected_value_of、variance_of、skewness_of、kurtosis_of或nth_moment_of随机变量(这也可以简单地是一个事件,在这种情况下,它将被解释为指示符)。最后,对于这些方法中的任何一个,您都可以传递一个包含事件列表的given关键字参数,这将使您获得的结果条件于所有这些事件的发生。幕后,SampleSpace将只是rerun您的实验10000次并平均您的随机变量或计算事件发生的频率(条件性)。您可以将iters关键字参数传递给任何方法或传递给SampleSpace.__init__以增加迭代次数。
要引用事件或随机变量,传递实验的实例变量或实例方法的字符串名称,或者传递一个包含变量/方法名称和lambda函数的数组。例如
space = SampleSpace(CoinTossExperiment(10))
space.probability_that('first_toss_is_heads')
space.probability_that(['n_heads', lambda h: h > 5])
space.expected_value_of('n_heads')
space.expected_value_of('n_heads', given=['first_toss_is_heads'])
space.probability_that('first_toss_is_heads, given=[['n_heads', lambda h: h > 3], 'last_toss_is_heads'])
此外,sample_space定义了一些有用的lambda返回方法(is_greater_than(x)、is_less_than(x)、is_at_least(x)、is_at_most(x)、equals(x))以方便使用。当然,您也可以在您的Experiment上定义实例方法以实现相同的目标。
该库还公开了一些基本的采样函数(Bern(p)、Bin(n,p)、RandomSign(p)和Categ(categories, weights))以协助定义实验。
轻量版
如果您不想定义完整的Experiment类,您也可以仅定义一个返回布尔值或数字的随机事件/随机变量函数,并调用probability_that/expected_value_of
import sample_space as ss
def weighted_coin_flip_is_heads(p=0.4):
return ss.Bern(p)
def n_weighted_heads(n=100, p=0.4):
return sum(weighted_coin_flip_is_heads(p) for _ in range(n))
print(ss.probability_that(weighted_coin_flip_is_heads))
print(ss.probability_that(lambda: weighted_coin_flip_is_heads(0.5))
print(ss.expected_value_of(n_weighted_heads))
print(ss.expected_value_of(lambda: n_weighted_heads(200, 0.3)))
许可协议
项目详细信息
sample_space-0.2.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 0471c6fcf5c1f0b5abe764ea651382d159278e7b7eef8a21fccaca527643578e |
|
MD5 | c6e40bbca72e85234c6c855788941146 |
|
BLAKE2b-256 | 191be464c47a65f6b7abe1029a4083c97fadbb995ceac4a2ba565b681a6eff92 |