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

时间:2015-05-04 19:53:20

标签: probability scilab cumulative-sum

I wrote a program that shows the graph of cumulative distribution function and PMF of a random discrete variable with geometric distribution. But I've encountered a problem: The sum of all the probabilities of PMF is not 1, but something very close to 1.When I saw this, I switched to Matlab, where I used the function: geocdf. Thus, I observed that the first value of CDF, taking p 0.6 and n = 10, is 0.84 and not 0.6 as expected. Can you, please, help me find out what's wrong with my program ? Here's my script written in 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);

1 个答案:

答案 0 :(得分:1)

  

PMF的所有概率之和不是1

您无法添加全部概率,因为几何分布会将非零概率分配给所有正整数。如果运行总和达到n = 10,则概率总和明显小于1.如果将其运行到20,则累计在输出中四舍五入为:

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  
  

CDF的第一个值,取p 0.6和n = 10,0.84,而不是预期的0.6

有两种版本的几何分布,正如Wikipedia在本文开头所述。您的代码遵循第一个约定,在1,2,3上支持PMF,.... Matlab' s geocdf使用第二个约定,在0,1,2,3上支持PMF。 .. geocdf(1,0.6)的输出为0.84,表示概率0和1的总和。geocdf(0,0.6)的输出为0.6。