Lecture 01: Introduction
Summary: The Jupyter notebook is a document with text, code and results.
This is a text cell, or more precisely a markdown cell.
- Pres Enter to edit the cell.
- Pres Ctrl+Enter to run the cell.
- Pres Shift+Enter to run the cell + advance.
We can make lists:
- First item
- Second item
Thirditem
We can also do LaTeX math, e.g. or
# this is a code cell
# let us do some calculations
a = 20
b = 30
c = a+b
# lets print the results (shown below the cell)
print(c)
We can now write some more text, and continue with our calculations.
d = c*2
print(d)
print(c*3)
Note: Despite JupyterLab is running in a browser, it is running offline (the path is something like localhost:8888/lab).
Consider the following consumer problem:
We can solve this problem numerically in a few lines of code.
- Choose some parameters:
alpha = 0.5
I = 10
p1 = 1
p2 = 2
- The consumer objective is:
def value_of_choice(x1,alpha,I,p1,p2):
# a. all income not spent on the first good
# is spent on the second
x2 = (I-p1*x1)/p2
# b. the resulting utility is
utility = x1**alpha * x2**(1-alpha)
return utility
- We can now use a function from the scipy module to solve the consumer problem.
# a. load external module from scipy
from scipy import optimize
# b. make value-of-choice as a funciton of only x1
obj = lambda x1: -value_of_choice(x1,alpha,I,p1,p2)
# c. call minimizer
solution = optimize.minimize_scalar(obj,bounds=(0,I/p1))
# d. print result
x1 = solution.x
x2 = (I-x1*p1)/p2
print(x1,x2)
Task: Solve the consumer problem with the CES utility funciton.
import numpy as np
# a. choose parameters
alpha = 0.5
beta = 0.000001
I = 10
p1 = 1
p2 = 2
# b. value-of-choice
def value_of_choice_ces(x1,alpha,beta,I,p1,p2):
x2 = (I-p1*x1)/p2
if x1 > 0 and x2 > 0:
utility = (alpha*x1**(-beta)+(1-alpha)*x2**(-beta))**(-1/beta)
else:
utility = 0
return utility
# c. objective
obj = lambda x1: -value_of_choice_ces(x1,alpha,beta,I,p1,p2)
# d. solve
solution = optimize.minimize_scalar(obj,bounds=(0,I/p1))
# e. result
x1 = solution.x
x2 = (I-x1*p1)/p2
print(x1,x2)
Consider the following AS-AD model:
where is the output gap, is the inflation gap, is a AR(1) demand shock, and is a AR(1) supply shock.
- Choose parameters:
a = 0.4
gamma = 0.1
phi = 0.9
delta = 0.8
omega = 0.15
sigma_x = 1
sigma_c = 0.4
T = 100
- Calculate composite parameters:
b = (1+a*phi*gamma)/(1+a*gamma)
beta = 1/(1+a*gamma)
- Define model functions:
y_hat_func = lambda y_hat_lag,z,z_lag,s,s_lag: b*y_hat_lag + beta*(z-z_lag) - a*beta*s + a*beta*phi*s_lag
pi_hat_func = lambda pi_lag,z,z_lag,s,s_lag: b*pi_lag + beta*gamma*z - beta*phi*gamma*z_lag + beta*s - beta*phi*s_lag
z_func = lambda z_lag,x: delta*z_lag + x
s_func = lambda s_lag,c: omega*s_lag + c
- Run the simulation:
import numpy as np
# a. set setup
np.random.seed(2015)
# b. allocate simulation data
x = np.random.normal(loc=0,scale=sigma_x,size=T)
c = np.random.normal(loc=0,scale=sigma_c,size=T)
z = np.zeros(T)
s = np.zeros(T)
y_hat = np.zeros(T)
pi_hat = np.zeros(T)
# c. run simulation
for t in range(1,T):
# i. update demand shocks and supply shocks
z[t] = z_func(z[t-1],x[t])
s[t] = s_func(s[t-1],c[t])
# ii. compute outgap and inflation based on shocks in t
y_hat[t] = y_hat_func(y_hat[t-1],z[t],z[t-1],s[t],s[t-1])
pi_hat[t] = pi_hat_func(pi_hat[t-1],z[t],z[t-1],s[t],s[t-1])
- Plot the simulation:
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(y_hat,label='$\\hat{y}$')
ax.plot(pi_hat,label='$\\hat{\pi}$')
ax.set_xlabel('time')
ax.set_ylabel('percent')
ax.set_ylim([-8,8])
ax.legend(loc='upper left');
I like the seaborn style:
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(y_hat,label='$\\hat{y}$')
ax.plot(pi_hat,label='$\\hat{pi}$')
ax.set_xlabel('time')
ax.set_ylabel('percent')
ax.set_ylim([-8,8])
ax.legend(loc='upper left',facecolor='white',frameon='True');
A module is a .py-file with functions you import and can then call in the notebook.
Try to open mymodule.py and have a look.
import mymodule
x = 5
y = mymodule.myfunction(x)
print(y)
Download course material
- Follow the installation guide
- If you have not yet restarted JupyterLab after installations, do it now.
- Make sure you are in the folder, where the course content should be located.
- Press the tab Git.
- Press
Clone a repository
- Paste in
https://github.com/NumEconCopenhagen/lectures-2022
and enter. - You now have all course material in the folder
lectures-2022
- Do the same thing for exercise class material, using the url
https://github.com/NumEconCopenhagen/exercises-2022
- Create a copy of the cloned folder, where you work with the code (otherwise you can not sync with updates)