lundi 17 août 2020

Why can't I use three if-statements instead of one if-statement and two elif-statements?

I'm struggling to understand the use of elif in a specific piece of code https://www.datacamp.com/community/tutorials/markov-chains-python-tutorial.

In the copy-pasted code below, once entering the while loop, there is one if statement (entered if activityToday == "Sleep") and two elif statements (entered if activityToday == "Run" or activityToday == "Icecream" respectively).

If I run the code, I get the expected output:

Start state: Run
Possible states: ['Run', 'Icecream', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.21

If I change the two elif statements to two if statements, I get some unexpected output:

Start state: Run
Possible states: ['Run', 'Run', 'Run']
End state after 2 days: Run
Probability of the possible sequence of states: 0.25

The problem is that I don't understand the unexpected output. I don't understand why I can't replace the two elif statements with two if statments, and run them as three separate loops instead of one loop with one if and two elif. Could someone please explain it to me?

Thanks for any help!

import numpy as np
import random as rm

# The statespace
states = ["Sleep","Icecream","Run"]

# Possible sequences of events
transitionName = [["SS","SR","SI"],["RS","RR","RI"],["IS","IR","II"]]

# Probabilities matrix (transition matrix)
transitionMatrix = [[0.2,0.6,0.2],[0.1,0.6,0.3],[0.2,0.7,0.1]]

# FROM THE TUTORIAL 

# A function that implements the Markov model to forecast the state/mood.
def activity_forecast(days):
    # Choose the starting state
    activityToday = "Run"
    print("Start state: " + activityToday)
    # Shall store the sequence of states taken. So, this only has the starting state for now.
    activityList = [activityToday]
    i = 0
    # To calculate the probability of the activityList
    prob = 1
    while i != days:
        if activityToday == "Sleep":
            change = np.random.choice(transitionName[0],replace=True,p=transitionMatrix[0])
            if change == "SS":
                prob = prob * 0.2
                activityList.append("Sleep")
                pass
            elif change == "SR":
                prob = prob * 0.6
                activityToday = "Run"
                activityList.append("Run")
            else:
                prob = prob * 0.2
                activityToday = "Icecream"
                activityList.append("Icecream")
        elif activityToday == "Run":                            # WHY CAN'T I CHANGE 'ELIF' TO 'IF' ??????
            change = np.random.choice(transitionName[1],replace=True,p=transitionMatrix[1])
            if change == "RR":
                prob = prob * 0.5
                activityList.append("Run")
                pass
            elif change == "RS":
                prob = prob * 0.2
                activityToday = "Sleep"
                activityList.append("Sleep")
            else:
                prob = prob * 0.3
                activityToday = "Icecream"
                activityList.append("Icecream")
        elif activityToday == "Icecream":                        # WHY CAN'T I CHANGE 'ELIF' TO 'IF' ??????
            change = np.random.choice(transitionName[2],replace=True,p=transitionMatrix[2])
            if change == "II":
                prob = prob * 0.1
                activityList.append("Icecream")
                pass
            elif change == "IS":
                prob = prob * 0.2
                activityToday = "Sleep"
                activityList.append("Sleep")
            else:
                prob = prob * 0.7
                activityToday = "Run"
                activityList.append("Run")
        i += 1  
    print("Possible states: " + str(activityList))
    print("End state after "+ str(days) + " days: " + activityToday)
    print("Probability of the possible sequence of states: " + str(prob))

# Function that forecasts the possible state for the next 2 days
activity_forecast(2)

Aucun commentaire:

Enregistrer un commentaire