5 | Statistics, Random Numbers & Simulation

In this section, we focus on using statistics functions in Matlab to analyze data and make inferences. Additionally, we look at using random numbers to run simulations.

At the end of this section you should be able to:

  1. Create histograms of datasets and select an appropriate number of bins

  2. Determine if data are normally distributed

  3. Determine probability using both data percentiles and normal distributions

  4. Create scalar, vector and matrix arrays of random numbers from uniform-, normal-, and integer distributions

  5. Use Monte Carlo simulation to create probability distributions of outputs based on the distributions of the input(s)

  6. Assess if enough repetitions of simulation were performed by looking at the convergence

Statistics in Matlab

Statistical Functions: using functions on data sets to find the mean, median, standard deviation and other values.

Histograms: Visualizing the distribution of data. We use the hist function to bin our data and plot it using bar in order to customize its appearance. More on choosing the bin sizing. Note: Matlab has changed the preferred method for histogram creation from “hist” to “histogram”. Both methods currently work but "hist" will likely be phased out in future versions.

Normality: testing data sets for normality using the Lilliefors test.

Additional information

Random Numbers

Generating Different Types of Random Numbers

Random Numbers: using Matlab to generate random numbers (and vectors and matrices) that are selected from a uniform distribution, normal distribution or random integers.

Sierpiński Sieve | Chaos Game (Live Example)

Take three points in the plane to form a triangle, you need not draw it

  1. Randomly select any point inside the triangle and consider that initial position

  2. Randomly select any one of three vertex points

  3. Move half the distance from your current position to the selected vertex

  4. Plot current position

  5. Repeat steps 3-5

Simulation and Monte Carlo Method

Simulation: using random numbers to create synthetic datasets using the Monte Carlo method. This method utilizes the Law of large numbers / Regression to the Mean.

Resonator

Using random numbers to create simulated data. A convergence test is run to ensure enough samples were created. The simulated data is analyzed for its statistical properties to make predictions about the yield in a manufacturing process.

Tile Cutting

Using random numbers to create simulated data. A convergence test is run to ensure enough samples were created. The simulated data is analyzed for its statistical properties to make predictions about the yield in a manufacturing process.

Lewis Carrol “Pillow Problem” (Live Example)

A bag contains one ball, known to be either red or blue with equal probability. A red ball is put in, the bag shaken, and a ball drawn out, which proves to be red. What is now the chance of drawing a red ball?

https://web.nmsu.edu/~pbaggett/Statistics/Lessons/Pillow/Pillow.html

Casino Craps (Live Example)

(Live Example): In a game of casino craps, we roll two dice and find their sum. The fundamental bet is “playing the pass line”, here

  • If the come-out roll is 7 or 11, the bet wins

  • If the come-out roll is 2, 3 or 12, the bet loses (known as “crapping out”)

  • If the roll is any other value, it establishes a point.

    • If, with a point established, that point is rolled again before a 7, the bet wins

    • If, with a point established, a 7 is rolled before the point is rolled again (“seven out”), the bet loses

What is the probability that we win (playing the pass line) a single game of craps?

https://crunchingnumbers.live/2016/01/24/monte-carlo-simulations-craps/

https://en.wikipedia.org/wiki/Craps#Pass_line

Video Game Battle (Live Example)

In Legend of Groöl, your BattleMage is battling a Grimthurst Orge. Your Mage has a 15% chance of missing an attack with their Windsong Cane. The Ogre is using a shield that blocks 10% of incoming attacks. If the Cane hits the Ogre, it deals a random amount of damage from a normal distribution with mean of 60 and standard deviation of 5. On a successful hit, you have a 20% chance of dealing a critical strike which deals double damage.

  • Construct a histogram of your actual damage (per attack) distribution in this battle

  • On an attack, how much damage (on average) can you expect to deal to the Ogre?

Lecture Code

More