Search code examples
pythonnumpyscipyprobabilitynormal-distribution

Probability of a point taken from a certain normal distribution will be less than or equal to than a point taken from another?


Suppose we have two independent normal distributions enter image description here

How do I calculate the probability of a certain point taken from distribution X1 being less than or equal to a certain point taken from distribution X2 in Python?

With reference to this I can say that the formula for greater than could be something like this,

Example,

m1, std1 = 1, 2 
m2, std2 = 2, 3

#then, 

enter image description here

and, enter image description here

# hence, 
from scipy.stats import norm
p = 1 - norm.cdf(-(m1 - m2) / np.sqrt(std1 + std2))
# p = 0.32736042300928847

I am looking for the code for enter image description here


Solution

  • Let Y be X₁ - X₂. You've already shown that Y ~ N(μ₁ - μ₂, σ₁² + σ₂²). You want P(Y ≤ 0). That is the CDF of Y evaluated at 0.

    In code:

    from math import sqrt
    from scipy.stats import norm
    
    m1, std1 = 1, 2
    m2, std2 = 2, 3
    
    m = m1 - m2
    std = sqrt(std1**2 + std2**2)
    print(norm.cdf(0, loc=m, scale=std))
    

    Output is

    0.6092443525006433