In reference to the error that arises, it is because you are calling the list as if it were a function, I guess what you wanted to do was call PronosticoActividad(dias)
therefore changing the name of the call, you would have it solved, although from what I see you have a couple of other errors, which I have taken the liberty to correct:
- When you call the
random.choice
method when you pass the optional parameters, you must name that parameter correctly, which in this case would be replace
and not replace.
- You should also bear in mind that when you concatenate strings with integers, floats, etc., you must make a string conversion but it will also cause an exception.
Here is the rectified code:
# import matplotlib.pyplot as plt
import numpy as np
# import seaborn as sns
# import random as rm
#Estados(Nodos)-->4 EVENTOS
Estados=["Dormir","Entrenar","Correr","Jugar"]
Lista_Actividad=[]
#Posibles secuencias de eventos.
NombreTransicion=[["DD","DE","DC","DJ"],["ED","EE","EC","EJ"],["CD","CE","CC","CJ"],["JD","JE","JC","JJ"]]
#Matriz de probabilidades(Matriz de transicion).
MatrizTransicion=[[0.1,0.4,0.3,0.2],[0.3,0.1,0.3,0.3],[0.2,0.2,0.3,0.3],[0.3,0.1,0.4,0.2]]
#Verificamos si la matriz esta correcta;debe sumar +1 cada matriz de transicion y dar como resultado 4.
if sum(MatrizTransicion[0])+sum(MatrizTransicion[1])+sum(MatrizTransicion[2])+sum(MatrizTransicion[3])!=4:
print("En alguna parte de la MatrizTransicion,hay un error;Verifique..")
else: print("¡¡¡No hay ningun Error!!!")
#Funcion que implementa la Cadena de Markov para pronosticar estados.
#1-Se Elije el estado inicial.
#2-Se Guarda el estado en una secuancia de array.
#3-Se calcula la probabilidad de Lista_Actividad
def PronosticarActividad(dias):
Actividad_Hoy="Dormir"
print("Empezar Estado: "+Actividad_Hoy)
Lista_Actividad=[Actividad_Hoy]
i=0
prob=1
while i!=dias:
if Actividad_Hoy=="Dormir":
cambio= np.random.choice(NombreTransicion[0],replace=True,p=MatrizTransicion[0])
if cambio=="DD":
prob= prob * 0.1
Lista_Actividad.append("Dormir")
pass
elif cambio=="DE":
prob=prob*0.4
Actividad_Hoy="Entrenar"
Lista_Actividad.append("Entrenar")
elif cambio=="DC":
prob=prob*0.3
Actividad_Hoy="Correr"
Lista_Actividad.append("Correr")
else:
prob=prob*0.2
Actividad_Hoy="Jugar"
Lista_Actividad.append("Jugar")
elif Actividad_Hoy=="Entrenar":
cambio=np.random.choice(NombreTransicion[1],replace=True,p=MatrizTransicion[1])
if cambio=="EE":
prob=prob*0.1
Lista_Actividad.append("Entrenar")
pass
elif cambio=="ED":
prob=prob*0.3
Actividad_Hoy="Dormir"
Lista_Actividad.append("Dormir")
elif cambio=="EC":
prob=prob*0.3
Actividad_Hoy="Correr"
Lista_Actividad.append("Correr")
else:
prob=prob*0.3
Actividad_Hoy="Jugar"
Lista_Actividad.append("Jugar")
elif Actividad_Hoy=="Correr":
cambio=np.random.choice(NombreTransicion[2],replace=True,p=MatrizTransicion[2])
if cambio=="CC":
pro=prob*0.3
Lista_Actividad.append("Correr")
pass
elif cambio=="CD":
prob=prob*0.2
Actividad_Hoy="Dormir"
Lista_Actividad.append("Dormir")
elif cambio=="CE":
prob=prob*0.2
Actividad_Hoy="Entrenar"
Lista_Actividad.append("Entrenar")
else:
prob=prob*0.3
Actividad_Hoy="Jugar"
Lista_Actividad.append("Jugar")
elif Actividad_Hoy=="Jugar":
cambio=np.random.choice(NombreTransicion[3],replace=True,p=MatrizTransicion[3])
if cambio=="JJ":
prob=prob*0.2
Lista_Actividad.append("Jugar")
pass
elif cambio=="JD":
prob=prob*0.3
Actividad_Hoy="Dormir"
Lista_Actividad.append("Dormir")
elif cambio=="JE":
prob=prob*0.1
Actividad_Hoy="Entrenar"
Lista_Actividad.append("Entrenar")
else:
prob=prob*0.4
Actividad_Hoy="Correr"
Lista_Actividad.append("Correr")
i+=1
#return Lista_Actividad
print("Possible states: " + str(Lista_Actividad))
print("End state after "+ str(dias) + " days: " + Actividad_Hoy)
print("Probability of the possible sequence of states: " + str(prob) )
PronosticarActividad(4)