% File name: bionmial_dist.m

% Generate random numbers having the binomial

% distribution.

% Two plots are produced. One on left is the

% plot of numbers that result.

% One on right is a histogram.

n=1000; %number of samples produced

x=rand(1,n); %generate n uniformly distributed

N = 50;

p = 0.1;

q = 1 - p;

CDF(1) = q^N;

for k = 2:50

prob(k) = nchoosek(N,k) * p^k * q^(N-k);

CDF(k) = prob(k)+CDF(k-1);

end

% Generate Poisson-distributed random numbers

% using the inversion method.

for i = 1:n

temp=find(CDF >= x(i));

if size(temp,2) == 0

y(i) = 0;

else

y(i) = temp(1)-1;

end

end

subplot(1,2,1)

% Plot the values of all the numbers generated

plot(y,'k');%plot the resulting data

box off

axis square

xlabel('Sample index')

ylabel('Random number value')

% Plot a histogram of the data to view the pdf

%x = 0:0.1:1;

subplot(1,2,2)

hist(y)

box off

axis square

xlabel('Bins')

ylabel('Bin count')

 


Go back to book Homepage Go back to book's matlab Homepage Go back to book's Chapter 1 Homepage

Copyright © 2001 Northstar Digital Design, Inc.