Tải bản đầy đủ (.pdf) (472 trang)

Quantitative Economics

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.01 MB, 472 trang )

QUANTITATIVE ECONOMICS
Thomas Sargent and John Stachurski
February 14, 2014
2
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
1 Introduction 9
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 What You Will Learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 PDF or HTML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 Structure of the Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Programming in Python 11
2.1 About Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Setting up Your Python Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 An Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.4 Python Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.6 How it Works: Data, Variables and Names . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.7 More Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3 The Scientific Libraries 95
3.1 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.2 SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.3 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.4 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
3.5 IPython Shell and Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4 Introductory Applications 153
4.1 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4.2 Finite Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
4.3 Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
4.4 Schelling’s Segregation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
4.5 LLN and CLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
4.6 Linear State Space Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205


4.7 A First Look at the Kalman Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
4.8 Infinite Horizon Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 238
4.9 LQ Control Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
4.10 Rational Expectations Equilibrium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
4.11 Asset Pricing in a Markov Environment . . . . . . . . . . . . . . . . . . . . . . . . . . 284
CONTENTS 4
5 Advanced Applications 295
5.1 Continuous State Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
5.2 Modeling Career Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
5.3 On-the-Job Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
5.4 Search with Offer Distribution Unknown . . . . . . . . . . . . . . . . . . . . . . . . . 324
5.5 Optimal Savings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
5.6 Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
5.7 Linear Stochastic Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
5.8 Estimation of Spectra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
5.9 Optimal Taxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
6 Solutions to Exercises 411
6.1 Exercises from An Introductory Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Solution to Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Solution to Exercise 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
6.2 Exercises from Python Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Solution to Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

6.3 Exercises from Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . 416
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
6.4 Exercises from More Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
6.5 Exercises from NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
6.6 Exercises from SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
6.7 Exercises from Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
6.8 Exercises from LLN and CLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
6.9 Exercises from Finite Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
6.10 Exercises from Schelling’s Segregation Model . . . . . . . . . . . . . . . . . . . . . . . . 429
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
CONTENTS 5
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
6.11 Exercises from Linear State Space Models . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432

Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
6.12 Exercises from A First Look at the Kalman Filter . . . . . . . . . . . . . . . . . . . . . . . 434
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
6.13 Exercises from Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
6.14 Exercises from Infinite Horizon Dynamic Programming . . . . . . . . . . . . . . . . . . . 438
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
6.15 Exercises from LQ Control Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
6.16 Exercises from Rational Expectations Equilibrium . . . . . . . . . . . . . . . . . . . . . . 445
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
6.17 Exercises from Search with Offer Distribution Unknown . . . . . . . . . . . . . . . . . . 450
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
6.18 Exercises from Modeling Career Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
6.19 Exercises from On-the-Job Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
6.20 Exercises from Asset Pricing in a Markov Environment . . . . . . . . . . . . . . . . . . . 457

Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
6.21 Exercises from Estimation of Spectra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
6.22 Exercises from Continuous State Markov Chains . . . . . . . . . . . . . . . . . . . . . . 459
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
6.23 Exercises from Optimal Savings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Solution to Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Solution to Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Solution to Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
6.24 Exercises from Optimal Taxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
Solution to Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
7 FAQs / Useful Resources 465
7.1 FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
7.2 How do I install Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
7.3 How do I start Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
7.4 How can I get help on a Python command? . . . . . . . . . . . . . . . . . . . . . . . . 465
7.5 Where do I get all the Python programs from the lectures? . . . . . . . . . . . . . . . 465
7.6 What’s Git? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
7.7 Other Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
7.8 IPython Magics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
7.9 IPython Cell Magics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
7.10 Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
8 References 467
CONTENTS 7
Note: You are currently viewing an automatically generated PDF version of our on-
line lectures, which are located at

