-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoissondistribution.m
More file actions
153 lines (134 loc) · 3.77 KB
/
poissondistribution.m
File metadata and controls
153 lines (134 loc) · 3.77 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function [result1, result2, sampleMean1, sampleVar1, sampleMean2, sampleVar2] = poissondistribution(lambda1, lambda2, N)
close all;
% for i=1:N
% result1(i) = poissrnd(lambda1);
% result2(i) = poissrnd(lambda2);
% end
sampleMean1=0;
sampleMean2=0;
%Generating samples
for i=1:N
k=0;
p=1;
u=rand;
p=p*u;
while p>=exp(-lambda1)
u=rand;
p=p*u;
k=k+1;
end
result1(i)=k;
sampleMean1=sampleMean1+result1(i);
end
sampleMean1 = sampleMean1/N;
for i=1:N
k=0;
p=1;
u=rand;
p=p*u;
while p>=exp(-lambda2)
u=rand;
p=p*u;
k=k+1;
end
result2(i)=k;
sampleMean2=sampleMean2+result2(i);
end
sampleMean2 = sampleMean2/N;
%Sample Variances
sampleVar1=0;
sampleVar2=0;
nSize=N;
for i=1:nSize
sampleVar1 = sampleVar1 + ((result1(i)-sampleMean1)*(result1(i)-sampleMean1));
end
sampleVar1 = sampleVar1/(N-1);
for i=1:nSize
sampleVar2 = sampleVar2 + ((result2(i)-sampleMean2)*(result2(i)-sampleMean2));
end
sampleVar2 = sampleVar2/(N-1);
%Mean
mean1 = lambda1;
mean2 = lambda2;
%Variance
variance1 = lambda1;
variance2 = lambda2;
% Draw random samples from uniform distribution in range 0 to 1:
n_samples = N;
figure;
subplot(2,1,1);
a = 1:1000;
p = result1;
stem(a, p);
set(gca, 'xlim', [1 1000]);
xlabel('Poisson distribution lambda=6, N=1000');
hold on;
%plot(result1, '--k');
str = sprintf('%d Generated Data', N);
title(str);
subplot(2,1,2);
a = 1:1000;
p = result2;
stem(a, p);
set(gca, 'xlim', [1 1000]);
xlabel('Poisson distribution lambda=4.5, N=1000');
hold on;
%plot(result2, '--k');
figure;
% Calculate histogram with bin width 0.1:
subplot(2,2,1);
binwidth = 1;
bins = 1:1:50;
N = histcounts(result1,bins); % Number of x values in each bin
f = N/n_samples/binwidth; % Observed frequency per x unit
bin_centres = (bins(1:end-1)+bins(2:end))/2;
%bar(bin_centres, f);
stem(bin_centres, f);
hold on;
plot(bin_centres, f, 'r', 'LineWidth', 2);
legend('Experimental');
% Calculate histogram with bin width 0.1:
subplot(2,2,2);
binwidth = 1;
bins = 1:1:50;
N = histcounts(result2, bins); % Number of x values in each bin
f = N/n_samples/binwidth; % Observed frequency per x unit
bin_centres = (bins(1:end-1)+bins(2:end))/2;
%bar(bin_centres, f);
stem(bin_centres, f);
hold on;
plot(bin_centres, f, 'r', 'LineWidth', 2);
legend('Experimental');
% Compare with analytic pdf for lambda1
subplot(2, 2, 3);
%figure;
for x=1:50
c(x) = (lambda1.^x)*exp(-lambda1)/factorial(x);
end
x=1:50;
hold on;
length(c)
length(x)
h = plot(x,c,'ok'); % Plot the PDF using circles
set(h,'MarkerFaceColor','w');
hold off;
str = sprintf('lambda: %d', 6);
title(str);
legend('Analytical');
% Compare with analytic pdf for lambda2
subplot(2, 2, 4);
%figure;
%x = 1:1:3;
%p = (lambda2.^x)*exp(-lambda2)/factorial(x);
for x=1:50
c(x) = (lambda2.^x)*exp(-lambda2)/factorial(x);
end
x=1:50;
hold on;
h = plot(x,c,'ok'); % Plot the PDF using circles
set(h,'MarkerFaceColor','w');
hold off;
str = sprintf('Binomial Distribution: n=50, p=0.5');
title(str);
legend('Analytical');
end