import numpy as np
import pandas as pd
import pandas_datareader
from types import SimpleNamespace
%load_ext autoreload
%autoreload 2
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
# Import additional libraries:
1. Regional CO emissions
In this exercise, we will investigate the development in CO2 emissions relative to GDP across the world. For this purpose, we apply data obtained from the World Bank Indicators.
You will need to use the following data sets for the period 1990-2016: 1. EN.ATM.CO2E.KT : CO2 emissions in kilotons. WB link 2. NY.GDP.MKTP.PP.KD : GDP, PPP-corrected in constant 2017 international dollars WB link 3. SP.POP.TOTL : total population WB link 4. country_region.xlsx : key between country names and world regions
Data sets 1-3 contain annual records for all the world's countries plus a large set of country aggregates. You can download each data_set
using the following API call
from pandas_datareader import wb
df = wb.download(indicator=data_set, country=[], start=1990, end=2016)
Notes:
The data set country_region.xlsx is available together with this notebook.
Setting countrty=[]
implies getting all countries in a data set by the syntax of the World Bank API.
Follow the links above if there is a problem with the API call and download manually.
Question 1: Data cleaning.
- Download data sets 1-3. Exclude all entries in the data sets that are not a country listed in country_region.xlsx.
- Assign a world region to each country in data sets 1-3 using country_region.xlsx.
- If a data-point in one data set is
NaN
for country in year , then delete it together with the corresponding data-points for country in year in the 2 other data sets.
Check that the resulting number of rows for a dataset containing GDP, population and CO2 is 4660.
Question 2: Regional CO2 emissions. Calculate the regional CO2 emissions per dollar GDP on an annual basis. That is, for region in year compute
where denotes the GDP of country .
Plot CO2 emissions per dollar GDP for all regions in the way you find most presentable and comment.
Question 3: Growth rates.
- For each region , calculate population weighted average and GDP pr year. See weighted average definition below.
- Calculate the annual growth rates of averaged GDP and CO2 for each region.
- Finally, create one subplot per region containing the two associated growth rates. Make a brief comment. (tip: better use a loop for this instead of code repetition)
The weighted averages of of GDP and CO2 for region is obtained by first calculating the weights:
for each country .
Then get average regional GDP by
and similarly for CO2.
The growth rate for regional GDP per capita is then
and similarly for regional CO2 pr capita
2. Risky assets
The consumption savings model with uncertainty in both income and interest rate.
A household lives two periods.
In the second period it gets utility from consuming and leaving a bequest,
- is cash-on-hand
- is consumption
- is end-of-period assets
- is the risk aversion coefficient
- is the strength of the bequest motive
- is the degree of luxuriousness in the bequest motive
- ensures the household cannot die in debt
The value function measures the household's value of having at the beginning of period .
In the first period, the household gets utility from consuming and takes into account that it will also live in the next-period, where it receives a stochastic income.
We assume that the consumption decision is made at the beginning of the period.
We denote the interest rate on savings from period 1 to 2 by . It is assumed to be unknown until the end of the period 1; ie. after the consumption decision is made.
- is cash-on-hand in period 1
- is consumption in period 1
- is end-of-period assets in period 1
- is the discount factor
- is the expectation operator conditional on information in the beginning of period 1
- is income in period 2
- is the level of income risk
- is the expected interest rate
- is the level of interest rate risk
- ensures the household cannot borrow
# Parameters
rho = 8
kappa = 0.5
nu = 0.2
r0 = 0.3
Delta_r = 0.29
Delta_y = 0.5
beta = 0.98
Note: and are somewhat extreme - that is just for exposition.
Question 1: Solve the model for both periods and obtain value functions together with the optimal consumption functions .
Plot in one graph and in another.
Question 2: Now set and solve the model once again. Plot the associated and compare them with the consumption functions from the solution in Question 1.
Question 3 Question 3 below has 2 options. You can freely choose which one you want to answer. (You can of course answer both if you like).
Question 3 - option 1: Simulate the period 2 choices of households both for the case where and where .
You can use the same distribution of for both cases as specified below.
Plot the two distributions of and comment.
Optionally, you can also inspect the two distributions of and check that you understand their shapes.
np.random.seed(2021)
simN = 50000
sim_m1 = np.fmax(np.random.normal(1, 0.05, size = simN), 0)
Question 3 - option 2: Generalizing the set of possible interest rate outcomes.
We now consider the case where the interest rate has different possible realizations.
Specifically, has possible outcomes higher than , and outcomes lower than , which are uniformly distributed:
Implement this generalized specification in code.
Test the model on the parameterization below and plot .
# New parameters:
r0 = 0.3
Delta_r = 0.05
n = 5
N = 2*5
# Remaining parameters are the same as in Question 1
3. Interpolation by polynomial
We can use interpolation when dealing with some function that is very heavy to calculate. That is, we have a large set of points on which we want to know the function value , but we might not have sufficiently computing power for it.
Therefore, we take out a subset and calulate on the elements of . Whenever we want to know for some that is not also in our pre-computation set , we use interpolation between neighboring points to in for which the function value has been calculated. To get an estimate of , we then interpolate between these pre-calculated neighboring points.
In lecture 11, we saw how to write up a linear interpolation between a set of pre-calculated function values.
In the current exercise, we will use a polynomial to interpolate between pre-calculated data points.
(Note: the type of interpolation we consider below is only practical in a modified version. It is useful as an introduction, however.)
Polynomial interpolation
Assume we have a set of data points .
We now want to create a polynomial of degree such that exactly for all .
We can obtain such a polynomial by the following steps:
Example
Say we have the function . We can then construct our set of data points .
To do so, we evaluate in the points :
Thus we have
Interpolating our function at some is then given by
Algorithm
First evaluate the function on the set of points so as to obtain the associated function values . Then follow the pseudo-code below.
Question 1: Implement the algorithm to interpolate function values of to the point
def polynomial_interpol(x, X, Y):
pass
You can test your polynomial_interpol
on the function
f = lambda x: x**3
X1 = np.array([1, 2, 2.5, 4])
X2 = np.array([0.5, 2.2, 7, 12.9])
Y1 = f(X1)
Y2 = f(X2)
x = 3.2
# Note: the result should be the same in both cases below
y1_interpol = polynomial_interpol(x, X1, Y1)
print(f'f(x) = {f(x): .7f} interpolated y = {y1_interpol: .7f}')
y2_interpol = polynomial_interpol(x, X2, Y2)
print(f'f(x) = {f(x): .7f} interpolated y = {y2_interpol: .7f}')