{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1**:\n", "\n", "Lookup a country in [World Bank database](https://data.worldbank.org), and format a string showing the growth rate of GDP over the last 2 years." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The growth rate in 2018 was 1.3\n", "The growth rate in 2019 was 0.55\n" ] } ], "source": [ "germany_gdp_growthrate = {\n", " 2018: 1.3,\n", " 2019: 0.55\n", "}\n", "\n", "for (k, v) in germany_gdp_growthrate.items():\n", " print(f\"The growth rate in {k} was {v}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([(2018, 1.3), (2019, 0.55)])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "germany_gdp_growthrate.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2:**\n", "\n", "Using the country you chose from exercise one, create a dictionary with GDP, population, and life expectancy for the years 1980, 1990, 2005, 2015. The years should be the keys and each value should be another dictionary with keys `GDP`, `population`, and `life_expectancy`." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "germany_wb_data = {\n", " 1980: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 1990: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2005: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2015: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", "}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
1980199020052015
GDP950.20950.20950.20950.20
population78000000.0078000000.0078000000.0078000000.00
life_expectancy72.6772.6772.6772.67
\n", "
" ], "text/plain": [ " 1980 1990 2005 2015\n", "GDP 950.20 950.20 950.20 950.20\n", "population 78000000.00 78000000.00 78000000.00 78000000.00\n", "life_expectancy 72.67 72.67 72.67 72.67" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(germany_wb_data)\n", "# pd.DataFrame.from_dict(germany_wb_data) # Additional arguments" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "pd.DataFrame.from_dict?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "germany_wb_data = {\n", " \"Germany\": {\n", " 1980: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 1990: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2005: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2015: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " }, \"Canada\": {\n", " 1980: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 1990: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2005: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " 2015: {\"GDP\": 950.2, \"population\": 78_000_000, \"life_expectancy\": 72.67},\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# This could read the dictionary from the cell above\n", "pd.json_normalize?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercises 3-4**\n", "\n", "For the next 2 exercises, recall that the net present value (NPV) of an asset that pays $x_t$ in period $t$ for periods $t=0$ to $t=T$ with a discount rate $r$ can be computed by\n", "\n", "$$\\text{NPV} \\equiv \\sum_{t=0}^T \\left( \\frac{1}{1 + r} \\right)^t x_t$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3**:\n", "\n", "Government bonds are often issued as *zero-coupon bonds* meaning that they make no payments throughout the entire time that they are held, but, rather make a single payment at the time of maturity.\n", "\n", "How much should you be willing to pay for a zero-coupon bond that pays \\$100 in 10 years with a yearly interest rate of 5%?\n", "\n", "(Compute this using a loop rather than plugging in directly)\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61.391325354075896" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = 0.05\n", "P = 100\n", "T = 10\n", "\n", "(1 / (1+r))**T * P" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "npv = 0.0\n", "\n", "for t in range(T + 1):\n", " c = P if t == T else 0\n", " npv = npv + (1 / (1+r))**t * c" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61.391325354075896" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "npv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 4**\n", "\n", "In economics, when an individual has some knowledge, skills, or education which provides them with a source of future income, we call it [human capital](https://en.wikipedia.org/wiki/Human_capital).\n", "\n", "When a student graduating from high school is considering whether to continue with post-secondary education, they may consider that it gives them higher paying jobs in the future, but requires that they don't begin working until after graduation.\n", "\n", "Consider the simplified example where a student has perfectly forecastable employment and is given two choices:\n", "\n", "1. Begin working immediately and make 40,000 a year until they retire 40\n", "years later.\n", "2. Pay 5,000 a year for the next 4 years to attend university, then\n", "get a job paying 50,000 a year until they retire 40 years after making\n", "the college attendance decision.\n", "\n", "Should the student enroll in school if the discount rate is r = 0.05?" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "r = 0.05\n", "T = 40\n", "\n", "w_hs = 40_000\n", "c_c = 5_000\n", "T_c = 4\n", "w_c = 50_000" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "720681.6268677661\n" ] } ], "source": [ "pdv_hs = 0\n", "for t in range(T):\n", " pdv_hs = pdv_hs + (1 / (1+r))**t * w_hs\n", "\n", "print(pdv_hs)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "696073.3919693314\n" ] } ], "source": [ "pdv_c = 0\n", "for t in range(T):\n", " if t < T_c:\n", " flow = -c_c\n", " else:\n", " flow = w_c\n", "\n", " pdv_c = pdv_c + (1 / (1+r))**t * flow\n", "\n", "print(pdv_c)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 5 }