从任何分布中抽取随机样本

时间:2016-10-26 13:21:56

标签: python sampling

我想从任何给定的分布中抽取随机样本。如果我按照gulp.task('json-merge',['json'], function(){ return gulp.src(directories.src + '/json-combined/**/*.json') var file = directories.src + '/json-combined/**/*.json' var pageName = path.basename(file, path.extname(file)) .pipe(jsonMerge({ fileName: pageName + '-combined.json', edit: function(parsedJson, file) { var component = path.basename(path.dirname(file.path)); var editedJson = {}; editedJson[camelCase(component)] = parsedJson; return editedJson; } })) .pipe(gulp.dest(paths.json.dest)) }); 文档,我可以从相对容易的"中提取样本。定义的函数,如

rv_continuous

其中class gaussian(ss.rv_continuous): def _pdf(self, x, mu, sigma, const): chi2 = ((x - mu)/sigma)**2 un_normalised = np.exp(-0.5 * chi2) return un_normalised/const 只是使用constquad找到的规范化常量。现在,我有

simps

我必须找到class omegaDistribution(ss.rv_continuous): def _pdf(self, x, phi, const): chi2 = np.zeros(len(x)) for i in range(len(x)): chi2[i] = np.dot((b - sin([x[i], phi[0]])/sigma).T, (b - sin([x[i], phi[0]]))/sigma) pdf_omega = np.exp(-0.5 * chi2) * omega_prior.pdf(x) return pdf_omega/const 的分布(在上面的代码中用omega表示)。我得到了x,如下所示:

enter image description here 但如果我试图绘制随机样本,它会说:

pdf

。 。 。

Traceback (most recent call last):   File "non_linear.py", line 84, in <module>
    samples = omega_dist.rvs(phi = 0.02, const = norm_const, size = 1); print samples   File "/Users/Harry/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py", line 950, in rvs
    vals = self._rvs(*args)   File "/Users/Harry/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py", line 885, in _rvs
    Y = self._ppf(U, *args)   File "/Users/Harry/anaconda/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py", line 898, in _ppf

请注意,分布不一定是高斯分布。另一种方法是采用一种接受拒绝抽样方法来抽取随机样本,但 chi2 = np.zeros(len(x))TypeError: object of type 'float' has no len() 似乎是一种更为通用的方法。

我的采样方式如下:

rv_continuous

0 个答案:

没有答案