$29
The objective of this lab assignment is to familiarize you with the numpy and matplotlib libraries in python.
• Problem on Probability
1.1 Theoretical Problems
(a) Let X FX be a random variable with given CDF FX . Let there be another random variable Y U nif[0; 1]. Express X in terms of Y and
prove your result. (1.5 Marks)
(b) Let X be an exponential distribution with parameter . Using the above
result, obtain X in terms of Y . (0.5 Marks)
1.2 Programming
Let X be from the exponential distribution with = 1:0. Complete the function modify uniform in the le probability.py to generate samples of this variable
using only the numpy.random.uniform function. (1 Mark)
• Vectorization
In this problem you will implement a function to compute the pair-wise L2 similarity between each pair of points in a set.
Let X 2 Rn d where n is the number of points in the the set and d is the number of dimensions of the basis if the points, the L2 similarity between two points x and y is de ned as
d
Xi
yi)2
d(x; y) =
(xi
=1
(a) Obtain a vector expression for d(x; y) when x; y 2 Rd
(1 Mark)
1
(b) Complete the function pairwise similarity looped in the le similarity.py
to obtain this matrix K using for loops. (0.5 Marks)
The above O(n2d) solution doesn’t scale well. However, numpy provides a powerful mechanism called vectorization which can speed up this process drastically.
(c) Complete the function pairwise similarity vec in the le similarity.py
to obtain K in a vectorized manner. Refer to the comments in the function
for an approach to this problem. (2 Marks)
(d) Run the le similarity.py for multiple values of d and n using the com-mand
$ python3 s i m i l a r i t y . py dim <d> num <n>
How do you expect time take for the vectorized and looped functions to grow with dimension and number of samples? Plot 4 graphs showing the time vs dimension and time vs number of samples for the two functions
and include these in your solutions le. (0.5 Mark)
• Probability and simulation
You are given a special coin for which probability of getting a head is 0.75 and probability of getting a Tail is 0.25. You are told to keep ipping the coin till you get two consecutive heads. What is the expected number of ips that you have to make?
(a)
Compute the expected value analytically.
(2 Marks)
(b)
Write a numpy program in the le simulation.py to simulate this exper-
iment for n = 10; 100; 1000; 10000 steps and plot a graph of the observed
expected value vs number of steps. Report your observations. Run each
experiment 10 times and plot error bars as well.
(1 Mark)
Submission Instructions
Collate all the theoretical solutions into ONE pdf le named answers.pdf. Add the graphs from Problems 2 and 3 as well. Submit this le along with the python les simulation.py, probability.py and similarity.py, zip them in a folder named <roll no> assignment 0.zip. The starter folder is present in the les uploaded with the assignment.
Upload the zipped folder to Moodle by Saturday, 22 August, 11:55 PM.
2