我想创建一个通用随机变量,其分布取决于某个条件,然后根据保留的分布生成一个随机变量序列。例如,
if (condition):
RV = np.random.normal
""" The above command will assign the standard normal distribution to RV.
I do not know how to assign the normal distribution with mean $m$ and variance sigma^2.
"""
else:
RV = np.random.exponential
""" Similar problem here.
I do not know how to assign the shape parameter of the exponential distribution.
"""
""" generate a sample of size 10 from the distribution a
"""
for i in range(0,9):
RV.next() # I would like a generic function "next" to generate the next number in the sequence
如果满足条件,上面的代码将创建具有标准正态分布的随机变量,否则它将为RV指定具有单位形状参数的指数数字生成器。
我想知道如何使用与默认赋值不同的均值(或分布的其他参数)初始化RV。
答案 0 :(得分:1)
使用scipy.stats。例如。
import scipy.stats
r = scipy.stats.uniform() # or other distribution chosen dynamically
random_range = r.rvs(size = 10)
答案 1 :(得分:0)
所以,从像Haskell这样的语言来看,部分功能就是如此。基本上,部分函数是一个函数...其中一些args
预先填充。
Python还支持部分函数,版本2.5及更高版本:https://docs.python.org/2/library/functools.html#functools.partial
from functools import partial
if (condition):
RV = partial(np.random.normal, loc=avg, scale=stdev)
else:
RV = partial(np.random.exponential, scale=scale)