Probability is simply the chance of the occurrence of an event. For example, tossing a fair coin has only two possibilities. Either heads or tails will come on top of the coin. The chance of either heads or tails is 50% or 0.5 (on a scale of 0-1).
Note: Commonly, the probability is scaled from 0 to 1.
The occurrence of getting either heads or tails in a fair, unbiased coin toss is always 0.5.
Experiment: This is the case study under consideration. For example, the coin toss is the experiment in our case.
Sample Space: This is the total number of possible outcomes in an experiment. For example, the total possible outcomes in a coin toss experiment is 2, either heads or tails. Sample space is commonly written as .
Event: This refers to a particular case in an experiment. For example, getting heads on the face of the coin is an event. The event is commonly written as .
Probability: This is the final result of the probability. The probability is commonly written as .
=
Let’s try the coin toss simulation using two methods:
random
modulerandom
module)import randomdef coinToss():return random.choice(["Heads", "Tails"])for i in range(5):print coinToss()
Line 1: import random
loads the random
module that contains many random number generation functions.
Line 3: A function named coinToss()
is defined.
Line 4: random.choice
returns a random value from the given list ["Heads", "Tails"]
. It either returns Heads
or Tails
randomly every time this line is executed.
Line 6-7: A for
loop is set up that calls coinToss()
five times, and a random value from ["Heads", "Tails"]
is returned every time.
In probability theory, binomial distribution is a probability distribution that gives only two possible results, either success or failure.
It uses the following equation:
Here, is the number of trials executed, is the number of successes, and is the probability.
In Python, the function used for binomial distribution is numpy.random.binomial(n,p)
. It comes from the numpy
module’s random
class. It takes two parameters. One is n
which is the number of trials, and the second is p
which is the probability of the event under consideration.
import numpydef coinToss(p):prob = numpy.random.binomial(1,p)return probprobability = 0.5samplespace = ["Heads", "Tails"]for i in range(5):result = coinToss(probability)print samplespace[result]
Line 1: import numpy
loads the numpy
module that contains the binomial
function.
Line 3: A function named coinToss()
is defined that takes the probability as a parameter.
Lines 4-5: The function numpy.random.binomial(1,p)
takes two parameters, the first is the number of trials and the second is the probability value. It then generates either 0
or 1
, which is stored in a variable prob
and returned.
Line 7: The probability
value is set to 0.5
which is the probability of a coin toss.
Line 8: An array named samplespace
is created to store heads and tails which can be displayed to the user.
Lines 10-12: A loop is set up that executes five times and calls the coinToss()
function and displays the result from samplespace
as Heads
or Tails
.
Free Resources