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)
True

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

Uniform Distribution

Each value us qually likely to occur. In other words, the probabilty of each value is equal

import seaborn as sns

# An example of uniform continuous distribution
x = np.random.uniform(0, 0.5, 10000)

sns.distplot(x)
plt.show()
/usr/local/lib/python3.9/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)

Continuous Variables

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
(1000,)
x.mean() 
-0.03956413608079184
x.std() # nice !
1.0007875375162334
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()
/usr/local/lib/python3.9/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.9/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)

NOTE: the y-axis here represents the probaility density and the x-axis is the continuous values

Conditional Events can be of 2 types

  • Dependent Events: Drawing 2 cards from the same deck without replacement.

  • Independent Events: One event does not affect the other such as, Throwing a dice and tossing a coin