{ "cells": [ { "cell_type": "markdown", "id": "54c925d4", "metadata": {}, "source": [ "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "07246e9e", "metadata": {}, "source": [ "# An Introductory Example\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "481f191a", "metadata": {}, "source": [ "## Contents\n", "\n", "- [An Introductory Example](#An-Introductory-Example) \n", " - [Overview](#Overview) \n", " - [The Task: Plotting a White Noise Process](#The-Task:-Plotting-a-White-Noise-Process) \n", " - [Version 1](#Version-1) \n", " - [Alternative Implementations](#Alternative-Implementations) \n", " - [Another Application](#Another-Application) \n", " - [Exercises](#Exercises) " ] }, { "cell_type": "markdown", "id": "c5d65ba9", "metadata": {}, "source": [ "## Overview\n", "\n", "We’re now ready to start learning the Python language itself.\n", "\n", "In this lecture, we will write and then pick apart small Python programs.\n", "\n", "The objective is to introduce you to basic Python syntax and data structures.\n", "\n", "Deeper concepts will be covered in later lectures.\n", "\n", "You should have read the [lecture](https://python-programming.quantecon.org/getting_started.html) on getting started with Python before beginning this one." ] }, { "cell_type": "markdown", "id": "b12f26e7", "metadata": {}, "source": [ "## The Task: Plotting a White Noise Process\n", "\n", "Suppose we want to simulate and plot the white noise\n", "process $\\epsilon_0, \\epsilon_1, \\ldots, \\epsilon_T$, where each draw $\\epsilon_t$ is independent standard normal.\n", "\n", "In other words, we want to generate figures that look something like this:\n", "\n", "![https://python-programming.quantecon.org/_static/lecture_specific/python_by_example/test_program_1_updated.png](https://python-programming.quantecon.org/_static/lecture_specific/python_by_example/test_program_1_updated.png)\n", "\n", " \n", "(Here $t$ is on the horizontal axis and $\\epsilon_t$ is on the\n", "vertical axis.)\n", "\n", "We’ll do this in several different ways, each time learning something more\n", "about Python." ] }, { "cell_type": "markdown", "id": "ce1ec143", "metadata": {}, "source": [ "## Version 1\n", "\n", "\n", "\n", "Here are a few lines of code that perform the task we set" ] }, { "cell_type": "code", "execution_count": null, "id": "8e5b4699", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "ϵ_values = np.random.randn(100)\n", "plt.plot(ϵ_values)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "e4011aae", "metadata": {}, "source": [ "Let’s break this program down and see how it works.\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "25fc95d2", "metadata": {}, "source": [ "### Imports\n", "\n", "The first two lines of the program import functionality from external code\n", "libraries.\n", "\n", "The first line imports [NumPy](https://python-programming.quantecon.org/numpy.html), a favorite Python package for tasks like\n", "\n", "- working with arrays (vectors and matrices) \n", "- common mathematical functions like cos and sqrt \n", "- generating random numbers \n", "- linear algebra, etc. \n", "\n", "\n", "After import numpy as np we have access to these attributes via the syntax np.attribute.\n", "\n", "Here’s two more examples" ] }, { "cell_type": "code", "execution_count": null, "id": "e2413185", "metadata": { "hide-output": false }, "outputs": [], "source": [ "np.sqrt(4)" ] }, { "cell_type": "code", "execution_count": null, "id": "dd06c21e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "np.log(4)" ] }, { "cell_type": "markdown", "id": "967905cc", "metadata": {}, "source": [ "We could also use the following syntax:" ] }, { "cell_type": "code", "execution_count": null, "id": "476d2228", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy\n", "\n", "numpy.sqrt(4)" ] }, { "cell_type": "markdown", "id": "8c04b451", "metadata": {}, "source": [ "But the former method (using the short name np) is convenient and more standard." ] }, { "cell_type": "markdown", "id": "d17babb6", "metadata": {}, "source": [ "#### Why So Many Imports?\n", "\n", "Python programs typically require several import statements.\n", "\n", "The reason is that the core language is deliberately kept small, so that it’s easy to learn and maintain.\n", "\n", "When you want to do something interesting with Python, you almost always need\n", "to import additional functionality." ] }, { "cell_type": "markdown", "id": "a49bdcf4", "metadata": {}, "source": [ "#### Packages\n", "\n", "\n", "\n", "As stated above, NumPy is a Python *package*.\n", "\n", "Packages are used by developers to organize code they wish to share.\n", "\n", "In fact, a package is just a directory containing\n", "\n", "1. files with Python code — called **modules** in Python speak \n", "1. possibly some compiled code that can be accessed by Python (e.g., functions compiled from C or FORTRAN code) \n", "1. a file called __init__.py that specifies what will be executed when we type import package_name \n", "\n", "\n", "You can check the location of your __init__.py for NumPy in python by running the code:" ] }, { "cell_type": "markdown", "id": "c0444647", "metadata": { "hide-output": false }, "source": [ "ipython\n", "import numpy as np\n", "\n", "print(np.__file__)\n", "\n" ] }, { "cell_type": "markdown", "id": "e328aa1b", "metadata": {}, "source": [ "#### Subpackages\n", "\n", "\n", "\n", "Consider the line ϵ_values = np.random.randn(100).\n", "\n", "Here np refers to the package NumPy, while random is a **subpackage** of NumPy.\n", "\n", "Subpackages are just packages that are subdirectories of another package.\n", "\n", "For instance, you can find folder random under the directory of NumPy." ] }, { "cell_type": "markdown", "id": "279e2e36", "metadata": {}, "source": [ "### Importing Names Directly\n", "\n", "Recall this code that we saw above" ] }, { "cell_type": "code", "execution_count": null, "id": "bc6cbe98", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np\n", "\n", "np.sqrt(4)" ] }, { "cell_type": "markdown", "id": "251d53ff", "metadata": {}, "source": [ "Here’s another way to access NumPy’s square root function" ] }, { "cell_type": "code", "execution_count": null, "id": "bb62086c", "metadata": { "hide-output": false }, "outputs": [], "source": [ "from numpy import sqrt\n", "\n", "sqrt(4)" ] }, { "cell_type": "markdown", "id": "7e1a96cb", "metadata": {}, "source": [ "This is also fine.\n", "\n", "The advantage is less typing if we use sqrt often in our code.\n", "\n", "The disadvantage is that, in a long program, these two lines might be\n", "separated by many other lines.\n", "\n", "Then it’s harder for readers to know where sqrt came from, should they wish to." ] }, { "cell_type": "markdown", "id": "90314732", "metadata": {}, "source": [ "### Random Draws\n", "\n", "Returning to our program that plots white noise, the remaining three lines\n", "after the import statements are" ] }, { "cell_type": "code", "execution_count": null, "id": "d03a257e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "ϵ_values = np.random.randn(100)\n", "plt.plot(ϵ_values)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "d1d6e0be", "metadata": {}, "source": [ "The first line generates 100 (quasi) independent standard normals and stores\n", "them in ϵ_values.\n", "\n", "The next two lines genererate the plot.\n", "\n", "We can and will look at various ways to configure and improve this plot below." ] }, { "cell_type": "markdown", "id": "20941660", "metadata": {}, "source": [ "## Alternative Implementations\n", "\n", "Let’s try writing some alternative versions of [our first program](#ourfirstprog), which plotted IID draws from the standard normal distribution.\n", "\n", "The programs below are less efficient than the original one, and hence\n", "somewhat artificial.\n", "\n", "But they do help us illustrate some important Python syntax and semantics in a familiar setting." ] }, { "cell_type": "markdown", "id": "5dbc0a7a", "metadata": {}, "source": [ "### A Version with a For Loop\n", "\n", "Here’s a version that illustrates for loops and Python lists.\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "900f127b", "metadata": { "hide-output": false }, "outputs": [], "source": [ "ts_length = 100\n", "ϵ_values = [] # empty list\n", "\n", "for i in range(ts_length):\n", " e = np.random.randn()\n", " ϵ_values.append(e)\n", "\n", "plt.plot(ϵ_values)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "f8bf72d4", "metadata": {}, "source": [ "In brief,\n", "\n", "- The first line sets the desired length of the time series. \n", "- The next line creates an empty *list* called ϵ_values that will store the $\\epsilon_t$ values as we generate them. \n", "- The statement # empty list is a *comment*, and is ignored by Python’s interpreter. \n", "- The next three lines are the for loop, which repeatedly draws a new random number $\\epsilon_t$ and appends it to the end of the list ϵ_values. \n", "- The last two lines generate the plot and display it to the user. \n", "\n", "\n", "Let’s study some parts of this program in more detail.\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "6c03b392", "metadata": {}, "source": [ "### Lists\n", "\n", "\n", "\n", "Consider the statement ϵ_values = [], which creates an empty list.\n", "\n", "Lists are a *native Python data structure* used to group a collection of objects.\n", "\n", "Items in lists are ordered, and duplicates are allowed in lists.\n", "\n", "For example, try" ] }, { "cell_type": "code", "execution_count": null, "id": "1300981e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x = [10, 'foo', False]\n", "type(x)" ] }, { "cell_type": "markdown", "id": "812f5d85", "metadata": {}, "source": [ "The first element of x is an [integer](https://en.wikipedia.org/wiki/Integer_%28computer_science%29), the next is a [string](https://en.wikipedia.org/wiki/String_%28computer_science%29), and the third is a [Boolean value](https://en.wikipedia.org/wiki/Boolean_data_type).\n", "\n", "When adding a value to a list, we can use the syntax list_name.append(some_value)" ] }, { "cell_type": "code", "execution_count": null, "id": "ae21ee7e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "id": "5b92ff18", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x.append(2.5)\n", "x" ] }, { "cell_type": "markdown", "id": "8c0d456b", "metadata": {}, "source": [ "Here append() is what’s called a *method*, which is a function “attached to” an object—in this case, the list x.\n", "\n", "We’ll learn all about methods [later on](https://python-programming.quantecon.org/oop_intro.html), but just to give you some idea,\n", "\n", "- Python objects such as lists, strings, etc. all have methods that are used to manipulate the data contained in the object. \n", "- String objects have [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods), list objects have [list methods](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), etc. \n", "\n", "\n", "Another useful list method is pop()" ] }, { "cell_type": "code", "execution_count": null, "id": "3a192265", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "id": "77db8e12", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x.pop()" ] }, { "cell_type": "code", "execution_count": null, "id": "8d3c6f0b", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x" ] }, { "cell_type": "markdown", "id": "8d871f43", "metadata": {}, "source": [ "Lists in Python are zero-based (as in C, Java or Go), so the first element is referenced by x" ] }, { "cell_type": "code", "execution_count": null, "id": "bfcb74b6", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x # first element of x" ] }, { "cell_type": "code", "execution_count": null, "id": "ff13ac5d", "metadata": { "hide-output": false }, "outputs": [], "source": [ "x # second element of x" ] }, { "cell_type": "markdown", "id": "964176c4", "metadata": {}, "source": [ "### The For Loop\n", "\n", "\n", "\n", "Now let’s consider the for loop from [the program above](#firstloopprog), which was" ] }, { "cell_type": "code", "execution_count": null, "id": "1ce6961b", "metadata": { "hide-output": false }, "outputs": [], "source": [ "for i in range(ts_length):\n", " e = np.random.randn()\n", " ϵ_values.append(e)" ] }, { "cell_type": "markdown", "id": "bef4943f", "metadata": {}, "source": [ "Python executes the two indented lines ts_length times before moving on.\n", "\n", "These two lines are called a code block, since they comprise the “block” of code that we are looping over.\n", "\n", "Unlike most other languages, Python knows the extent of the code block *only from indentation*.\n", "\n", "In our program, indentation decreases after line ϵ_values.append(e), telling Python that this line marks the lower limit of the code block.\n", "\n", "More on indentation below—for now, let’s look at another example of a for loop" ] }, { "cell_type": "code", "execution_count": null, "id": "f239fc5a", "metadata": { "hide-output": false }, "outputs": [], "source": [ "animals = ['dog', 'cat', 'bird']\n", "for animal in animals:\n", " print(\"The plural of \" + animal + \" is \" + animal + \"s\")" ] }, { "cell_type": "markdown", "id": "becdff18", "metadata": {}, "source": [ "This example helps to clarify how the for loop works: When we execute a\n", "loop of the form" ] }, { "cell_type": "markdown", "id": "0637e629", "metadata": { "hide-output": false }, "source": [ "python3\n", "for variable_name in sequence:\n", " \n", "\n" ] }, { "cell_type": "markdown", "id": "26d23207", "metadata": {}, "source": [ "The Python interpreter performs the following:\n", "\n", "- For each element of the sequence, it “binds” the name variable_name to that element and then executes the code block. \n", "\n", "\n", "The sequence object can in fact be a very general object, as we’ll see\n", "soon enough." ] }, { "cell_type": "markdown", "id": "9c38079f", "metadata": {}, "source": [ "### A Comment on Indentation\n", "\n", "\n", "\n", "In discussing the for loop, we explained that the code blocks being looped over are delimited by indentation.\n", "\n", "In fact, in Python, **all** code blocks (i.e., those occurring inside loops, if clauses, function definitions, etc.) are delimited by indentation.\n", "\n", "Thus, unlike most other languages, whitespace in Python code affects the output of the program.\n", "\n", "Once you get used to it, this is a good thing: It\n", "\n", "- forces clean, consistent indentation, improving readability \n", "- removes clutter, such as the brackets or end statements used in other languages \n", "\n", "\n", "On the other hand, it takes a bit of care to get right, so please remember:\n", "\n", "- The line before the start of a code block always ends in a colon \n", " - for i in range(10): \n", " - if x > y: \n", " - while x < 100: \n", " - etc., etc. \n", "- All lines in a code block **must have the same amount of indentation**. \n", "- The Python standard is 4 spaces, and that’s what you should use. " ] }, { "cell_type": "markdown", "id": "a73d8615", "metadata": {}, "source": [ "### While Loops\n", "\n", "\n", "\n", "The for loop is the most common technique for iteration in Python.\n", "\n", "But, for the purpose of illustration, let’s modify [the program above](#firstloopprog) to use a while loop instead.\n", "\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "id": "3e405535", "metadata": { "hide-output": false }, "outputs": [], "source": [ "ts_length = 100\n", "ϵ_values = []\n", "i = 0\n", "while i < ts_length:\n", " e = np.random.randn()\n", " ϵ_values.append(e)\n", " i = i + 1\n", "plt.plot(ϵ_values)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "c1d136d3", "metadata": {}, "source": [ "A while loop will keep executing the code block delimited by indentation until the condition (i < ts_length) is satisfied.\n", "\n", "In this case, the program will keep adding values to the list ϵ_values until i equals ts_length:" ] }, { "cell_type": "code", "execution_count": null, "id": "d452d14d", "metadata": { "hide-output": false }, "outputs": [], "source": [ "i == ts_length #the ending condition for the while loop" ] }, { "cell_type": "markdown", "id": "96460736", "metadata": {}, "source": [ "Note that\n", "\n", "- the code block for the while loop is again delimited only by indentation. \n", "- the statement i = i + 1 can be replaced by i += 1. " ] }, { "cell_type": "markdown", "id": "7f0c593f", "metadata": {}, "source": [ "## Another Application\n", "\n", "Let’s do one more application before we turn to exercises.\n", "\n", "In this application, we plot the balance of a bank account over time.\n", "\n", "There are no withdraws over the time period, the last date of which is denoted\n", "by $T$.\n", "\n", "The initial balance is $b_0$ and the interest rate is $r$.\n", "\n", "The balance updates from period $t$ to $t+1$ according to $b_{t+1} = (1 + r) b_t$.\n", "\n", "In the code below, we generate and plot the sequence $b_0, b_1, \\ldots, b_T$.\n", "\n", "Instead of using a Python list to store this sequence, we will use a NumPy\n", "array." ] }, { "cell_type": "code", "execution_count": null, "id": "a5a3a4ba", "metadata": { "hide-output": false }, "outputs": [], "source": [ "r = 0.025 # interest rate\n", "T = 50 # end date\n", "b = np.empty(T+1) # an empty NumPy array, to store all b_t\n", "b = 10 # initial balance\n", "\n", "for t in range(T):\n", " b[t+1] = (1 + r) * b[t]\n", "\n", "plt.plot(b, label='bank balance')\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "00172228", "metadata": {}, "source": [ "The statement b = np.empty(T+1) allocates storage in memory for T+1\n", "(floating point) numbers.\n", "\n", "These numbers are filled in by the for loop.\n", "\n", "Allocating memory at the start is more efficient than using a Python list and\n", "append, since the latter must repeatedly ask for storage space from the\n", "operating system.\n", "\n", "Notice that we added a legend to the plot — a feature you will be asked to\n", "use in the exercises." ] }, { "cell_type": "markdown", "id": "14016ee8", "metadata": {}, "source": [ "## Exercises\n", "\n", "Now we turn to exercises. It is important that you complete them before\n", "continuing, since they present new concepts we will need." ] }, { "cell_type": "markdown", "id": "bdbf1c3f", "metadata": {}, "source": [ "## Exercise 3.1\n", "\n", "Your first task is to simulate and plot the correlated time series\n", "\n", "$$\n", "x_{t+1} = \\alpha \\, x_t + \\epsilon_{t+1}\n", "\\quad \\text{where} \\quad\n", "x_0 = 0\n", "\\quad \\text{and} \\quad t = 0,\\ldots,T\n", "$$\n", "\n", "The sequence of shocks $\\{\\epsilon_t\\}$ is assumed to be IID and standard normal.\n", "\n", "In your solution, restrict your import statements to" ] }, { "cell_type": "code", "execution_count": null, "id": "3a748b84", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "7ff01040", "metadata": {}, "source": [ "Set $T=200$ and $\\alpha = 0.9$." ] }, { "cell_type": "markdown", "id": "9de63401", "metadata": {}, "source": [ "## Solution to[ Exercise 3.1](https://python-programming.quantecon.org/#pbe_ex1)\n", "\n", "Here’s one solution." ] }, { "cell_type": "code", "execution_count": null, "id": "64a879f1", "metadata": { "hide-output": false }, "outputs": [], "source": [ "α = 0.9\n", "T = 200\n", "x = np.empty(T+1)\n", "x = 0\n", "\n", "for t in range(T):\n", " x[t+1] = α * x[t] + np.random.randn()\n", "\n", "plt.plot(x)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "cba1cf7b", "metadata": {}, "source": [ "## Exercise 3.2\n", "\n", "Starting with your solution to exercise 1, plot three simulated time series,\n", "one for each of the cases $\\alpha=0$, $\\alpha=0.8$ and $\\alpha=0.98$.\n", "\n", "Use a for loop to step through the $\\alpha$ values.\n", "\n", "If you can, add a legend, to help distinguish between the three time series.\n", "\n", "- If you call the plot() function multiple times before calling show(), all of the lines you produce will end up on the same figure. \n", "- For the legend, noted that suppose var = 42, the expression f'foo{var}' evaluates to 'foo42'. " ] }, { "cell_type": "markdown", "id": "98e4d793", "metadata": {}, "source": [ "## Solution to[ Exercise 3.2](https://python-programming.quantecon.org/#pbe_ex2)" ] }, { "cell_type": "code", "execution_count": null, "id": "268f812a", "metadata": { "hide-output": false }, "outputs": [], "source": [ "α_values = [0.0, 0.8, 0.98]\n", "T = 200\n", "x = np.empty(T+1)\n", "\n", "for α in α_values:\n", " x = 0\n", " for t in range(T):\n", " x[t+1] = α * x[t] + np.random.randn()\n", " plt.plot(x, label=f'$\\\\alpha = {α}$')\n", "\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "8b9ffcb0", "metadata": {}, "source": [ ">**Note**\n", ">\n", ">f'\\$\\\\alpha = {α}\\$' in the solution is an application of [f-String](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings), which allows you to use {} to contain an expression.\n", "\n", "The contained expression will be evaluated, and the result will be placed into the string." ] }, { "cell_type": "markdown", "id": "7ed0d6cb", "metadata": {}, "source": [ "## Exercise 3.3\n", "\n", "Similar to the previous exercises, plot the time series\n", "\n", "$$\n", "x_{t+1} = \\alpha \\, |x_t| + \\epsilon_{t+1}\n", "\\quad \\text{where} \\quad\n", "x_0 = 0\n", "\\quad \\text{and} \\quad t = 0,\\ldots,T\n", "$$\n", "\n", "Use $T=200$, $\\alpha = 0.9$ and $\\{\\epsilon_t\\}$ as before.\n", "\n", "Search online for a function that can be used to compute the absolute value $|x_t|$." ] }, { "cell_type": "markdown", "id": "d4560682", "metadata": {}, "source": [ "## Solution to[ Exercise 3.3](https://python-programming.quantecon.org/#pbe_ex3)\n", "\n", "Here’s one solution:" ] }, { "cell_type": "code", "execution_count": null, "id": "4021f321", "metadata": { "hide-output": false }, "outputs": [], "source": [ "α = 0.9\n", "T = 200\n", "x = np.empty(T+1)\n", "x = 0\n", "\n", "for t in range(T):\n", " x[t+1] = α * np.abs(x[t]) + np.random.randn()\n", "\n", "plt.plot(x)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "ef479bc2", "metadata": {}, "source": [ "## Exercise 3.4\n", "\n", "One important aspect of essentially all programming languages is branching and\n", "conditions.\n", "\n", "In Python, conditions are usually implemented with if–else syntax.\n", "\n", "Here’s an example, that prints -1 for each negative number in an array and 1\n", "for each nonnegative number" ] }, { "cell_type": "code", "execution_count": null, "id": "6db015e1", "metadata": { "hide-output": false }, "outputs": [], "source": [ "numbers = [-9, 2.3, -11, 0]" ] }, { "cell_type": "code", "execution_count": null, "id": "3d279e62", "metadata": { "hide-output": false }, "outputs": [], "source": [ "for x in numbers:\n", " if x < 0:\n", " print(-1)\n", " else:\n", " print(1)" ] }, { "cell_type": "markdown", "id": "53ea79b2", "metadata": {}, "source": [ "Now, write a new solution to Exercise 3 that does not use an existing function\n", "to compute the absolute value.\n", "\n", "Replace this existing function with an if–else condition." ] }, { "cell_type": "markdown", "id": "106e1db5", "metadata": {}, "source": [ "## Solution to[ Exercise 3.4](https://python-programming.quantecon.org/#pbe_ex4)\n", "\n", "Here’s one way:" ] }, { "cell_type": "code", "execution_count": null, "id": "c2f33a98", "metadata": { "hide-output": false }, "outputs": [], "source": [ "α = 0.9\n", "T = 200\n", "x = np.empty(T+1)\n", "x = 0\n", "\n", "for t in range(T):\n", " if x[t] < 0:\n", " abs_x = - x[t]\n", " else:\n", " abs_x = x[t]\n", " x[t+1] = α * abs_x + np.random.randn()\n", "\n", "plt.plot(x)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "993004ca", "metadata": {}, "source": [ "Here’s a shorter way to write the same thing:" ] }, { "cell_type": "code", "execution_count": null, "id": "f9994182", "metadata": { "hide-output": false }, "outputs": [], "source": [ "α = 0.9\n", "T = 200\n", "x = np.empty(T+1)\n", "x = 0\n", "\n", "for t in range(T):\n", " abs_x = - x[t] if x[t] < 0 else x[t]\n", " x[t+1] = α * abs_x + np.random.randn()\n", "\n", "plt.plot(x)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "897af3f3", "metadata": {}, "source": [ "## Exercise 3.5\n", "\n", "Here’s a harder exercise, that takes some thought and planning.\n", "\n", "The task is to compute an approximation to $\\pi$ using [Monte Carlo](https://en.wikipedia.org/wiki/Monte_Carlo_method).\n", "\n", "Use no imports besides" ] }, { "cell_type": "code", "execution_count": null, "id": "3f22a7fc", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "c8d4efa9", "metadata": {}, "source": [ "Your hints are as follows:\n", "\n", "- If $U$ is a bivariate uniform random variable on the unit square $(0, 1)^2$, then the probability that $U$ lies in a subset $B$ of $(0,1)^2$ is equal to the area of $B$. \n", "- If $U_1,\\ldots,U_n$ are IID copies of $U$, then, as $n$ gets large, the fraction that falls in $B$, converges to the probability of landing in $B$. \n", "- For a circle, $area = \\pi * radius^2$. " ] }, { "cell_type": "markdown", "id": "5b28c63f", "metadata": {}, "source": [ "## Solution to[ Exercise 3.5](https://python-programming.quantecon.org/#pbe_ex5)\n", "\n", "Consider the circle of diameter 1 embedded in the unit square.\n", "\n", "Let $A$ be its area and let $r=1/2$ be its radius.\n", "\n", "If we know $\\pi$ then we can compute $A$ via\n", "$A = \\pi r^2$.\n", "\n", "But here the point is to compute $\\pi$, which we can do by\n", "$\\pi = A / r^2$.\n", "\n", "Summary: If we can estimate the area of a circle with diameter 1, then dividing\n", "by $r^2 = (1/2)^2 = 1/4$ gives an estimate of $\\pi$.\n", "\n", "We estimate the area by sampling bivariate uniforms and looking at the\n", "fraction that falls into the circle." ] }, { "cell_type": "code", "execution_count": null, "id": "f56a9888", "metadata": { "hide-output": false }, "outputs": [], "source": [ "n = 1000000 # sample size for Monte Carlo simulation\n", "\n", "count = 0\n", "for i in range(n):\n", "\n", " # drawing random positions on the square\n", " u, v = np.random.uniform(), np.random.uniform()\n", "\n", " # check whether the point falls within the boundary\n", " # of the unit circle centred at (0.5,0.5)\n", " d = np.sqrt((u - 0.5)**2 + (v - 0.5)**2)\n", "\n", " # if it falls within the inscribed circle, \n", " # add it to the count\n", " if d < 0.5:\n", " count += 1\n", "\n", "area_estimate = count / n\n", "\n", "print(area_estimate * 4) # dividing by radius**2" ] } ], "metadata": { "date": 1695106394.2376952, "filename": "python_by_example.md", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "An Introductory Example" }, "nbformat": 4, "nbformat_minor": 5 }