Probabilistic Perspective
Notebook Cell
!pip install otter-graderNotebook Cell
fname = "ProbabilisticPerspective.zip"
URL = f"https://raw.githubusercontent.com/wecacuee/ECE490-F25-Neural-Networks/refs/heads/master/notebooks/02-linear-models/{fname}"
import urllib
from zipfile import ZipFile
urllib.request.urlretrieve(URL, fname)
ZipFile(fname).extractall()
import otter
grader = otter.Notebook(tests_dir="./tests")Optimization perspective is not enough¶
So far in this course, we stuck to an optimization perspective on Machine Learning. We identified the following steps:
Collecting a training dataset
Selecting a model with weights (also called the parameters) .
Selecting a loss function
Training: Minimizing the total or average loss function to find the optimal weights
For a new input , use as the prediction.
For linear models (or other “simple” models) this perspective works fine. However, in general it does not.
Question Imagine you are designing a pedestrian detector (input: image, output: a box around all pedestrians in the image) for an autonomous car (or a face detector for your ring-camera based security system). You collect a training dataset of pedestrians in Iceland and then try to deploy it in India.
How well is your pedestrian detector likely to work? Very good, very bad, okayish.
Why?
How would describe the reason mathematically?
Answer
The pedestrian detector is likely to be very bad to okayish.
Because pedestrians in Iceland are likely to look different from pedestrians in India. Evidence from facial recognition.
The real world data is called test data. Mathematically, all the samples in both training data and test data must be identically distributed. In other words, they must have the same probability distribution. Let be the true but unknown probability distribution. All samples, in the training data and the test data must come from the same distribution, and .
From a Machine Learning engineer perspective, you can try to get close to this ideal by carefully collecting your training data so that matches the test data as faithfully as possible.
Independent and Identically distributed¶
A random variable is said to be independent from random variable , denoted as if either of the three equivalent conditions are met:
In typical machine learning, we want the data samples to be Independent and Identically Distributed, in short the i.i.d assumption.
Maximum Likelihood estimation¶
The optimization perspective can be related to maximum likelihood estimation under the iid (Independent and Indentically distributed) assumpotion.
Let the and be random vectors for all . Model the probability distribution as a negative log of the loss function:
If the samples are IID, then we can write the probability of the entire dataset as products of sample probabilities
A product of exponents is the summation of their powers,
Denote
Minimizing the loss is same as maximizing the probability .
No free lunch theorem¶
In the era of so much machine learning hype it is important to look back at David Wolpert’s No free lunch theorems. ECE590 students must read Wolpert’s 1996 paper “The Lack of A Priori Distinctions Between Learning Algorithms” and mark the terms, symbols and concepts that do not make sense.


Expectation vs Mean¶
Given i.i.d. random variables with distribution , write the mathematical definition for the mean and that of the expectation. What is the difference?
Question:¶
What is the diference between Empirical risk and Expected risk?
Answer:¶
Empirical risk is the average loss over the dataset while Expected risk is the expectation of the loss when the inputs and outputs are treated as random variables.
For example, if a dataset with and is given and the model predicts labels . Then the empirical risk is the average loss over the dataset samples,
On the other hand to compute Expected risk, we assume that the dataset was independently sampled (IID assumption) from a distribution where the input is an instance of random variable and is an instance of random variable .
where .
If and are continous random variables, then the expectation is the weighted integraal over all possible values of values of and weighted by the joint probability density function (PDF) of and .
where and and is the sample space for and .
Generalization gap bounds in Machine Learning¶
There are many generalization gap bounds in machine learning, often studied under Machine Learning theory; Probably Approximatly Correct (PAC) theory. Mohri 2012, Foundations of Machine Learning is an excellent book to learn more about it.
If the loss is between [0, 1], then for any with Probability at least , we have
where Rademacher Complexity is defined as


