Unit 6: Simulations · Exploring simulations

Article

What a simulation is

8 min read

Before a bridge is built, engineers want to know if it will stand in a storm. They cannot build it first and wait for a storm. So they build the bridge inside a computer and send a pretend storm at it. That is a simulation.

A program that imitates something real

A simulation is a program that imitates a real thing or a real process, so we can see what happens without doing it for real. A flight simulator, a computer storm hitting a bridge and a game that grows a city are all simulations.

A simulation cannot copy everything. The real world has endless details. So the programmer first builds a model: a simplified description of the real thing. A model is an abstraction. It keeps the details that matter for the question and leaves the others out on purpose.

From the real thing to a prediction

  1. The real thing

    A bridge in a storm.

    keep what matters
  2. A model

    Only the chosen details: wind speed, weight, strength of the steel.

    write it as code
  3. A simulation

    A program that runs the model.

    run it
  4. A prediction

    What the real bridge might do.

Every step leaves something behind. What you leave out can change the answer.
Kept in the modelLeft out of the model
Wind speedThe colour of the paint
Weight of the bridgeBirds sitting on the rail
Strength of the steelThe exact shape of each bolt

Leaving out the paint colour is a good choice, because it does not change whether the bridge falls. Leaving out the wind would be a bad choice.

Why simulate?

We simulate when trying the real thing is a bad idea. The real thing may be:

Too...Example
DangerousHow a fire spreads through a building
ExpensiveCrashing 100 real cars to test a new design
SlowHow a river carves a valley over thousands of years
BigHow a galaxy turns
SmallHow the parts of a tiny cell move

A simulation is also repeatable. You can run it a thousand times and change one setting each time, such as "What if the wind is twice as strong?" This lets you test ideas and make predictions before you spend any money or risk anyone.

Only as good as its assumptions

An assumption is something the programmer decides to treat as true, to keep the model simple. This tiny simulation assumes that the number of rabbits doubles every month and that none ever die.

Runs in your browser

After 8 months it prints 512 rabbits. In real life the rabbits would run out of food long before that. Change growth to 1.5 and run it again: a different assumption gives a different prediction. The results of a simulation are only as good as the assumptions in it.

Check your understanding

Which statement best describes a simulation?

Bias: the danger of what was left out

A simulation can contain bias: it gives a wrong or unfair picture, because of the choices made when it was built. Imagine a simulation of people leaving a stadium. It assumes that everyone walks at the same speed. It leaves out children, older people and people in wheelchairs. Exits designed from it could be too slow for those people.

Bias is not always on purpose. It often comes from a detail that nobody thought of, or from data that came from only one kind of person. To reduce bias, ask "Who or what is missing?" and compare the results with real data.

Check your understanding

A simulation of people leaving a stadium assumes that everyone walks at the same speed. It leaves out people in wheelchairs. What is this an example of?

Real experiment or simulation?

Neither one is always better.

Real experimentSimulation
DangerMay be dangerousSafe
Cost and timeOften highUsually low and fast
Repeat itHardEasy, as many times as you like
Change one settingHardEasy
Can it be wrong?Real resultsWrong if the model is wrong

A more detailed simulation is usually more accurate, but it is harder to write and slower to run. So a programmer always trades accuracy against speed and cost. A good plan is to use the simulation to find the best few ideas, then test those few for real.

Worked example: the school canteen

A school has 200 students who arrive at the canteen in 20 minutes. The head teacher asks: "Would a third till stop the long queue?"

A real test means changing the canteen for a week and annoying hundreds of hungry students. A simulation can run in seconds with 2, 3 and 4 tills. The programmer assumes that each student takes 30 seconds to serve and that students arrive evenly. These assumptions are probably wrong on pizza day, when serving takes longer. So the answer is a prediction, not a fact. The school can check it with one real trial.

Where Unit 6 goes

This unit fits on one map. Come back to it when you want to see where a lesson belongs.

Simulations
  • Exploring simulations

    • a program that imitates a real process
    • an abstraction that leaves details out
    • simulations in science and in medicine
  • Simulating randomness

    • RANDOM(a, b) gives every whole number from a to b
    • 30% is RANDOM(1, 100) ≤ 30
    • many runs estimate a chance
  • Creating simulations

    • decide what to keep
    • write the rules as code
    • run it and compare with real data
  • Keep in mind

    • too dangerous or slow or costly or big or small to try
    • results are only as good as the assumptions
    • bias can come from what was left out

Common mistakes

  • "A simulation is the real thing." It is a model. It leaves details out on purpose.
  • "More detail is always better." More detail costs time and effort. Keep what matters for the question.
  • "A simulation proves what will happen." It predicts what might happen, if the assumptions are right.
  • "Only wrong code gives bias." Bias can come from what the designer left out, even when the code is perfect.
  • "If a real experiment is possible, a simulation is useless." A simulation is still useful: it is cheaper, safer and repeatable.

On the exam

You may be asked why a simulation is used (the real thing is too dangerous, slow, costly, big or small), or to name a benefit (it is safe, cheap, fast and repeatable) or a limitation (it only follows its assumptions, and it may have bias). Another common question gives a simulation and asks what was left out or simplified, and how that might change the result.

Key words

Simulation: a program that imitates a real thing or process.

Model: a simplified description of something real.

Abstraction: leaving out details that do not matter, to keep the ones that do.

Assumption: something the programmer treats as true to keep the model simple.

Bias: an unfair or wrong picture caused by what the designer chose, left out or used as data.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.