Objectives

  1. Understand how computers can be used to represent real-world phenomena or outcomes
  2. Compare simulations with real-world contexts.
  3. Implement code to mimic real world situations, problems, or phenomena.

What are simulations by College Board definition?

  • Simulations are abstractions that mimic more complex objects or phenomena from the real world
    • Purposes include drawing inferences without the constraints of the real world
  • Simulations use varying sets of values to reflect the changing state of a real phenomenon
  • Often, when developing a simulation, it is necessary to remove specific details or simplify aspects
    • Simulations can often contain bias based on which details or real-world elements were included/excluded
  • Simulations allow the formulation of hypotheses under consideration
  • Variability and randomness of the world is considered using random number generators
  • Examples: rolling dice, spinners, molecular models, analyze chemicals/reactions...

Analyzing an Example: Air-Traffic Simulator

  • Say we want to find out what the optimal number of aircrafts that can be in the air in one area is.
  • A simulation allows us to explore this question without real world contraints of money, time, safety
    • Unfortunately we can't just fly 67 planes all at once and see what happens
  • Since the simulation won't be able to take all variables into control, it may have a bias towards one answer
  • Will not always have the same result

Functions we often need (python)

import random # a module that defines a series of functions for generating or manipulating random integers
random.choice() #returns a randomly selected element from the specified sequence
random.choice(mylist) # returns random value from list
random.randint(0,10) #randomly selects an integer from given range; range in this case is from 0 to 10
random.random() #will generate a random float between 0.0 to 1.

Functions we often need (js)

// Math.random(); returns a random number
// Math.floor(Math.random() * 10); // Returns a random integer from 0 to 9:

College Board Question 1

Question: The following code simulates the feeding of 4 fish in an aquarium while the owner is on a 5-day trip:

numFish ← 4

foodPerDay ← 20

foodLeft ← 160

daysStarving ← 0

    REPEAT 5 TIMES {

    foodConsumed ← numFish * foodPerDay

    foodLeft ← foodLeft - foodConsumed

    IF (foodLeft < 0) {

    daysStarving ← daysStarving + 1

    }

}

  • This simulation simplifies a real-world scenario into something that can be modeled in code and executed on a computer.
  • Summarize how the code works:

Examples

Card Flip

import random

cards = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] 
suits = ["Diamonds", "Hearts", "Spades", "Clubs"]

print(random.choice(cards) + " of " + random.choice(suits))
Ace of Diamonds

Coin Flip

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 1) #picks either 0 or 1 randomly 
    if randomflip == 0: #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 1: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
Tails
Tails
Tails
Tails
Tails

Your turn: Change the code to make it simulate the flipping of a weighted coin.

Weighted Coin, 75 percent chance of heads and 25 percent chance of tails

import random

def coinflip():         #def function 
    randomflip = random.randint(0, 3) #picks either 0 or 1 randomly 
    if randomflip <= 3: #assigning 0 to be heads--> if 0 is chosen then it will print, "Heads"
        print("Heads")
    else:
        if randomflip == 4: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
            print("Tails")

#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
Heads
Heads
Heads
Heads
Heads

Adding images (in Python)

  • Add a heads and tails images into your images directory with the correct names and run the code below
import random

# importing Image class from PIL package
from PIL import Image
 
# creating a object
im = Image.open(r"../images/HeadsOn.png")
image = Image.open(r"../images/TailsOn.png")

i=random.randint(0,1)

if i == 1:
    print("heads")
    display(im)

else:
    print("tails")
    display(image)
tails

In order to display an image in python, we can use the PIL package we previously learned about.

Spin the Wheel

import random

print("Spin the wheel!")
print("----------------------------------")

n = 300
blue = 0
red = 0
 
for i in range(n):
    spin = random.randint(1,2)
    if spin == 1: # head
        blue = blue + 1
    else:         # tail
        red = red + 1

image = Image.open(r"../images/redblue.png")
 
print('Number of blue:', blue)
print('Number of red:', red)
display(image)
Spin the wheel!
----------------------------------
Number of blue: 150
Number of red: 150

Your turn: Add a visual to the simulation!

Population Growth and Plots

import random

totalPopulation = 50 
growthFactor = 1.00005
dayCount = 0 #Every 2 months the population is reported

while totalPopulation < 1000000:
    totalPopulation *= growthFactor
    #Every 56th day, population is reported
    dayCount += 1
    if dayCount == 56: 
        dayCount = 0
        print(totalPopulation)

# I cleared the cell output cause it was long

Here we initialize the total population to be 50, then set the growth factor as 1.00005 (.005 percent change). It will print the population every 56th day until it reaches one million. It multiplies the current population by the growth factor in each iteration, and increments the day count. When the day count reaches 56, it prints the current population and resets the day count to 0.

Note! This simulation assumes that the growth factor remains constant as time progresses, which may not be a realistic assumption in real-world scenarios.

import matplotlib.pyplot as plt

# Define the initial population and growth rate
population = 100
growth_rate = 0.05