Bayes Rule¶
Question:¶
Given two random variables and specify the relationship between and using Bayes rule?
Question:¶
Using CIFAR 10 dataset you want to find your neural network with weights . Define Prior, Likelihood and Posterior in terms of the weights and data . Write Bayes rule to determin posterior from likehood and prior.
Answer:¶
Prior is i.e. the probability of weights before we have seen the dataset.
Likelihood is i.e. the probability of observing the dataset once if we pick a particular choice of weighs .
Posterior is i.e the probability of chosing weights given the dataset .
By Bayes rule, we have:
Question:¶
How can regularization can be interpretted as an application of the Bayes theorem?
Given the dataset , a model , a regularizer and a loss function , show that the following optimization problem can be interpreted as maximum-a-posteriori estimation. In the process show that for the interpretation, we need the IID (independently, identically distributed) assumption over the dataset. List any other assumptions that you need for the interpretation.
where is some positive constant that balances between the loss function and the regularizer.
Answer:¶
Let the and be random vectors for all . Model the probability distribution as a negative log of the loss function:
If the samples are IID, then we can write the probability of the entire dataset as products of sample probabilities
A product of exponents is the summation of their powers,
Denote
The original optimization problem can be written as:
Taking negative exponent on both sides turns the problem into a maximization problem because is a monotonically decreasing function.
The first term is the same as maximizing the likelihood . If we interpret the second term as a prior:
then we can rewrite the original optimization problem as
By Bayes theorem , hence we can write the optimization problem as maximizing the posterior
We can ignore the evidence term , because it is independent of the optimization variable. The original problem reduces to maximizing the posterior, hence maximum a posteriori:
Question:¶
You are doing 0-1 binary classification on the MNIST hand digit classification task. Let detecting the digit 1 be considered the positive class. Using this example, define the terms False negative, False positive, Accuracy, Precision, recall, F1-score.
Answer:¶
False negative: is the case when the predictor classifies the digit as negative i.e. 0 but it is actually 1.
False positive: is the case when the predictor classifies the digit as positive i.e. 1 but it is actually 0.
Accuracy: Total correct classifictions/Total samples .
Precision: True positives / Predicted positives.
Recall: True positives / All actual positives.
Question:¶
What is a ROC (Receiver Operating characteristics) for a binary classifier? Argue why it lies above the diagonal for any reasonable classifier? Also, argue why it must be concave?
Answer:¶

ROC curve is plot between False positive rate (FPR) and True positive rate (TPR) .
ROC curve is always lies above the diagonal because you can construct a trivial classifier that predicts the positive class with probability , i.e. without even looking at the input data . This trivial classifier will form the diagonal of the ROC curve with both FPR and TPR being equal to . Any classifier that works better than this trivial classifier will lie above the diagonal.
Why ROC curve is a concave curve
Suppose you are given hyperparameter values on the ROC curve of a classifier such that:
The first classifier 1 has true positive rate (TPR) is at given FPR .
Similarly second classifier 2 has , assume it’s TPR is .
If you pick the classifier randomly with probability and the other one with probability , and take the output of the picked classifier.
Then you have FPR of this new classifier as . If you get a better TPR than this randomized classifier, the ROC curve will be concave .
The definition of concavity: A function is concave if for all points , and all the following condition is true, .
Question:¶
What is the likelihood ratio test for a binary classifier? Is it better or worse than Posterior ratio test?
Answer:¶
For a binary classifier, likelihood is the probability of observing the evidence given a hypothesis class ,
Likelihood ratio test gives us a classifier that predicts the positive class if the likelihood ratio exceeds a given threshold ,
Posterior is the probability of hypothesis class given an observed evidence ,
Let the posterior ratio test be,
Posterior ratio test and likelihood ratio test are equivalent, because the condition for posterior ratio test can be written as likelihood ratio test,
The last condition is the same condition in the likelihood ratio test with .
Submission¶
Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. Please save before exporting!
Upload the generated zip file to the gradescope autograder
# Save your notebook first, then run this cell to export your submission.
grader.export(run_tests=True)