Numeric

Using Numpy

Selection from w3resources and rougier/numpy100

Exercise 1  

  • Write a NumPy program to generate five random numbers from the normal distribution.
# code here

Exercise 2 Write a NumPy program to generate six random integers between 10 and 30.

# code here

Exercise 3 Create a 3x3 matrix with values ranging from 0 to 8

# code here

Exercise 4 Create 2d array \(M\) such of size 3*3 such that \(M_{ij} = i\times j\)

# code here

Exercise 5 Create 3 vectors of length 5 and create a matrix where each column is one of the vector.

# code here

Exercise 6 Create 3 vectors of length 5 and create a matrix where each row is one of the vector.

# code here

Exercise 7 Find indices of non-zero elements from np.array([1,2,0,0,4,0]). Replace them with -1.0

# code here

Exercise 8 Write a NumPy program to normalize a 3x3 random matrix. (Define norm \(|x|=\sqrt{\sum x_i^2}\) and compute \(M/|M|\))

# code here

Exercise 9 Create 2d array \(M\) such of size 3*3 such that \(M_{ij} = i\times j\)

# code here

Exercise 10 Take a random matrix \(A\) of size \(N\times 2\) (N=10) where each line represents a different 2d point. Compute the euclidean distance matrix such that \(E_{ij}\) is the distance between point \(i\) and point \(j\).

# code here

Exercise 11 Create A of size \(10\times 3\). Create matrix \(B\) with the same column as \(A\) reordered by sum of absolute values.

Simulating an AR1

Take an AR1 process \(x_t = A \rho x_{t-1} + \epsilon_t\) with \(\epsilon_t \sim \Sigma\) where \(\Sigma\) is a positive definite matrix.

Exercise 12 Define 2x2 matrices \(A\) and \(\Sigma\), the latter being symmetric positive definite

# code here

Exercise 13 Compute asymptotic variance using matrix algebra (there is a recursive formula)

# code here

Exercise 14 Simulate \(N\) draws for \(T\) periods and store the result in sim.

# code here

Exercise 15 Compute ergodic variance (bonus compute std of the variance estimate)

# code here

Exercise 16 Plot a few simulations on the same graph

# code here

Exercise 17 Plot asymptotic distribution (seaborn)

# code here

Exercise 18 Bonus: write a faster simulation routine.

# code here