# Define the number of years to simulate
num_years = 50

# Create lists to store the population and year values
populations = [population]
years = [0]

# Simulate population growth for the specified number of years
for year in range(1, num_years+1):
    # Calculate the new population size
    new_population = population + (growth_rate * population)
    # Update the population and year lists
    populations.append(new_population)
    years.append(year)
    # Set the new population as the current population for the next iteration
    population = new_population
    
# Plot the population growth over time
plt.plot(years, populations)
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('Population Growth Simulation')
plt.show()

If we create quantative data, we can plot it using the Matplotlib library.

Example on how simplification can cause bias

import random

beak =  ["small-beak", "long-beak", "medium-beak"],
wing = ["small-wings", "large-wings", "medium-wings"],
height = ["short", "tall","medium"]


naturaldisaster = ["flood", "drought", "fire", "hurricane", "dustbowl"]


print("When a" , random.choice(naturaldisaster) , "hit",  random.choice(height), "birds died") 
When a dustbowl hit tall birds died

How does this simulation have bias?

It is random, that is the bias. It makes it seem that if a small-winged bird dies to a drought, then that is the problem for a small-winged bird, but it is random and could have happened to a bird of any trait.

JS examples

Hacks

  • Answer all questions and prompts in the notes (0.2)
  • Create a simulation
    1. Create a simulation that uses iteration and some form of data collection (list, dictionary...) (0.4)
      • try creating quantative data and using the Matplotlib library to display said data
      • Comment and describe function of each parts

Shown within the flowchart and in notes in code

    - How does your simulation help solve/mimic a real world problem?

It helps people to see how an illness can be spread throughout a school, this is important due to the effects of Covid on us as a community within the past 3 years.

    - Is there any bias in your simulation? Meaning, are there any discrepancies between your program and the real event?
It assumes that everyone has the same probability to become sick. It does not factor in people taking protective measures, or people who got sick from other causes outside of school.
import random
import matplotlib.pyplot as plt

num_people = 1 # initial number of people

days = []
people_per_day = []

prob_spread = 0.4 # chance of it spreading

num_days = 30 # length of simulation

for day in range(num_days): # runs code for each day
    days.append(day)
    people_per_day.append(num_people)
    
    new_people = 0
    for person in range(num_people):
        if random.random() < prob_spread:
            new_people += 1
    
    num_people += new_people

plt.plot(days, people_per_day) # for the graph
plt.title('Spread of Rumor')
plt.xlabel('Days')
plt.ylabel('Number of People')
plt.show()
  • Answer these simulation questions (0.3) 1: A theme park wants to create a simulation to determine how long it should expect the wait time at its most popular ride. Which of the following characteristics for the virtual patrons would be most useful? Select two answers

The correct answers are A and B as they both affect the users chances of going to a ride. A, would help to determine if people would even like the ride, and B would determine if the ride would be popular based on their distance. C does not matter as food would not correspond to what people ride, and the length of their stay would not matter either.

A programmer has created a program that models the growth of foxes and rabbits. Which of the following potential aspects of the simulation does NOT need to be implemented?

The correct answer is A because the amount of food each rabbit eats does not contribute to the relationship between the fox and the rabbit.

The heavy use of chemicals called chlorofluorocarbons (CFCs) has caused damage to the Earth’s ozone layer, creating a noticeable hole over Antarctica. A scientist created a simulation of the hole in the layer using a computer, which models the growth of the hole over many years. Which of the following could be useful information that the simulation could produce?

The correct answer is A because the model would benefit by knowing more information on how the ozone layer functioned.

Suppose that an environmentalist wanted to understand the spread of invasive species. What would be a benefit of doing this with a simulation, rather than in real life?

The answer is D because all of the answers above are true to the benefits

A program is being created to simulate the growth of a brain-based on randomly determined environmental factors. The developer plans to add a feature that lets the user quickly run several hundred simulations with any number of factors kept constant. Why would this be useful? Select two answers.

The correct answers are B and C because they are both true on the benefits of simulations

Which of the following statements describes a limitation of using a computer simulation to model a real-world object or system?

The answer is C because the only way around this is by adding more and more info to the simulation, eventually making it useless as it would be a waste of time to run, simplifications are necessary.

  • Bonus: take a real world event and make a pseudocode representation or pseudocode on a flowchart of how you would make a simulation for it (up to +0.1 bonus)

Pseudocode: Initialize variables:

  • number_students: the total number of students in the school
  • number_sick: the number of students currently sick
  • infection_rate: the probability that a healthy student will get sick if they come into contact with a sick student
  • days: the number of days to simulate

Create an empty list to store the number of sick students each day.

For each day in the simulation: Calculate the number of healthy students (number_students - number_sick). For each sick student: Generate a random number between 0 and 1. If the number is less than the infection rate, choose a random healthy student and make them sick. Update the total number of sick students (number_sick) based on the newly infected students. Add the current value of num_sick to the list of sick students for the day.

Plot the number of sick students over time using a line graph.

Flowcharrt here