lois_normales.py

Created by erwan-gouello

Created on May 08, 2019

1.04 KB

Ensemble de fonctions de calculs des lois normales. On suppose que X suit une loi normale de paramètres mu et sigma

*normalcdf(a,mu,sigma) calcule P(X<a)

*normalcdf2(a,b,mu,sigma) calcule P(a<X<b)

*invnorm(area,mu,sigma) calcule t tel que P(X<t)=area

*invnormright(area,mu,sigma) calcule t tel que P(X>t)=area

*invnormcentral(area,mu,sigma) calcule les deux valeurs t1 et t2 centrées autour de mu avec P(t1<X<t2)=area


from math import *

# calcule P(X<a) quand X suit N(mu,sigma)
def normalcdf(a,mu,sigma):
  return (0.5)*(1+erf((a-mu)/(sqrt(2)*sigma)))

# calcule P(a<X<b) quand X suit N(mu,sigma)
def normalcdf2(a,b,mu,sigma):
  return normalcdf(b,mu,sigma)-normalcdf(a,mu,sigma)

# calcule t tel que as P(X<t)=area
def invnorm(area,mu,sigma):
  #quelques cas particuliers
  if area==0.5:
    return mu
  elif area==1:
    return 10**100
  elif area==0:
    return -10**100
  elif area>0.5:
    min=mu
    max=mu+9*sigma
  else:
    min=mu-9*sigma
    max=mu
  #dichotomie
  while max-min>10**(-17):
    milieu=(max+min)/2
    if normalcdf(milieu,mu,sigma)==area:
      return milieu
    elif normalcdf(milieu,mu,sigma)<area:
      min=milieu
    else:
      max=milieu
  return (max+min)/2
  
  
# calcule t tel que P(X>t)=area
def invnormright(area,mu,sigma):
  return invnorm(1-area,mu,sigma)
  
#calcule t1 et t2 centres autour de mu tel que 
#P(t1<X<t2)=area
def invnormcentral(area,mu,sigma):
  t=invnorm((1-area)/2,mu,sigma)
  return (t,2*mu-t)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.