The Poisson distribution and binomial distribution converge when the number of trials is large and the probability of success is small. Write the MATLAB function poissonPMF.m (without using MATLAB's built-in statistics functions). Your function should take n, lambda, and a vector x (representing a vector of integers [0, 1, 2, ..., n] where each value in this vector is evaluated) as input parameters and return a vector representing the pmf values for each value in x. Using your binomialPMF.m, comment on the following observations for both binomial and Poisson distributions using the following parameters:
- n=10, p=0.5
- n=20, p=0.25
- n=30, p=0.1
- n=50, p=0.01
- n=100, p=0.001
function pmf = binomial_pmf(x, n, p)
% BINOMIAL_PMF: Binomial probability mass function.
% pmf = BINOMIAL_PMF(x, n, p) returns the binomial probability mass
% function with parameters n and p at the values in x.
% (typically x is a vector of integers 0, 1, 2, ..., n)
%
% The size of "pmf" is a vector of the same length as x.
% Error-checking
if nargin < 3
error('binomial_pmf: Too few inputs', 'Requires 3 input arguments');
end
% Initialize the pmf vector to zero
pmf = zeros(length(x), 1);
for i = 1:length(x)
k = x(i); % select the ith value in the x vector
%.... you now compute log p(k) as follows - we will compute the value
% of pk in the log domain to avoid computing very small or very large
% numbers that would cause underflow or overflow.
%....you will need to fill in the rest of details below......
%.... first use nchoosek.m to compute the binomial coefficient "n choose k"
%.... and take the log of this to begin computing log p(k)
%... use "help nchoosek" at the MATLAB prompt to find out what it does
logpk = log(nchoosek(n, k));
%....now compute the rest of the expression for log p(k)
% where we need to compute log(p^k) and log[(1-p)^(n-k)]
logpk = logpk + log(p^k) + log((1 - p)^(n - k));
% finally convert back to get p(k) by computing exp(logpk)
pmf(i) = exp(logpk);
end