问题描述
我试图模拟事件(一辆车进入隧道)的发生,而事实证明是一个泊松过程。
对于每1分钟的间隔,我计算/获得了平均值:
- 在此期间进入隧道的车辆数量。
- 每辆车进入隧道之间的时间(预计到达时间)
例如,对于分钟10:37-38,平均值为5辆,平均到达间隔时间为12秒
要取样10:37-38分钟,请执行以下操作:
- 具有平均值5的泊松分布以确定多少项目将到达,分配给X
- 采样平均值的1/12 X次的指数分布,以得到到达间时间y_0,y_1 ..._ y_x
- 将到达时间相加并分配给K
- 如果 K 大于60秒, 2
- 累积各种计数器
- 最后打印统计信息。
代码如下:
#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秒
所以我的问题如下:
-
有没有更好的更高效的技术,以确保总的到达间隔时间永远不会超过周期中的最大秒数?
li>
-
为什么平均到达间隔时间偏斜?对于上面的例子,我期望它大约是12 - 我认为有一个错误的代码,但似乎不能把我的手指。
听起来像是你试图模拟,其中lambda(t)在分段中定义到最近的分钟。
正确的方式这是与。基本上,在时间t 1,t 2,t 3,...处找到最大λ(t)并生成伪到达。 rate lambda 。对于在时间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:
- Number of vehicles that enter the tunnel during that period.
- 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:
- Sample a Poisson distribution with a mean of 5 to determine how many items will arrive, assign to X
- Sample an exponential distribution of mean 1/12 X times to derive inter-arrival times y_0,y_1..._y_x
- Sum the interarrival times and assign to K
- If K is larger than 60seconds go to step 2
- Accumulate various counters
- 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:
The rejection part is very costly
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:
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?
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 t, t, t,... at rate lambda. For each pseudo-arrival at time t, accept it as a an actual arrival with probability lambda(t) / lambda. The result is a sequence of the times at which vehicles arrive at the tunnel.
这篇关于模拟间隔时间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!