This PDF is generated from a set of source files that are orientated towards the website
and to HTML output. At this stage the presentation quality is a bit less consistent than
the website, and some internal links might not work.
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
CONTENTS 8
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
“Science is what we understand well enough to explain to a computer; art is everything
else.” – Donald E. Knuth
This website contains a sequence of lectures on economic modeling, focusing on the use of pro-
gramming and computers for both problem solving and building intuition
The primary programming language used in the lecture series is Python, a general purpose, open
source programming language with excellent scientific libraries
(We’ll tell you more about Python and why we chose it in the next lecture)
At this stage, the level of the lectures varies from advanced undergraduate to graduate, although
we intend to add more elementary applications in the near future
The lectures are suitable for courses in quantitative methods and computational techniques, and
also for self study and independent study groups
To aid self study, all exercises have solutions
Our solutions are not the last word on each exercise — instead they provide one approach that
demonstrates good coding practices
If you work through the majority of the course and do the exercises, you will learn
• how to analyze a number of fundamental economic problems, from job search and neigh-
borhood selection to optimal fiscal policy
• the core of the Python programming language, including the main scientific libraries
• good programming style
• how to work with modern software development tools such as debuggers and version con-
trol
1.3. PDF OR HTML? 10
• a number of mathematical topics central to economic modeling, such as
– dynamic programming

– finite and continuous Markov chains
– filtering and state space models
– Fourier transforms and spectral analysis
– etc., etc.
• related numerical methods
– function approximation
– numerical optimization
– simulation based techniques and Monte Carlo
– recursion
– etc., etc.
You can view these lectures on-line or download the PDF version
(If you are reading this on-line, see the menu bar at the top of the page to download the PDF)
If you decide to use the PDF, please be aware that
1. the PDF is automatically generated from source that is orientated towards the website and
to HTML output, not PDF
2. the website will be change regularly, so each PDF will soon become out of date
Nonetheless, we appreciate that PDF is sometimes more convenient for reading than a live website
The first two parts of the course deal with the core Python language and the scientific libraries
The third part of the course contains easier applications
In these applications, coding strategies are discussed slowly and in depth
The fourth part of the course is more advanced, and the lectures can be read selectively, according
to your interests
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
This first part of the course provides a relatively fast-paced introduction to the Python program-
ming language
In this lecture we will
• Outline what Python is
• Showcase some of its abilities
• Compare it to some other languages
When we show you Python code, it is not our intention that you seek to follow all the details, or

try to replicate all you see
We will work through all of the Python material step by step later in the lecture series
Our only objective for this lecture is to give you some feel of what Python is, and what it can do
Python is a general purpose programming language conceived in 1989 by Dutch programmer
Guido van Rossum
Python is free and open source
Community-based development of the core language is coordinated through the Python Software
Foundation
Python is supported by a vast collection of standard and external software libraries
Python has experienced rapid adoption in the last decade, and is now one of the most popular
programming languages
The PYPL index gives some indication of how its popularity has grown
2.1. ABOUT PYTHON 12
Common Uses Python is a general purpose language used in almost all application domains
• communications
• web development
• CGI and graphical user interfaces
• games
• multimedia, data processing, security, etc., etc., etc.
Used extensively by Internet service and high tech companies such as
• Google
• Dropbox
• Reddit
• YouTube
• Walt Disney Animation, etc., etc.
Often used to teach computer science and programming
• Introduction to computer science at edx/MIT
• Computer science 101 at Udacity
For reasons we will discuss, Python is particularly popular within the scientific community
• academia, NASA, CERN, etc.

• Meteorology, computational biology, chemistry, machine learning, artificial intelligence, etc.,
etc.
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 13
(To get an idea, you might like to browse some of the seminar topics from the most recent SciPy
conference)
Features
• A high level language suitable for rapid development
• Design philosophy emphasizes simplicity and readability
• Relatively small core language supported by many libraries
• A multiparadigm language, in that multiple programming styles are supported (procedural,
object-oriented, functional, etc.)
• Interpreted rather than compiled
Over the last decade, Python has become one of the core languages of scientific computing
This section briefly showcases some examples of Python for scientific programming
• All of these topics will be covered in detail later on
• Click on any figure to expand it
Numerical programming Fundamental matrix and array processing capabilities are provided
by the excellent NumPy library
NumPy provides the basic array data type plus some simple processing operations
For example
The SciPy library is built on top of NumPy and provides additional functionality For example,
let’s calculate

2
−2
φ(z)dz where φ is the standard normal density
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 14
SciPy includes many of the standard routines used in

