Random Variables
➢Discrete Random Variables
➢Continuous Random Variables
➢The Expectation of a Random Variable
➢The Variance of a Random Variable
2.1 Discrete Random Variable
2.1.1 Definition of a Random Variable (1/2)
• Random variable
• A numerical value to each outcome of a particular experiment
-3 -2 -1 0 1 2 3
2.1.1 Definition of a Random Variable (2/2)
• Example 1 : Machine Breakdowns
• Sample space : S = {electrical , mechanical , misuse}
• Each of these failures may be associated with a repair cost
• State space : {50, 200,350}
• Cost is a random variable : 50, 200, and 350
2.1.2 Probability Mass Function (1/2)
2.1.2 Probability Mass Function (1/2)
• Example 1 : Machine Breakdowns
• P (cost=50)=0.3, P (cost=200)=0.2,
P (cost=350)=0.5
• 0.3 + 0.2 + 0.5 =1
xi 50 200 350
0.3 0.2 0.5
pi
f ( x)
0.5
0.3
0.2
50 200 350 Cost($)
2.1.3 Cumulative Distribution Function (1/2)
• Cumulative Distribution Function
• Function : F ( x) = P( X x) F ( x) = P( X = y )
y: y x
• Abbreviation : c.d.f
F ( x)
1.0
0.5
0.3
0 50 200 350 x($cost)
2.1.3 Cumulative Distribution Function (2/2)
• Example 1 : Machine Breakdowns
− x 50 F ( x) = P (cost x) = 0
50 x 200 F ( x) = P (cost x) = 0.3
200 x 350 F ( x) = P (cost x) = 0.3 + 0.2 = 0.5
350 x F ( x) = P (cost x) = 0.3 + 0.2 + 0.5 = 1.0
2.2 Continuous Random Variables
2.2.1 Example of Continuous Random Variables (1/1)
• Example 14 : Metal Cylinder Production
• Suppose that the random variable X is the diameter of a randomly chosen
cylinder manufactured by the company. Since this random variable can take
any value between 49.5 and 50.5, it is a continuous random variable.
2.2.2 Probability Density Function (1/4)
• Probability Density Function (p.d.f.)
• Probabilistic properties of a continuous random variable
f ( x) 0
statespace
f ( x)dx = 1
2.2.2 Probability Density Function (2/4)
• Example 14
• Suppose that the diameter of a metal cylinder has a p.d.f
f ( x) = 1.5 − 6( x − 50.2) 2 for 49.5 x 50.5
f ( x) = 0, elsewhere
f ( x)
49.5 50.5 x
2.2.2 Probability Density Function (3/4)
• This is a valid p.d.f.
50.5
49.5
(1.5 − 6( x − 50.0) 2 )dx = [1.5 x − 2( x − 50.0)3 ]50.5
49.5
= [1.5 50.5 − 2(50.5 − 50.0)3 ]
−[1.5 49.5 − 2(49.5 − 50.0)3 ]
= 75.5 − 74.5 = 1.0
2.2.2 Probability Density Function (4/4)
• The probability that a metal cylinder has a diameter between 49.8 and
50.1 mm can be calculated to be
50.1
49.8
(1.5 − 6( x − 50.0) 2 )dx = [1.5 x − 2( x − 50.0)3 ]50.1
49.8
= [1.5 50.1 − 2(50.1 − 50.0)3 ]
−[1.5 49.8 − 2(49.8 − 50.0)3 ]
f ( x) = 75.148 − 74.716 = 0.432
49.5 49.8 50.1 50.5 x
2.2.3 Cumulative Distribution Function (1/3)
• Cumulative Distribution Function
x
F ( x) = P( X x) = f ( y )dy
−
dF ( x)
f ( x) =
dx
P ( a X b) = P ( X b) − P ( X a )
= F (b) − F (a )
P ( a X b) = P ( a X b)
2.2.2 Probability Density Function (2/3)
• Example 14
x
F ( x) = P( X x) = (1.5 − 6( y − 50.0) 2 ) dy
49.5
= [1.5 y − 2( y − 50.0)3 ]49.5
x
= [1.5 x − 2( x − 50.0)3 ] − [1.5 49.5 − 2(49.5 − 50.0)3 ]
=1.5 x − 2( x − 50.0)3 − 74.5
P(49.7 X 50.0) = F (50.0) − F (49.7)
= (1.5 50.0 − 2(50.0 − 50.0)3 − 74.5)
−(1.5 49.7 − 2(49.7 − 50.0)3 − 74.5)
= 0.5 − 0.104 = 0.396
2.3 The Expectation of a Random Variable
2.3.1 Expectations of Discrete Random Variables
(1/2)
• Expectation of a discrete random variable with p.m.f
P ( X = xi ) = pi
E ( X ) = pi xi
i
• Expectation of a continuous random variable with p.d.f f(x)
E( X ) = state space
xf ( x)dx
• The expected value of a random variable is also called the mean of the
random variable
2.3.1 Expectations of Discrete Random
Variables (2/2)
• Example 1 (discrete random variable)
• The expected repair cost is
E (cost) = ($50 0.3) + ($200 0.2) + ($350 0.5) = $230
2.3.2 Expectations of Continuous Random
Variables (1/2)
• Example (continuous random variable)
• The expected diameter of a metal cylinder is
50.5
E( X ) = x(1.5 − 6( x − 50.0)2 )dx
49.5
• Change of variable: y=x-50
0.5
E ( x) = ( y + 50)(1.5 − 6 y 2 )dy
−0.5
0.5
= (−6 y 3 − 300 y 2 + 1.5 y + 75)dy
−0.5
= [−3 y 4 / 2 − 100 y 3 + 0.75 y 2 + 75 y ]0.5
−0.5
= [25.09375] − [−24.90625] = 50.0
2.4 The variance of a Random Variable
2.4.1 Definition and Interpretation of Variance
(1/2)
2.4.1 Definition and Interpretation of
Variance (2/2)
Var( X ) = E (( X − E ( X )) 2 )
= E ( X 2 − 2 XE ( X ) + ( E ( X )) 2 )
= E ( X 2 ) − 2 E ( X ) E ( X ) + ( E ( X )) 2
= E ( X 2 ) − ( E ( X )) 2
f ( x)
Two distribution with
identical mean values but
different variances
x
2.4.2 Examples of Variance Calculations (1/1)
• Example 1
Var( X ) = E (( X − E ( X )) 2 ) = pi ( xi − E ( X )) 2
i
= 0.3(50 − 230) 2 + 0.2(200 − 230) 2 + 0.5(350 − 230) 2
= 17,100 = 2
= 17,100 = 130.77
Skewness of Statistical Data
• Skewness is a measure of the asymmetry of the probability
distribution of a real-valued random variable about its mean. In
simpler terms, it indicates whether the data is concentrated
more on one side of the mean compared to the other side.
Interpretation of skewness
• Skewness is a measure of the asymmetry of a distribution. It tells us
about the extent to which the data deviates from a symmetric
distribution. Here's how to interpret skewness:
• Skewness value around 0: A skewness value close to zero (between
-0.5 and 0.5) indicates that the distribution is approximately
symmetric. This means that the data are evenly distributed around the
mean, with roughly equal frequencies of values on both sides.
[Link] skewness: If the skewness is negative (less than -0.5), it
indicates that the left tail of the distribution is longer or stretched out
compared to the right tail. In other words, the majority of the data points
are concentrated on the right side of the distribution, with a few
extremely low values dragging the mean to the left.
[Link] skewness: Conversely, if the skewness is positive (greater than
0.5), it suggests that the right tail of the distribution is longer or stretched
out relative to the left tail. In this case, most of the data points are
clustered on the left side of the distribution, with a few extremely high
values pulling the mean to the right.
[Link] of skewness: The magnitude of the skewness value indicates
the degree of asymmetry. Larger positive or negative values imply greater
asymmetry. Skewness values less than -1 or greater than 1 are
considered highly skewed.
Statistics Correlation
• Correlation measures the relationship between two variables.
• Correlation Coefficient
• The correlation coefficient measures the relationship between two
variables.
• The correlation coefficient can never be less than -1 or higher than
1.
• 1 = there is a perfect linear relationship between the variables (like
Average_Pulse against Calorie_Burnage)
• 0 = there is no linear relationship between the variables
• -1 = there is a perfect negative linear relationship between the
variables (e.g. Less hours worked, leads to higher calorie burnage
during a training session)
Correlation Matrix
• A matrix is an array of numbers arranged in rows and columns.
• A correlation matrix is simply a table showing the correlation coefficients
between variables.
Heat Map to Visualize the Correlation Matrix
What is Covariance?
• For two random variables X and Y:
• It measures how much two variables vary together:
• Positive covariance → as X increases, Y tends to increase.
• Negative covariance → as X increases, Y tends to decrease.
• Zero covariance → no linear relationship between X and Y.
Pair Plots
• A pairs plot allows us to see both distribution of single variables and
relationships between two variables.
• Simple 2D Scatter plot is used to understand the relationship or pattern between
two variables or dimensions in our dataset.
3 dimensions or features in our dataset as we humans do have the capability to visualize more
than 3 dimensions?
One solution to this problem is pair plots. They are used to plot features when we have more
than three dimensions. As the name suggests we actually do pairs of features and plot them all.
For example,
let’s say we have four features ‘sepal_length’, ‘sepal_width’, ‘petal_length’ and ‘petal_width’ in
our iris dataset. In that case, we will have 4C2 plots i.e. 6 unique plots. The pairs in this case will
be :
1.(sepal_length, sepal_width)
2.(sepal_length, petal_length)
3.(sepal_length, petal_width)
4.(sepal_width, petal_length)
5.(sepal_width, petal_width)
6.(petal_length, petal_width).
Data Cleaning & Preprocessing:- Missing Data
• When some entries in your dataset are blank, NaN (Not a Number),
or NULL, that means the data is missing.
Why Missing Data Happens
•Human input errors
•Device/sensor failure
•Data extraction issues
•Survey non-response
If not handled, missing data can:
•Distort model training
•Reduce accuracy
•Cause code errors
Method For Missing Data
Method Description Use Case
When few values are
Remove rows or columns
Dropna (Deletion) missing and removing them
containing missing values
won’t hurt data quality
Replace missing numerical values Works well if data is
Mean Imputation
with the mean of the column roughly symmetric
Replace missing categorical values For non-numeric data like
Mode Imputation
with the most frequent category Gender or City
Remove duplicates and outliers (Z-score, IQR method)
import pandas as pd
# Example dataset
Before feeding data into any model, data = {
'Age': [25, 30, 25, 40, 100],
we must ensure: 'Salary': [40000, 50000, 40000, 70000, 100000],
• Duplicates → Don’t bias the model }
by repeating the same sample. df = [Link](data)
• Outliers → Extreme values can print("Original Data:")
distort means, variances, and print(df)
regression fits.
# Remove duplicates
df = df.drop_duplicates()
print("\nAfter Removing Duplicates:")
print(df)
Detect and Remove Outliers
There are several methods, the two most common are:
A) Z-Score Method
Z-score tells how far a data point is from the mean in units of standard deviation (σ):
B) IQR Method (Interquartile Range)
IQR measures the middle 50% of data:
Interpretation:
•Any value below lower bound or above upper bound is dropped.
Step 3: Find Q1 (First Quartile) and Q3 (Third
Step 2: Find the Median (Q2)
Quartile)
Any value < 39.0 or > 67.0 is an outlier.
Outliers = 90, 95
Data Cleaning & Preprocessing
Feature scaling: StandardScaler and MinMaxScaler
• Feature Scaling is one of the most important preprocessing steps in machine
learning, especially before using models like KNN, SVM, Logistic Regression, or
Gradient Descent–based algorithms.
What is Feature Scaling?
Feature scaling ensures that all features contribute equally to the model by bringing them to a
similar range.
Feature Meaning Range
Age Person’s age 0–100
Income Annual income 0–1,000,000
If you don’t scale:
•Income dominates Age due to large numeric range.
•Gradient descent converges slowly.
•Distance-based models (KNN, clustering) give biased results.
Types of Scaling
We’ll focus on two most common techniques:
Sampling and Data Splitting
Random Sampling vs Stratified Sampling
Random Sampling
•Each data point is chosen completely at random from the dataset.
•The probability of selection is the same for all samples.
•It may not preserve class proportions, which can cause imbalance
in train/test sets.
Use When: The dataset is large and already balanced.
Example:
If you have 1000 samples of students (500 passed, 500 failed) —
random sampling might accidentally select 700 passed and 300 failed in training →
imbalance!
Stratified Sampling
•Ensures each class (or category) is represented in the same
proportion in train/test sets.
•It’s especially useful in classification problems.
Use When: Dataset is small or class distribution is imbalanced.
Example:
If 70% of students passed and 30% failed in the original dataset,
then stratified sampling ensures 70/30 proportion in both train and
test sets.
Sampling and Data Splitting
Train-Test Split using sklearn.model_selection
Purpose
•To evaluate the model’s ability to generalize to unseen data.
•We divide data into:
•Training Set: Used to learn model parameters.
•Test Set: Used to assess performance.
What is a Normal Distribution?
• A Normal (Gaussian) distribution is a continuous probability distribution that
describes how values of a variable are distributed — most values cluster around
the mean, and probabilities taper symmetrically toward the tails.
• It is the bell-shaped curve you’ve seen often.
Multivariate Normal Distribution
• The Multivariate Normal Distribution (MVN) generalizes the normal distribution
to two or more variables that may be correlated.
Example