Assignment 0
Assignment 0
1 Questions
This assignment has no marks, but its completion will unlock 7 grace days for you across the
semester. If you do not submit Assignment 0, there will be no grace days provided. Please submit
your code as a single iPynb notebook, and a single PDF with all your theory solutions.
1. Numpy: The following questions should be done using numpy operations. Each exercise
can be solved with a maximum of 4-5 lines of Python code. Please avoid the use of iterative
constructs (such as for loops) to the extent possible, and use matrix/vector operations to
achieve the objectives.
Please use https://docs.python.org/3/tutorial/ for any reference required.
(a) Import the numpy package under the name np. Print the numpy version and the
configuration.
(b) Create a null vector of size 10, and output the vector to the terminal.
(c) Create a null vector of size 10 but the fifth value which is 1. Output the vector to the
terminal.
(d) Reverse a vector (first element becomes last).
(e) Create an n × n array with checkerboard pattern of zeros and ones.
(f) Given an n × n array, sort the rows of array according to mth column of array.
(g) Create an n × n array with (i + j)th -entry equal to i + j.
(h) Consider a (6, 7, 8) shape array, what is the index (x, y, z) of the 100th element (of the
entire structure)?
(i) Multiply a 5 × 3 matrix by a 3 × 2 matrix (real matrix product).
(j) Create random vector of size 10 and replace the maximum value by 0.
(k) How to find the closest value (to a given scalar) in an array?
(l) Subtract the mean of each row from each corresponding row of a matrix.
1
(m) Consider a given vector, how to add 1 to each element indexed by a second vector (be
careful with repeated indices - you should consider it only once)?
(n) How to find the most frequent value in an array?
(o) Extract all the contiguous 3 × 3 blocks from a random 10 × 10 matrix.
(p) Compute the rank, trace and determinant of a matrix.
2. Linear Algebra:
3. Probability:
• If two binary random variables X and Y are independent, are X̄ (X̄ is the complement
of X) and Y also independent? Prove your claim.
• Show that if two variables x and y are independent, then their covariance is zero.
• By using a change of variables, verify that the univariate Gaussian distribution given
by:
2 1 1 2
N (x|µ, σ ) = exp − 2 (x − µ)
(2πσ 2 )1/2 2σ
satisfies the equation: Z ∞
E[x] = N (x|µ, σ 2 )xdx = µ
−∞
2
with respect to σ 2 , verify that the Gaussian satisfies the equation:
Z ∞
2
E[x ] = N (x|µ, σ 2 )x2 dx = µ2 + σ 2
−∞
• There are two coins C1 and C2. C1 has a equal prior on a head (H = 1) or tail
(T = 0) and the fate of C2 is dependent on C1. If C1 is a head, C2 will be a head
with probability 0.7. If C1 is a tail, C2 will be a head with probability 0.5. C1 and C2
are tossed in sequence once, and the observed sum of the two coins, S = C1 + C2, is 1.
What is the probability that C1 = T and C2 = H (Hint: use Bayes theorem)?
• Suppose that we have three coloured boxes r (red), b (blue), and g (green). Box r
contains 3 apples, 4 oranges, and 3 limes, box b contains 1 apple, 1 orange, and 0 limes,
and box g contains 3 apples, 3 oranges, and 4 limes. If a box is chosen at random with
probabilities p(r) = 0.2, p(b) = 0.2, p(g) = 0.6, and a piece of fruit is removed from
the box (with equal probability of selecting any of the items in the box), then what is
the probability of selecting an apple? If we observe that the selected fruit is in fact an
orange, what is the probability that it came from the green box?