numpy.random.Generator.dirichlet

方法

random.Generator.dirichlet(alpha, size=None)

从Dirichlet分布中提取样本。

size Dirichlet分布的维数k的样本。Dirichlet分布的随机变量可以看作Beta分布的多元推广。狄里克莱分布是贝叶斯推理中多项式分布的共轭先验。

参数
alpha浮点数序列,长度k

分布参数(长度 k 长度样品 k

sizeint或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n) 然后 m * n * k 抽取样品。默认值为“无”,在这种情况下为长度向量 k 返回。

返回
samples恩达雷

提取的样品,形状 (size, k) .

加薪
ValueError

如果有任何值 alpha 小于或等于零

笔记

Dirichlet分布是向量上的分布 x 符合条件的 x_i>0\sum_{{i=1}}^k x_i = 1 .

概率密度函数 p 一类Dirichlet分布随机向量的性质 X 与…成比例

System Message: WARNING/2 (p(x)\propto\prod{i=1}^{k}{x^{\alpha{i-1}}u i},)

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...i=1}^{k}{x^{\alpha{i-1}}u i},\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 564 bytes). Transcript written on math.log.

在哪里? \alpha 是包含正浓度参数的向量。

该方法使用以下属性进行计算:let Y 是一个随机向量,其分量遵循标准伽马分布,然后 X = \frac{{1}}{{\sum_{{i=1}}^k{{Y_i}}}} Y 狄利克雷分布吗

工具书类

1

David McKay,“信息理论、推理和学习算法”,第23章,http://www.interrunce.org.uk/mackay/itila/

2

维基百科,“dirichlet distribution”,https://en.wikipedia.org/wiki/dirichlet_distribution

实例

以维基百科中引用的一个例子为例,如果要将字符串(每个初始长度为1.0)切割成具有不同长度的k个片段,则可以使用这种分布,其中每个片段平均具有指定的平均长度,但允许片段的相对大小发生一些变化。

>>> s = np.random.default_rng().dirichlet((10, 5, 3), 20).transpose()
>>> import matplotlib.pyplot as plt
>>> plt.barh(range(20), s[0])
>>> plt.barh(range(20), s[1], left=s[0], color='g')
>>> plt.barh(range(20), s[2], left=s[0]+s[1], color='r')
>>> plt.title("Lengths of Strings")
../../../_images/numpy-random-Generator-dirichlet-1.png