2014-09-12 47 views
2

我最初的问题是创建一个具有特定长度(x = 100)的行和特定位置(pos = 50)的障碍的情景。进行多轮取样,在其中制定一定数量的随机数(p)。产生的数字可能会落在障碍的左侧或右侧。程序输出屏障左侧生成的最大数量与右侧生成的最小数量之间的差值。这是更清楚看到这里:按顺序读取MATLAB中的矩阵列

在这个例子中,系统已经创建4个数字(A,B,C,d)。它会忽略a和d并输出b和c之间的差异。本质上,它将输出仍然包含屏障的线条的最小可能片段。

我一直在使用要做到这一点的代码是:

x = 100;  % length of the grid 
pos = 50;  % position of the barrier 
len1 = 0;  % left edge of the grid 
len2 = x;  % right edge of the grid 

sample = 1000; % number of samples to make 
nn  = 1:12 % number of points to generate (will loop over these) 

len = zeros(sample, length(nn)); % array to record the results 

for n = 1:length(nn)      % For each number of pts to generate 
    numpts = nn(n); 
    for i = 1:sample      % For each round of sampling, 
     p = round(rand(numpts,1) * x); % generate 'numpts' random points. 
     if any(p>pos)     % If any are to the right of the barrier, 
      pright = min(p(p>pos));  % pick the smallest. 
     else 
      pright = len2; 
     end 
     if any(p<pos)     % If any are to the left of the barrier, 
      pleft = max(p(p<pos));  % pick the largest. 
     else 
      pleft = len1; 
     end 
     len(i,n) = pright - pleft;  % Record the length of the interval. 
    end 
end 

我现在的问题:我想使这个更复杂。例如,我希望能够在每轮中使用不止一个随机数。具体而言,我想用不同的平均值涉及这对泊松分布:

% Create poisson distributions for λ = 1:12 
range = 0:20; 
for l=1:12; 
    y = poisspdf(range,l); 
    dist(:,l) = y; 
end 

由此,我想带1000个样本的每个λ但每一轮的1000个样本内,随机数计数是无所有1000个样品的时间更长。相反,它取决于泊松分布。例如,1的平均值内时,概率为:

0 - 0.3678 
1 - 0.3678 
2 - 0.1839 
3 - 0.0613 
4 - 0.0153 
5 - 0.0030 
6 - 0.0005 
7 - 0.0001 
8 - 0.0000 
9 - 0.0000 
10 - 0.0000 
11 - 0.0000 
12 - 0.0000 

所以对于第一轮的1000个样本,其中367将被进行生成只是1号,367进行生成2-号码,183执行生成3个号码等。然后程序将使用它从平均值2获得的新值重复这一操作,依此类推。然后,我想简单地将所有片段大小(pright-pleft)收集到一个矩阵的列中 - 每个λ的列。

我知道我可以做这样的事情:

amount = dist*sample 

要乘以样本量的泊松分布,以获得多少每个数生成它应该做的 - 但是我真的停留在如何将这变成for循环,并改变代码来解决这个新问题。我也不确定如何读取矩阵中的列以使用每个概率值来确定它应该做的每种类型的RNG的数量。

任何帮助将不胜感激,

安娜。

+0

您好,欢迎SO!你已经努力写出包括代码在内的详细问题,这很好。然而,至少对我而言,目前还不清楚这是什么。 “这解决了存在一条特定长度(x = 100)和特定位置(pos = 50)的障碍的问题” - 什么障碍,你说什么长度?请编辑您的问题,以便您首先简要说明上下文和背景,然后说明具体问题,然后进入代码。 – 2014-09-12 11:57:49

+0

谢谢你的欢迎。希望我的编辑能够清楚一点。 – AnnaSchumann 2014-09-12 12:29:47

+0

只是出于纯粹的好奇心:这是什么应用?它与所谓的Mac Arthurs“破碎棒模型”有些相关吗? http://en.wikipedia.org/wiki/Niche_apportionment_models 看起来你似乎在某种程度上像在该模型中分割棒。 – Nras 2014-09-12 12:46:04

回答

1

如果您有统计工具箱,您可以使用random从已知的pdf对象中生成一个随机变量向量。更好的是,跳过PDF步骤并使用poissrnd生成随机变量。将值舍入为最接近的整数,然后调用rand,因为您已经在这样做了。在你的循环中,简单地迭代你生成的泊松分布随机数的向量。

例子:

x = 100;  % length of the grid 
pos = 50;  % position of the barrier 
len1 = 0;  % left edge of the grid 
len2 = x;  % right edge of the grid 

sample = 1000; % number of samples to make 
lambda = 1:12; % lambdas 

Rrnd = round(poissrnd(repmat(lambda,sample,1))); 
len = zeros(size(Rrnd)); % array to record the results 

for n = lambda;       % For each number of pts to generate 
    for i = 1:sample      % For each round of sampling, 
     numpts = Rrnd(i,n); 
     p = round(rand(numpts,1) * x); % generate 'numpts' random points. 
     len(i,n) = min([p(p>pos);len2]) - max([p(p<pos);len1]); % Record the length 
    end 
end 
+0

这似乎是完美的。谢谢。我根本不考虑这样做,也不知道poissrnd的存在。 – AnnaSchumann 2014-09-12 14:48:52

+0

没问题。我缩短了一点,因为当任何(p 2014-09-12 15:31:06