• linear algebra
• integration
• interpolation
• optimization
• distributions and random number generation
• signal processing
• etc., etc.
Graphics The most popular and comprehensive Python library for creating figures and graphs
is Matplotlib
• Plots, histograms, contour images, 3D, bar charts, etc., etc.
• Output in many formats (PDF, PNG, EPS, etc.)
• LaTeX integration
Example 2D plot with embedded LaTeX annotations
Example contour plot
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 15
Example 3D plot
More examples can be found in the Matplotlib thumbnail gallery
Other graphics libraries include
• VPython — 3D graphics and animations
• pyprocessing — a ‘Processing’-like graphics environment
• Many more, but we will use only Matplotlib
Symbolic Algebra Sometimes it’s useful to be able to manipulate symbolic expressions in the
spirit of Mathematica / Maple
The SymPy library provides this functionality from within the Python shell
We can manipulate expressions
solve polynomials
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 16
and calculate limits, derivatives and integrals

The beauty of importing this functionality into Python is that we are working within a fully
fledged programming language
Can easily create tables of derivatives, generate LaTeX output, add it to figures, etc., etc.
Statistics Python’s data manipulation and statistics libraries have improved rapidly over the last
few years
Pandas One of the most popular libraries for working with data is pandas
Pandas is fast, efficient, flexible and well designed
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 17
Here’s a simple example
Other Useful Statistics Libraries
• statsmodels — various statistical routines
• scikit-learn — machine learning in Python (sponsored by Google, among others)
• pyMC — for Bayesian data analysis
• pystan Bayesian analysis based on stan
Networks and Graphs Python has many libraries for studying graphs
One well-known example is NetworkX
• Standard graph algorithms for analyzing network structure, etc.
• Plotting routines
• etc., etc.
Here’s some example code that generates and plots a random graph, with node color determined
by shortest path length from a central node
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 18
The figure it produces looks as follows
Cloud Computing Running your Python code on massive servers in the cloud is becoming eas-
ier and easier
An excellent example is Wakari — we’ll discuss how to get started with Wakari in the next lecture
Another alternative is PiCloud
See also

• Amazon Elastic Compute Cloud
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 19
• The Google App Engine (Python, Java, PHP or Go)
• Pythonanywhere
• Sagemath Cloud
Parallel Processing Apart from the cloud computing options listed above, you might like to
consider
• Parallel computing through IPython clusters
• The Starcluster interface to Amazon’s EC2
• GPU programming through Copperhead or Pycuda
Interfacing with C or Fortran But isn’t Fortran / C faster for scientific computing (along with
everything else)?
In one sense the answer is “yes”—these languages compile into native machine code, which runs
very fast
However, it turns out that more lines of scientific code are written in other languages, like Python
Why is this the case?
The reason is that your time is a far more valuable resource than the computer’s time
The correct objective function to minimize is
An ideal language would minimize both terms on RHS, but there is a trade-off here
• To minimize the first term, optimize for humans
• To minimize the second term, optimize for computers
Higher level languages such as Python are optimized for humans
Lower level languages such as Fortran and C are optimized for computers
Lower level languages run faster and give greater control, at the cost of
• taking longer to write and debug
• more details to address (declaring variables, memory allocation/deallocation, etc.)
• requiring boilerplate code, writing of which is error prone and very tedious
For these reasons, the modern scientific paradigm is to combine the strengths of high and low
level languages as follows:

1. Write a prototype program in a high-level language such as Python
2. If the program is too slow, then profile it to find out where the bottlenecks are
3. Rewrite those and only those small parts of the code in Fortran / C
4. Rewrite the existing Python program to call this new Fortran / C code when necessary
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 20
There are many ways to accomplish this — we will cover several in a later lecture
Other Developments There are many other interesting developments with scientific program-
ming in Python
Some representative examples include
• IPython notebook — Python in your browser with code cells, embedded images, etc.
• Numba — speed up scientific Python code
• Blaze — a generalization of NumPy
• PyMC — Bayesian statistical modeling in Python
• PyTables — manage large data sets
• CVXPY — convex optimization in Python
To some extent this question is inevitable, given that MATLAB is still the most common scripting
language for numerical computing within economics
MATLAB and Python are both high quality tools
As for any other pair of tools — a shifting spanner versus a socket wrench, say — neither is
“better” than the other
Moreover, MATLAB and Python are similar in many respects
Both are high productivity scripting languages, with slow loops offset by fast vectorized opera-
tions
Both have excellent graphics capabilities, and a long list of libraries for scientific computing
Nonetheless, Python has some important strengths that are driving its rapid uptake in scientific
computing
Open Source One obvious advantage is that Python is free and open source
When you start out with Python, the “free” component of this pair will probably be the most
appealing

