\n",
" \n",
" \n",
" \n",
"

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# An Introductory Example\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"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) \n",
" - [Solutions](#Solutions) "
]
},
{
"cell_type": "markdown",
"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",
"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.\n",
"\n",
"We run the following command first, which helps ensure that plots appear in the\n",
"notebook if you run it on your own machine."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"plt.rcParams['figure.figsize'] = (10,6)\n",
"\n",
"ϵ_values = np.random.randn(100)\n",
"plt.plot(ϵ_values)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s break this program down and see how it works.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"np.sqrt(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"np.log(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We could also use the following syntax:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy\n",
"\n",
"numpy.sqrt(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But the former method (using the short name `np`) is convenient and more standard."
]
},
{
"cell_type": "markdown",
"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",
"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",
"In fact, you can find and explore the directory for NumPy on your computer\n",
"easily enough if you look around.\n",
"\n",
"On this machine, it’s located in"
]
},
{
"cell_type": "markdown",
"metadata": {
"hide-output": false
},
"source": [
"```ipython\n",
"anaconda3/lib/python3.7/site-packages/numpy\n",
"```\n"
]
},
{
"cell_type": "markdown",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing Names Directly\n",
"\n",
"Recall this code that we saw above"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"np.sqrt(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here’s another way to access NumPy’s square root function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"from numpy import sqrt\n",
"\n",
"sqrt(4)"
]
},
{
"cell_type": "markdown",
"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",
"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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"ϵ_values = np.random.randn(100)\n",
"plt.plot(ϵ_values)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"## Alternative Implementations\n",
"\n",
"Let’s try writing some alternative versions of [our first program](#ourfirstprog), which plotted IID draws from the 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",
"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,
"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",
"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",
"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",
"For example, try"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x = [10, 'foo', False]\n",
"type(x)"
]
},
{
"cell_type": "markdown",
"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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x.append(2.5)\n",
"x"
]
},
{
"cell_type": "markdown",
"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, 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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x.pop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists in Python are zero-based (as in C, Java or Go), so the first element is referenced by `x[0]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x[0] # first element of x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"x[1] # second element of x"
]
},
{
"cell_type": "markdown",
"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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"for i in range(ts_length):\n",
" e = np.random.randn()\n",
" ϵ_values.append(e)"
]
},
{
"cell_type": "markdown",
"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,
"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",
"metadata": {},
"source": [
"This example helps to clarify how the `for` loop works: When we execute a\n",
"loop of the form"
]
},
{
"cell_type": "markdown",
"metadata": {
"hide-output": false
},
"source": [
"```python3\n",
"for variable_name in sequence:\n",
" ```
\n",
"```\n"
]
},
{
"cell_type": "markdown",
"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",
"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",
"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,
"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",
"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",
"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,
"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[0] = 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",
"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",
"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",
"metadata": {},
"source": [
"### Exercise 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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set $ T=200 $ and $ \\alpha = 0.9 $."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 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",
"Hints:\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 the expression `'foo' + str(42)` evaluates to `'foo42'`. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 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",
"metadata": {},
"source": [
"### Exercise 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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"numbers = [-9, 2.3, -11, 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"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.\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"## Solutions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1\n",
"\n",
"Here’s one solution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"α = 0.9\n",
"T = 200\n",
"x = np.empty(T+1)\n",
"x[0] = 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",
"metadata": {},
"source": [
"### Exercise 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"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] = 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",
"metadata": {},
"source": [
"### Exercise 3\n",
"\n",
"Here’s one solution:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"α = 0.9\n",
"T = 200\n",
"x = np.empty(T+1)\n",
"x[0] = 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",
"metadata": {},
"source": [
"### Exercise 4\n",
"\n",
"Here’s one way:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"α = 0.9\n",
"T = 200\n",
"x = np.empty(T+1)\n",
"x[0] = 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",
"metadata": {},
"source": [
"Here’s a shorter way to write the same thing:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"α = 0.9\n",
"T = 200\n",
"x = np.empty(T+1)\n",
"x[0] = 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",
"metadata": {},
"source": [
"### Exercise 5\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,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
"n = 100000\n",
"\n",
"count = 0\n",
"for i in range(n):\n",
" u, v = np.random.uniform(), np.random.uniform()\n",
" d = np.sqrt((u - 0.5)**2 + (v - 0.5)**2)\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": 1627364944.6224174,
"filename": "python_by_example.md",
"kernelspec": {
"display_name": "Python",
"language": "python3",
"name": "python3"
},
"title": "An Introductory Example"
},
"nbformat": 4,
"nbformat_minor": 4
}
```