by Fedor Iskhakov, ANU
Description: Examples of questions and answers in the exam.
In Python code when assigning a value to a variable, how many spaces should be used around the “=” sign according to PEP8?
How does convergence rate of the value function iterations (successive approximation) algorithm depend on the discount coefficient $ \beta $?
In NumPy what arithmetic operations differ between the two-dimensional array type and the matrix type variables?
import numpy as np
a = np.array([[1,2,3],[0,3,5],[7,4,1]])
b = np.matrix([[1,2,3],[0,3,5],[7,4,1]])
print(a+a,b+b,sep='\n',end='\n\n')
print(a-a,b-b,sep='\n',end='\n\n')
print(a*a,b*b,sep='\n',end='\n\n')
print(a/a,b/b,sep='\n',end='\n\n')
print(a@a,b@b,sep='\n',end='\n\n')
[[ 2 4 6] [ 0 6 10] [14 8 2]] [[ 2 4 6] [ 0 6 10] [14 8 2]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[ 1 4 9] [ 0 9 25] [49 16 1]] [[22 20 16] [35 29 20] [14 30 42]] [[ 1. 1. 1.] [nan 1. 1.] [ 1. 1. 1.]] [[ 1. 1. 1.] [nan 1. 1.] [ 1. 1. 1.]] [[22 20 16] [35 29 20] [14 30 42]] [[22 20 16] [35 29 20] [14 30 42]]
/usr/local/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: invalid value encountered in true_divide import sys
Write the Bellman equation for the infinite horizon formulations of the following problem:
Write a list comprehension expression to produce pairs of squares and cubes of all integers between 1 to 10 inclusive.
squares_and_cubs = [(x**2,x**3) for x in range(1,11)]
print(squares_and_cubs)
[(1, 1), (4, 8), (9, 27), (16, 64), (25, 125), (36, 216), (49, 343), (64, 512), (81, 729), (100, 1000)]
The code below outputs a negative value. This is strange because it calculates an average of the powers of 2, which are all positive numbers. Explain what is going on and fix the bug with minimal change to the code.
import numpy as np
a = 2**np.arange(100)
print(a.mean())
-2.56
Derive your own algorithm and implement it in a function that takes an integer decimal number and outputs its binary representation. Write your code in the cell below and run the test cases.
def binary(x):
'''Input: integer in decimal, output: vector of 0 and 1 to represent the input in binary'''
# write your code here
return []
print(binary(1))
print(binary(2))
print(binary(3))
print(binary(4))
print(binary(1673))
print(binary(3428))
[] [] [] [] [] []
def binary(x):
'''Input: integer in decimal, output: vector of 0 and 1 to represent the input in binary'''
d = []
while True:
d.append(x%2)
x//=2
if x==0:
return d[::-1]
for i in range(32):
print(i,binary(i),sep=' = ')
print(binary(1))
print(binary(2))
print(binary(3))
print(binary(4))
print(binary(1673))
print(binary(3428))
0 = [0] 1 = [1] 2 = [1, 0] 3 = [1, 1] 4 = [1, 0, 0] 5 = [1, 0, 1] 6 = [1, 1, 0] 7 = [1, 1, 1] 8 = [1, 0, 0, 0] 9 = [1, 0, 0, 1] 10 = [1, 0, 1, 0] 11 = [1, 0, 1, 1] 12 = [1, 1, 0, 0] 13 = [1, 1, 0, 1] 14 = [1, 1, 1, 0] 15 = [1, 1, 1, 1] 16 = [1, 0, 0, 0, 0] 17 = [1, 0, 0, 0, 1] 18 = [1, 0, 0, 1, 0] 19 = [1, 0, 0, 1, 1] 20 = [1, 0, 1, 0, 0] 21 = [1, 0, 1, 0, 1] 22 = [1, 0, 1, 1, 0] 23 = [1, 0, 1, 1, 1] 24 = [1, 1, 0, 0, 0] 25 = [1, 1, 0, 0, 1] 26 = [1, 1, 0, 1, 0] 27 = [1, 1, 0, 1, 1] 28 = [1, 1, 1, 0, 0] 29 = [1, 1, 1, 0, 1] 30 = [1, 1, 1, 1, 0] 31 = [1, 1, 1, 1, 1] [1] [1, 0] [1, 1] [1, 0, 0] [1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1] [1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0]
Consider the consumption/savings problem in the infinite horizon. Each period the agent chooses how much to consume from the cash-in-hand $ M_t $ (without borrowing). The amount remaining after consumption grows stochastically with the gross return taking one of five values presented in the table below (independently distributed across time). In addition, each period the agent receives a non-stochastic government support payment of $10 (in thousands). Return on savings and the latter payment are the only sources of income.
The instantaneous utility of consumption is given by $ u(C_t)=\log(C_t) $.
Stochastic gross returns on savings are given by
R | 0.85 | 0.95 | 1.05 | 1.10 | 1.40 |
---|---|---|---|---|---|
p | 0.05 | 0.20 | 0.55 | 0.15 | 0.05 |
.
the optimal policy function, assuming that the choices are made on a discrete grid with step $1, and the upper bound of the problem is set to $200.