It means that you, your coauthors and your students can install Python and any of its libraries on
all of your computers without cost, or having to bother about licenses
Over time, however, you will most likely come to value the “open source” property of Python as
much, if not more
The first advantage of open source libraries is that you can read them
For example, let’s say you want to know exactly how pandas computes Newey-West covariance
matrices
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.1. ABOUT PYTHON 21
No problem: You can go ahead and read the code
While dipping into external library code might seem daunting at first, it’s very useful for
1. Helping you understand the details of a particular implementation
2. Building your programming skills by showing you code written by first rate programmers
A second advantage of open source libraries is that you can change them
In particular, if the functionality provided by a given library is not exact what you want, you can
always open up your trusty editor and modify it
This can also seem daunting to beginners, but with time you will find that it can be extremely
useful
A third advantage of open source libraries is that active projects tend to respond fast to new de-
mand
If, for example, Microsoft modifies the format of its Excel files, new Python readers and decoders
will rapidly appear
A fourth, more philosophical advantage of open source software is that it conforms to the scientific
ideal of reproducibility
Since all the source code is visible, research you produce using Python will be more open, more
transparent and hence more reproducible
Flexibility and Broad Scope As mentioned above, Python is a general purpose programming
language with a wide range of applications
As such, it can be used for just about any task you are likely to face, from dynamic programming to
web scraping, forecasting with support vector machines, sentiment analysis via Twitter, building a

graphical front end for an experiment, or sending yourself emails to remind you of your mother’s
birthday
Moreover, Python has a vibrant and friendly community, and a massive array of third party li-
braries for almost any purpose
To learn more, you might like to
• Browse some Python projects on GitHub
• Have a look at some of the IPython notebooks people have shared on various scientific topics
• Visit the Python Package Index
• View some of the question people are asking about Python on Stackoverflow
• Keep up to date on what’s happening in the Python community with the Python subreddit
• Etc.
Syntax and Design Another nice feature of Python is its elegant syntax — we’ll see many exam-
ples later on
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.2. SETTING UP YOUR PYTHON ENVIRONMENT 22
Elegant code might sound superfluous, but in fact it’s highly beneficial because it makes the syntax
easy to read and easy to remember
Remembering how to read from files, sort dictionaries and other such routine tasks means that
you don’t need to break your flow of thought in order to hunt down correct syntax on the Internet
Closely related to elegant syntax is elegant design
Features like iterators, generators, decorators, list comprehensions, etc. make Python highly ex-
pressive, allowing you to get more done with less code
Namespaces improve productivity by cutting down on bugs and syntax errors
Julia is an open source language for scientific computing launched in early 2012
Julia is similar to MATLAB and Python in the sense that it is a dynamically typed scripting lan-
guage suitable for rapid development
Julia also offers the promise of fast loops by using a just-in-time compiler
We are excited about Julia, and believe it will turn into a very useful tool
On the other hand, Julia is still well below version 1, and hence might break backwards compati-
bility at some stage over the coming year or two

(This is a good thing — it’s not good for a project to lock down its design too early)
In addition, Julia lacks the massive scientific programming ecosystem that has gathered around
Python
We will wait until Julia has developed a bit more, and then consider treating Julia in our lectures
alongside Python
Additional comments:
• Work has already begun on integrating Julia and Python, so that Julia code can be called
from Python code and vice versa
• Fast loops are already available in Python via Numba or Parakeet, although these project are
still works in progress
The objective of this lecture is to help you
1. Get a Python environment up and running with all the necessary tools
2. Install the Python programs that underpin these lectures
3. Make sure that you know how to run these programs with Python
4. Learn how to modify them, and also write your own scripts
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.2. SETTING UP YOUR PYTHON ENVIRONMENT 23
Warning: The core Python package is easy to install, but is most likely not what you should
choose for these lectures. The reason is that these lectures require the entire scientific pro-
gramming ecosystem, which the core installation doesn’t provide. Please read the following
carefully.
If you have problems with what follows, feel free to contact us via —
we might not be able to help, but we’ll try to give suggestions and improve these notes from your
feedback
Let’s start with the most standard set-up: an installation of all the necessary bits and pieces onto
the machine sitting in front of you
The key components to install are
1. Python and its scientific libraries
2. A decent text editor, for editing Python scripts
3. The Python files for this course

