Special Note: This lecture was delivered using BlackBoard Collaborate Ultra, due to weather closure.

Writing Pseudocode

When faced with a problem such as that in Problem Set 3, I will often write out the flow of my simulation before I start coding. This is called pseudocode in computer programing. In this problemset I would probably write out the pseudocode in several steps.

Number of coin flips it takes to get to 100 heads

Let’s start by considering Problem 3 in Problem Set 2. Let’s work this out together as a group, then see if we can come up with the actual code.

Red fox - arctic fox Interactions

Now on to our main task for the night, prepping you for Problem Set 3.

First, let’s think about simulating a single bout between a red and arctic fox. Note - in all of my pseudocode the indented space is meant to show the various nested levels of my loops or logical conditionals.

if red fox attacks first (won the last bout)

  simulate the probability of the red fox winning when attacking

  if red fox wins
    increase the red fox win count by 1
    indicate that the red fox won
  else if red fox lost
    increase the arctic fox win count by 1
    indicate that the red fox lost

else if red fox defends (lost the last bout)

  simuulate the probability of the red fox winning when defending
  
  if red fox wins
    increase red fox win count by 1
    indicate that the red fox won
  else if red fox lost
    increase the arctic fox win count by 1
    indicate that the red fox lost

Ok, now if I wanted to keep track of who wins overall, I’ll put the above code into a while loop.

while red fox wins < 21 OR arctic fox wins < 21
  if red fox attacks first (won the last bout)
  
    simulate the probability of the red fox winning when attacking
  
    if red fox wins
      increase the red fox win count by 1
      indicate that the red fox won
    else if red fox lost
      increase the arctic fox win count by 1
      indicate that the red fox lost
  
  else if red fox defends (lost the last bout)
  
    simuulate the probability of the red fox winning when defending
    
    if red fox wins
      increase red fox win count by 1
      indicate that the red fox won
    else if red fox lost
      increase the arctic fox win count by 1
      indicate that the red fox lost

if red fox wins >= 21
  note that red fox won
else 
  note that arctic fox won

Last thing, if I wanted to determine a probability of a red fox winning, I could replicate this while loop a set number of times, and keep track of how many times the red fox won overall. Then divide the number of times the red fox won by the number of times I replicatd the while loop.

for ind in 1:1000  #I'm replicating the while loop a 1000 times

  while red fox wins < 21 OR arctic fox wins < 21
    if red fox attacks first (won the last bout)
    
      simulate the probability of the red fox winning when attacking
    
      if red fox wins
        increase the red fox win count by 1
        indicate that the red fox won
      else if red fox lost
        increase the arctic fox win count by 1
        indicate that the red fox lost
    
    else if red fox defends (lost the last bout)
    
      simuulate the probability of the red fox winning when defending
      
      if red fox wins
        increase red fox win count by 1
        indicate that the red fox won
      else if red fox lost
        increase the arctic fox win count by 1
        indicate that the red fox lost
  
  if red fox wins >= 21
    note that red fox won
    increase the red fox total win count by 1
  else 
    note that arctic fox won
    increase the arctic fox total win count by 1


divide total red fox win count by number of replicates (here, 1000)

Now that we have the pseudocode, we can go through this and begin writing our R code.

Additional tips

  • We learned about for loops in the last class. You can simulate the battle between an red fox and arctic fox using a for loop, but using a while loop will be more efficient. Here is a good site for information on how to setup a while loop: http://www.programiz.com/r-programming/while-loop

  • I strongly suggest that you start by simulating the outcome of a single bout between an arctic and red fox, given that the red fox attacks first. Look at how often the red fox wins versus loses.

  • Next simulate a bout when the red fox is on the defensive. How often does the red fox win?

  • As your loop progresses, you will need a way to track who won the previous match. This will help you determine if the red fox is attacking or defending in the current bout.

  • The idea of using a while loop comes from the fact that your simulation should last until a particular criteria is met, specifically until either the the red fox or arctic fox has won 21 bouts. Remember, it’s the first animal to 21 wins that wins overall. If you use the while loop, you shouldn’t need a for loop at all. You will need some indicator of how many bouts have been won by each animal.

  • Within your while loop, you will likely want two different levels of ifelse (or if and else) conditionals. One level will help keep track of whether the red fox is attacking or defending, which depends on the outcome of the previous bout. The second level will help keep track of whether the red fox wins or loses the current bout. Your going to want to monitor both how many wins the red fox and arctic fox have had, and who won the last bout.