Probabilities Notes
My learning notes for probabilities.
- Random Variables
- Probabilty Distribution
- Joint Probability
- Conditional Probability
- Marginal Probability
- Uniform Distribution
- Continuous Variables
- Conditional Events can be of 2 types
Random Variables
Random variables are the variables that can take multiple values depending on the result of a random event. e.g. Throwing a dice, tossing a coin, height of a person, size of a rock, etc.
These can be Discrete
: fixed number of values it can take, for example, tossing a coin can take only 2 values, heads or tails
or Continuous
: any(infine) number of possible values, for example, size of a rock, could be as small as a bug or as large as the mount everest.
Probabilty Distribution
The probability of a random variable describes the probability of each possible result or outcome. In case of a coin toss the probability of both heads and tails is 1/2 so, the probability distribution is 1/2, 1/2. The PD always sums to 1. Higher probaility implies more likely
- The probaility distribution of discrete random variable is called
Probaility Mass Function
- The PD of continuous random variable is called
Probabilty Density Function
import numpy as np
import random
import matplotlib.pyplot as plt
# Probaility Mass Function of "Throwing a Dice"
num_throws = 10000
outcomes = np.zeros(num_throws)
discrete_outcomes = [1, 2, 3, 4, 5, 6]
for i in range(num_throws):
outcomes[i] = random.choice(discrete_outcomes)
val, counts = np.unique(outcomes, return_counts=True)
prob = counts / num_throws
plt.bar(val, prob)
plt.ylabel("Probability")
plt.xlabel("Outcome")
plt.show()
plt.close()
# verify the output is close enough to theoretical value
np.allclose(prob, np.ones(len(prob)) * 1/ 6, atol=0.05)
An example of probabilty distributions of 2 discrete random variables
Joint Probability
P(X=x, Y=y)
What is the probabilty of throwing a dice and getting 1 and
tossing a coin and getting head or tail
Conditional Probability
P(X=x | Y=y)
What is the probability of throwing a dice and getting 1 given that
tossing a coing resulted in a head
Marginal Probability
P(X=x)
What is the probabilty of throwing a dice a getting 1
probabilty distribution of subset of variables
import seaborn as sns
# An example of uniform continuous distribution
x = np.random.uniform(0, 0.5, 10000)
sns.distplot(x)
plt.show()
np.random.seed(123)
# draw 1000 values from a normal distribution with mean=0 and std=1
# drawing values implies that the output values will always satisfy the constrains of the normal distribution
# these constrains in this case are when we find their mean, it will be 0 and their standard deviation will
# be 1
x = np.random.normal(0, 1, 1000)
x.shape
x.mean()
x.std() # nice !
np.random.seed(123)
x = np.random.normal(0, 1, 1000)
y = np.random.normal(0, 1, 1000)
sns.distplot(x)
plt.title('x')
plt.xlim(-4, 4)
plt.show()
sns.distplot(y)
plt.title('y')
plt.xlim(-4, 4)
plt.show()
NOTE
: the y-axis here represents the probaility density and the x-axis is the continuous values