-
Notifications
You must be signed in to change notification settings - Fork 0
/
npoisson.m
36 lines (34 loc) · 1.14 KB
/
npoisson.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
% Chrysa Tsimperi
% Data Analysis 2021
function mxV = npoisson(lambda,n,M,displayhist,nbin)
% mxV = npoisson(lambda,n,M,displayhist,nbin)
% This function simulates the mean of the Poisson distribution with
% parameter 'lambda'. A number of samples 'M' of size 'n' are generated
% from this Poisson distribution and the histogram of the sample means is
% plotted if 'displayhist==1' using the given 'nbin'.
% The sample means are returned in the array 'mxV'.
% INPUT
% - lambda : parameter of the Poisson distribution
% - n : sample size
% - M : number of samples
% - displayhist : if 1 display hist (default), otherwise don't
% - nbin : number of bins for histogram (default sqrt(M/5))
% OUTPUT
% - mxV : Mx1 array of sample means
if nargin==4
nbin=round(sqrt(M/5));
elseif nargin==3
nbin=round(sqrt(M/5));
displayhist=1;
end
xM = poissrnd(lambda,n,M);
mxV = mean(xM);
if displayhist
figure(gcf)
clf
histfit(mxV,nbin)
xlabel('sample mean')
ylabel('counts')
title(sprintf('mean of %d Poisson(lambda=%2.2f) samples of size %d',...
M,lambda,n))
end