Fork me on GitHub

Gamma分布

http://cos.name/2013/01/lda-math-gamma-function/ 神奇的Gamma函数

其中 α 称为 shape parameter, 主要决定了分布曲线的形状;而β 称为 rate parameter 或者inverse scale parameter (1/β 称为scale parameter),主要决定曲线有多陡。

x=0:0.01:10;
color=['b','g','r','k','c','y','m']
a=ones(length(x),10);
% for i=1:10
%     a(:,i)=gampdf(x,i,1);
%     legend(num2str(i))
%     hold on
% end
% plot(x,a)
% b=num2str(1:10)
% legend(b(1),b(5),b(9),b(13),b(17),b(21),b(25),b(29),b(33),b(36:37))
for i=1:7
    subplot(3,1,1)
    plot(x,gampdf(x,i,1),color(i))
    hold on
end
t=[]
for i=1:7
    t=[t;strcat('\beta=',num2str(i))];
end
legend(t)

for i=1:7
    subplot(3,1,2)
    plot(x,gampdf(x,1,i),color(i))
    hold on
end
t=[]
for i=1:7
    t=[t;strcat('\alpha=',num2str(i))];
end
legend(t)

for i=1:7
    subplot(3,1,3)
    plot(x,gampdf(x,i,i),color(i))
    hold on
end
t=[]
for i=1:7
    str=sprintf('\\alpha=%d,\\beta=%d',i,i);
    t=[t;str];
end
legend(t)

Beta分布

$\alpha$,$\beta$代表先验,都等于1,则是uniform,相等情况下,越大,则概率为0.5的后验概率越大。均值为$\frac{\alpha}{  (\alpha+\beta)}$

 

Dirichlet分布生成随机数

程序,摘自topictoolbox,
function r = drchrnd(a,n)
% take a sample from a dirichlet distribution
p = length(a);
r = gamrnd(repmat(a,n,1),1,n,p);
r = r ./ repmat(sum(r,2),1,p);

这样生成的r就是服从dirichlet distribution的样本。就是这么简单,
a= drchrnd([1 1 1],10)

a =

    0.0703 0.4272 0.5025
    0.8037 0.0126 0.1837
    0.3358 0.5767 0.0875
    0.8500 0.0952 0.0549
    0.0230 0.5806 0.3964
    0.4690 0.0569 0.4741
    0.4175 0.1979 0.3846
    0.2218 0.6157 0.1624
    0.5576 0.0332 0.4091
    0.1335 0.7247 0.1418

topic toolbox中,之所以可以这么写,充分利用了dirichlet distribution和gamma分布之间的关系。经过推导可以证明,dirichlet distribution可以看作是多个gamma(ai,1)的乘积(包括除)。同时利用了gamma的分布的一个重要性质,xi~gamma(ai,b)分布,则sum(xi)~gamma(sum(ai),b)分布。

 

 

posted on 2013-08-02 10:54  huashiyiqike  阅读(3525)  评论(1编辑  收藏  举报