MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   确定随机变量的概率质量函数 (https://www.labfans.com/bbs/showthread.php?t=24071)

poster 2019-12-10 20:48

确定随机变量的概率质量函数
 
如果我们有一个离散的随机变量x,并且在X(n)中有关于它的数据,那么在matlab中我们如何确定概率质量函数pmf(X)?



[B]回答:[/B]

您可以至少以八种不同的方式执行此操作(其他解决方案中已经提到了其中一些)。

假设我们有一个离散随机变量的样本:

X = randi([-9 9], [100 1]); 考虑这些等效的解决方案(请注意,我对可能值的范围不作任何假设,只是它们是整数):

[V,~,labels] = grp2idx(X); mx = max(V); %# TABULATE (internally uses HIST) t = tabulate(V); pmf1 = t(:, 3) ./ 100; %# HIST (internally uses HISTC) pmf2 = hist(V, mx)' ./ numel(V); %#' %# HISTC pmf3 = histc(V, 1:mx) ./ numel(V); %# ACCUMARRAY pmf4 = accumarray(V, 1) ./ numel(V); %# SORT/FIND/DIFF pmf5 = diff( find( [diff([0;sort(V)]) ; 1] ) ) ./ numel(V); %# SORT/UNIQUE/DIFF [~,idx] = unique( sort(V) ); pmf6 = diff([0;idx]) ./ numel(V); %# ARRAYFUN pmf7 = arrayfun(@(x) sum(V==x), 1:mx)' ./ numel(V); %#' %# BSXFUN pmf8 = sum( bsxfun(@eq, V, 1:mx) )' ./ numel(V); %#' 请注意,使用GRP2IDX来获取从1开始的索引,该索引对应于pmf的条目(映射由labels给出)。上面的结果是:

>> [labels pmf] ans = -9 0.03 -8 0.07 -7 0.04 -6 0.07 -5 0.03 -4 0.06 -3 0.05 -2 0.05 -1 0.06 0 0.05 1 0.04 2 0.07 3 0.03 4 0.09 5 0.08 6 0.02 7 0.03 8 0.08 9 0.05

[url=https://stackoverflow.com/questions/4068403]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:30

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.