Scilab 中用于计算几何离散随机变量的累积分布函数的程序问题

Issue with program in Scilab for calculating the cumulative distribution function of a geometric discrete random variable

我编写了一个程序,显示具有几何分布的随机离散变量的累积分布函数和 PMF 的图形。但是我遇到了一个问题:PMF的所有概率之和不是1,而是非常接近1.When的东西我看到了这个,我切换到Matlab,在那里我使用了函数:geocdf。因此,我观察到 CDF 的第一个值,取 p 0.6 和 n = 10,是 0.84 而不是预期的 0.6。你能帮我找出我的程序有什么问题吗?这是我用 Scilab 编写的脚本:

   n = input('n = '); 
tab = zeros(2, n);
p = input('p = ');//probability
q = 1 - p;

for k = 1:n   
    tab(1,k) = k;
    tab(2,k) = (q^(k - 1))*p;  
end

subplot(1,2,1);
plot(tab(1,:), tab(2,:), "-");


F = cumsum(tab(2, :));//cumulative distribution
subplot(1,2,2);
plot2d2(tab(1,:), F);

disp([tab' F']);
Mean = 1/p;
Variance = q/(p^2);

mprintf('\nMedia = %g, Dispersia = %g', Mean, Variance);

The sum of all the probabilities of PMF is not 1

您不可能将 所有 概率相加,因为几何分布将非零概率分配给所有正整数。如果你 运行 总和达到 n=10,则概率总和明显小于 1。如果你 运行 它达到 20,则输出中的累积值四舍五入为 1:

1.     0.6          0.6        
2.     0.24         0.84       
3.     0.096        0.936      
4.     0.0384       0.9744     
5.     0.01536      0.98976    
6.     0.006144     0.995904   
7.     0.0024576    0.9983616  
8.     0.0009830    0.9993446  
9.     0.0003932    0.9997379  
10.    0.0001573    0.9998951  
11.    0.0000629    0.9999581  
12.    0.0000252    0.9999832  
13.    0.0000101    0.9999933  
14.    0.0000040    0.9999973  
15.    0.0000016    0.9999989  
16.    0.0000006    0.9999996  
17.    0.0000003    0.9999998  
18.    0.0000001    0.9999999  
19.    4.123D-08    1.0000000  
20.    1.649D-08    1.0000000  

the first value of CDF, taking p 0.6 and n = 10, 0.84 and not 0.6 as expected

几何分布有两个版本,正如Wikipedia在文章开头所解释的那样。您的代码遵循第一个约定,集合 1,2,3,.... 支持 PMF。Matlab 的 geocdf 使用第二个约定,PMF 支持 0,1,2,3... 输出geocdf(1,0.6)的输出为0.84,代表0和1的概率之和。geocdf(0,0.6)的输出为0.6。