pythonrandomprobability

What is the probability that a value from one uniform distribution will be greater than a value from another?


Let our distributions both be uniform, the first (A) with bounds (ψ,Ψ) and the second (B) with bounds (φ,Φ). What is the probability of A.random() > B.random()?

#                               A>B?                             #
#  ------------------------------------------------------------  #
#    φ         Φ                 ψ+++++++++Ψ                    ==> ψ >= Φ : ALWAYS a max (100%)
#    ψ---------Ψ                 φ         Φ                    ==> φ >= Ψ : NEVER a max (0%)
#                      ψ-------φ????Ψ           Φ               ==> Φ >= Ψ >= φ : SOMETIMES a max
#                      φ       ψ????Φ+++++++++++Ψ               ==> Ψ >= Φ >= ψ : SOMETIMES a max

I have come up with this so far, but I'm lost on how to complete it:

if ψ >= Φ:                                # ALWAYS a max (ψ >= Φ)
    return 1
elif φ >= Ψ:                              # NEVER a max (φ >= Ψ)
    return 0
elif Φ >= Ψ and Ψ >= φ:                   # SOMETIMES a max (Φ >= Ψ >= φ)
    range_A = Ψ - ψ
    range_B = Φ - φ
    overlap = Ψ - φ                          # [???? region]
    overlap_chance = <<<X>>>
    return overlap_chance
elif Ψ >= Φ and Φ >= ψ:                   # SOMETIMES a max (Ψ >= Φ >= ψ)
    range_A = Ψ - ψ
    range_B = Φ - φ
    overlap = Φ - ψ                          # [???? region]
    overlap_chance = <<<X>>>
    all_good = Ψ - Φ                         # [++++ region]
    all_good_chance = all_good / range_chosen
    return overlap_chance + all_good_chance

How would I calculate overlap_chance?

My best guess is that it's simply a 50/50 when both are within the region ((overlap / range_A) * (overlap / range_B) * 0.5), but I can't seem to come up with a convincing proof other than "my gut says so."


Note 1: I will be running this code repeatedly for my use-case, and would prefer not to run simulations if possible. Exactness is also important for proving soundness (presuming sound floating-point operations).

Note 2: I see the question "How to use normal distribution to calculate probability that one player will score more points than another in a game?" that seems to be similar, but I have no experience with R, nor an understanding of how to apply it to a uniform distribution. Hence my own attempt above.


Solution

  • This is based on my earlier comment - I like the geometric argument.

    Sketch a graph with variable 1 on the x axis, variable 2 on the y axis. Plot the rectangle that corresponds to their bounds. This is the probability space and it is uniform, so the probability density is 1/area. Draw the line y=x. Find the fraction of the rectangle defined by the two sets of bounds, [a,A] and [b,B] respectively, that lies below-right of this line.

    The six cases are pictured: enter image description here

    The cases 1-6 correspond to the successive cases below. Areas are either triangles, complement of triangles, or trapezia.

    def prob( a, A, b, B ):                # assume A > a and B > b or problem is undefined
        area = ( A - a ) * ( B - b )       # whole area of rectangle
        if b >= A:
            P = 0.0
        elif B <= a:
            P = 1.0
        elif b <= a <= B:
            if A >= B:
                P = 1 - 0.5 * ( B - a ) ** 2 / area
            else:
                P = ( A - a ) * ( ( a + A ) / 2 - b ) / area
        elif a <= b <= A:
            if B >= A:
                P = ( B - b ) * ( A - ( b + B ) / 2 ) / area
            else:
                P = 0.5 * ( A - b ) ** 2 / area
    
        return P
    
    
    
    a, A, b, B = 1, 3, -1, 2
    print( prob( a, A, b, B ) )            # 0.916666... or 11/12