Clear Filters
Clear Filters

poissrnd(7,100,1)

3 views (last 30 days)
Nicole
Nicole on 30 May 2023
Edited: Nicole on 30 May 2023
r=poissrnd(7,100,1)
tabulate(r)
This produces a 3 column table, the percent is not CDF. Is there an easy way to create a 4th column of the CDF?
Thanks

Answers (1)

Shaik mohammed ghouse basha
Edited: Shaik mohammed ghouse basha on 30 May 2023
Hello,
As per my understanding of the question you have generated random numbers from poisson distribution with rate parameter (lambda) 7 in a 100 X 1 array. After generating you tabulated them which shows three columns consisting of value, count, percent. Third column shows the percent of each value and not CDF. Now you want to display CDF as the fourth column.
CDF measures probability of a random variable taking on a specific value or a value less than or equal to a given value. For a value the CDF is the sum of all individual probabilities of values less than or equal to given value.
In order to get this we first calculate the probabilities of each value and then add them in a way similar to prefix sum array.
lambda = 7;
x = 100;
y = 1;
r = poissrnd(lambda, x, y);
% r has the random numbers generated from poisson distribution.
T = tabulate(r);
% T stores value, its count, its frequency.
P = T(:, 2)/sum(T(:, 2));
%P is a column vector which is made by dividing the count of each value
%with sum of all counts. This is nothing but probability of each value
cdf = cumsum(P);
%cumsum is used to add the probabilities similar to prefix sum. For ith
%element in cdf we add all values form 1 to i in P
T = [T, cdf];
%add generated cdf as a column to T
disp(T);
1.0000 0 0 0 2.0000 1.0000 1.0000 0.0100 3.0000 5.0000 5.0000 0.0600 4.0000 5.0000 5.0000 0.1100 5.0000 18.0000 18.0000 0.2900 6.0000 15.0000 15.0000 0.4400 7.0000 19.0000 19.0000 0.6300 8.0000 13.0000 13.0000 0.7600 9.0000 10.0000 10.0000 0.8600 10.0000 8.0000 8.0000 0.9400 11.0000 2.0000 2.0000 0.9600 12.0000 1.0000 1.0000 0.9700 13.0000 1.0000 1.0000 0.9800 14.0000 0 0 0.9800 15.0000 1.0000 1.0000 0.9900 16.0000 1.0000 1.0000 1.0000
If you want fourth column to be CDF percent instead of probability, you can just multiply 100 when P column vector is calculated.
For further reference of functions used in code you can go throguh these links:

Tags

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!