# Law of Large Numbers

The **law of large numbers** is a powerful law in Data Science that states our average result will **tend towards the expected value the more simulations (or trials) we run**. This law is incredibly valuable and is the key idea behind why we can use simulation to predict the outcomes of complex events.

We can see the law of large numbers in action through simulating a simple game where someone randomly chooses a number **1**, **99** or **101** (just those three numbers!).

- The
**expected average**we can calculate mathematically: (1 + 99 + 101) = 201. 201 / 3 =**67**. - The law of large number tells us that the average of our simulations will
**tend closer to 67**the longer we run our simulation.

To test this, we'll run a simulation a total of **1,000,000** times and then analyze the average when including only the first 10 simulations, the first 100, the first 1,000, the first 10,000, and then all 1,000,000.

## Simulation

num | |
---|---|

0 | 99 |

1 | 1 |

2 | 101 |

3 | 99 |

4 | 99 |

... | ... |

999995 | 101 |

999996 | 101 |

999997 | 101 |

999998 | 99 |

999999 | 99 |

### First 10 Simulations

The following code finds the **mean (average)** of only the first 10 runs of the simulation:

90

Knowing that the expected value is 67, we can calculate the error as being off by **23**! Having so few simulations (just 10) does not provide a very accurate approximation and this is why we're always going to run thousands of simulations!

### First 100 Simulations

73.18

With an additional 90 simulations, the average is **tending towards the expected value** with an error of only 6.18. This is still nearly 10% of the expected value and quite a bit of error.

### First 1,000 Simulations

68.01

With an additional 900 simulations, the average **continues to tend towards the expected value** of 67 -- now with an error of just 1.01. The error has been reduced to less than 2% from the expected value.

### First 10,000 Simulations

66.8696

With 9,000 additional simulations, the error from the expected value is now just **0.1304** and just a tiny fraction of what it was with just 10 or 100 simulations.

### All 1,000,000 Simulations

67.060252

After 1,000,000 simulations, the average value is **incredibly close** to the expected value (simulation error of 0.060252). In this example, you saw the **law of large numbers** play out across through the simulation!