模拟间隔时间的问题 [英] Problems Simulating Interarrival Times

查看:380
本文介绍了模拟间隔时间的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模拟事件(一辆车进入隧道)的发生,而事实证明是一个泊松过程。





对于每1分钟的间隔,我计算/获得了平均值:


  1. 在此期间进入隧道的车辆数量。

  2. 每辆车进入隧道之间的时间(预计到达时间)

例如,对于分钟10:37-38,平均值为5辆,平均到达间隔时间为12秒

要取样10:37-38分钟,请执行以下操作:


  1. 具有平均值5的泊松分布以确定多少项目将到达,分配给X

  2. 采样平均值的1/12 X次的指数分布,以得到到达间时间y_0,y_1 ..._ y_x

  3. 将到达时间相加并分配给K

  4. 如果 K 大于60秒, 2

  5. 累积各种计数器

  6. 最后打印统计信息。

代码如下:

  #include< iostream> 
#include< cstdio>
#include< random>
#include< algorithm>
#include< iterator>

int main()
{

double mean_num_itms = 5.0;
double mean_inter_time = 12; // seconds
double max_sec_in_period = 60; // seconds

unsigned int rounds = 10000;

std :: random_device r;
std :: exponential_distribution< double>指数(1.0 / mean_inter_time);
std :: poisson_distribution< double> poisson(mean_num_itms);

double total_itms = 0;
double total_inter_time = 0;

for(std :: size_t i = 0; i {
//确定多少项目将在时间段内到达
unsigned int num_itms =(unsigned int)(poisson(r));

total_itms + = num_itms;

//获取'num_itms'的到达时间
double last_arrival_time = 0;
do
{
last_arrival_time = 0;
for(unsigned int j = 0; j< num_itms; ++ j)
{
double current_arrival_time = exponential(r);
last_arrival_time + = current_arrival_time;
}

}
//拒绝超过期间跨度的任何一组到达时间。
while(last_arrival_time> max_sec_in_period);

total_inter_time + = last_arrival_time;

}

printf(Mean items per minute:%8.3f \\\
,total_itms / rounds);
printf(平均到达间隔时间:%8.3fsec \\\
,total_inter_time / total_itms);

return 0;
}

上述代码的问题是:



  • 拒绝部分是非常昂贵的


  • 平均到达间隔时间的结果是不正确的:




    • 平均每分钟项目:5.014

    • 平均到达间隔时间:7.647秒


  • 所以我的问题如下:


    1. 有没有更好的更高效的技术,以确保总的到达间隔时间永远不会超过周期中的最大秒数?

      li>
    2. 为什么平均到达间隔时间偏斜?对于上面的例子,我期望它大约是12 - 我认为有一个错误的代码,但似乎不能把我的手指。



    解决方案

    听起来像是你试图模拟非均匀泊松过程,其中lambda(t)在分段中定义到最近的分钟。



    正确的方式这是与减薄。基本上,在时间t 1,t 2,t 3,...处找到最大λ(t)并生成伪到达。 rate lambda max 。对于在时间t i的每个伪到达,接受它作为具有概率λ(t i)/ lambda sub max的实际到达。结果是一系列车辆到达隧道的时间。


    I'm attempting to simulate the occurrence of an event (a vehicle entering a tunnel), which as it turns out is a Poisson process.

    I've broken the day up into 1 minute intervals, starting from 9am to 5pm.

    For each 1 minute interval, I've computed/obtained the mean:

    1. Number of vehicles that enter the tunnel during that period.
    2. Time between each vehicle entering the tunnel (expected interarrival time)

    For example for the minute 10:37-38 the mean is 5 vehicles with a mean inter-arrival time of 12seconds

    To sample the 10:37-38 minute I do the following:

    1. Sample a Poisson distribution with a mean of 5 to determine how many items will arrive, assign to X
    2. Sample an exponential distribution of mean 1/12 X times to derive inter-arrival times y_0,y_1..._y_x
    3. Sum the interarrival times and assign to K
    4. If K is larger than 60seconds go to step 2
    5. Accumulate various counters
    6. Finally print statistics.

    The code is as follows:

    #include <iostream>
    #include <cstdio>
    #include <random>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
    
       double mean_num_itms = 5.0;
       double mean_inter_time = 12; //seconds
       double max_sec_in_period = 60; //seconds
    
       unsigned int rounds = 10000;
    
       std::random_device r;
       std::exponential_distribution<double> exponential(1.0 / mean_inter_time);
       std::poisson_distribution<double> poisson(mean_num_itms);
    
       double total_itms = 0;
       double total_inter_time = 0;
    
       for (std::size_t i = 0; i < rounds; ++i)
       {
          //Determine how many items will arrive in time period
          unsigned int num_itms = (unsigned int)(poisson(r));
    
          total_itms += num_itms;
    
          //Get the interarrival times for the 'num_itms'
          double last_arrival_time = 0;
          do
          {
             last_arrival_time = 0;
             for (unsigned int j = 0; j < num_itms; ++j)
             {
                double current_arrival_time = exponential(r);
                last_arrival_time += current_arrival_time ;
             }
    
          }
          //Reject any group of arrival times that exceed period span.
          while (last_arrival_time > max_sec_in_period);
    
          total_inter_time += last_arrival_time;
    
       }
    
       printf("Mean items per minute:   %8.3f\n"   ,total_itms / rounds);
       printf("Mean inter-arrival time: %8.3fsec\n",total_inter_time / total_itms);
    
       return 0;
    }
    

    The problem with the code above is:

    1. The rejection part is very costly

    2. The results for the mean inter-arrival time is incorrect:

      • Mean items per minute: 5.014
      • Mean inter-arrival time: 7.647sec

    So my questions are as follows:

    1. Is there a better more efficient technique to ensure that the total inter-arrival times never exceed the maximum number of seconds in the period?

    2. Why is the mean inter-arrival time skewed down? for the example above I expect it to be roughly 12 - I think there's a bug in the code but can't seem to put my finger on it.

    解决方案

    Sounds like you're trying to simulate a non-homogeneous Poisson process where lambda(t) is being defined in piece-wise segments to the nearest minute.

    The correct way to do this is with "thinning". Basically, find the maximum lambda(t) and generate pseudo-arrivals at times t1, t2, t3,... at rate lambdamax. For each pseudo-arrival at time ti, accept it as a an actual arrival with probability lambda(ti) / lambdamax. The result is a sequence of the times at which vehicles arrive at the tunnel.

    这篇关于模拟间隔时间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