HW 3 Part 2-2

HW 3 Part 2-2

The multivariate normal probability density function is

\[ p\left(\boldsymbol z \right)=\left(2\pi\right)^{-\left(\frac{T}{2}\right)}\det\left(\Sigma\right)^{-\frac{1}{2}}\exp\left(-.5\left(\boldsymbol z-\mu\right)^{\prime}\Sigma^{-1}\left(\boldsymbol z-\mu\right)\right) \tag{1}\]
  • where \( \boldsymbol \mu =\operatorname{E} \boldsymbol z \) is the mean of the \(Tx1\)-dimensional random vector \( \boldsymbol z\)

  • and \( \Sigma=\operatorname{E}\left(\boldsymbol z -\boldsymbol \mu \right)\left(\boldsymbol z- \boldsymbol \mu \right)^\prime = \operatorname{cov}(\boldsymbol z) \) is the covariance matrix of \( \boldsymbol z \)

# TODO
# import numpy

2.1 Create Mu as

\[\begin{split} \boldsymbol \mu = \begin{bmatrix} 1 \\ 1 \end{bmatrix} \end{split}\]
# TODO 
#MU = ...
#assert Mu.shape==(2,1)

2.2 Create SIGMA as

\[\begin{split} \mathbf{\Sigma} = \begin{bmatrix} 1 & 0.9 \\ 0.9 & 1 \end{bmatrix} \end{split}\]
# TODO 
#SIGMA = ...
#assert SIGMA.shape==(2,2)

2.3 Compute the determinant of SIGMA

\[\det\left(\Sigma\right)\]

HINT: see here

#TODO
#detSIGMA = ...

2.4 Compute the inverse of SIGMA

\[\Sigma^{-1}\]

HINT: see here

#TODO
#invSIGMA = ...

2.5 Generate 1 draw from bivariate normal distribution with mean Mu and covariance SIGMA, using

gen = np.random.default_rng(100)

HINT: the multivariate_normal function expects mean as a 1-D array. What is the dimension of Mu?

#TODO
# gen = ...
# z = 
# print(z)

2.6 Compute the difference between z and Mu and assign it to variable residual which has the same dimensions as Mu

Hint check the shape of z

#TODO
#residual = ...

2.7 Compute

\[\left(\boldsymbol z-\mu\right)^{\prime}\Sigma^{-1}\left(\boldsymbol z-\mu\right)\]

and assign the value to variable dist

#TODO
#dist = ...

2.8 Compute

\[ \left(2\pi\right)^{-\left(\frac{T}{2}\right)}\det\left(\Sigma\right)^{-\frac{1}{2}} \]

and assign ot to variable const

#TODO
#const = 

2.9 Compute the value probability density function

\[ p\left(\boldsymbol z \right)=\left(2\pi\right)^{-\left(\frac{T}{2}\right)}\det\left(\Sigma\right)^{-\frac{1}{2}}\exp\left(-.5\left(\boldsymbol z-\mu\right)^{\prime}\Sigma^{-1}\left(\boldsymbol z-\mu\right)\right) \tag{1}\]

and assign it to variable likelihood

#TODO
#likelihood = ...

2.10 Compute the log of the likelihood and assign it to variable log_likelihood

#TODO
#log_likelihood = ...

2.11 Write a function which takes a T dimensional vector z, a T-dimensional vector Mu, and a TxT dimensional matrix SIGMA, and returns the value of the gaussian log-likelihood. Test it with the z, Mu, and SIGMA you generated above.

HINT: Since we want the log-likelihood, it is not necesary to first compute the likelihood, as you did above. You can apply the log to the expression in (1) (or google gaussian loglikelihood) and implement that formula directly. That would be simpler, but following the same steps as above is also acceptable as a solution.

def get_log_lik(z, Mu, SIGMA):
    #TODO
    # ...
    #log_likelihood = ...
    return log_likelihood
#log_lik = get_log_lik(z, Mu, SIGMA)
#np.testing.assert_almost_equal(log_likelihood, log_lik)