1. Model Overview
- 该算法的思路与逆变换法一致: 通过生成 模拟泊松分布的分布函数, 再寻找其对应的自变量值 . 不同之处在于泊松分布的函数正常计算较为复杂, 故采取递推方式计算.
- 递推的思路: 首先仍生成一均匀分布 代表泊松分布函数, 然后开始循环讨论. 从 开始, 看 的值是否大于生成的 的值. 若是则该 即为想要模拟的 , 若不是则 , 继续讨论.
- 简而言之, 对于递推形式, 算法的核心在于依次比较 与 的大小, 第一个使得 的 即为所求的 .
2. Simulation Goal
Generate a random variable following a Poisson distribution with parameter :
3. Simulation Method
Since the Poisson CDF involves factorials and is costly to calculate directly, use a recurrence:
Algorithm:
- Generate a random number .
- Set , , .
- If , set and stop.
- Set , , .
- Go to Step 3.
4. Code
% function X = cspoirnd(lam,n)
% This function will generate Poisson(lambda)
function x = cspoirnd(lam, n)
x = zeros(1, n);
j = 1;
while j <= n
flag = 1;
% initialize quantities
u = rand(1);
i = 0;
p = exp(-lam);
F = p;
while flag
if u <= F
x(j) = i;
flag = 0;
j = j + 1;
else
p = lam * p / (i + 1);
i = i + 1;
F = F + p;
end
end
end