简体   繁体   English

R中的指数分布模拟

[英]Exponential Distribution Simulation in R

I have the following graph: 我有以下图表:

在此输入图像描述

I am told the following information: 我被告知以下信息:

(1) vertex A to vertex X is described by an exponential distribution with lambda = 4; (1)顶点A到顶点X由指数分布描述,λ= 4;

(2) vertex A to vertex Y is described by an exponential distribution with lambda = 2.5; (2)顶点A到顶点Y由指数分布描述,λ= 2.5;

(3) vertex X to vertex Y identical to vertex Y to vertex X, and it is described by an exponential distribution with lambda = 10; (3)顶点X到顶点Y与顶点Y相同,到顶点X,用λ= 10的指数分布描述;

(4) vertex X to vertex B is described by an exponential distribution with lambda = 3; (4)顶点X到顶点B用λ= 3的指数分布描述; and, finally, 最后,

(5) vertex Y to vertex B is described by an exponential distribution with lambda = 5. (5)顶点Y到顶点B由λ= 5的指数分布描述。

Let's assume that I'm taking the fastest path between vertices every simulation. 让我们假设我在每个模拟中采用顶点之间的最快路径。

I now want to know the average time it takes to get from vertex A to vertex B. 我现在想知道从顶点A到顶点B的平均时间。

My R code is as follows: 我的R代码如下:

# Generate/simulate 1000 random numbers for each of the internode paths.

            AtoX <- rexp(1000, 4)
            AtoY <- rexp(1000, 2.5)
            XtoY <- rexp(1000, 10)
            XtoB <- rexp(1000, 3)
            YtoB <- rexp(1000, 5)

    # Length of path from A to X to Y and A to Y to X.

            AYX = AtoY + XtoY
            AXY = AtoX + XtoY

    # Total time of paths from A to B. 

            AXB = AtoX + XtoB
            AYB = AtoY + YtoB
            AXYB = AtoX + XtoY + YtoB
            AYXB = AtoY + XtoY + XtoB

    # Taking the fastest path of all paths. 

            minAXB = min(AXB)
            minAYB = min(AYB)
            minAXYB = min(AXYB)
            minAYXB = min(AYXB)

    # Taking an average of the fastest paths.

            averageTravelTime = 
              mean(minAXB + minAYB + minAXYB + minAYXB)

Does this look correct? 这看起来是否正确? I would appreciate it if people could please take the time to review my code for this problem. 如果人们可以请花时间审查我的代码来解决这个问题,我将不胜感激。

  1. It depends on the interpretation, but I'd say that you need to simulate times from X to Y and from Y to X separately, although with the same rate. 这取决于解释,但我要说你需要分别模拟从X到Y和从Y到X的时间,尽管速率相同。 If a train goes both ways and on average the speed is the same, it doesn't mean that two trains leaving from X and Y will arrive to the other point in the same time. 如果火车双向行驶并且平均速度相同,则并不意味着从X和Y出发的两列火车将在同一时间到达另一点。

  2. You are not using 你没有使用

     AYX <- AtoY + XtoY AXY <- AtoX + XtoY 

    so they are redundant. 所以他们是多余的。

  3. Writing minAXB <- min(AXB) doesn't really make sense. 编写minAXB <- min(AXB)并没有多大意义。 You simulate 1000 travel times for each edge, AXB is a vector of 1000 times of route AXB and now you are choosing the shortest one across the time.. 您为每条边模拟1000次行程时间, AXB是路径AXB的1000倍向量,现在您选择的是最短的一次。

  4. Similarly, averageTravelTime doesn't make sense because minAXB + minAYB + minAXYB + minAYXB is just a number, not a vector. 同样, averageTravelTime没有意义,因为minAXB + minAYB + minAXYB + minAYXB只是一个数字,而不是一个载体。

Hence, I think the code should be 因此,我认为代码应该是

set.seed(1)
AtoX <- rexp(1000, 4)
AtoY <- rexp(1000, 2.5)
XtoY <- rexp(1000, 10)
YtoX <- rexp(1000, 10) # added
XtoB <- rexp(1000, 3)
YtoB <- rexp(1000, 5)

AXB <- AtoX + XtoB
AYB <- AtoY + YtoB
AXYB <- AtoX + XtoY + YtoB
AYXB <- AtoY + YtoX + XtoB # changed XtoY to YtoX

TravelTimes <- pmin(AXB, AYB, AXYB, AYXB)
averageTravelTime <- mean(TravelTimes)

See ?pmin . ?pmin For every day it chooses the fastest travel time and returns a vector of length 1000. 每天它选择最快的旅行时间并返回长度为1000的向量。

As a bonus, the following shows how many times which route was the fastest 作为奖励,以下显示哪条路线最快的次数

table(apply(cbind(AXB, AYB, AXYB, AYXB), 1, which.min))
#   1   2   3   4 
# 317 370 240  73 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM