Probability Basics

In this section we’re going to learn how to work with probabilities.

Probability lets us model the liklihood that particular events will happen

There are three videos for this section. Watch them below


Events

You can also view this video on YouTube

A sample space is the set of possible outcomes of an experiment. When rolling a dice, the sample space contains six outcomes:

⚀ ⚁ ⚂ ⚃ ⚄ ⚅

An event is a set of outcomes. For example the event of rolling an even number on a dice contains three outcomes:

⚁ ⚃ ⚅ 

If all outcomes have equal chance of happening, the probability of an event is the number of favourable outcomes, divided by the total number of outcomes (e.g. 2/6)

For example, if we’re drawing red and blue balls from a bag, drawing a ball is an outcome. We might group these outcomes into a event R for drawing a red ball and an event B for drawing a blue ball. There are 10 outcomes in total: 6 red outcomes and 4 blue outcomes:

Above, \( P(R) = \frac{4}{10} \) and \( P(B) = \frac{6}{10} \).

The chance that event A doesn’t happen is the same as the chance that anything else happens instead. This is the same as:

\[ P(A') = 1 - P(A) \]

which in the example above means \( P(R’) = \frac{6}{10} \) and \( P(B’) = \frac{4}{10} \)


Adding Probabilities

You can also view this video on YouTube

Events can overlap. The intersection of the two events corresponds to the outcomes where the events both happen.

To find the chance that event A happens or event B happens, you add up their probabilities and take away the chance they both happen.

\[ P(A \cup B) = P(A) + P(B) - P(B \cap A ) \]

In the example shown here, the probability of either A or B happening is given by \( P(A \cup B) = \frac{(10+2)}{18} + \frac{(6+2)}{18} - \frac{2}{18} = 1 \)


Multiplying Probabilities

You can also view this video on YouTube

To find the chance of event A given event B you find the chance that A and B co-occour and divide by the probability of B. Read the following “the probability of A given B”:

\[ P(A | B) = \frac{ P(B \cap A) }{ P(B) } \]

In the venn diagram above, the probability \( P(A | B) = \frac{2}{8} / \frac{4}{8} = \frac{2}{4} \)

To find the chance that two events co-occour you multiply their the probability of event A occouring given B by the probability of B.

\[ P(A \cap B) = P(A | B) \times P(B) \]

In venn diagram, the probability of both A and B happening is given by \( P(A \cap B) = \frac{2}{4} \times \frac{4}{8}= \frac{2}{8} \)


Random numbers in Java

You can generate a random number in Java using the Math.random() method from java.lang.Math package.

This gives you a number between 0 and 1. You can scale this manually to get a number in any range you like, as you can see in the demo below.


Random numbers in Python

Generating random numbers in Python uses the random package.

import random
print(random.random()) # Generates a number between 0-1
print(random.randint(1,6) # Generates an integer between 1-6 (inclusive)

Questions

1. Check your understanding

You have a deck of 52 cards (4 suits, no jokers). What is the probability of drawing the following. Express your answers as a fraction in lowest terms.

Check Answers

You have a 12 sided dice. What is the probability of rolling the following. Express your answers as a fraction in lowest terms.

Check Answers

You flip a coin twice. What is the probability of the following. Express your answers as a fraction in lowest terms.

Check Answers

You roll two 20-sided dice. What is the probability of the following. Express your answers as a fraction in lowest terms.

Check Answers

2. Dice roller

Write a Java function int dice(int count, int sides) which simulates any number of dice rolls with a given number of sides. E.g. “roll 7 four-sided dice and return the sum”

3. Question Generator

The following is currently a bit broken as the labels don’t change when you generate a new question. You can still hover over to see the number of outcomes in each subset. (I think) blue = A, orange = B, green = C.

Generate
Show Answer


Summary

In this section we have learned about how to work with probabilities.

  • You should be able to determine the probabilities of simple events.
  • You should be able to find the probabilities \( P(A’) \), \( P(A \cup B ) \), \( P(A \cap B ) \), \( P(A | B ) \)

In the next section we are going to look at combinatorics