了解非齐次泊松过程 Matlab 代码

Understanding Non-homogeneous Poisson Process Matlab code

我找到了以下 Matlab 代码来模拟非齐次泊松过程

function x = nonhomopp(intens,T)
% example of generating a 
% nonhomogeneousl poisson process on [0,T] with intensity function intens

x = 0:.1:T;
m = eval([intens 'x']);
m2 = max(m); % generate homogeneouos poisson process
u = rand(1,ceil(1.5*T*m2));
y = cumsum(-(1/m2)*log(u)); %points of homogeneous pp
y = y(y<T); n=length(y); % select those points less than T
m = eval([intens 'y']); % evaluates intensity function
y = y(rand(1,n)<m/m2); % filter out some points
hist(y,10)

% then run
% t = 7 + nonhomopp('100-10*',5)

我是 Matlab 的新手,无法理解它的工作原理。我已经阅读了所有这些函数的 Mathworks 页面,但在四个地方感到困惑:

1) 为什么函数定义为x 然后区间也叫x?这是否是对符号的滥用?

2) 方括号如何影响eval,

eval([intens 'x'])

为什么 x 在单引号中?

3) 为什么他们使用 cumsum 而不是 sum

4)给定的强度函数是\lambda (t) = 100 - 10*(t-7) with 7 \leq t \leq 12t = 7 + nonhomopp('100-10*',5)如何表示这个?

对不起,如果这么多,谢谢!

回答2)。那是一段不必要的复杂代码。要理解它,只评估方括号及其内容。它会生成字符串 100-10*x,然后对其进行评估。这是一个没有 eval 的版本,而是使用匿名函数。它应该是这样实现的。

function x = nonhomopp(intens,T)
% example of generating a 
% nonhomogeneousl poisson process on [0,T] with intensity function intens

x = 0:.1:T;
m = intens(x);
m2 = max(m); % generate homogeneouos poisson process
u = rand(1,ceil(1.5*T*m2));
y = cumsum(-(1/m2)*log(u)); %points of homogeneous pp
y = y(y<T); n=length(y); % select those points less than T
m = intens(y); % evaluates intensity function
y = y(rand(1,n)<m/m2); % filter out some points
hist(y,10)

可以这样调用

t = 7 + honhomopp(@(x)(100-10*x),5)
  1. 函数未定义为x: x只是输出变量。在 Matlab 中,函数声明为 function [output variable(s)] = <function name>(input variables)。如果该函数只有一个输出,则可以省略方括号(就像您的情况一样)。相反,无论有多少个输入参数,输入参数周围的括号都是强制性的。用 end 结束函数体也是一个好习惯,就像你用循环和 if/else.
  2. 一样
  3. eval 使用字符串作为输入,方括号显然是将字符串 'intens' 与字符串 'x' 连接起来。 x 在引号中是因为 eval 同样适​​用于字符串格式的输入,即使它指的是变量。
  4. cumsumsum 的行为不同。 sum return 是一个标量,它是数组所有元素的总和,而 cumsum return 是另一个包含累积和的数组。如果我们的数组是 [1:5]sum([1:5]) 将是 return 15,因为它是 1+2+3+4+5。相反,cumsum([1:5]) 将 return [1 3 6 10 15],其中输出数组的每个元素都是输入数组中先前元素(本身包括在内)的总和。
  5. 什么命令 t = 7 + nonhomopp('100-10*',5) returns 只是时间的值 t 而不是 lambda 的值,实际上通过查看 t 最小值是7,最大值为 12。泊松分布本身是 return 通过直方图编辑的。