grapheproba.py

Created by nicolas-patrois

Created on April 27, 2018

1.35 KB

Calcule automatiquement l’état stable et les relations de récurrence comme explicites d’un graphe probabiliste à deux états (programme de TES spécialité).


def grapheproba():
 P=print
 P("La matrice de transition est [[m11 m12][m21 m22]].")
 P("La matrice de l'etat initial est [p1 p2].")
 P("Entrer les valeurs connues, par exemple m11=0.3 ou p2=0.2.")
 m=[[None,None],[None,None]]
 p0=[None,None]

 while [None,None] in m or None in p0:
  v=input()
  if "=" not in v:
   P("Entrer une egalite.")
   continue
  v=v.split("=")
  if v[0] not in ("m11","m12","m21","m22","p1","p2"):
   P("Entrer mij=... ou pi=...")
   continue
  try:
   v[1]=float(v[1])
   i=int(v[0][1])-1
   if v[0][0]=="m":
    j=int(v[0][2])-1
  except:
   P("Erreur de valeur.")
   continue
  if not 0<=v[1]<=1:
   P("Une probabilite est entre 0 et 1.")
   continue
  if v[0][0]=="m" and m[i][j]!=None:
   P("Deja occupe.")
   continue
  if v[0][0]=="p" and p0[i]!=None:
   P("Deja occupe.")
   continue
  if v[0][0]=="m":
   m[i][j]=v[1]
   m[i][1-j]=1-v[1]
  else:
   p0[i]=v[1]
   p0[1-i]=1-v[1]
  P("OK.")

 P("Matrice de transition :")
 P(" ".join(map(str,m[0])))
 P(" ".join(map(str,m[1])))
 P("Etat initial :")
 P("("+" ".join(map(str,p0))+")")

 p=(m[1][0]/(m[0][1]+m[1][0]),m[0][1]/(m[0][1]+m[1][0]))
 P("Etat stable :")
 P("(%f %f)."%p)

 q=m[0][0]-m[1][0]

 P("pn=(an bn) avec :")
 P("a(n+1)=%f*a(n)+%f"%(q,m[1][0]))
 P("b(n+1)=%f*b(n)+%f"%(q,m[0][1]))
 P("a(n)=%f*%f^n+%f"%(p0[0]-p[0],q,p[0]))
 P("b(n)=%f*%f^n+%f"%(p0[1]-p[1],q,p[1]))

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 cookies policy.