2

假设我想模拟一个粒子状态,它在给定帧中可以是正常 (0) 或兴奋 (1)。粒子在 f% 的时间内处于激发态。如果粒子处于激发态,它会持续约 L 帧(具有泊松分布)。我想模拟 N 个时间点的状态。所以输入例如:

N = 1000;
f = 0.3;
L = 5;

结果会是这样的

state(1:N) = [0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ... and so on]

总和(状态)/N 接近 0.3

怎么做?谢谢!

4

2 回答 2

2
%% parameters
f = 0.3; % probability of state 1
L1 = 5;  % average time in state 1
N = 1e4;
s0 = 1; % init. state
%% run simulation
L0 = L1 * (1 / f - 1); % average time state 0 lasts
p01 = 1 / L0; % probability to switch from 0 to 1
p10 = 1 / L1; % probability to switch from 1 to 0
p00 = 1 - p01;
p11 = 1 - p10;
sm = [p00, p01; p10, p11];  % build stochastic matrix (state machine)
bins = [0, 1]; % possible states
states = zeros(N, 1);
assert(all(sum(sm, 2) == 1), 'not a stochastic matrix');
smc = cumsum(sm, 2); % cummulative matrix
xi = find(bins == s0);
for k = 1 : N
    yi = find(smc(xi, :) > rand, 1, 'first');
    states(k) = bins(yi);
    xi = yi;
end
%% check result
ds = [states(1); diff(states)];
idx_begin = find(ds == 1 & states == 1);
idx_end = find(ds == -1 & states == 0);
if idx_end(end) < idx_begin(end)
    idx_end = [idx_end; N + 1];
end
df = idx_end - idx_begin;
fprintf('prob(state = 1) = %g; avg. time(state = 1) = %g\n', sum(states) / N, mean(df));
于 2012-03-25T10:30:29.950 回答
2

激发态的平均长度为 5。正常状态的平均长度,因此应该在 12 左右才能获得。

策略可以是这样的。

  • 从状态 0 开始
  • a从具有均值的泊松分布中抽取一个随机数L*(1-f)/f
  • a用零填充状态数组
  • b从具有均值的泊松分布中抽取一个随机数L
  • 用状态数组填充状态数组b
  • 重复

另一种选择是考虑切换概率,其中 0->1 和 1->0 概率不相等。

于 2012-03-25T10:07:48.200 回答