0

If you take a random and uniformly continuous number that is generated by the sum of $x1+x2$ (so $y=x1+x2$), the probability $P(0.9<y<=1.8)$ the calculated results are:

$y$~$u(0,1) = 0.575$

$y$~$exp(2) = 0.3371$

$x1~u(0,1)$ $x2~u(0,2)$

$P(y=0.25)=0.8$ $P(y=1.5)=0.2 = 0.2$

3 of the 4 simulated values are way off, and I'm concerned it's because of the mathematical logic behind the way I'm trying to generate and then sort the numbers by $P(0.9<Y<=1.8)$. My code in Python is:

n = 10000
x11 = [random.random() for i in range(n)]
x12 = [random.random() for i in range(n)]
x21 = [-0.5*(log(1-random.random())) for i in range(n)]
x22 = [-0.5*(log(1-random.random())) for i in range(n)]
x31 = [random.random() for i in range(n)]
x32 = [random.uniform(0,2) for i in range(n)]
x41 = [0.25 if random.random() < 0.8 else 1.5 for r in range(n)]
x42 = [0.25 if random.random() < 0.8 else 1.5 for r in range(n)]

x11 through x42 are pairs of cases that I'm trying to get the probability that the y for each of summed pairs in 10000 will fall between 0.9 and 1.8. So x11 and x12 are combined and then the $P(0.9<y<=1.8)$ is found.

def test():
  x1,x2,c = 0.0,0.0,0.0
  for i in range(10000):
    if random.random()< 0.8:
      x1 += 0.25
    else:
      x2 += 1.5
    y = x1 + x2
    if y>0.9 and y<=1.8:
      c = c + 1
  return x1,x2,c

I tried to do it by a for loop above and that gave similarly wrong results to what I've included at the bottom.

Right below here, you'll see about 3 ways I tried unsuccessfully to simulate this in the function sim(a,b)

def sim(a,b):
  #pyab1 = sum([a for a in a if a>0.9 and a<=1.8])/10000
  #pyab2 = sum([b for b in b if b>0.9 and b<=1.8])/10000
  #print "*****",float(pyab1+pyab2)
  #print a+b
  #array1 = [[a],[b]]
  array1 = a+b
  #array1.extend(a)
  #array1.extend(b)
  #c = 0
  #for y in array1:
    #if y>0.9 and y<=1.8:
      #c = c + 1
  pyab = sum([y for y in array1 if y>0.9 and y<=1.8])/10000
  print("P(a < x <= b) : {0:8.4f}".format(pyab))

Here's the output followed by the values it's supposed to give, but this shows how far off the results are.

case 1: P(a < x <= b) : 0.7169 #should be 0.575 
case 2: P(a < x <= b) : 0.4282 #should be 0.3371 
case 3: P(a < x <= b) : 0.5966 #should be 0.4413 
case 4: P(a < x <= b) : 0.5595 #should be 0.2 

This isn't really a coding problem as the stochastic logic is the problem here and it went a little outside of what SO usually does. I'm very new to Python so please be patient if my question seems to have an obvious solution that I missed.

stackuser
  • 101
  • 2
  • It is difficult to make sense of the first part of this question. How is it you can define $y$ as the sum of two variables, yet declare that $y$ has either? both? of a uniform or exponential distribution? The probability statements don't seem correct: for any such variable $y$, $\Pr(y=0.25)$ must be $0$, not anything positive. Perhaps you could explain what you're doing in English, in as simple and clear terms as possible, so that readers can be sure you are asking what you intend to ask. – whuber Nov 10 '12 at 22:55
  • I'm trying to more fully grasp the theory behind the calculated values (to match with the sim), but I'll answer you as best I can. If you're asking about the y -- that is y=x1+x2 where x1,x2 are independently realized random variables. This is often known as the joint sum of independent probabilities. If you're asking about the last case, where P(y=0.25)=0.8,P(y=1.5)=0.2 -- that basically says "if the random sum is <= 0.25 then it is 0.8. if the random sum is >= 1.5 then it is 1.00". – stackuser Nov 10 '12 at 23:13
  • 1
    Unfortunately, that's *not* what those equations mean, which is why there's so much potential for confusion. You will benefit greatly by avoiding the mathematical notation and explaining in your own words what you know and what you're trying to find out. (For more information about sums of uniform random variables, please see the recent thread at http://stats.stackexchange.com/questions/41467. Reading it may acquaint you better with notation, terminology, and the concepts.) – whuber Nov 10 '12 at 23:20

0 Answers0