For now we’ll assume you are running Windows or Mac OS — there’s a brief discussion of Linux
below
Python Distributions For Windows and Mac OS, the best thing to do is to install one of the
distributions that contains Python and the scientific librarires
The three best known are
• Anaconda
• Canopy
• Python(x,y) (Windows only)
All are free, or have free versions
Opinions differ as to which distribution is the best
Lately we hear good things about Anaconda, and Anaconda works on all platforms, so we’re
going to recommend it as the one you should install
Note: Some Windows users have reported finding Python(x,y) to be more straightforward and
more stable. If you are using Windows and have problems with Anaconda, please try uninstalling
Anaconda and installing Python(x,y).
You might like to start the download process now, making sure you choose the right version for
your operating system
For Anaconda, note:
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.2. SETTING UP YOUR PYTHON ENVIRONMENT 24
• If you already have some other Python distro installed, you might be better off uninstalling
before you start
• If you are asked during installation process whether you’d like to make Anaconda your
default Python installation, go ahead and say ‘yes’
• Otherwise, you can accept all of the defaults
Text Editors The next thing you’ll need to install is a decent text editor
A text editor is the application you will use to write and edit Python programs
Perhaps you already have a favorite text editor that knows how to interact with Python
One that we recommend is Sublime Text, a popular and highly regarded text editor with a rela-
tively moderate learning curve

Sublime Text is not free, but it does have an unlimited trial period, so you can take your time and
see if you like it
However, there are many others, and a lot of them are free — you can find out more by googling
for Python text editors
If you want a top quality free editor and don’t mind a sharper learning curve, try Emacs
If you want an outstanding free text editor and don’t mind a seemingly vertical learning curve
plus long days of pain and suffering while all your neural pathways are rewired, try Vim
Git Another piece of software we suggest you install is Git
Git is a tool for managing collections of files, typically software libraries
Very often, these libaries of code — called repositories — are stored on GitHub, a free hosting
service
Our repository is no exception — you can find it here
There are two ways to download a repository from GitHub — either using Git or just downloading
the zip file
If you’re happy with the latter, then you can go ahead — you’ll see the ‘Download ZIP’ button on
the right-hand side of the main page
(Make sure you remember where you unzip the directory, and make it somewhere you can easily
navigate to)
However, learning about Git is an excellent investment, and we recommend that you go ahead
and download it
Note: We are pointing you to the plain vanilla command line version of Git, since we find it best
ourselves. If you use Windows and have trouble with the instructions below, one option is to use
the fancier GUI version you can find here.
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014
2.2. SETTING UP YOUR PYTHON ENVIRONMENT 25
Obtaining the Main Repository As discussed above, if you definitely don’t want to learn Git,
then you can just download the zip file containing the main repository, and open it in a sensible
place
(In all of what follows, the term ‘main repository’ always refers to our GitHub repository)
Alternatively, assuming that you’ve installed Git, you can do the following

Open up a terminal (MacOS) or Powershell (Windows)
Here’s a picture of the Powershell — you can click on it to enlarge
If you look closely, you’ll see that we’ve typed two commands at the prompt
The first was — in fact we just typed and hit the Tab key, and Powershell
guessed the rest ( is change directory)
The purpose of this command was to take us to a reasonable location to put the main repository
The next command we typed was
This looks complicated, but it’s just in front of the URL for our main repository
Note: Did you get an error message? Are you using Windows? It might be that Powershell can’t
find Git. If so, please follow these instructions. (Thanks to Tom Ward for the link.)
In response to this command we see some output, and if we now list the current directory (type
) we see a new directory called
Now let’s enter that directory, via (or and then Tab), and then the subdi-
rectory , via ( and then Tab)
Here’s the picture so far (click to enlarge)
Now if you type for list, you should see a whole lot of Python files — it means you’re done
installing the repository
THOMAS SARGENT AND JOHN STACHURSKI February 14, 2014

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×