{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## DeLong's Macroeconomics \n", "\n", "Last edited: 2019-08-17\n", "\n", "# Introduction: Python and Economics \n", "\n", "#### Due ???? via upload to ??? \n", "\n", "### J. Bradford DeLong \n", "\n", " \n", "\n", "You should have gotten to this point vis this link: \n", "\n", " \n", "\n", "Welcome to Economics 101B: Macroeconomic Theory! This introductory notebook will familiarize you with some of the basic strategies for data analysis that will be useful to you throughout the course. It will cover an overview of our software, an introduction to programming, and some economics.\n", "\n", " \n", "\n", "### Table of Contents \n", "\n", "1. [Computing Environment](#computing_environment)\n", "2. [Introduction to Coding Concepts](#programming_concepts)\n", " 1. [Python Basics](#python_basics)\n", " 2. [Pandas](#pandas)\n", " 3. [Visualization](#viz)\n", "3. [Math Review](#math)\n", "4. [Economics Review: Measuring the Economy](#chapter_2)\n", "6. [Economics Review: Economic Thought](#thought)\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Our Computing Environment: The Jupyter Notebook \n", "This webpage is called a Jupyter notebook. A notebook is a place to write programs and view their results. \n", "\n", " \n", "\n", "### Markdown Text Cells \n", "\n", "In a notebook, each rectangle containing text or code is called a *cell*.\n", "\n", "Text cells (like this one) can be edited by double-clicking on them. They're written in a simple format called [Markdown](http://daringfireball.net/projects/markdown/syntax) to add formatting and section headings. You don't need to learn Markdown, but you might want to.\n", "\n", "After you edit a text cell, click the \"run cell\" button at the top that looks like ▶| to confirm any changes. (Try not to delete the instructions of the lab.)\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Understanding Check 1** This paragraph is in its own text cell. Try editing it so that this sentence is the last sentence in the paragraph, and then click the \"run cell\" ▶| button . This sentence, for example, should be deleted. So should this one.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Code Cells \n", "Other cells contain code in the Python 3 language. Running a code cell will execute all of the code it contains.\n", "\n", "To run the code in a code cell, first click on that cell to activate it. It'll be highlighted with a little green or blue rectangle. Next, either press ▶| or hold down the `shift` key and press `return` or `enter`.\n", "\n", "Try running this cell:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Hello, World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And this one:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"\\N{WAVING HAND SIGN}, \\N{EARTH GLOBE ASIA-AUSTRALIA}!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fundamental building block of Python code is an expression. Cells can contain multiple lines with multiple expressions. When you run a cell, the lines of code are executed in the order in which they appear. Every `print` expression prints a line. Run the next cell and notice the order of the output." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"First this line is printed,\")\n", "print(\"and then this one.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Understanding Check 2** Change the cell above so that it prints out:\n", "\n", " First this line,\n", " then the whole 🌏,\n", " and then this one." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't be scared if you see a \"Kernel Restarting\" message! Your data and work will still be saved. Once you see \"Kernel Ready\" in a light blue box on the top right of the notebook, you'll be ready to work again. You should rerun any cells with imports, variables, and loaded data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Writing Jupyter notebooks \n", "You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you'll need to create your own cells for text and code.\n", "\n", "To add a cell, click the + button in the menu bar. It'll start out as a text cell. You can change it to a code cell by clicking inside it so it's highlighted, clicking the drop-down box next to the restart (⟳) button in the menu bar, and choosing \"Code\".\n", "\n", "**Understanding Check 3** Add a code cell below this one. Write code in it that prints out:\n", " \n", " A whole new cell! ♪🌏♪\n", "\n", "(That musical note symbol is like the Earth symbol. Its long-form name is `\\N{EIGHTH NOTE}`.)\n", "\n", "Run your cell to verify that it works." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Errors \n", "\n", "Python is a language, and like natural human languages, it has rules. It differs from natural language in two important ways:\n", "1. The rules are *simple*. You can learn most of them in a few weeks and gain reasonable proficiency with the language in a semester.\n", "2. The rules are *rigid*. If you're proficient in a natural language, you can understand a non-proficient speaker, glossing over small mistakes. A computer running Python code is not smart enough to do that.\n", "\n", "Whenever you write code, you'll make mistakes. When you run a code cell that has errors, Python will sometimes produce error messages to tell you what you did wrong.\n", "\n", "Errors are okay; even experienced programmers make many errors. When you make an error, you just have to find the source of the problem, fix it, and move on.\n", "\n", "We have made an error in the next cell. Run it and see what happens." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"This line is missing something.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should see something like this (minus our annotations):\n", "\n", "\n", "\n", "The last line of the error output attempts to tell you what went wrong. The *syntax* of a language is its structure, and this `SyntaxError` tells you that you have created an illegal structure. \"`EOF`\" means \"end of file,\" so the message is saying Python expected you to write something more (in this case, a right parenthesis) before finishing the cell.\n", "\n", "There's a lot of terminology in programming languages, but you don't need to know it all in order to program effectively. If you see a cryptic message like this, you can often get by without deciphering it. (Of course, if you're frustrated, feel free to ask a friend or post on the class Piazza.)\n", "\n", "**Understanding Check 4** Try to fix the code above so that you can run the cell and see the intended message instead of an error." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you are comfortable with our computing environment, we are going to be moving into more of the fundamentals of Python, but first, run the cell below to ensure all the libraries needed for this notebook are installed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install numpy\n", "!pip install pandas\n", "!pip install matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to Programming Concepts " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part 1: Python Basics \n", "\n", "Before getting into the more advanced analysis techniques that will be required in this course, we need to cover a few of the foundational elements of programming in Python.\n", "\n", " \n", "\n", "#### A. Expressions \n", "\n", "The departure point for all programming is the concept of the __expression__. An expression is a combination of variables, operators, and other Python elements that the language interprets and acts upon. Expressions act as a set of instructions to be fed through the interpreter, with the goal of generating specific outcomes. See below for some examples of basic expressions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Examples of expressions:\n", "\n", "#addition\n", "print(2 + 2)\n", "\n", "#string concatenation \n", "print('me' + ' and I')\n", "\n", "#you can print a number with a string if you cast it \n", "print(\"me\" + str(2))\n", "\n", "#exponents\n", "print(12 ** 2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will notice that only the last line in a cell gets printed out. If you want to see the values of previous expressions, you need to call `print` on that expression. Try adding `print` statements to some of the above expressions to get them to display.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### B. Variables \n", "\n", "In the example below, `a` and `b` are Python objects known as __variables__. We are giving an object (in this case, an `integer` and a `float`, two Python data types) a name that we can store for later use. To use that value, we can simply type the name that we stored the value as. Variables are stored within the notebook's environment, meaning stored variable values carry over from cell to cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 4\n", "b = 10/5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that when you create a variable, unlike what you previously saw with the expressions, it does not print anything out." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Notice that 'a' retains its value.\n", "print(a)\n", "a + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Question 1: Variables\n", "\n", "See if you can write a series of expressions that creates two new variables called \n", "__x__ and __y__ and assigns them values of __10.5__ and __7.2__. Then assign their product to the variable __combo__ and print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Fill in the missing lines to complete the expressions.\n", "\n", "\n", "\n", "x = 10.5\n", "y = 7.2\n", "combo = x * y\n", "\n", "print(combo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running the cell below will give you some feed back on your responses. Though the OK tests are not always comprehensive (passing all of the tests does not guarantee full credit for questions), they give you a pretty good indication as to whether or not you're on track.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### C. Lists \n", "\n", "The next topic is particularly useful in the kind of data manipulation that you will see throughout 101B. The following few cells will introduce the concept of __lists__ (and their counterpart, `numpy arrays`). Read through the following cell to understand the basic structure of a list. \n", "\n", "A list is an ordered collection of objects. They allow us to store and access groups of variables and other objects for easy access and analysis. Check out this [documentation](https://www.tutorialspoint.com/python/python_lists.htm) for an in-depth look at the capabilities of lists.\n", "\n", "To initialize a list, you use brackets. Putting objects separated by commas in between the brackets will add them to the list. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# an empty list\n", "lst = []\n", "print(lst)\n", "\n", "# reassigning our empty list to a new list\n", "lst = [1, 3, 6, 'lists', 'are' 'fun', 4]\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access a value in the list, put the index of the item you wish to access in brackets following the variable that stores the list. Lists in Python are zero-indexed, so the indicies for `lst` are 0, 1, 2, 3, 4, 5, and 6." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Elements are selected like this:\n", "example = lst[2]\n", "\n", "# The above line selects the 3rd element of lst (list indices are 0-offset) and sets it to a variable named example.\n", "print(example)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is important to note that when you store a list to a variable, you are actually storing the **pointer** to the list. That means if you assign your list to another variable, and you change the elements in your other variable, then you are changing the same data as in the original list. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1,2,3] #original list\n", "b = a #b now points to list a \n", "b[0] = 4 \n", "print(a[0]) #return 4 since we modified the first element of the list pointed to by a and b " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Slicing lists\n", "\n", "As you can see from above, lists do not have to be made up of elements of the same kind. Indices do not have to be taken one at a time, either. Instead, we can take a slice of indices and return the elements at those indices as a separate list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### This line will store the first (inclusive) through fourth (exclusive) elements of lst as a new list called lst_2:\n", "lst_2 = lst[1:4]\n", "\n", "lst_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Question 2: Lists\n", "\n", "Build a list of length 10 containing whatever elements you'd like. Then, slice it into a new list of length five using a index slicing. Finally, assign the last element in your sliced list to the given variable and print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "### Fill in the ellipses to complete the question.\n", "...\n", "...\n", "...\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can also be operated on with a few built-in analysis functions. These include `min` and `max`, among others. Lists can also be concatenated together. Find some examples below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A list containing six integers.\n", "a_list = [1, 6, 4, 8, 13, 2]\n", "\n", "# Another list containing six integers.\n", "b_list = [4, 5, 2, 14, 9, 11]\n", "\n", "print('Max of a_list:', max(a_list))\n", "print('Min of b_list:', min(a_list))\n", "\n", "# Concatenate a_list and b_list:\n", "c_list = a_list + b_list\n", "print('Concatenated:', c_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### D. Numpy Arrays \n", "\n", "Closely related to the concept of a list is the array, a nested sequence of elements that is structurally identical to a list. Arrays, however, can be operated on arithmetically with much more versatility than regular lists. For the purpose of later data manipulation, we'll access arrays through [Numpy](https://docs.scipy.org/doc/numpy/reference/routines.html), which will require an import statement.\n", "\n", "Now run the next cell to import the numpy library into your notebook, and examine how numpy arrays can be used." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize an array of integers 0 through 9.\n", "example_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", "\n", "# This can also be accomplished using np.arange\n", "example_array_2 = np.arange(10)\n", "print('Undoubled Array:')\n", "print(example_array_2)\n", "\n", "# Double the values in example_array and print the new array.\n", "double_array = example_array*2\n", "print('Doubled Array:')\n", "print(double_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This behavior differs from that of a list. See below what happens if you multiply a list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "example_list * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that instead of multiplying each of the elements by two, multiplying a list and a number returns that many copies of that list. This is the reason that we will sometimes use Numpy over lists. Other mathematical operations have interesting behaviors with lists that you should explore on your own.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### E. Looping \n", "\n", "[Loops](https://www.tutorialspoint.com/python/python_loops.htm) are often useful in manipulating, iterating over, or transforming large lists and arrays. The first type we will discuss is the __for loop__. For loops are helpful in traversing a list and performing an action at each element. For example, the following code moves through every element in example_array, adds it to the previous element in example_array, and copies this sum to a new array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_list = []\n", "\n", "for element in example_array:\n", " new_element = element + 5\n", " new_list.append(new_element)\n", "\n", "new_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most important line in the above cell is the \"`for element in...`\" line. This statement sets the structure of our loop, instructing the machine to stop at every number in `example_array`, perform the indicated operations, and then move on. Once Python has stopped at every element in `example_array`, the loop is completed and the final line, which outputs `new_list`, is executed. It's important to note that \"element\" is an arbitrary variable name used to represent whichever index value the loop is currently operating on. We can change the variable name to whatever we want and achieve the same result, as long as we stay consistent. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "newer_list = []\n", "\n", "for completely_arbitrary_name in example_array:\n", " newer_element = completely_arbitrary_name + 5\n", " newer_list.append(newer_element)\n", " \n", "newer_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For loops can also iterate over ranges of numerical values. If I wanted to alter `example_array` without copying it over to a new list, I would use a numerical iterator to access list indices rather than the elements themselves. This iterator, called `i`, would range from 0, the value of the first index, to 9, the value of the last. I can make sure of this by using the built-in `range` and `len` functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(len(example_array)):\n", " example_array[i] = example_array[i] + 5\n", "\n", "example_array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Other Types of Loops\n", "\n", "The __while loop__ repeatedly performs operations until a conditional is no longer satisfied. A conditional is a [boolean expression](https://en.wikipedia.org/wiki/Boolean_expression), that is an expression that evaluates to `True` or `False`. \n", "\n", "In the below example, an array of integers 0 to 9 is generated. When the program enters the while loop on the subsequent line, it notices that the maximum value of the array is less than 50. Because of this, it adds 1 to the fifth element, as instructed. Once the instructions embedded in the loop are complete, the program refers back to the conditional. Again, the maximum value is less than 50. This process repeats until the the fifth element, now the maximum value of the array, is equal to 50, at which point the conditional is no longer true and the loop breaks." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "while_array = np.arange(10) # Generate our array of values\n", "\n", "print('Before:', while_array)\n", "\n", "while(max(while_array) < 50): # Set our conditional\n", " while_array[4] += 1 # Add 1 to the fifth element if the conditional is satisfied \n", " \n", "print('After:', while_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Question 3: Loops\n", "\n", "In the following cell, partial steps to manipulate an array are included. You must fill in the blanks to accomplish the following:\n", "\n", "1. Iterate over the entire array, checking if each element is a multiple of 5\n", "2. If an element is not a multiple of 5, add 1 to it repeatedly until it is\n", "3. Iterate back over the list and print each element.\n", "\n", "> Hint: To check if an integer `x` is a multiple of `y`, use the modulus operator `%`. Typing `x % y` will return the remainder when `x` is divided by `y`. Therefore, (`x % y != 0`) will return `True` when `y` __does not divide__ `x`, and `False` when it does." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Make use of iterators, range, length, while loops, and indices to complete this question.\n", "question_3 = np.array([12, 31, 50, 0, 22, 28, 19, 105, 44, 12, 77])\n", "\n", "...\n", "...\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### F. Functions! \n", "\n", "Functions are useful when you want to repeat a series of steps on multiple different objects, but don't want to type out the steps over and over again. Many functions are built into Python already; for example, you've already made use of `len()` to retrieve the number of elements in a list. You can also write your own functions, and at this point you already have the skills to do so.\n", "\n", "Functions generally take a set of __parameters__ (also called inputs), which define the objects they will use when they are run. For example, the `len()` function takes a list or array as its parameter, and returns the length of that list.\n", "\n", "The following cell gives an example of an extremely simple function, called `add_two`, which takes as its parameter an integer and returns that integer with, you guessed it, 2 added to it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# An adder function that adds 2 to the given n.\n", "def add_two(n):\n", " return n + 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "add_two(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Easy enough, right? Let's look at a function that takes two parameters, compares them somehow, and then returns a boolean value (`True` or `False`) depending on the comparison. The `is_multiple` function below takes as parameters an integer `m` and an integer `n`, checks if `m` is a multiple of `n`, and returns `True` if it is. Otherwise, it returns `False`. \n", "\n", "`if` statements, just like `while` loops, are dependent on boolean expressions. If the conditional is `True`, then the following indented code block will be executed. If the conditional evaluates to `False`, then the code block will be skipped over. Read more about `if` statements [here](https://www.tutorialspoint.com/python/python_if_else.htm)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_multiple(m, n):\n", " if (m % n == 0):\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_multiple(12, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_multiple(12, 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Sidenote:** Another way to write `is_multiple` is below, think about why it works.\n", "\n", " def is_multiple(m, n):\n", " return m % n == 0\n", " \n", "Since functions are so easily replicable, we can include them in loops if we want. For instance, our `is_multiple` function can be used to check if a number is prime! See for yourself by testing some possible prime numbers in the cell below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Change possible_prime to any integer to test its primality\n", "# NOTE: If you happen to stumble across a large (> 8 digits) prime number, the cell could take a very, very long time\n", "# to run and will likely crash your kernel. Just click kernel>interrupt if it looks like it's caught.\n", "\n", "possible_prime = 9999991\n", "\n", "for i in range(2, possible_prime):\n", " if (is_multiple(possible_prime, i)):\n", " print(possible_prime, 'is not prime') \n", " break\n", " if (i >= possible_prime/2):\n", " print(possible_prime, 'is prime')\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Question 4: Writing Functions\n", "\n", "In the following cell, complete a function that will take as its parameters a list and two integers x and y, iterate through the list, and replace any number in the list that is a multiple of x with y.\n", "> Hint: use the is_multiple() function to streamline your code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def replace_with_y(lst, x, y):\n", " for i in range(...):\n", " if(...):\n", " ...\n", " return lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part 2: Pandas Dataframes \n", "\n", "We will be using Pandas dataframes for much of this class to organize and sort through economic data. [Pandas](http://pandas.pydata.org/pandas-docs/stable/) is one of the most widely used Python libraries in data science. It is commonly used for data cleaning, and with good reason: it’s very powerful and flexible, among many other things. Like we did with `numpy`, we will have to import `pandas`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating dataframes \n", "\n", "The rows and columns of a pandas dataframe are essentially a collection of lists stacked on top/next to each other. For example, if I wanted to store the top 10 movies and their ratings in a datatable, I could create 10 lists that each contain a rating and a corresponding title, and these lists would be the rows of the table:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top_10_movies = pd.DataFrame(data=np.array(\n", " [[9.2, 'The Shawshank Redemption (1994)'],\n", " [9.2, 'The Godfather (1972)'],\n", " [9., 'The Godfather: Part II (1974)'],\n", " [8.9, 'Pulp Fiction (1994)'],\n", " [8.9, \"Schindler's List (1993)\"],\n", " [8.9, 'The Lord of the Rings: The Return of the King (2003)'],\n", " [8.9, '12 Angry Men (1957)'],\n", " [8.9, 'The Dark Knight (2008)'],\n", " [8.9, 'Il buono, il brutto, il cattivo (1966)'],\n", " [8.8, 'The Lord of the Rings: The Fellowship of the Ring (2001)']]), columns=[\"Rating\", \"Movie\"])\n", "top_10_movies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, we can store data in a dictionary instead of in lists. A dictionary keeps a mapping of keys to a set of values, and each key is unique. Using our top 10 movies example, we could create a dictionary that contains ratings a key, and movie titles as another key." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top_10_movies_dict = {\"Rating\" : [9.2, 9.2, 9., 8.9, 8.9, 8.9, 8.9, 8.9, 8.9, 8.8], \n", " \"Movie\" : ['The Shawshank Redemption (1994)',\n", " 'The Godfather (1972)',\n", " 'The Godfather: Part II (1974)',\n", " 'Pulp Fiction (1994)',\n", " \"Schindler's List (1993)\",\n", " 'The Lord of the Rings: The Return of the King (2003)',\n", " '12 Angry Men (1957)',\n", " 'The Dark Knight (2008)',\n", " 'Il buono, il brutto, il cattivo (1966)',\n", " 'The Lord of the Rings: The Fellowship of the Ring (2001)']}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can use this dictionary to create a table with columns `Rating` and `Movie`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top_10_movies_2 = pd.DataFrame(data=top_10_movies_dict, columns=[\"Rating\", \"Movie\"])\n", "top_10_movies_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how both ways return the same table! However, the list method created the table by essentially taking the lists and making up the rows of the table, while the dictionary method took the keys from the dictionary to make up the columns of the table. In this way, dataframes can be viewed as a collection of basic data structures, either through collecting rows or columns.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reading in Dataframes \n", "\n", "Luckily for you, most datatables in this course will be premade and given to you in a form that is easily read into a pandas method, which creates the table for you. A common file type that is used for economic data is a Comma-Separated Values (.csv) file, which stores tabular data. It is not necessary for you to know exactly how .csv files store data, but you should know how to read a file in as a pandas dataframe. You can use the \"read_csv\" method from pandas, which takes in one parameter which is the path to the csv file you are reading in." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will read in a .csv file that contains quarterly real GDI, real GDP, and nominal GDP data in the U.S. from 1947 to the present." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# Run this cell to read in the table\n", "accounts = pd.read_csv(\"https://delong.typepad.com/files/quarterly_accounts.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `pd.read_csv` function expects a path to a .csv file as its input, and will return a data table created from the data contained in the csv.\n", "We have provided `Quarterly_Accouunts.csv` in the data directory, which is all contained in the current working directory (aka the folder this assignment is contained in). For this reason, we must specify to the `read_csv` function that it should look for the csv in the data directory, and the `/` indicates that `Quarterly_Accounts.csv` can be found there. \n", "\n", "Here is a sample of some of the rows in this datatable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Indexing Dataframes \n", "\n", "Oftentimes, tables will contain a lot of extraneous data that muddles our data tables, making it more difficult to quickly and accurately obtain the data we need. To correct for this, we can select out columns or rows that we need by indexing our dataframes. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The easiest way to index into a table is with square bracket notation. Suppose you wanted to obtain all of the Real GDP data from the data. Using a single pair of square brackets, you could index the table for `\"Real GDP\"`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run this cell and see what it outputs\n", "accounts[\"Real GDP\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how the above cell returns an array of all the real GDP values in their original order.\n", "Now, if you wanted to get the first real GDP value from this array, you could index it with another pair of square brackets:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[\"Real GDP\"][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandas columns have many of the same properties as numpy arrays. Keep in mind that pandas dataframes, as well as many other data structures, are zero-indexed, meaning indexes start at 0 and end at the number of elements minus one. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you wanted to create a new datatable with select columns from the original table, you can index with double brackets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Note: .head() returns the first five rows of the table\n", "accounts[[\"Year\", \"Quarter\", \"Real GDP\", \"Real GDI\"]].head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use column indices instead of names." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[[0, 1, 2, 3]].head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, you can also get rid of columns you dont need using `.drop()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts.drop(\"Nominal GDP\", axis=1).head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, you can use square bracket notation to index rows by their indices with a single set of brackets. You must specify a range of values for which you want to index. For example, if I wanted the 20th to 30th rows of `accounts`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[20:31]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Filtering Data \n", "\n", "As you can tell from the previous, indexing rows based on indices is only useful when you know the specific set of rows that you need, and you can only really get a range of entries. Working with data often involves huge datasets, making it inefficient and sometimes impossible to know exactly what indices to be looking at. On top of that, most data analysis concerns itself with looking for patterns or specific conditions in the data, which is impossible to look for with simple index based sorting. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thankfully, you can also use square bracket notation to filter out data based on a condition. Suppose we only wanted real GDP and nominal GDP data from the 21st century:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[accounts[\"Year\"] >= 2000][[\"Real GDP\", \"Nominal GDP\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `accounts` table is being indexed by the condition `accounts[\"Year\"] >= 2000`, which returns a table where only rows that have a \"Year\" greater than $2000$ is returned. We then index this table with the double bracket notation from the previous section to only get the real GDP and nominal GDP columns." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose now we wanted a table with data from the first quarter, and where the real GDP was less than 5000 or nominal GDP is greater than 15,000." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[(accounts[\"Quarter\"] == \"Q1\") & ((accounts[\"Real GDP\"] < 5000) | (accounts[\"Nominal GDP\"] > 15000))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many different conditions can be included to filter, and you can use `&` and `|` operators to connect them together. Make sure to include parantheses for each condition!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to reorganize data to make it more convenient is to sort the data by the values in a specific column. For example, if we wanted to find the highest real GDP since 1947, we could sort the table for real GDP:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts.sort_values(\"Real GDP\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But wait! The table looks like it's sorted in increasing order. This is because `sort_values` defaults to ordering the column in ascending order. To correct this, add in the extra optional parameter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts.sort_values(\"Real GDP\", ascending=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can clearly see that the highest real GDP was attained in the first quarter of this year, and had a value of 16903.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Useful Functions for Numeric Data \n", "\n", "Here are a few useful functions when dealing with numeric data columns.\n", "To find the minimum value in a column, call `min()` on a column of the table." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[\"Real GDP\"].min()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To find the maximum value, call `max()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[\"Nominal GDP\"].max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And to find the average value of a column, use `mean()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts[\"Real GDI\"].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part 3: Visualization " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you can read in data and manipulate it, you are now ready to learn about how to visualize data. To begin, run the cells below to import the required packages we will be using." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using US unemployment data from [FRED](https://fred.stlouisfed.org/) to show what we can do with data. The statement below will put the csv file into a pandas DataFrame." ] }, { "cell_type": "code", "execution_count": 3, "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", " \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", " \n", " \n", " \n", "
datetotal_unemployedmore_than_15_weeksnot_in_labor_searched_for_workmulti_jobsleaverslosershousing_price_index
011/1/1016.98696253167085.763.0186.07
112/1/1016.68549260968996.461.2183.27
21/1/1116.28393280068166.560.1181.35
32/1/1116.08175273067416.460.2179.66
43/1/1115.98166243467356.460.3178.84
\n", "
" ], "text/plain": [ " date total_unemployed more_than_15_weeks \\\n", "0 11/1/10 16.9 8696 \n", "1 12/1/10 16.6 8549 \n", "2 1/1/11 16.2 8393 \n", "3 2/1/11 16.0 8175 \n", "4 3/1/11 15.9 8166 \n", "\n", " not_in_labor_searched_for_work multi_jobs leavers losers \\\n", "0 2531 6708 5.7 63.0 \n", "1 2609 6899 6.4 61.2 \n", "2 2800 6816 6.5 60.1 \n", "3 2730 6741 6.4 60.2 \n", "4 2434 6735 6.4 60.3 \n", "\n", " housing_price_index \n", "0 186.07 \n", "1 183.27 \n", "2 181.35 \n", "3 179.66 \n", "4 178.84 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "unemployment_data = pd.read_csv(\"https://delong.typepad.com/detailed_unemployment.csv\")\n", "unemployment_data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the advantages of pandas is its built-in plotting methods. We can simply call `.plot()` on a dataframe to plot columns against one another. All that we have to do is specify which column to plot on which axis. Something special that pandas does is attempt to automatically parse dates into something that it can understand and order them sequentially.\n", "\n", "**Sidenote:** `total_unemployed` is a percent." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment_data.plot(x='date', y='total_unemployed')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The base package for most plotting in Python is `matplotlib`. Below we will look at how to plot with it. First we will extract the columns that we are interested in, then plot them in a scatter plot. Note that `plt` is the common convention for `matplotlib.pyplot`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total_unemployed = unemployment_data['total_unemployed']\n", "not_labor = unemployment_data['not_in_labor_searched_for_work']\n", "\n", "#Plot the data by inputting the x and y axis\n", "plt.scatter(total_unemployed, not_labor)\n", "\n", "# we can then go on to customize the plot with labels\n", "plt.xlabel(\"Percent Unemployed\")\n", "plt.ylabel(\"Total Not In Labor, Searched for Work\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Though matplotlib is sometimes considered an \"ugly\" plotting tool, it is powerful. It is highly customizable and is the foundation for most Python plotting libraries. Check out the [documentation](https://matplotlib.org/api/pyplot_summary.html) to get a sense of all of the things you can do with it, which extend far beyond scatter and line plots. An arguably more attractive package is [seaborn](https://seaborn.pydata.org/), which we will go over in future notebooks.\n", "\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Question 5: Plotting\n", "\n", "Try plotting the total percent of people unemployed vs those unemployed for more than 15 weeks." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "...\n", "...\n", "\n", "plt.scatter(total_unemployed, unemp_15_weeks)\n", "plt.xlabel(...)\n", "plt.ylabel(...)\n", "\n", "# note: plt.show() is the equivalent of print, but for graphs\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Math Concepts Review \n", "\n", "These questions are math review.\n", " \n", " For questions that are to be answered numerically, there is a code cell that starts with __# ANSWER__ and has a variable currently set to underscores. Replace those underscores with your final answer. It is okay to make other computations in that cell and others, so long a the given variable matches your answer.\n", "\n", "For free response questions, write your answers in the provided markdown cell that starts with ANSWER:. Do not change the heading, and write your entire answer in that one cell.\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Math as a Tool " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose a quantity grows at a steady proportional rate of 3% per year...\n", "\n", "...How long will it take to double?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ANSWER\n", "TIME_TO_DOUBLE = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quadruple?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ANSWER\n", "TIME_TO_QUADRUPLE = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Grow 1024-fold?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ANSWER\n", "TIME_TO_1024 = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose we have a quantity x(t) that varies over time following the equation: \n", "\n", "$ \\frac{dx(t)}{dt} = -(0.06)x + 0.36 $\n", "\n", "$ \\frac{dx(t)}{dt} - \\frac{d(6)}{dt} = -(0.06)(x - 6) $\n", "\n", "$ \\frac{dx(t)}{dt} = -(0.06)(x - 6) $\n", "\n", "$ \\frac{dy(t)}{dt} = -(0.06)y $" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without integrating the equation:\n", "\n", "\n", "$1.$ Tell me what the long-run steady-state value of $x$--that is, the limit of $x$ as $t$ approaches in infinity--is going to be." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "steady_state_val = 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ Suppose that the value of $x$ at time $t=0$, $x(0)$ equals 12. Once again, without integrating the equation, tell me how long it will take x to close half the distance between its initial value of 12 and its steady-state value. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "half_dist_time = 12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ How long will it take to close 3/4 of the distance? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "three_fourth_time = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ $7/8$ of the distance? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "seven_eighth_time = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$5.$ $15/16$ of the distance?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fifteen_sixteenth = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you are allowed to integrate $\\frac{dx(t)}{dt} = -(0.06)x + 0.36$.\n", "\n", "$1.$ Write down and solve the indefinite integral." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:\n", " \n", "\n", " \n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ Write down and solve the definite integral for the initial condition $x(0) = 12$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ Write down and solve the definite integral for the initial condition $x(0) = 6$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:\n", " \n", "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose we have a quantity $z = (\\frac{x}{y})^\\beta$\n", "\n", "Suppose $x$ is growing at 4% per year and that $\\beta=1/4$:\n", "\n", "$1.$ How fast is $z$ growing if $y$ is growing at 0% per year? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "zero_per_growth = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ If $y$ is growing at 2% per year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "two_per_growth = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ If $y$ is growing at 4% per year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "four_per_growth = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Rule of 72 (Use it for the next four questions)\n", "\n", "1. If a quantity grows at about 3% per year, how long will it take to double?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time_to_double = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ If a quantity shrinks at about 4% per year, how long will it take it to halve itself?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time_to_half = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ If a quantity doubles five times, how large is it relative to its original value?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "doubled_five_times_ratio = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ If a quantity halves itself three times, how large is it relative to its original value?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "halved_three_times_ratio = __" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Interactive Model for Rule of 72" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In future problem sets, you will build models of your own, but for now, look over this code. Its a simple model that shows what happens as you adjust a single parameter (the interest rate) and its effect on the outcome (the time to double). First we need to make sure all of our packages are imported." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from ipywidgets import interact, IntSlider\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our model is going to be graph that shows what happens as the interest rate varies." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def graph_rule_of_72(interest_rate):\n", " # np.linspace takes values evenly spaced between a stop and end point. In this case,\n", " # will take 30 values between 1 and 10. These will be our x values in the graph.\n", " x = np.linspace(1,10,30)\n", " \n", " # Here we create are corresponding y values\n", " y = 72 / x\n", " \n", " print('Time to double:', 72 / interest_rate, 'years')\n", " \n", " # graphing our lines\n", " plt.plot(x,y)\n", " # graphing the specific point for our interest_rate\n", " plt.scatter(interest_rate, 72 / interest_rate, c='r')\n", " \n", " plt.xlabel('interest rate (%)')\n", " plt.ylabel('time (years)')\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we call `interact`, select the function that we want to interact with (`graph_rule_of_72`) and tell it what the value we want its parameters to take on. In this case, `graph_rule_of_72` only takes one parameter, `interest_rate`, and we choose to put an adjustable slider there. You can check out the [ipywidget examples](https://github.com/jupyter-widgets/ipywidgets/blob/master/docs/source/examples/Index.ipynb) for more uses." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(graph_rule_of_72, interest_rate=IntSlider(min=1,max=10,step=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Why do DeLong and Olney think that the interest rate and the level of the stock market are important macroeconomic variables?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What are the principal flaws in using national product per worker as a measure of material welfare? Given these flaws, why do we use it anyway?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What is the difference between the nominal interest rate and the real interest rate? Why do DeLong and Olney think that the real interest rate is more important?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Review: Measuring the Economy Concepts and Quantities " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### National Income and Product Accounting\n", "\n", "Explain whether or not, why, and how the following items are included in the calculations of national product:\n", "\n", "$1.$ Increases in business inventories. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ Fees earned by real estate agents on selling existing homes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ Social Security checks written by the government. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ Building of a new dam by the Army Corps of Engineers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$5.$ Interest that your parents pay on the mortgage they have on their house." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$6.$ Purchases of foreign-made trucks by American residents" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In or Out of National Product? And Why\n", "\n", "Explain whether or not, why, and how the following items are included in the calculation of national product:\n", "\n", "$1.$ The sale for \\$25,000 of an automobile that cost \\$20,000 to manufacture that had been produced here at home last year and carried over in inventory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ The sale for \\$35,000 of an automobile that cost \\$25,000 to manufacture newly- made at home this year." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ The sale for \\$45,000 of an automobile that cost \\$30,000 to manufacture that was newly-made abroad this year and imported." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ The sale for \\$25,000 of an automobile that cost \\$20,000 to manufacture that was made abroad and imported last year." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In or Out of National Product? And Why II\n", "\n", "Explain whether or not, why, and how the following items are included in the calculation of GDP:\n", "\n", "$1.$ The purchase for \\$500 of a dishwasher produced here at home this year." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ The purchase for $500 of a dishwasher made abroad this year." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ The purchase for $500 of a used dishwasher." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ The manufacture of a new dishwasher here at home for $500 of a dishwasher that\n", "then nobody wants to buy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Components of National Income and Product\n", "\n", "Suppose that the appliance store buys a refrigerator from the manufacturer on December 15, 2018 for \\$600, and that you then buy that refrigerator on January 15, 2019 for \\$1000:\n", "\n", "$1.$ What is the contribution to GDP in 2018? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "contribution_2018 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ How is the refrigerator accounted for in the NIPA in 2019?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ What is the contribution to GDP in 2018?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "contribution_2019 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ How is the refrigerator accounted for in the NIPA in 2019?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# These lines are reading in CSV files and creating dataframes from then, you don't have to change about them!\n", "\n", "import pandas as pd\n", "import numpy as np\n", "\n", "unemployment = pd.read_csv(\"data/Unemployment.csv\")\n", "quarterly_acc = pd.read_csv(\"data/Quarterly_Accounts.csv\")\n", "from_2007 = quarterly_acc.loc[(quarterly_acc[\"Year\"].isin(np.arange(2007, 2018)))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Estimating National Product\n", "\n", "The Bureau of Economic Analysis measures national product in two different ways: as total expenditure on the economy’s output of goods and services and as the total income of everyone in the economy. Since – as you learned in earlier courses – these two things are the same, the two approaches should give the same answer. But in practice they do not.\n", "\n", "We have provided a data table `quarterly_gdp` that contains quarterly data on real GDP measured on the expenditure side (referred to in the National Income and Product Accounts as “Real Gross Domestic Product, chained dollars”) and real GDP measured on the income side (referred to as “Real Gross Domestic Income, chained dollars”). The table refers to Real Gross Dometic Product as \"Real GDP\" and to Real Gross Dometic Income as \"Real GDI\", and they are measured in billions of dollars. (Note: You will not have to use Nominal GDP)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another table, `from_2007`, has been created from `quarterly_gdp`, and includes information from 2007 to 2017. \n", "Below is a snippet from `from_2007`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from_2007.head(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$1.$ Compute the growth rate at an annual rate of each of the two series by quarter for\n", "2007:Q1–2012:Q4." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdi_rate = ___\n", "gdp_rate = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ Describe any two things you see when you compare the two series that you find\n", "interesting, and explain why you find them interesting." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Calculating Real Magnitudes:\n", "\n", "$1.$ When you calculate real national product, do you do so by dividing nominal national product by the price level or by subtracting the price level from nominal national product? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ When you calculate the real interest rate, do you do so by dividing the nominal interest rate by the price level or by subtracting the inflation rate from the nominal interest rate? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ Are your answers to (a) and (b) the same? Why or why not?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Unemployment Rate\n", "\n", "Use the `unemployment` table provided to answer the following questions. ***All numbers (other than percents) are in the thousands.***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the first five entries of the table." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### What, roughly, was the highest level the U.S. unemployment rate (measured as Percent Unemployed of Labor Force in the table) reached in:\n", "\n", "$1.$ The 20th century?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment_20th = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ The past fifty years? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment_past_50 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ The twenty years before 2006?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment_before_2006 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ Given your answers to (1) through (3), Do you think there is a connection between your answer to the question above and the fact that Federal Reserve Chair Alan Greenspan received a five-minute standing ovation at the end of the first of many events marking his retirement in 2005?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The State of the Labor Market\n", "\n", "$1.$ About how many people lose or quit their jobs in an average year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average_quitters = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ About how many people get jobs in an average year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average_getters = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ About how many people are unemployed in an average year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average_unemployed = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$4.$ About how many people are at work in an average year?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average_workers = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$5.$ About how many people are unemployed now?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployed_now = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### National Income Accounting:\n", "\n", "$1.$ What was the level of real GDP in 2005 dollars in 1970?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "real_gdp_2005 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$2.$ What was the rate of inflation in the United States in 2000?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inflation_rate_2000 = ___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$3.$ Explain whether or not, how, and why the following items are included in the calculation of GDP: (i) rent you pay on an apartment, (ii) purchase of a used textbook, (iii) purchase of a new tank by the Department of Defense, (iv) watching an advertisement on youtube." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ANSWER:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Congratulations, you have finished your first assignment for Econ 101B! Run the cell below to submit all of your work. Make sure to check on OK to make sure that it has uploaded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some materials this notebook were taken from [Data 8](http://data8.org/), [CS 61A](http://cs61a.org/), and [DS Modules](http://data.berkeley.edu/education/modules) lessons." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "\n", " \n", "\n", "## Introduction: Python and Economics \n", "\n", "\n", "\n", "### Catch Our Breath—Further Notes:\n", "\n", "
\n", "\n", "----\n", "\n", "* Weblog Support \n", "* nbViewer \n", "\n", " \n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \n", "\n", "## Programming Dos and Don'ts... \n", "\n", "### A Running List... \n", "\n", "1. Do restart your kernel and run cells up to your current working point every fifteen minutes or so. Yes, it takes a little time. But if you don't, sooner or later the machine's namespace will get confused, and then you will get confused about the state of the machine's namespace, and by assuming things about it that are false you will lose hours and hours...\n", " \n", "2. Do reload the page when restarting the kernel does not seem to do the job...\n", " \n", "3. Do edit code cells by copying them below your current version and then working on the copy: when you break everything in the current cell (as you will), you can then go back to the old cell and start fresh...\n", " \n", "4. Do exercise agile development practices: if there is a line of code that you have not tested, test it. The best way to test is to ask the machine to echo back to you the thing you have just created in its namespace to make sure that it is what you want it to be. Only after you are certain that your namespace contains what you think it does should you write the next line of code. And then you should immediately test it...\n", " \n", "5. Do take screenshots of your error messages...\n", " \n", "6. Do google your error messages: Ms. Google is your best friend here...\n", " \n", "7. Do not confuse assignment (\"=\") and test for equality (\"==\"). In general, if there is an \"if\" anywhere nearby, you should be testing for equality. If there is not, you should be assignment a variable in your namespace to a value. Do curse the mathematicians 500 years ago who did not realize that in the twenty-first century it would be very convenient if we had different and not confusable symbols for equals-as-assignment and equals-as-test...\n", "\n", "8. Do expect things to go wrong: it's not the end of the world, or even a threat to your soul. Back up to a stage where things were working as expected, and then try to figure out why things diverged from your expectations. Here, for example, we have Gandalf the Grey, Python νB, confronting unexpected behavior from a Python pandas.DataFrame. Yes, he is going to have to upgrade his entire system and reboot. But in the end he will be fine: \n", "\"Tools\n", "\n", "On the elective affinity between computer programming and sorcery: \n", "\n", " \n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Fine Chancery Hand of the Twenty-First Century \n", "\n", "### David Guarino, UCB '07, on LinkedIn \n", "\n", "\"Https" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "" ] } ], "metadata": { "anaconda-cloud": {}, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 4 }