Fork me on GitHub

随笔 - 211  文章 - 59  评论 - 38  阅读 - 41万 

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分布

αα,ββ代表先验,都等于1,则是uniform,相等情况下,越大,则概率为0.5的后验概率越大。均值为α(α+β)α(α+β)

 

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   huashiyiqike  阅读(3651)  评论(1编辑  收藏  举报
编辑推荐:
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
阅读排行:
· 为DeepSeek添加本地知识库
· .NET程序员AI开发基座:Microsoft.Extensions.AI
· 精选4款基于.NET开源、功能强大的通讯调试工具
· 数据不出内网:基于Ollama+OneAPI构建企业专属DeepSeek智能中台
· 大模型工具KTransformer的安装
点击右上角即可分享
微信分享提示