100% found this document useful (4 votes)
156 views

Instant download Introduction to Python for Econometrics, Statistics and Data Analysis Kevin Sheppard pdf all chapter

Python

Uploaded by

hudriista
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
156 views

Instant download Introduction to Python for Econometrics, Statistics and Data Analysis Kevin Sheppard pdf all chapter

Python

Uploaded by

hudriista
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Download Full Version ebookmass - Visit ebookmass.

com

Introduction to Python for Econometrics,


Statistics and Data Analysis Kevin Sheppard

https://ebookmass.com/product/introduction-to-python-for-
econometrics-statistics-and-data-analysis-kevin-sheppard/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmass.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Introduction to Python for Econometrics, Statistics and


Data Analysis. 5th Edition Kevin Sheppard.

https://ebookmass.com/product/introduction-to-python-for-econometrics-
statistics-and-data-analysis-5th-edition-kevin-sheppard/

ebookmass.com

Introduction to Statistics and Data Analysis 6th Edition


Roxy Peck

https://ebookmass.com/product/introduction-to-statistics-and-data-
analysis-6th-edition-roxy-peck/

ebookmass.com

Statistics and Data Analysis for Nursing Research (2nd


Edition ) 2nd Edition

https://ebookmass.com/product/statistics-and-data-analysis-for-
nursing-research-2nd-edition-2nd-edition/

ebookmass.com

The Virtues in Psychiatric Practice John R. Peteet

https://ebookmass.com/product/the-virtues-in-psychiatric-practice-
john-r-peteet/

ebookmass.com
Sustainable Resource Management: Modern Approaches and
Contexts 1st Edition Chaudhery Mustansar Hussain

https://ebookmass.com/product/sustainable-resource-management-modern-
approaches-and-contexts-1st-edition-chaudhery-mustansar-hussain/

ebookmass.com

Excel VBA Programming For Dummies 6th Edition Dick


Kusleika

https://ebookmass.com/product/excel-vba-programming-for-dummies-6th-
edition-dick-kusleika/

ebookmass.com

Study Guide for Foundations and Adult Health Nursing 8th


Edition

https://ebookmass.com/product/study-guide-for-foundations-and-adult-
health-nursing-8th-edition/

ebookmass.com

Lippincott Illustrated Reviews: Biochemistry (Lippincott


Illustrated Reviews Series) 7th Edition, (Ebook PDF)

https://ebookmass.com/product/lippincott-illustrated-reviews-
biochemistry-lippincott-illustrated-reviews-series-7th-edition-ebook-
pdf/
ebookmass.com

Philosophy of Life: German Lebensphilosophie 1870-1920


Prof Frederick C. Beiser

https://ebookmass.com/product/philosophy-of-life-german-
lebensphilosophie-1870-1920-prof-frederick-c-beiser/

ebookmass.com
Peace through Self-Determination. Success and Failure of
Territorial Autonomy Felix Schulte

https://ebookmass.com/product/peace-through-self-determination-
success-and-failure-of-territorial-autonomy-felix-schulte/

ebookmass.com
Introduction to Python for
Econometrics, Statistics and Data Analysis
3rd Edition, 1st Revision

Kevin Sheppard
University of Oxford

Monday 9th September, 2019


2

©2019 Kevin Sheppard


Changes since the Third Edition

• Verified that all code and examples work correctly against 2019 versions of modules. The notable
packages and their versions are:

– Python 3.7 (Preferred version)


– NumPy: 1.16
– SciPy: 1.3
– pandas: 0.25
– matplotlib: 3.1

• Python 2.7 support has been officially dropped, although most examples continue to work with 2.7.
Do not Python 2.7 in 2019 for numerical code.

• Small typo fixes, thanks to Marton Huebler.

• Fixed direct download of FRED data due to API changes, thanks to Jesper Termansen.

• Thanks for Bill Tubbs for a detailed read and multiple typo reports.

• Updated to changes in line profiler (see Ch. 24)

• Updated deprecations in pandas.

• Removed hold from plotting chapter since this is no longer required.

• Thanks for Gen Li for multiple typo reports.

• Tested all code on Pyton 3.6. Code has been tested against the current set of modules installed by
conda as of February 2018. The notable packages and their versions are:

– NumPy: 1.13
– Pandas: 0.22
ii
Notes to the 3rd Edition

This edition includes the following changes from the second edition (August 2014):

• Rewritten installation section focused exclusively on using Continuum’s Anaconda.

• Python 3.5 is the default version of Python instead of 2.7. Python 3.5 (or newer) is well supported by
the Python packages required to analyze data and perform statistical analysis, and bring some new
useful features, such as a new operator for matrix multiplication (@).

• Removed distinction between integers and longs in built-in data types chapter. This distinction is
only relevant for Python 2.7.

• dot has been removed from most examples and replaced with @ to produce more readable code.

• Split Cython and Numba into separate chapters to highlight the improved capabilities of Numba.

• Verified all code working on current versions of core libraries using Python 3.5.

• pandas

– Updated syntax of pandas functions such as resample.


– Added pandas Categorical.
– Expanded coverage of pandas groupby.
– Expanded coverage of date and time data types and functions.

• New chapter introducing statsmodels, a package that facilitates statistical analysis of data. statsmod-
els includes regression analysis, Generalized Linear Models (GLM) and time-series analysis using
ARIMA models.
iv
Changes since the Second Edition

• Fixed typos reported by a reader – thanks to Ilya Sorvachev

• Code verified against Anaconda 2.0.1.

• Added diagnostic tools and a simple method to use external code in the Cython section.

• Updated the Numba section to reflect recent changes.

• Fixed some typos in the chapter on Performance and Optimization.

• Added examples of joblib and IPython’s cluster to the chapter on running code in parallel.

• New chapter introducing object-oriented programming as a method to provide structure and orga-
nization to related code.

• Added seaborn to the recommended package list, and have included it be default in the graphics
chapter.

• Based on experience teaching Python to economics students, the recommended installation has
been simplified by removing the suggestion to use virtual environment. The discussion of virtual
environments as been moved to the appendix.

• Rewrote parts of the pandas chapter.

• Changed the Anaconda install to use both create and install, which shows how to install additional
packages.

• Fixed some missing packages in the direct install.

• Changed the configuration of IPython to reflect best practices.

• Added subsection covering IPython profiles.

• Small section about Spyder as a good starting IDE.


vi
Notes to the 2nd Edition

This edition includes the following changes from the first edition (March 2012):

• The preferred installation method is now Continuum Analytics’ Anaconda. Anaconda is a complete
scientific stack and is available for all major platforms.

• New chapter on pandas. pandas provides a simple but powerful tool to manage data and perform
preliminary analysis. It also greatly simplifies importing and exporting data.

• New chapter on advanced selection of elements from an array.

• Numba provides just-in-time compilation for numeric Python code which often produces large per-
formance gains when pure NumPy solutions are not available (e.g. looping code).

• Dictionary, set and tuple comprehensions

• Numerous typos

• All code has been verified working against Anaconda 1.7.0.


viii
Contents

1 Introduction 1
1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Important Components of the Python Scientific Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.A Additional Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 Python 2.7 vs. 3 (and the rest) 19


2.1 Python 2.7 vs. 3.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Intel Math Kernel Library and AMD’s GPUOpen Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 Other Variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.A Relevant Differences between Python 2.7 and 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3 Built-in Data Types 23


3.1 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2 Core Native Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 Additional Container Data Types in the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4 Python and Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

4 Arrays and Matrices 39


4.1 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.2 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3 1-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.4 2-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.5 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.6 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.7 Accessing Elements of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.8 Slicing and Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.9 import and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
x CONTENTS

4.10 Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52


4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

5 Basic Math 57
5.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.3 Addition (+) and Subtraction (-) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.4 Multiplication (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.5 Matrix Multiplication (@) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.6 Array and Matrix Division (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.7 Exponentiation (**) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.8 Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.9 Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.10 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

6 Basic Functions and Numerical Indexing 63


6.1 Generating Arrays and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.2 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.3 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.4 Complex Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.5 Set Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.6 Sorting and Extreme Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.7 Nan Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.8 Functions and Methods/Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

7 Special Arrays 77
7.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

8 Array and Matrix Functions 79


8.1 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
8.2 Shape Information and Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
8.3 Linear Algebra Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

9 Importing and Exporting Data 93


9.1 Importing Data using pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.2 Importing Data without pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.3 Saving or Exporting Data using pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
9.4 Saving or Exporting Data without pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
9.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
CONTENTS xi

10 Inf, NaN and Numeric Limits 103


10.1 inf and NaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.2 Floating point precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

11 Logical Operators and Find 107


11.1 >, >=, <, <=, ==, != . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
11.2 and, or, not and xor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
11.3 Multiple tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
11.4 is* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
11.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

12 Advanced Selection and Assignment 113


12.1 Numerical Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
12.2 Logical Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
12.3 Performance Considerations and Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
12.4 Assignment with Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

13 Flow Control, Loops and Exception Handling 127


13.1 Whitespace and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
13.2 if . . . elif . . . else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
13.3 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
13.4 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
13.5 try . . . except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
13.6 List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
13.7 Tuple, Dictionary and Set Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
13.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

14 Dates and Times 137


14.1 Creating Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
14.2 Dates Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
14.3 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

15 Graphics 141
15.1 seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
15.2 2D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
15.3 Advanced 2D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
15.4 3D Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
15.5 General Plotting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
15.6 Exporting Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
15.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
xii CONTENTS

16 pandas 161
16.1 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
16.2 Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
16.3 Time-series Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
16.4 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
16.5 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
16.6 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

17 Structured Arrays 203


17.1 Mixed Arrays with Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
17.2 Record Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

18 Custom Function and Modules 207


18.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
18.2 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
18.3 Example: Least Squares with Newey-West Covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
18.4 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
18.5 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
18.6 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
18.7 PYTHONPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
18.8 Python Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
18.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
18.A Listing of econometrics.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

19 Probability and Statistics Functions 225


19.1 Simulating Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
19.2 Simulation and Random Number Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
19.3 Statistics Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
19.4 Continuous Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
19.5 Select Statistics Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
19.6 Select Statistical Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
19.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

20 Statistical Analysis with statsmodels 243


20.1 Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

21 Non-linear Function Optimization 247


21.1 Unconstrained Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
21.2 Derivative-free Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
21.3 Constrained Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
21.4 Scalar Function Minimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
21.5 Nonlinear Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
CONTENTS xiii

21.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

22 String Manipulation 259


22.1 String Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
22.2 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
22.3 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
22.4 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
22.5 Safe Conversion of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

23 File System Operations 271


23.1 Changing the Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
23.2 Creating and Deleting Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
23.3 Listing the Contents of a Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
23.4 Copying, Moving and Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
23.5 Executing Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
23.6 Creating and Opening Archives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
23.7 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
23.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

24 Performance and Code Optimization 277


24.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
24.2 Timing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
24.3 Vectorize to Avoid Unnecessary Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
24.4 Alter the loop dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
24.5 Utilize Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
24.6 Use In-place Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
24.7 Avoid Allocating Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
24.8 Inline Frequent Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
24.9 Consider Data Locality in Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
24.10Profile Long Running Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
24.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

25 Improving Performance using Numba 287


25.1 Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
25.2 Supported Python Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
25.3 Supported NumPy Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
25.4 Diagnosing Performance Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
25.5 Replacing Python function with C functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
25.6 Other Features of Numba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
25.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
xiv CONTENTS

26 Improving Performance using Cython 303


26.1 Diagnosing Performance Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
26.2 Interfacing with External Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
26.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

27 Executing Code in Parallel 319


27.1 map and related functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
27.2 multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
27.3 joblib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
27.4 IPython’s Parallel Cluster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
27.5 Converting a Serial Program to Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
27.6 Other Concerns when executing in Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

28 Object-Oriented Programming (OOP) 335


28.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
28.2 Class basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
28.3 Building a class for Autoregressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
28.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

29 Other Interesting Python Packages 347


29.1 scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.2 mlpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.3 NLTK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.4 pymc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.5 pystan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.6 pytz and babel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
29.7 rpy2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
29.8 PyTables and h5py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
29.9 Theano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

30 Examples 349
30.1 Estimating the Parameters of a GARCH Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
30.2 Estimating the Risk Premia using Fama-MacBeth Regressions . . . . . . . . . . . . . . . . . . . . . . . . . 354
30.3 Estimating the Risk Premia using GMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
30.4 Outputting LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

31 Quick Reference 363


31.1 Built-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
31.2 NumPy (numpy) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
31.3 SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
31.4 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
31.5 pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
CONTENTS xv

31.6 IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395


xvi CONTENTS
Chapter 1

Introduction

1.1 Background

These notes are designed for someone new to statistical computing wishing to develop a set of skills nec-
essary to perform original research using Python. They should also be useful for students, researchers or
practitioners who require a versatile platform for econometrics, statistics or general numerical analysis
(e.g. numeric solutions to economic models or model simulation).
Python is a popular general–purpose programming language that is well suited to a wide range of prob-
lems.1 Recent developments have extended Python’s range of applicability to econometrics, statistics, and
general numerical analysis. Python – with the right set of add-ons – is comparable to domain-specific
languages such as R, MATLAB or Julia. If you are wondering whether you should bother with Python (or
another language), an incomplete list of considerations includes:
You might want to consider R if:

• You want to apply statistical methods. The statistics library of R is second to none, and R is clearly
at the forefront of new statistical algorithm development – meaning you are most likely to find that
new(ish) procedure in R.

• Performance is of secondary importance.

• Free is important.

You might want to consider MATLAB if:

• Commercial support and a clear channel to report issues is important.

• Documentation and organization of modules are more important than the breadth of algorithms
available.

• Performance is an important concern. MATLAB has optimizations, such as Just-in-Time (JIT) com-
pilation of loops, which is not automatically available in most other packages.

You might want to consider Julia if:


1
According to the ranking on http://www.tiobe.com/tiobe-index/, Python is the 5th most popular language. http://
langpop.corger.nl/ ranks Python as 4th or 5th .
2 Introduction

• Performance in an interactive based language is your most important concern.

• You don’t mind learning enough Python to interface with Python packages. The Julia ecosystem is
in its infancy and a bridge to Python is used to provide important missing features.

• You like living on the bleeding edge and aren’t worried about code breaking across new versions of
Julia.

• You like to do most things yourself.

Having read the reasons to choose another package, you may wonder why you should consider Python.

• You need a language which can act as an end-to-end solution that allows access to web-based ser-
vices, database servers, data management and processing and statistical computation. Python can
even be used to write server-side apps such as a dynamic website (see e.g. http://stackoverflow.
com), apps for desktop-class operating systems with graphical user interfaces, or apps for tablets and
phones apps (iOS and Android).

• Data handling and manipulation – especially cleaning and reformatting – is an important concern.
Python is substantially more capable at data set construction than either R or MATLAB.

• Performance is a concern, but not at the top of the list.2

• Free is an important consideration – Python can be freely deployed, even to 100s of servers in on a
cloud-based cluster (e.g. Amazon Web Services, Google Compute or Azure).

• Knowledge of Python, as a general purpose language, is complementary to R/MATLAB/Julia/Ox/-


GAUSS/Stata.

1.2 Conventions

These notes will follow two conventions.

1. Code blocks will be used throughout.


"""A docstring
"""

# Comments appear in a different color

# Reserved keywords are highlighted


and as assert break class continue def del elif else
except exec finally for from global if import in is
lambda not or pass print raise return try while with yield

# Common functions and classes are highlighted in a


# different color. Note that these are not reserved,

2
Python performance can be made arbitrarily close to C using a variety of methods, including Numba (pure python), Cython
(C/Python creole language) or directly calling C code. Moreover, recent advances have substantially closed the gap with respect
to other Just-in-Time compiled languages such as MATLAB.
1.3 Important Components of the Python Scientific Stack 3

# and can be used although best practice would be


# to avoid them if possible
array matrix range list True False None

# Long lines are indented


some_text = 'This is a very, very, very, very, very, very, very, very, very, very, very, very
long line.'

2. When a code block contains >>>, this indicates that the command is running an interactive IPython
session. Output will often appear after the console command, and will not be preceded by a com-
mand indicator.
>>> x = 1.0
>>> x + 2
3.0

If the code block does not contain the console session indicator, the code contained in the block is
intended to be executed in a standalone Python file.
import numpy as np

x = np.array([1,2,3,4])
y = np.sum(x)
print(x)
print(y)

1.3 Important Components of the Python Scientific Stack

1.3.1 Python

Python 3.5 (or later) is required. This provides the core Python interpreter. Most of the examples should
work with the latest version of Python 2.7 as well.

1.3.2 NumPy

NumPy provides a set of array and matrix data types which are essential for statistics, econometrics and
data analysis.

1.3.3 SciPy

SciPy contains a large number of routines needed for analysis of data. The most important include a wide
range of random number generators, linear algebra routines, and optimizers. SciPy depends on NumPy.

1.3.4 Jupyter and IPython

IPython provides an interactive Python environment which enhances productivity when developing code
or performing interactive data analysis. Jupyter provides a generic set of infrastructure that enables IPython
to be run in a variety of settings including an improved console (QtConsole) or in an interactive web-
browser based notebook.
4 Introduction

1.3.5 matplotlib and seaborn

matplotlib provides a plotting environment for 2D plots, with limited support for 3D plotting. seaborn is
a Python package that improves the default appearance of matplotlib plots without any additional code.

1.3.6 pandas

pandas provides high-performance data structures.

1.3.7 statsmodels

statsmodels is pandas-aware and provides models used in the statistical analysis of data including linear
regression, Generalized Linear Models (GLMs), and time-series models (e.g., ARIMA).

1.3.8 Performance Modules

A number of modules are available to help with performance. These include Cython and Numba. Cython
is a Python module which facilitates using a simple Python-derived creole to write functions that can be
compiled to native (C code) Python extensions. Numba uses a method of just-in-time compilation to
translate a subset of Python to native code using Low-Level Virtual Machine (LLVM).

1.4 Setup

The recommended method to install the Python scientific stack is to use Continuum Analytics’ Anaconda.
Appendix 1.A.3 describes a more complex installation procedure with instructions for directly installing
Python and the required modules when it is not possible to install Anaconda.

Continuum Analytics’ Anaconda

Anaconda, a free product of Continuum Analytics (www.continuum.io), is a virtually complete scientific


stack for Python. It includes both the core Python interpreter and standard libraries as well as most mod-
ules required for data analysis. Anaconda is free to use and modules for accelerating the performance of
linear algebra on Intel processors using the Math Kernel Library (MKL) are provided. Continuum Analyt-
ics also provides other high-performance modules for reading large data files or using the GPU to further
accelerate performance for an additional, modest charge. Most importantly, installation is extraordinarily
easy on Windows, Linux, and OS X. Anaconda is also simple to update to the latest version using

conda update conda


conda update anaconda

Windows

Installation on Windows requires downloading the installer and running. Anaconda comes in both Python
2.7 and 3.x flavors, and the latest Python 3.x is the preferred choice. These instructions use ANACONDA
to indicate the Anaconda installation directory (e.g. the default is C:\Anaconda). Once the setup has
completed, open a command prompt (cmd.exe) and run
1.4 Setup 5

cd ANACONDA\Scripts
conda update conda
conda update anaconda
conda install html5lib seaborn

which will first ensure that Anaconda is up-to-date. conda install can be used later to install other pack-
ages that may be of interest. Note that if Anaconda is installed into a directory other than the default, the
full path should not contain Unicode characters or spaces.

Notes

The recommended settings for installing Anaconda on Windows are:

• Install for all users, which requires admin privileges. If these are not available, then choose the “Just
for me” option, but be aware of installing on a path that contains non-ASCII characters which can
cause issues.

• Add Anaconda to the System PATH - This is important to ensure that Anaconda commands can be
run from the command prompt.

• Register Anaconda as the system Python unless you have a specific reason not to (unlikely).

If Anaconda is not added to the system path, it is necessary to add the ANACONDA and ANACONDA\Scripts
directories to the PATH using
set PATH=ANACONDA;ANACONDA\Scripts;%PATH%

before running Python programs.

Linux and OS X

Installation on Linux requires executing


bash Anaconda3-x.y.z-Linux-ISA.sh

where x.y.z will depend on the version being installed and ISA will be either x86 or more likely x86_64.
Anaconda comes in both Python 2.7 and 3.x flavors, and the latest Python 3.x is the preferred choice. The
OS X installer is available either in a GUI installed (pkg format) or as a bash installer which is installed
in an identical manner to the Linux installation. It is strongly recommended that the anaconda/bin is
prepended to the path. This can be performed in a session-by-session basis by entering
export PATH=ANACONDA/bin;$PATH

On Linux this change can be made permanent by entering this line in .bashrc which is a hidden file located
in ~/. On OS X, this line can be added to .bash_profile which is located in the home directory (~/).3
After installation completes, execute
conda update conda
conda update anaconda
conda install html5lib seaborn

3
Use the appropriate settings file if using a different shell (e.g. .zshrc for zsh).
6 Introduction

which will first ensure that Anaconda is up-to-date and then to install the Intel Math Kernel library-linked
modules, which provide substantial performance improvements – this package requires a license which
is free to academic users and low cost to others. If acquiring a license is not possible, omit this line.
conda install can be used later to install other packages that may be of interest.

Notes

All instructions for OS X and Linux assume that ANACONDA/bin has been added to the path. If this is not
the case, it is necessary to run

cd ANACONDA
cd bin

and then all commands must be prepended by a . as in

./conda update conda

1.5 Using Python

Python can be programmed using an interactive session using IPython or by directly executing Python
scripts – text files that end with the extension .py – using the Python interpreter.

1.5.1 Python and IPython

Most of this introduction focuses on interactive programming, which has some distinct advantages when
learning a language. The standard Python interactive console is very basic and does not support useful
features such as tab completion. IPython, and especially the QtConsole version of IPython, transforms
the console into a highly productive environment which supports a number of useful features:

• Tab completion - After entering 1 or more characters, pressing the tab button will bring up a list of
functions, packages, and variables which match the typed text. If the list of matches is large, pressing
tab again allows the arrow keys can be used to browse and select a completion.

• “Magic” function which make tasks such as navigating the local file system (using %cd ~/directory/
or just cd ~/directory/ assuming that %automagic is on) or running other Python programs (using run
program.py) simple. Entering %magic inside and IPython session will produce a detailed description
of the available functions. Alternatively, %lsmagic produces a succinct list of available magic com-
mands. The most useful magic functions are

– cd - change directory

– edit filename - launch an editor to edit filename


– ls or ls pattern - list the contents of a directory

– run filename - run the Python file filename

– timeit - time the execution of a piece of code or function


1.5 Using Python 7

• Integrated help - When using the QtConsole, calling a function provides a view of the top of the help
function. For example, entering mean( will produce a view of the top 20 lines of its help text.

• Inline figures - Both the QtConsole and the notebook can also display figure inline which produces a
tidy, self-contained environment. This can be enabled by entering %matplotlib inline in an IPython
session.

• The special variable _ contains the last result in the console, and so the most recent result can be
saved to a new variable using the syntax x = _.

• Support for profiles, which provide further customization of sessions.

1.5.2 Launching IPython

OS X and Linux

IPython can be started by running


ipython

in the terminal. IPython using the QtConsole can be started using


jupyter qtconsole

A single line launcher on OS X or Linux can be constructed using


bash -c "jupyter qtconsole"

This single line launcher can be saved as filename.command where filename is a meaningful name (e.g.
IPython-Terminal) to create a launcher on OS X by entering the command
chmod 755 /FULL/PATH/TO/filename.command

The same command can to create a Desktop launcher on Ubuntu by running


sudo apt-get install --no-install-recommends gnome-panel
gnome-desktop-item-edit ~/Desktop/ --create-new

and then using the command as the Command in the dialog that appears.

Windows (Anaconda)

To run IPython open cmd and enter IPython in the start menu. Starting IPython using the QtConsole is
similar and is simply called QtConsole in the start menu. Launching IPython from the start menu should
create a window similar to that in figure 1.1.
Next, run

jupyter qtconsole --generate-config

in the terminal or command prompt to generate a file named jupyter_qtconsole_config.py. This file contains
settings that are useful for customizing the QtConsole window. A few recommended modifications are
8 Introduction

Figure 1.1: IPython running in the standard Windows console (cmd.exe).


c.IPythonWidget.font_size = 11
c.IPythonWidget.font_family = "Bitstream Vera Sans Mono"
c.JupyterWidget.syntax_style = 'monokai'

These commands assume that the Bitstream Vera fonts have been locally installed, which are available
from http://ftp.gnome.org/pub/GNOME/sources/ttf-bitstream-vera/1.10/. Opening QtConsole should
create a window similar to that in figure 1.2 (although the appearance might differ) if you did not use the
recommendation configuration.

1.5.3 Getting Help

Help is available in IPython sessions using help(function). Some functions (and modules) have very long
help files. When using IPython, these can be paged using the command ?function or function? so that the
text can be scrolled using page up and down and q to quit. ??function or function?? can be used to type
the entire function including both the docstring and the code.

1.5.4 Running Python programs

While interactive programming is useful for learning a language or quickly developing some simple code,
complex projects require the use of complete programs. Programs can be run either using the IPython
magic work %run program.py or by directly launching the Python program using the standard interpreter
using python program.py. The advantage of using the IPython environment is that the variables used in
the program can be inspected after the program run has completed. Directly calling Python will run the
program and then terminate, and so it is necessary to output any important results to a file so that they
can be viewed later.4
4
Programs can also be run in the standard Python interpreter using the command:
exec(compile(open(’filename.py’).read(),’filename.py’,’exec’))
1.5 Using Python 9

Figure 1.2: IPython running in a QtConsole session.


To test that you can successfully execute a Python program, input the code in the block below into a
text file and save it as firstprogram.py.
# First Python program
import time

print('Welcome to your first Python program.')


input('Press enter to exit the program.')
print('Bye!')
time.sleep(2)

Once you have saved this file, open the console, navigate to the directory you saved the file and enter
python firstprogram.py. Finally, run the program in IPython by first launching IPython, and the using
%cd to change to the location of the program, and finally executing the program using %run firstprogram.py.

1.5.5 %pylab and %matplotlib

When writing Python code, only a small set of core functions and variable types are available in the in-
terpreter. The standard method to access additional variable types or functions is to use imports, which
explicitly allow access to specific packages or functions. While it is best practice to only import required
functions or packages, there are many functions in multiple packages that are commonly encountered
in these notes. Pylab is a collection of common NumPy, SciPy and Matplotlib functions that can be eas-
ily imported using a single command in an IPython session, %pylab. This is nearly equivalent to calling
from pylab import *, since it also sets the backend that is used to draw plots. The backend can be manu-
ally set using %pylab backend where backend is one of the available backends (e.g., qt5 or inline). Similarly
%matplotlib backend can be used to set just the backend without importing all of the modules and func-
tions come with %pylab .
Most chapters assume that %pylab has been called so that functions provided by NumPy can be called
10 Introduction

Figure 1.3: A successful test that matplotlib, IPython, NumPy and SciPy were all correctly installed.
without explicitly importing them.

1.5.6 Testing the Environment

To make sure that you have successfully installed the required components, run IPython using shortcut
or by running ipython or jupyter qtconsole run in a terminal window. Enter the following commands,
one at a time (the meaning of the commands will be covered later in these notes).
>>> %pylab qt5
>>> x = randn(100,100)
>>> y = mean(x,0)
>>> import seaborn
>>> plot(y)
>>> import scipy as sp

If everything was successfully installed, you should see something similar to figure 1.3.

1.5.7 jupyter notebook

A jupyter notebook is a simple and useful method to share code with others. Notebooks allow for a fluid
synthesis of formatted text, typeset mathematics (using LATEX via MathJax) and Python. The primary method
for using notebooks is through a web interface, which allows creation, deletion, export and interactive
editing of notebooks.
1.5 Using Python 11

Figure 1.4: The default IPython Notebook screen showing two notebooks.
To launch the jupyter notebook server, open a command prompt or terminal and enter

jupyter notebook

This command will start the server and open the default browser which should be a modern version of
Chrome (preferable), Chromium or Firefox. If the default browser is Safari, Internet Explorer or Edge, the
URL can be copied and pasted into Chrome. The first screen that appears will look similar to figure 1.4,
except that the list of notebooks will be empty. Clicking on New Notebook will create a new notebook,
which, after a bit of typing, can be transformed to resemble figure 1.5. Notebooks can be imported by
dragging and dropping and exported from the menu inside a notebook.

1.5.8 Integrated Development Environments

As you progress in Python and begin writing more sophisticated programs, you will find that using an In-
tegrated Development Environment (IDE) will increase your productivity. Most contain productivity en-
hancements such as built-in consoles, code completion (or IntelliSense, for completing function names)
and integrated debugging. Discussion of IDEs is beyond the scope of these notes, although Spyder is a
reasonable choice (free, cross-platform). Aptana Studio is another free alternative. My preferred IDE is
PyCharm, which has a community edition that is free for use (the professional edition is low cost for aca-
demics).
12 Introduction

Figure 1.5: An IPython notebook showing formatted markdown, LATEX math and cells containing code.
Spyder

Spyder is an IDE specialized for use in scientific applications of Python rather than for general purpose
application development. This is both an advantage and a disadvantage when compared to a full featured
IDE such as PyCharm, Python Tools for Visual Studio (PVTS), PyDev or Aptana Studio. The main advantage
is that many powerful but complex features are not integrated into Spyder, and so the learning curve is
much shallower. The disadvantage is similar - in more complex projects, or if developing something that is
not straight scientific Python, Spyder is less capable. However, netting these two, Spyder is almost certainly
the IDE to use when starting Python, and it is always relatively simple to migrate to a sophisticated IDE if
needed.
Spyder is started by entering spyder in the terminal or command prompt. A window similar to that in
figure 1.6 should appear. The main components are the editor (1), the object inspector (2), which dynam-
ically will show help for functions that are used in the editor, and the console (3). By default, Spyder opens
a standard Python console, although it also supports using the more powerful IPython console. The object
inspector window, by default, is grouped with a variable explorer, which shows the variables that are in
memory and the file explorer, which can be used to navigate the file system. The console is grouped with
an IPython console window (needs to be activated first using the Interpreters menu along the top edge),
and the history log which contains a list of commands executed. The buttons along the top edge facilitate
saving code, running code and debugging.

1.6 Exercises

1. Install Python.

2. Test the installation using the code in section 1.5.6.

3. Configure IPython using the start-up script in section ??.


1.A Additional Installation Issues 13

Figure 1.6: The default Spyder IDE on Windows.


4. Customize IPython QtConsole using a font or color scheme. More customizations can be found by
running ipython -h.

5. Explore tab completion in IPython by entering a<TAB> to see the list of functions which start with
a and are loaded by pylab. Next try i<TAB>, which will produce a list longer than the screen – press
ESC to exit the pager.

6. Launch IPython Notebook and run code in the testing section.

7. Open Spyder and explore its features.

1.A Additional Installation Issues

1.A.1 Frequently Encountered Problems

All

Whitespace sensitivity

Python is whitespace sensitive and so indentation, either spaces or tabs, affects how Python interprets
files. The configuration files, e.g. ipython_config.py, are plain Python files and so are sensitive to whitespace.
Introducing white space before the start of a configuration option will produce an error, so ensure there
is no whitespace before active lines of a configuration.
14 Introduction

Windows

Spaces in path

Python may work when directories have spaces.

Unicode in path

Python does not always work well when a path contains Unicode characters, which might occur in a user
name. While this isn’t an issue for installing Python or Anaconda, it is an issue for IPython which looks in
c:\user\username\.ipython for configuration files. The solution is to define the HOME variable before launch-
ing IPython to a path that has only ASCII characters.

mkdir c:\anaconda\ipython_config
set HOME=c:\anaconda\ipython_config
c:\Anaconda\Scripts\activate econometrics
ipython profile create econometrics
ipython --profile=econometrics

The set HOME=c:\anaconda\ipython_config can point to any path with directories containing only ASCII
characters, and can also be added to any batch file to achieve the same effect.

OS X

Installing Anaconda to the root of the partition

If the user account used is running as root, then Anaconda may install to /anaconda and not ~/anaconda by
default. Best practice is not to run as root, although in principle this is not a problem, and /anaconda can
be used in place of ~/anaconda in any of the instructions.

1.A.2 register_python.py

A complete listing of register_python.py is included in this appendix.


# -*- encoding: utf-8 -*-
#
# Script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# Adapted by Ned Batchelder from a script
# written by Joakim Law for Secret Labs AB/PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm

import sys
from _winreg import *

# tweak as necessary
version = sys.version[:3]
1.A Additional Installation Issues 15

installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)


installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)

def RegisterPy():
try:
reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
except EnvironmentError:
try:
reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
except Exception, e:
print "*** Unable to register: %s" % e
return

SetValue(reg, installkey, REG_SZ, installpath)


SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
print "--- Python %s at %s is now registered!" % (version, installpath)

if __name__ == "__main__":
RegisterPy()

1.A.3 Setup using Virtual Environments

The simplest method to install the Python scientific stack is to use directly Continuum Analytics’ Ana-
conda. These instructions describe alternative installation options using virtual environments, which al-
low alternative configurations to simultaneously co-exist on a single system. The primary advantage of a
virtual environment is that it allows package versions to be frozen so that code that upgrading a module
or all of Anaconda does not upgrade the packages in a particular virtual environment.

Windows

Installation on Windows requires downloading the installer and running. These instructions use ANA-
CONDA to indicate the Anaconda installation directory (e.g. the default is C:\Anaconda). Once the setup
has completed, open a command prompt (cmd.exe) and run

cd ANACONDA
conda update conda
conda update anaconda
conda create -n econometrics qtconsole notebook matplotlib numpy pandas scipy spyder statsmodels
conda install -n econometrics cython lxml nose numba numexpr pytables sphinx xlrd xlwt html5lib
seaborn
16 Introduction

which will first ensure that Anaconda is up-to-date and then create a virtual environment named econo-
metrics. Using a virtual environment is a best practice and is important since component updates can
lead to errors in otherwise working programs due to backward incompatible changes in a module. The
long list of modules in the conda create command includes the core modules. conda install contains the
remaining packages and is shown as an example of how to add packages to an existing virtual environment
after it has been created. It is also possible to install all available Anaconda packages using the command
conda create -n econometrics anaconda.
The econometrics environment must be activated before use. This is accomplished by running
ANACONDA\Scripts\activate.bat econometrics

from the command prompt, which prepends [econometrics] to the prompt as an indication that virtual
environment is active. Activate the econometrics environment and then run
cd c:\
ipython

which will open an IPython session using the newly created virtual environment.
Virtual environments can also be created using specific versions of packages using pinning. For ex-
ample, to create a virtual environment naed python2 using Python 2.7 and NumPy 1.10,

conda create -n python2 python=2.7 numpy=1.10 scipy pandas

which will install the requested versions of Python and NumPy as well as the latest version of SciPy and
pandas that are compatible with the pinned versions.

Linux and OS X

Installation on Linux requires executing


bash Anaconda3-x.y.z-Linux-ISA.sh

where x.y.z will depend on the version being installed and ISA will be either x86 or more likely x86_64.
The OS X installer is available either in a GUI installed (pkg format) or as a bash installer which is installed
in an identical manner to the Linux installation. After installation completes, change to the folder where
Anaconda installed (written here as ANACONDA, default ~/anaconda) and execute
cd ANACONDA
cd bin
./conda update conda
./conda update anaconda
./conda create -n econometrics qtconsole notebook matplotlib numpy pandas scipy spyder statsmodels
./conda install -n econometrics cython lxml nose numba numexpr pytables sphinx xlrd xlwt html5lib
seaborn

which will first ensure that Anaconda is up-to-date and then create a virtual environment named econo-
metrics with the required packages. conda create creates the environment and conda install installs ad-
ditional packages to the existing environment. conda install can be used later to install other packages
that may be of interest. To activate the newly created environment, run
source ANACONDA/bin/activate econometrics
1.A Additional Installation Issues 17

and then run the command


ipython

to launch IPython using the newly created virtual environment.


18 Introduction
Chapter 2

Python 2.7 vs. 3 (and the rest)

Python comes in a number of flavors which may be suitable for econometrics, statistics and numerical
analysis. This chapter explains why 3.5, the latest release of Python 3, was chosen for these notes and
highlights some of the available alternatives.

2.1 Python 2.7 vs. 3.x

Python 2.7 is the final version of the Python 2.x line – all future development work will focus on Python 3.
The reasons for using 3.x (especially 3.5+) are:

• Virtually all modules needed to perform data analysis and econometrics are tested using Python 3.5
(in addition to other versions).

• Python 3 has introduced some nice language changes that help for numerical computing, such as
using a default division that will produce a floating point number when dividing two integers. Old
versions of Python would produce 0 when evaluating 1/2.

• A new operator has been introduced that will simplify writing matrix-intensive code (@, as in x @ y).

• Anaconda fully supports 3.5.

While it was once the case that Python 2.7 was a better choice, there are now clear reasons to prefer 3.5.

2.2 Intel Math Kernel Library and AMD’s GPUOpen Libraries

Intel’s MKL and AMD’s GPUOpen libraries provide optimized linear algebra routines. The functions in
these libraries execute faster than basic those in linear algebra libraries and are, by default, multithreaded
so that a many linear algebra operations will automatically make use all of the processors on your system.
Most standard builds of NumPy do not include these, and so it is important to use a Python distribution
built with an appropriate linear algebra library (especially if computing inverses or eigenvalues of large
matrices). The three primary methods to access NumPy built with the Intel MKL are:

• Use Anaconda on any platform which comes with MKL.

• Use the pre-built NumPy binaries made available by Christoph Gohlke for Windows.
20 Python 2.7 vs. 3 (and the rest)

• Follow instructions for building NumPy on Linux with MKL (which is free on Linux).

There are no pre-built libraries using AMD’s GPUOpen, and so it is necessary to build NumPy from scratch
if using an AMD processor (or buy an Intel system, which is an easier solution).

2.3 Other Variants

Some other variants of the recommended version of Python are worth mentioning.

2.3.1 Enthought Canopy

Enthought Canopy is an alternative to Anaconda. It is available for Windows, Linux and OS X. Canopy
is regularly updated and is currently freely available in its basic version. The full version is also freely
available to academic users. Canopy is built using MKL, and so matrix algebra performance is very fast.

2.3.2 IronPython

IronPython is a variant which runs on the Common Language Runtime (CLR , aka Windows .NET). The
core modules – NumPy and SciPy – are available for IronPython, and so it is a viable alternative for nu-
merical computing, especially if already familiar with the C# or interoperation with .NET components
is important. Other libraries, for example, matplotlib (plotting) are not available, and so there are some
important limitations.

2.3.3 Jython

Jython is a variant which runs on the Java Runtime Environment (JRE). NumPy is not available in Jython
which severely limits Jython’s usefulness for numeric work. While the limitation is important, one advan-
tage of Python over other languages is that it is possible to run (mostly unaltered) Python code on a JVM
and to call other Java libraries.

2.3.4 PyPy

PyPy is a new implementation of Python which uses Just-in-time compilation and LLVM to accelerate
code, especially loops (which are common in numerical computing). It may be anywhere between 2 - 500
times faster than standard Python. Unfortunately, at the time of writing, the core library, NumPy is only
partially implemented, and so it is clearly not ready for use. Current plans are to have a version ready in
the near future, and if so, PyPy may become the a viable choice for numerical computing.

2.A Relevant Differences between Python 2.7 and 3

Most differences between Python 2.7 and 3 are not important for using Python in econometrics, statistics
and numerical analysis. If one wishes to use Python 2.7, it is important to understand these differences.
Note that these differences are important in stand-alone Python programs.
2.A Relevant Differences between Python 2.7 and 3 21

2.A.1 print

print is a function used to display test in the console when running programs. In Python 2.7, print is a
keyword which behaves differently from other functions. In Python 3, print behaves like most functions.
The standard use in Python 2.7 is
print 'String to Print'

while in Python 3 the standard use is


print('String to Print')

which resembles calling a standard function. Python 2.7 contains a version of the Python 3 print, which
can be used in any program by including
from __future__ import print_function

at the top of the file.

2.A.2 division

Python 3 changes the way integers are divided. In Python 2.7, the ratio of two integers was always an
integer, and so results are truncated towards 0 if the result was fractional. For example, in Python 2.7, 9/5
is 1. Python 3 gracefully converts the result to a floating point number, and so in Python 3, 9/5 is 1.8. When
working with numerical data, automatically converting ratios avoids some rare errors. Python 2.7 can use
the Python 3 behavior by including
from __future__ import division

at the top of the program.

2.A.3 range and xrange

It is often useful to generate a sequence of numbers for use when iterating over data. In Python 2.7, best
practice is to use the keyword xrange to do this. In Python 3, this keyword has been renamed range.

2.A.4 Unicode strings

Unicode is an industry standard for consistently encoding text. The computer alphabet was originally
limited to 128 characters which is insufficient to contain the vast array of characters in all written lan-
guages. Unicode expands the possible space to allow for more than 1,000,000 characters. Python 3 treats
all strings as unicode unlike Python 2.7 where characters are a single byte, and unicode strings require the
special syntax u'unicode string' or unicode('unicode string'). In practice this is unlikely to impact most
numeric code written in Python except possibly when reading or writing data. If working in a language
where characters outside of the standard but limited 128 character ASCII set are commonly encountered,
it may be useful to use
from __future__ import unicode_literals

to help with future compatibility when moving to Python 3.


22 Python 2.7 vs. 3 (and the rest)
Exploring the Variety of Random
Documents with Different Content
into the revenge of the Bonny Earl’s death against
the Marquis of Huntly, and for this service they had 1624.
obtained certain lands from the Moray family. Now,
that the Earl of Moray was reconciled with Huntly, he did not see any
occasion longer to patronise or favour the MacIntoshes; so he
attempted to remove them from the lands formerly conferred upon
them. ‘This the Clan Chattan could hardly endure,’ says Sir Robert
Gordon: about Whitsuntide, assembling five hundred men under
their infant chief’s uncle, Lachlan MacIntosh [afterwards, by the by,
a stout loyalist in the Civil War], ‘they keepit the fields in their
Highland weed upon foot, with swords, bows, arrows, targets,
hagbuts, pistols, and other Highland arms, and first began to rob
and spulyie the earl’s tenants (who laboured their possessions) of
their haill goods, geir, insight plenishing [household furniture], horse,
nolt, sheep, corns, and cattle, and left them nothing that they could
get within their bounds; syne fell in sorning throughout Moray,
Stratherrick, Urquhart, Ross, Sutherland, Brae of Mar, and divers
other parts, taking their meat and food perforce where they could
not get it willingly, frae friends as well as frae their foes, yet still kept
themselves from shedding of innocent blood.’
The Earl of Moray first brought a band of Monteith Highlanders
against these marauders; but the expedition seems to have failed.
Another enterprise of the same kind was no more successful. It was
not till he went to London, and procured a power of lieutenancy in
the north from the king, that he brought the MacIntoshes to
subjection. The affair had a very characteristic ending. ‘Some slight
loons [poor fellows], followers of the Clan Chattan, were execute;
but the principal outbreakers and malefactors were spared and never
troubled.’ Further, the ‘honest men’ who had disobeyed the order for
refusing all supply to the MacIntoshes, being put to trial, the odd
scene was presented of the criminals standing as witnesses against
them; and while these culprits obtained pardon, their humane
resetters ‘were soundly fined in as great sums as their estates might
bear, and some above their estates were fined, and every one
warded within the Tolbooth of Elgin, till the last mite was paid.’—
Spal. ‘The fines were granted by his majesty to the Earl of Moray, as
the fines for resetting the Clan Gregor were given to the Earl of
Argyle; but these fines did not much advantage either of these two
earls.’—G. H. S.

Dissent from the ‘comely order’ of church matters


was still making itself apparent. We hear at this June 10.
time of many people in Edinburgh holding private
meetings for religious exercises, in contempt of the 1624.
ordinary services of their regular pastors in the
parish churches. ‘Like as they have assumed to these their seditious
conventicles the name of Congregations, and done what in them lies
falsely to impress on the hearts of his majesty’s people a persuasion
that his majesty persecutes the sincere professors of true religion,
and introduces corruption in the church-government.’ Considering
how such practices ‘brought forth damnable sects of Anabaptists,
Families of Love, Brownists, Arminians, Illuminati, and mony such
pests, enemies to religion, authority, and peace, and occasions the
murder of millions of people,’ the Privy Council thought proper to
issue a proclamation, strictly forbidding all such meetings.
The Council had at the same time before them a set of Edinburgh
citizens, partly the same as those whom the king had proposed to
banish a few years before413—namely, William Rig of Aitherny, one
of the bailies, John Hamilton, apothecary, John Mean, merchant, and
John Dickson, ‘flesher’—who had again come into collision with the
ecclesiastical authorities. At the usual congregational meeting before
the celebration of the communion, Rig—‘puffed up,’ says
Spottiswoode, ‘by a conceit of his own abilities’—took it upon him to
challenge Dr Forbes ‘for sundry points of doctrine delivered by him in
his sermons.’ Dr Forbes was a man of remarkable learning and
dignity of character, for which reasons he was in time appointed
bishop of Edinburgh by Charles I. It did not seem to him proper that
he should be liable to the censure of a lay citizen, and he therefore
declined to listen to the bailie. Rig then openly threatened the clergy,
‘that, unless they returned to the old form of administering the holy
communion, the whole people would forsake them;’ and in this he
was supported by his friends Mean, Hamilton, and Dickson. The
Council took the affair up as an attempt to produce a schism in the
church and a violation of the law. They answered, however—if we
are to believe one of their own party—‘so wisely, punctually, and
modestly, that the Council admired them.’ They were, nevertheless,
to satisfy the king, sent to various prisons, as guilty of a
misdemeanour. They ‘remained there, till by great dealing, pains,
and moyen, they were relieved again.’—Row.
William Rig and John Mean appear, from the report
of their contemporary and friend, Mr John 1624.
Livingstone, to have been earnest Christians of the
evangelical type. Rig was ‘much exercised in spirit, and of great
experience in the ways of God. I have been several times with him in
private meetings, and observed that when he prayed, he began with
bitter and heavy complaints and confession beyond any. He spent his
income chiefly on pious uses.’ Mean ‘used both summer and winter
to rise about three o’clock in the morning, and always, as he put on
his clothes, he used to sing some part of a psalm, and then went to
his closet, where he was employed in religious exercises till six. By
that time, the rest of his family being got up, he worshipped with
them, and then went to his shop. He was so much master of the
Scripture, [that] though he had been half sleeping, he could have
corrected readers if they miscalled or wrong cited ony scripture.’414
During the time when the king was pressing on the innovations in
the church, dissentients of this kind were rising everywhere
throughout the southern districts of Scotland, many of them lairds, a
few of them nobles, but most of them belonging to the middle
classes of society. Of the lairds, Livingstone enumerates Halhill
(Fife), Crosshill (Lanarkshire), Cunningham-head, Cessnock, and
Rowallan (Ayrshire). There was also a number of ladies, some of
them of noble birth, who embraced and strongly held fast the
evangelical views. Such were Margaret Countess of Wigton, Anne
Marchioness of Hamilton, the Countess of Eglintoun, and Lady
Loudon. For the time, these people, as well as the more earnest of
the clergy, were kept silent under the frown of an imperious
government, or made themselves but little heard; but the fire
burned not the less intensely for being covered up; and when the
time for resistance came, it was ready to break forth with the
greater violence that it had been so long suppressed.
Almost as a matter of course, while these
Presbyterian recusants were in hands, the state 1624. July 1.
authorities took some order with papistry. John
Gordon of Craig in Aberdeenshire had attracted their notice as ‘an
excommunicat trafficking papist,’ who, not content with blaspheming
the truth and its preachers himself, did all that he could to ‘withhold
his people from coming to the kirk, boasting [threatening] some,
and persuading others;’ thus, it is alleged, ‘he steirs up mony not
weel satled in their religion to imitate him in his contemptuous and
lawless proceedings, and in effect has cassen that pairt of the
country lowss.’ The Council now charged Gordon to appear and
answer for his offences. They likewise despatched an order to the
magistrates of Aberdeen, for the routing up of a set of Catholics who
for some time had been allowed to live peaceably there,
commanding that they be taken and warded till further orders.—P. C.
R. The government could calculate with tolerable security on the
feeling of the great bulk of the people, that by thus striking a blow
at popery, they would be allowed without much remonstrance to
deal that severity towards puritanism which would frighten it from a
troublesome opposition to the now semi-episcopalian establishment.
John Gordon of Craig was obliged for the time to leave the kingdom;
but somehow the king was always forgiving to papists, and we
accordingly find that in January 1625, having made submission and
promised good behaviour in future, this ‘excommunicat trafficking
papist’ was allowed to return to Scotland (P. C. R.), but not
ultimately to rest there, as will hereafter be seen.
A Border thief, described as Adie Usher in
Birkinhaugh, servant of Robert Elliot of Redheugh, July 21.
was condemned and hanged at Edinburgh for
sundry acts of cattle-stealing. In most of his proceedings he had
been accompanied by his son, Willie Usher, a mere boy, who was
also presented for trial, but spared on account of his youth.—Pit.
After Willie Usher had spent some months in the Thieves’ Hole in
Edinburgh, the Lords of the Privy Council received a complaint from
him, ‘heavily regretting his hard estate and condition by his
detention, thir mony owks bygane, miserably in ward in the Thieves’
Hole of Edinburgh, without possibility or mean to entertein himself,
he being a young innocent boy not past the age of fourteen years,
and his umwhile father having underlain his punishment and sufferit
death for the crime laid to the said Willie’s charge.’ The Lords
consequently ordered the magistrates ‘to attend the commodity of
some ship going to the Low Countries,’ and see Willie set aboard
thereof, ‘and mak intimation to the said Willie that if at ony time
hereafter he sall return without licence, it sall be capital unto him.’—
P. C. R.
The master of Adie Usher seems to have been
under suspicion of a concern in his delinquencies. 1624.
In November, when about to fly from the city on
account of infection, the Privy Council entered an order in the case
of Lady Jean Stewart, whose husband, Robert Elliot of Redheugh,
had been for some time a prisoner in the Tolbooth of Edinburgh. She
had represented ‘the utter distress, misery, and want whereunto she
and her poor children are reduced, having contracted great debts
and impandit her abulyiements and clothes for enterteinment of her
husband in ward—and she is brocht to that pitch of necessity, that
she has nowther means to live nor credit to afford him ony further
supply.’ The Council ordered her a hundred merks for past charges,
and granted her the sum of ‘threttein shillings and four pennies’
during pleasure—apparently meaning a daily allowance of 1s. 1-
1/3d. sterling.—P. C. R.

Poland is described as in this age swarming with


Scotch pedlers. Its port, Dantzig, contained a Aug. 30.
number of settled merchants of a respectable
order, some of whom were seen from time to time returning to their
native country with considerable realised wealth. Formerly, the
Scotch merchants at Dantzig, having a kind of rule and governance
among themselves, lived in such a way as to secure the esteem of
the people of the country. But latterly, ‘discipline being dissolved, the
most part of them use such a dissolute form of living, that they are
odious to the inhabitants, hurtful to themselves, and despised by
strangers, to the great ignominy of the whole nation.’ There was also
a continual immigration of multitudes of miserable, debauched, and
weakly people from Scotland, including ‘exorbitant numbers of
young boys and maids unfit for any service,’ reminding us of the
overflowings of the Irish population into England, Scotland, and the
United States of America in more recent days.415 During this
summer, owing, doubtless, to the pressure of the famine, this
scandalous system had been carried to such a height, that the
Scotch merchants were threatened with expulsion from the city. In
this exigency, they wrote to the king, craving his intercession. Patrick
Gordon, who acted as agent for the king in Dantzig, also wrote,
apparently, at the same time, shewing how matters stood, and
entreating that some order and rule should be established among his
countrymen, as they should not otherwise be able much longer to
withstand the strength of their enemies.416
The king wrote to the Earl of Mar, requesting him
to send into Argyleshire and Glenorchy, for four or Nov. 1.
five couples of earth-dogs (terriers), which he was
desirous of obtaining in order to transmit them to 1624.
France. His majesty further requested ‘that ye
have a special care that the oldest of them be not passing three
years of age, and that ye send them not all in one ship, but some in
one ship, and other some in another, lest one ship should
miscarry.’417
The same Earl of Mar, having to spend the winter of 1631 in Stirling,
and designing to amuse himself with fox-hunting, sent a letter to his
cousin, the Laird of Glenurchy, entreating the favour of ‘a couple of
good earth-dogs;’ and adding, what shews the importance of the
favour, ‘I pray you use me as familiarly as I do you, for without
ceremony, cousin, you shall not have a friend over whom ye have
greater power than over me.’ P. S.—‘What ye send me, let it be
good, although it be but one.’

There is at this time a glimpse of rationality


regarding witchcraft in the public authorities, in as Nov. 2.
far as the Privy Council deemed it right to hesitate
about the granting of commissions for the trial of persons charged
with that crime. The Council had been troubled by the importunity of
persons seeking for such commissions, and at the same time
concerned to find that the informations on which the commissions
were sought for ‘seemed to be very obscure and dark.’ As anxious
for the truth, and to the intent that neither should the innocent be
molested nor the guilty escape, they now arranged that all
informations should henceforth pass through the hands of the
bishop of the diocese, ‘to be seen and considered by him, and such
of the ministry as he shall call unto him.’—P. C. R.
We have here a revelation of that doubt about the reality of
witchcraft which is suspected to have lurked in the minds of all the
principal official people throughout the seventeenth century. It was a
time of comparative triumph for the established church. The bishops
were not particularly in need of popularity. They could afford to be
easy with both Romanists and necromancers. It was precisely in
such circumstances that we could expect to find the chief
administrative body letting slip a doubt as to the soundness of many
of the alleged instances of sorcery lately subjected to trial.
The pest, which had been for some time before in
Holland, broke out ‘in sundry houses in Edinburgh, Nov. 23.
to the great terror of the whole town. It began in
Paul Hay418 a merchant’s house, a month before, 1624.
and was not known till now; therefore the more
dangerous, because hard to discern the clean from the unclean.
Upon the last day of November, the president and other lords of
Council and Session, meeting together, resolve to rise, and continue
the session till the 8th of Januar.’—Cal.
One consequence of the occurrence of the pest at this time was,
that the king’s design of enforcing a communion at Christmas, where
all the people should kneel, was frustrated. Another result generally
satisfactory was a relaxation of the severity against the Edinburgh
citizens who were banished and imprisoned for opposing the new
ceremonies. William Rig was allowed to leave his prison of
Blackness, and remain for fifteen days with his wife at his house of
Morton, where she was ‘very heavily visite with infirmity and
sickness.’ Mean, having ‘a numerous family and his wife grit with
child, and nane to have ane care for order-taking with them, how
they sall be providit for and governit in this [time of] danger,’ was in
like manner permitted to repair to Edinburgh, to see after them, and
there remain till the 15th of January. So also John Hamilton was
relieved from the Tolbooth to attend on his wife, who chanced to be
in the same delicate condition as Mrs Mean. After all, ‘the pest raged
not; few houses were infected with it; so that it appeared the chief
end wherefore the Lord had sent it, was to disappoint the king by
scattering the people.’—Cal.

Amidst the alarms regarding the pest, people


heard of a strange case of personal quarrel and Dec.
vindictiveness. One William Hamilton, a soldier, son
of the deceased William Hamilton, ‘called of Inchmachan,’ was lately
come from the Low Countries, avowing ‘a settled purpose and
resolution to appeal Captain Harie Bruce to the single combat, or
otherwise to watch the opportunity to bereave him of his life.’ The
Privy Council was obliged to take means for preventing a hostile
collision.—P. C. R.
The Privy Council readily apprehended that the
prosecution of ‘this damnable and cruel intention’ Dec. 8.
would both breed danger to the parties and
produce great trouble and controversy among their 1624.
friends, to the disturbance of his majesty’s peace,
if timous remeed be not provided. They therefore summoned the
parties before them to give assurance of their good behaviour.
Deeming, as was formerly remarked, anything that illustrates the
progress of the arts as worthy of notice in this record, though
perhaps trifling in itself, we may advert to Mr Alexander Hamilton,
brother to the secretary Earl of Melrose, as having now obtained a
patent of twenty-one years for a new cart invented by him, ‘wherein
greater weight and burdens may with far less force be drawn, and
conveniently carried, than hath been done with ony other kind of
cart hitherto known or heretofore used.’419
‘Sandy Hamilton,’ or ‘Dear Sandy,’ as he was called, was a man of
note on account of his skill in some of the useful arts, particularly in
those connected with the munitions of war. He practised these arts
for some time in Germany, whence he was recalled to England,
where the king granted him pensions and allowances to the amount
of £800 sterling per annum. When the Civil War broke out, he joined
his countrymen, and helped to fit out the Covenanting army of 1640
with a species of short but effective gun, which was carried slung
between two horses, and the serviceableness of which was proved
at the battle of Newburn-ford, when the Scots crossed the Tyne in
the face of the enemy and became masters of Newcastle.

In this year we have the latest known notice of a


woman of extraordinary attainments who had lived 1624.
for many years in Edinburgh, practising an art in
which she was long after pronounced to have never been excelled.
Caligraphy, or the art of beautiful writing, was in greater vogue in
the seventeenth century than in our more utilitarian days. Under
what circumstances Esther Inglis, a Frenchwoman residing in the
Scottish capital, came to give her days to so laborious an art, we do
not learn. Neither are we aware how it was that Esther came to live
in the Scottish capital. There, however, we find her, so early as 1599,
writing one of the little manuscript volumes which have given her
celebrity. This book, preserved in the Bodleian Library, is entitled Les
Proverbes de Salomon, escrites en diverses sortes de Lettres, par
Esther Anglois, Françoise. A Lislesbourg en Ecosse. 1599. ‘This
delicate performance,’ says Ballard,420 ‘gains the admiration of all
who see it; every chapter is wrote in a different hand; as is the
dedication, and some other things at the beginning of the book,
which makes near forty several sorts of hands. The beginnings and
endings of the chapters are adorned with most beautiful head and
tail pieces, and the margins are elegantly decorated with the pen, in
imitation, I suppose, of the beautiful old manuscripts. The book is
dedicated to the Earl of Essex, Queen Elizabeth’s great favourite. At
the beginning are his arms, neatly drawn, with all its quarterings—in
number fifty-six. In the fifth leaf is her own picture, done with the
pen, in the habit of that time. In her right hand, a pen, the left
resting upon a book opened; in one of the leaves of which is written
De l’Eternel le bien: de moi le mal, ou rien. On the table before her
there is likewise a music-book lying open, which perhaps intimates
that she had some skill in that art. Under the picture is an epigram
in Latin, made by Andrew Melvin; and on the next page another,
composed by the same author, which is as follows:

Æmula naturæ manus exprimit una figuras


Mille, animans pictis Signa pusilla notis,
Signa creans animata, polûm spirantia signa:
Quæ picturata margine limbus obit.
Mirum opus: at mage mira Manus; mira omnia vincit
Mens manui moderans, dum manus urget opus.

Andræus Melvinus.

Thus translated into English:

One hand dame nature’s mimic does express


Her larger figures, to the life, in less.
In the rich border of her work do stand,
Afresh created by her curious hand,
The various signs and planets of the sky,
Which seem to move and twinkle in our eye.
Much we the work, much more the hand admire,
Her fancy guiding this does raise our wonder higher.

Another of Esther’s transcripts was entitled Historiæ Memorabiles


Genesis, 1600. A copy of the French Psalms, written by her, and
presented to Queen Elizabeth, is in the library of Christ Church,
Oxford. There is also in the Bodleian a manuscript of hers, entitled
Les Vingts et Six Quatrains de Guy de Faur, Sieur de Pybrac, escrits
par Esther Inglis, pour son dernier Adieu, ce 21 Jour de Juin 1617. It
seems to have been a gift to the celebrated Dr Hall—subsequently
Bishop of Norwich—on parting from him at the time of the king’s
visit to Scotland. The latest known of Esther’s works is a volume
preserved in the Royal Library, Esther Inglis’s Fifty Emblems, dated
at Edinburgh 1624.
When the king was at Stirling, Esther’s son
presented to him a little book entitled Sidus 1624.
Celeste, and he experienced some of James’s
good-natured patronage in consequence. In June 1620, Esther is
found addressing the king in behalf of this son, who, having
completed a school-course, ‘would gladly follow theology.’ But ‘as
Dædalus was not able to free himself of his imprisonment in the isle
Creta but by the help of wings made of pens and wax, even so my
son is not able to free himself of inability to effectuate this his
affection, but by the wings of your majesty’s letter, composed by pen
and wax, through which he may wing his flight happily to some
fellowship, either in Cambridge or Oxford, as occasion sall fall out.’ If
so far favoured by his majesty, ‘I may have my tossed mind relieved
of the great care I have perpetually for this said youth.’—An. Scot.
Ballard states, on the authority of a memorandum of Hearne, the
antiquary, that Esther Inglis was married to a Scotsman, named
Bartholomew Kello, and had a son, named Samuel Kello, who was
educated at Christ Church College, Oxford, and was afterwards
minister at Speckshall, in Suffolk.

‘At this time arose great discontentment betwixt


the provost, bailies, and council of Edinburgh, and 1625. Mar.
their ministers, because the ministers had
procured the king’s letter and direction to the magistrates and
council, for augmentation of their yearly stipends. They were not
content with twelve hundred merks for every one, beside their house
mail [rent], which was more than their predecessors, worthier than
they, had, but importunately craved two thousand. The people,’ says
the zealous Presbyterian historian, ‘detested them for their ambition,
their avarice, and malice at honest and godly professors. They were
well fingerfed in other men’s houses, howbeit they had sufficient to
maintain them at home.’—Cal.
In June 1626, Charles I. enjoined the magistrates to give each of
their ministers £100 sterling of yearly stipend, with a free house.—
Bal.

The news of the death of King James—which


occurred on the 27th of March—reached Edinburgh Mar. 30.
on the 30th, at the outbreak of a storm of
extraordinary violence which raged along the 1625.
whole coast, destroying much shipping, and
throwing down several harbours. ‘The water raise above the harbour
of Leith, and ran into the houses of the town; yea, the boats and
barks within the same floated so above the shore, that some of
them were cast away upon the sides of the houses; and great ships
therein could not be keepit, with all their anchors and cables, from
doing great skaith, ilk ane to ane other, whereof the like was never
heard tell of in our days. Sundry mariners, keeping their ships [fra]
skaith, were hurt themselves, and in special James Langlands and
Robert Dury, two masters of ships, very expert in that art, were
baith cast away, working for the relief of their awn ships.’—Jo. H.
‘The like harm was done in sundry other parts upon the coast along
the Firth, in Saltpreston, Kirkcaldy, Ardross, and other parts. Salt-
pans were overthrown, ships and boats broken, coal-heughs beside
Culross drowned. The like of this tempest was not seen in our time,
nor the like of it heard in this country in any age preceding. It was
taken by all men to be a forerunner of some great alteration. And,
indeed, the day following—to wit, the last of March—sure report was
brought hither from court, that the king departed this life, the Lord’s
day before, the 27th of March.’—Cal.
This was long after remembered as the storm of the Borrowing
Days, such being a popular appellation for the last three days of
March, as expressed in a well-known popular rhyme. It is a
proverbial observation of the weather, which seems to be justified by
fact, the bad weather being connected with the vernal equinox.
END OF VOL. I.
Edinburgh:
Printed by W. and R. Chambers.

House of Robert Gourlay, a rich Edinburgh


Citizen of 1574.
See pages 143 and 255.

FOOTNOTES:
1 ‘A man of science as well as of philosophic mind would employ
himself well in examining those accounts of prodigies in the early
annalists and chroniclers, which of late years have been regarded
as only worthy of contempt.’—Southey—Omniana, i. 266.
2 De Fratribus Minoribus nulla est quæstio, professi siquidem
simulatam paupertatem, nulla prædia, nullos fundos habent; sed
sub prætextu pietatis ex interceptis testamentis, et stultæ pietatis
zelo, ditissimi facti sunt: quod ex eventu, post infelicem pugnam
de Flodden, compertum est: nam qui eo pugnaturi
proficiscebantur, nisi confessione facta remissionem a Fratribus
Minoribus impetrassent, omnia mala ominabantur. Interea omnem
pecuniam, monumenta, et si quid pretiosum alioqui habebant,
eorum fidei committebant, sperantes, se mortuis, illos ea quæ
credebantur omnia fide integra posteris suis restituros: at illi,
eorum qui in prælio occubuerunt, nec fidem reposcere poterant,
bona in fundi comparatione, et ecclesiæ et monasterii
exstructione ad sui ordinis homines convertebant: nec aliter
accidit in acie Pinquini.—Craig, Jus Feudale, lib. i.
3 Registrum Episcopatus Aberdonensis, ii., 309, 310.
4 As often as I turn my eyes to the niceness and elegance of our
own times, the ancient manners of our forefathers appear sober
and venerable, but withal rough and horrid.—Buchanan: De Jure
Regni, as quoted by Dugald Stewart in Preliminary Dissertation,
Encyclopædia Britannica.
5 This phrase occurs in an order of the provost of Edinburgh (Earl
of Arran), dated 1518, excusing Francis Bothwell from taking the
part of Little John.—Napier’s Life of Napier of Merchiston, p. 53.
6 See the Rev. Joseph Hunter’s tract, The Ballad Hero Robin
Hood, 1852; making it at length tolerably certain that the outlaw
lived in the reign of Edward II, and for a short time held office in
that king’s household.
7 Arnot’s History of Edinburgh.
8 Scots Acts, 1555.
9 Persons in the employment of the craftsmen; journeymen.
10 From a sculpture on the Magdalen Chapel, Cowgate,
Edinburgh.
11 Refreshment at 4 o’clock afternoon. Latterly, the term has
been applied to tea-drinking.
12 A road in the line of the present Princes Street.
13 Knox says she frowned here, and gave the books to Arther
Erskine, the captain of her guard, ‘the maist pestilent papist
within the realm.’
14 Anti-tune, antiphone, or response.
15 Notes to Ancient Scottish Poems from the Bannatyne
Manuscript, 1770.
16 From a unique copy of this tract a reprint was given by Mr
John Robertson to the Bannatyne Club, 1833.
17 See under October 1570; also April 5, 1603.
18 Comedy of Errors, Act III. sc. 2.
19 In July 1538, there is an entry in the treasurer’s books, of 14s.
‘to Alexander Naper for mending of the Queen’s sadill and her
cheriot, in Sanct Androis.’ In January 1541-2, there is another: ‘To
mend the Quenis cheriot vi-1/4 elnis blak velvet, £16, 17s. 6d.’
Besides something for cramosie, satin, and fringes.
20 History of the Family of Mackenzie, MS. in possession of J. W.
Mackenzie, Esq., W.S., Edinburgh.
21 A tract containing the disputation was printed by Lekprivik in
1563, and has been republished, Edinburgh, George Ramsay &
Co., 1812. Dr M‘Crie, in his Life of John Knox, gives an ample
abstract of this curious pamphlet.
22 Randolph to Cecil, Edin. Nov. 30, 1562. Chalmers’s Life of
Queen Mary.
23 Edin. Council Register, apud Maitland.
24 In England, the spring of 1562 had been marked by excessive
rains, and the harvest was consequently bad. Towards the end of
the year, plague broke out in the crowded and harassed
population of Havre, in France, then undergoing a siege, and
from the garrison it was imparted to England, which had been
prepared for its reception by the famine. There it prevailed
throughout the whole year 1563, carrying off 20,000 persons in
London alone. ‘The poor citizens,’ says Stowe, ‘were this year
plagued with a threefold plague—pestilence, dearth of money,
and dearth of victuals; the misery whereof were too long here to
write. No doubt the poor remember it.’ On account of the plague
at Michaelmas, no term was kept, and there was no lord-mayor’s
dinner! The plague spread into Germany, where it was estimated
to have carried off 300,000 persons.
25 See notes to Scott’s Lay of the Last Minstrel.
26 This curious contract is printed entire in Pitcairn, iii. 390.
27 Scott’s notes, ut supra.
28 There is a place called Tarlair near Banff.
29 Nicol Burne’s Disputation, p. 143.
30 While Drury lay before the castle, Lord Fleming entered into a
hostile correspondence with Sir George Carey, one of Elizabeth’s
officers. This is given in Holinshed’s Chronicle.
31 Mr Pennant, from whom the above translation is borrowed,
says, by a strange mistake, ‘on one of the deer.’
32 William Barclay, De Regno et Regali Potestate adversus
Monarchomachos. Parisiis, 1600. This author was a native of
Aberdeenshire, but finally settled at Angers, in France, as
Professor of Civil Law in the University there. He died in 1604.
Bishop Geddes, in introducing this extract from Barclay’s forgotten
work to the notice of the Society of the Antiquaries of Scotland
(1782), remarks that a still more grand entertainment of the
same kind was given in 1529 to King James V., his mother, Queen
Margaret, and the pope’s legate, by the then Earl of Athole, and
that an account of the affair has been preserved in Lindsay of
Pitscottie’s History of Scotland. The venerable bishop adds: ‘Need
I take notice that the hunting described by Barclay bears some
resemblance to the batidas of the present king of Spain, where
several huntsmen form a line and drive the deer through a
narrow pass, at one side of which the king, with some attendants,
has his post, in a green but of boughs, and slaughters the poor
animals as they come out almost as fast as charged guns can be
put into his hand and he fire them. These are things sufficiently
known; and the same manner of stag-hunting is practised in Italy,
Germany, and other parts of Europe.’421
33 Gunn’s Historical Enquiry respecting the Harp in the Highlands.
1807.
34 Agnes Strickland’s Life of Queen Mary.
35 Archæologia Scotica, ii. 287.
36 Richard Bannatyne’s Memorials, p. 238.
37 Dalyell’s Darker Superstitions of Scotland, p. 130.
38 Walter Goodall and Miss Agnes Strickland have been misled by
the description of the place in Bothwell’s Act of Forfeiture—‘ad
pontes, vulgo vocatos foulbriggs‘—into the belief that the queen
was seized at the suburb of Edinburgh formerly called Foulbriggs,
and now Fountain Bridge. In reality, the expression in the Act,
rightly translated, applies to the place indicated in the Diurnal of
Occurrents—‘at the Briggs, commonly called Foulbriggs,’ the
syllable foul being presumably a vulgar casual addition which the
ancient marshy condition of the place rendered appropriate. All
the other contemporary writers place the scene of the seizure at
the Almond—Buchanan, Birrel, and Herries—while Sir James
Melville, who was one of the party seized, says ‘betwixt Linlithgow
and Edinburgh’—an expression he could scarcely have used if the
fact had happened close to the city. In Ane Chronicle of the Kings
of Scotland, printed by the Maitland Club, and apparently
contemporary, the brig of Awmont is the locality assigned. But the
most powerful evidence on the subject, and what sets the matter
at rest, is a Remission under the Privy Seal, of date October 1,
1567, to Andrew Redpath, for his being concerned in ‘besetting
the queen’s way ... near the water of Awmond, and for taking and
ravishing her,’ &c. It may be remarked that there is no evidence of
the suburb alluded to by Miss Strickland having been called
Foulbriggs, or having existed at all, at that time, while we have
proof of the existence of a place on the Almond Water, under the
name of the Briggs, long before this time. In the Register of the
Privy Seal is ‘ane lettre maid to Robert Hamilton in Briggis,
makand him capitane and kepar of the place and palace of
Linlithgow,’ &c. 1543, Aug. 22.
39 Privy Seal Register.
40 Carries.
41 Nickname.
42 Garret.
43 Searches.
44 Thievery.
45 Ere.
46 Till.
47 Ancient Scottish Poems, 2 vols. 1786.
48 Border Minstrelsy, i. 157.
49 Burgh Record of Canongate, Maitland Club Mis., ii. 303.
50 Babees, halfpence, from bas billon, a low piece of money.
51 Hume’s Hist. House of Douglas.
52 Privy Seal Register.
53 Discoverie and Historie of the Gold Mynes in Scotland. Written
in 1619. Bannatyne Club, 1825.
54 Holinshed’s Chronicle.
55 The original, preserved in the General Register House, is
printed at length in Pitcairn, iii. 394.
56 Privy Seal Register.
57 Council Register, quoted in Maitland’s History of Edinburgh, p.
32.
58 Ane Breve Descriptioun of the Pest, &c. 1568.
59 Mr M. Napier’s Notes to Spottiswoode’s History, Spot. Club
edition.
60 Where Napier had other estates.
61 The bishop was about to go to York, to attend the
investigation respecting the queen.
62 Justiciary Records, MS., Adv. Lib., quoted by Mr Mark Napier.
63 Burgh Records of Canongate, Mait. Club Mis., ii. 313.
64 The pest was severe in London in autumn 1569, whether by
communication from Scotland does not appear.
65 Ane Addicioun of Scottis Cornicklis and Deidis, printed from an
original manuscript by Thomas Thomson, Esq.
66 Memorials of George Bannatyne. Edited by Sir Walter Scott.
Bannatyne Club-book, 1829.
67 Extracts from Canongate Council Register, Maitland Club
Miscellany, ii. 814.
68 Ane Trajedie in forme of ane Diallog betwix Honour, Gude
Fame, and the Authour heirof, in ane Trance. Lekprevik, 1570.
69 Dalyell’s Illustrations of Scottish History, p. 521.
70 Harrison’s translation, apud Holinshed.
71 Extracta e Chronicis Scocie. Edin. 1842.
72 Sir William Sinclair, who records these curious particulars, was
Lord Justice-general of Scotland, and altogether an estimable
person. According to Father Hay: ‘He gathered a great many
manuscripts, which had been taken by the rabble out of our
monasteries in the time of the Reformation.’—Genealogy of the
Sinclairs of Roslin, edited by James Maidment, Esq. 1835. See
something further about him under June 1623.
73 The distance from Bathgate to Edinburgh is eighteen miles.
74 Bannatyne’s Journal, 46.
75 Calderwood, iii. 20, 167, and note.
76 The couplet almost verbatim occurs in the prophecies of
Bertlingtoun, in R. Waldegrave’s brochure, already quoted (under
Jan. 1, 1561-2):

‘However it happen for to fall,


The Lyon shall be lord of all.’

77 Eupham M‘Calyean subsequently attained still higher notoriety


in the character of a witch. See under Dec. 26, 1590.
78 The whole series is printed in Abbotsford Miscellany, p. 5.
79 Crawford’s Memoirs, 215.
80 The Lady Scotland is understood to address ‘the richt
honorable and godly learnit gentleman, the Laird of Dun, minister
of God’s word.’
81 Bruised.
82 He ‘wes extremelie pynit in the beitis lang of befoir.’—D. O.
83 Calderwood, iii. 393.
84 The word its did not then exist, and writers were forced to use
either his or her instead.
85 Humboldt’s Cosmos.
86 Brewster’s Encyclopædia.
87 Tytler, vii. 388.
88 Under the care of John Smith, youngest, the secretary of the
Club. 1832.
89 So called ‘for that in old Fathers’ days the people would that
day shear their heads and clip their beards, and so make them
honest against Easter Day.’—Authority quoted in Brand’s Pop.
Antiquities, by Ellis.
90 ‘Robert Gurlay, the duke’s servant,’ is the last in the list of
persons forfeited by the parliament of James VI., August 1571.
91 Calderwood.
92 Register of the General Kirk of Edinburgh, Maitland Club Mis.,
i. 101.
93 Reg. of Gen. Kirk of Edinburgh, Maitland Club Mis., i. 111.
94 As this conduct was such as might lead to a collision between
the parties, it is not easy to see how it illustrates the author’s
proposition of Wedderburn’s pacific temper.
95 From a copy in the editor’s possession of a manuscript long
preserved in Broomhouse, Berwickshire.
96 ‘There was presented to the Queen Regent (1558), by Robert
Ormiston, a calf having two heads, whereat she scripped
[mocked], and said: “It was but a common thing.”‘—Knox.
97 Coloured stripes sewed on a garment.
98 Fringes or trimmings.
99 This seems too high a phrase of compliment for the Regent
Morton. His Grace was the ordinary phrase, according to Sir
James Melville.
100 Aberdeen Council Register, Spal. Cl. Mis. i. 30.
101 Abbotsford Miscellany, 45.
102 Hist. of the House of Douglas, ii. 260.
103 The wife of the earl—Margaret Fleming, relict of the Master
of Montrose and the Master of Erskine—was believed to have the
powers of incantation. See under June 19, 1566.
104 The seat of the Earl of Montrose, on the skirts of the Ochil
Hills.
105 Crawford’s Officers of State. Moysie’s Memoirs.
106 As much as to say, ‘Sport, and be at your ease.’
107 Moysie.
108 Calderwood.
109 Arranged—not lying as rubbish.
110 Documents Relative to Royal Receptions, 4to. Edinburgh,
1822.
111 Maitland Club Miscellany, ii. 19.
112 General Assembly, April 1578.
113 A house called the Novum Hospitium, in the Priory Park. It
has long been demolished, excepting only the court-gate.
114 Atkinson’s Discoverie of Gold Mynes in Scotland.
115 Trans. Ant. Soc. Scot. iii. 312.
116 Row’s History of the Kirk of Scotland.
117 The original of this document, commonly called the King’s
Confession, is preserved in the Advocates’ Library.
118 See under May 1574.
119 Fr. clientèle, dependents.
120 This Scotch law-phrase has become familiar in England,
under the form of ‘art and part,’ and is not in general correctly
understood. The first word is not art, but airt, meaning direction,
implying that the accused was believed to have counselled and
guided the actual perpetrators of the crime.
121 Powerfully.
122 A strange thing.
123 See under Jan. 1, 1561-2.
124 For the above illustrations of his remark, the author is
indebted to Mr Mark Napier’s curious notes to the edition of
Spottiswoode’s History published by the Spottiswoode Club.
125 ‘Item, to ane pyper and ane young boy his sone that playit in
Dalkeytht upon Sonday the xj day of Junii, fra the kirk to the
castell befoir his Hienes ... xxs.‘
126 A noteworthy anecdote of this lady is stated in Anderson’s
History of the Family of Fraser. On the death of her first husband,
the tutorship of her infant son, Lord Lovat, became a matter of
contention between the child’s grand-uncle, Fraser of Struie, and
his uncle Thomas; and it seemed likely there would be a fight
between their various partisans. In these circumstances, a clerical
gentleman of the clan, Donald Fraser Dhu, entreated the widow
to interfere, and ask Struie to retire. She gave an evasive reply,
remarking that whatever might befall, ‘not a drop of Stewart
blood would be spilt.’ The mediator then drew his dirk, and told
her ladyship with a fierce oath, that her blood would be the first
that would be spilt, if she did not do as he requested. She then
complied, and Thomas, the child’s uncle, was accordingly elected
as tutor.
127 Calderwood.
128 Melville’s Diary.
129 He states that David Riccio was buried by the queen in the
royal vault, ‘almost in the arms of Magdalene Valois,’ and thence
draws a shameful inference against the chastity of Mary. To
dedicate to the young king a book in which he endeavoured to
prove his mother an adulteress, and the murderer of her
husband, gives a strange idea of the sense of that age regarding
the rules of good taste, to say nothing more.
130 On this occasion Captain Lammie was killed. Sir Walter Scott,
in relating the incident in the Border Minstrelsy, expresses a hope
that he was ‘the same miscreant who, in the day of Queen Mary’s
distress, “his ensign being of white taffety, had painted on it the
cruel murder of King Henry, and laid down before her majesty, at
what time she presented herself as prisoner to the Lords.”—
Birrel’s Diary.’ It was very probably so, as we find that he then, as
well as now, was a hired soldier of the government. As his
painted ensign makes rather a conspicuous appearance in
Scottish history, it may be not unworthy of notice that the
following entry occurs in the Lord Treasurer’s books, under March
18, 1567-8, nine months after the incident in question: ‘To
Captain Andro Lambie for his expenses passand of Glasgow to
Edinburgh to uplift certain men of weir, and to mak ane
Handsenyie of white taffety, £25.’ He was then acting for the
Regent Moray. It seems probable that, having spoiled his ensign
by the picture of the king’s murder, he was now gratified with a
new one at the expense of his employer.
131 In the parish of Carluke, Lanarkshire.
132 He remained at this fine old castle twelve days, attended by
Arran, Sir Robert Melville, Secretary Maitland, Ferniehirst, Colonel
Stuart, and the Master of Gray; and regaled with ‘the play of
Robin Hood.’ ‘After the banquet was ended, Arran fell deadly
sick.’—Cal.
133 History of King James VI.
134 Darker Superstitions of Scotland, p. 484.
135 Note in Maitland Club Miscellany, iv. 123.
136 Gregory’s History of the Western Highlands and Isles, p. 234.
137 Estate—piece of ground.
138 Threatens.
139 A horn had originally, or perhaps was still used, in
proclaiming a man rebel; hence the term, horning, or being put to
the horn.
140 October ... 1587, ‘his majesty raid with ane host to Peebles,
for order-taking with the broken men, and returnit the tent day.’—
Moysie’s Memoirs.
141 It is understood that this was the place of worship formed
out of the choir or eastern portion of the church of St Giles.
Opposite to the pulpit, which was attached to the first pillar from
the east end, was the royal gallery or loft, also attached to a
pillar. Thus the king and the minister were sufficiently near each
other for the colloquies in which they occasionally indulged. See
Wilson’s Memorials of Edinburgh.
142 Harrington’s Nugæ Antiquæ, by Park. 2 vols. 1804. Vol. i., p.
369.
143 A light bark with one mast.
144 Rascal.
145 Worthless fellows.
146 Value.
147 A bulk, a corpse.
148 A gun in the poop of the ship.
149 Discharged.
150 Maitland Club Miscellany, i. 276.
151 Transverse.
152 It is to be feared that Abacuck was a person of a litigious and
troublesome temper. A complaint was made against him before
the Privy Council by Kenneth M‘Kenzie of Kintail, to the effect that
Bisset had purchased letters to force Kenneth to produce a
clansman named Rory M‘Allister M‘Kenzie, alleged to be at the
horn for default in a civil cause. It was alleged that, knowing that
on the case being called, he (Kenneth) could shew many good
arguments for exonerating himself of this responsibility, Bisset
had delayed the calling, in hopes of being able to do it when
Kenneth should not be at hand to make his own defence. The
matter being brought fully before the Lords in the presence of
parties, it was decreed that Kenneth should be absolved from the
duty implied in Bisset’s letters.—P. C. R.
In July 1608, Abacuck was involved in a still worse-looking affair.
He was charged before the Privy Council with having prosecuted
Mr William Reid, of Aberdeen, in a malicious manner at law, from
no cause but that of ‘some little eleist’ fallen out between him and
Andrew Reid, brother of William, in which the said William had no
interest. He had also traduced William Hay in regard to the
propriety of his marriage, though it was well known to be ‘an
honest and famous marriage.’ The Council found the charge just,
and commanded Abacuck’s proceedings to be stopped.
153 Melville Diary, 291.
154 The conduct of the clergy on this occasion is defended, but in
rather subdued terms, by Dr M‘Crie, Life of Andrew Melville, i.
395.
155 Statistical Acc. of Scot. ed. 1845, v. 258.
156 A leek (Fr. cibolle).
157 Maitland Club Mis., i. 278.
158 See the entire letter in Blackwood’s Magazine, ii. 313.
159 Chronicle Kings of Scotland.
160 Moysie’s Memoirs.
161 Act of Privy Council, Notes to Waverley Novels (Legend of
Montrose).
162 James Melville’s Diary.
163 In this article, both editions of Moysie are used.
164 Birrel’s Diary.
165 Latterly called the West Bow.
166 A public weighing-machine at the head of the West Bow.
167 Johnston’s Hist. Scot. MS.
168 From the reprint of a rare contemporary tract, in Papers
relative to the Marriage of James VI. (Bannatyne Club), 1828.
169 Regals, or rigols, an ancient musical instrument, composed of
a series of reeded tubes resting on a bellows, which the player
worked with his left hand. See Dalyell’s Musical Memoirs of
Scotland, 1849, p. 117.
One is at a loss to understand how the poet thought of
expressing his admiration of the strings of the organ and regals.
170 Burel’s Description of the Queen’s Entry, &c., 1590, in
Watson’s Collection of Scottish Poetry, 1712.
171 Johnston’s Hist. Scot. MS.
172 Edin. Council Record.
173 Maitland Club Misc., i. 280.
174 The entire letter is printed in Blackwood’s Magazine, ii. 628,
and in the Caldwell Papers.
175 Calderwood.
176 ‘Wha were lately pardonit by his majesty for slaughter of the
Laird of Dawick’s son.’
177 Mr C. Innes’s preface to Black Book of Taymouth, xxv.
178 Anderson’s Hist. of the Frasers, p. 102.
179 See onward, under May 1600.
180 See onward, under August 1618.
181 Britain’s Distemper, by Patrick Gordon, Spald. Club.
182 Tytler’s History, quoting letters in the State-paper Office.
183 This lady did not long enjoy the position of a duchess. She
died on the 11th of May 1592, and was ‘buried in the Trinity
College, in the east end thereof, very solemnly.’—Jo. Hist. When
the Trinity College Church was taken down, that its site might
form part of a railway station, the remains of a female, believed
to be those of the royal foundress, Mary de Gueldres, were found
in a side-aisle, and duly re-interred in the royal sepulchre at
Holyrood. Afterwards, the remains of another female, who had
apparently been buried under circumstances of distinction, were
found in the east end of the church, and suspected by some to be
the remains of the queen. The probability is, that these latter
remains were those of the youthful Sophia Ruthven, Duchess of
Lennox.
184 May 19, 1591, the town-council of Aberdeen made
arrangements for the support of one Robert Abell, who was
‘visited with leprosy, and thereby unable to win his living or
frequent honest men’s society.’ He was placed in the house here
described.—Ab. C. R. In 1612, the magistrates made the like
provision for Agnes Jameson, spouse to Patrick Jack, ‘vexed and
diseased with the sickness of leprosy,’ although she was not born
and bred in the burgh.
185 Edin. Council Record. See Professor Simpson’s curious
Notices of Leprosy in Scotland, Edin. Med. and Surg. Journal, No.
149.
186 Now La Mancha.
187 It will be found that the body of the Bonny Earl remained
above ground for six years, probably with a view to keeping up
the popular indignation against his murderers. (See under
February 16, 1597-8.)
188 It is necessary to distinguish this from the murder of another
Laird of Brackla in 1667, on which a ballad has been composed.
See Jamieson’s Northern Ballads.
189 In a memoir of the family of Grant, written by Mr James
Chapman, minister of Cromdale, in 1729, and preserved in the
Macfarlane Collections in the Advocates’ Library, there is a curious
traditionary anecdote, which the writer connects with the murder
of the Laird of Brackla, and yet dates in 1540. It is given in the
following terms: ‘[James Laird of Grant, called Shemus nan
Creagh, or James the Ravager] distinguished himself in assisting
the Earl of Huntly, his cousin, against the insults of several
enemies, and particularly in revenging the murder of Gordon
Baron of Brackla, on Dee water-side, who was murdered by the
countrymen there. The revenge went such a length, that above
sixscore orphans were left in the desolate country on Deeside,
nobody knowing who their parents were. These miserable
orphans were, out of pity and commiseration, carried by the Earl
of Huntly into his castle, where they were maintained and fed
thus. A long trough of wood was made, wherein was put pottage
or any other kind of food allowed them; and the young ones,
sitting round about the trough, did eat their meat out of it as well
as they could. The Laird of Grant visiting the earl, was, for
diversion’s sake, brought to see the orphans slabbing at the
trough; which comical sight so surprised him, that he proposed to
carry one-half of them to Balcastle, alleging that, having a hand
in destroying their parents, he was bound in justice to take a
concern in their preservation and maintenance. Those of them
that were brought to Castle-Grant are to this day called Slioch
Namor—that is, the Posterity of the Trough.’ As Shemus nan
Creagh died in 1553, and the Grants were not engaged on the
Earl of Huntly’s side on this occasion, but participated with their
relatives and allies the Mackintoshes in suffering from his
vengeance, it may be presumed that this barbarous tale refers to
the date assigned for it by Chapman—namely, a period fully fifty
years earlier than the murder of the Laird of Brackla. It has
nevertheless been introduced by Sir Walter Scott in his Tales of a
Grandfather, as applicable to the reign of James VI.; and the
reader who turns it up there, may experience some amusement
in contrasting its ample and picturesque details with the simple
original anecdote as above narrated.
190 The Earl of Angus in this anecdote was a Protestant, and
succeeded by the earl noticed in the preceding article, who was
of the ancient faith.
191 Fairnyear, the last year: the phrase means, formerly a lord.
192 Andrew Wauchope of Niddry, and John Hamilton, younger, of
Samuelston.
193 The king, probably from recollection of some incident of their
early school-days, used to recognise the grave earl by the name
of Jock o’ Sklaitts.
194 The above anecdote was communicated to me by Sir Walter
Scott in 1827, immediately after he had derived it from the Earl of
Haddington (Earl Charles), to whom, I suppose, it had come
through his predecessors, the descendants of Lord Mar’s brother-
statesman, Thomas, first Earl of Haddington.
195 Letters and State Papers of the Reign of James VI.
Abbotsford Club Series. 1838. P. 16.
196 Calderwood. History of James VI. Pitcairn’s Criminal Trials.
Gregory’s History of the Western Highlands and Isles.
197 This interlined in the manuscript in a different hand. Another
report is, that Lord Maxwell was slain by Willie Johnston, nephew
of the Galliard, mentioned under July 22, 1593.
198 G. L. Meason’s preface to Discoverie and Historie of the Gold
Mynes of Scotland. Bannatyne Club. 1825.
199 Archæologia Scotica, iv. 404.
200 Napier’s Life of Napier of Merchiston.
201 Another writer represents the Master of Montrose as setting
upon Sir James Sandilands.
202 The writer of this curious story speaks of the form of the
funeral as rare.
203 Council Register in Maitland.
204 Patrick Anderson’s History MS. He adds: ‘I was at the time by
chance an eye-witness myself.’
205 Hist. K. Ja. 6.
206 March 16, 1575-6, John Macmoran, messenger, reported to
the Privy Council, that in January last, when using his office in
execution of letters upon Patrick M‘Kie, burgess of Wigton, he had
been set upon by Alexander M‘Kie of Myreton and his two
brothers, who cruelly struck and chased him, giving him
despiteful words, and threatening him with worse if he ever again
came there in a professional capacity. The offenders, failing to
appear on call to answer for this outrage, were put to the horn.—
P. C. R.
207 See ante, p. 143.
208 Lady Yester in her widowhood founded a church in
Edinburgh, which has perpetuated her name. Her ladyship, after
the above date, brought Lord Yester two sons, the elder of whom
earned on the line of the family, and was the first Earl of
Tweeddale.
209 Patrick Anderson’s Hist. MS. Genealogy of the Hays of
Tweeddale.
210 Thrown down.
211 For the ballad of Kinmont Willie, and many particulars of the
affair, see Minstrelsy of the Scottish Border.
212 Napier’s Life of Napier, 4to, p. 247.
213 Wood’s Peerage, quoting Urquhart.
214 Cockalane—Fr. coq-à-l’âne, defined in the dictionary of the
Academy, ‘Discours qui n’a point de suite, de liaison, de raison.’
Equivalent to the English phrase, a cock-and-bull story. The word
occurs in at least one English author—Etheridge.
215 Through his connection with the Lovat family, his wife being
the mother of the present Lord Lovat, he was sheltered for some
time in a small island in the lake of Bruiach, a few miles from
Beaufort Castle.—Anderson’s Hist. Acc. Fraser Family, p. 90, note.
216 Spottiswoode, iii. 40. Johnston’s Hist. Scot. MS. Scott’s
Staggering State of Scots Statesmen.
217 He was put to the horn, and an edict of Privy Council
denounced those who should ‘reset’ him.
218 Letter of Sir Thomas Hamilton, king’s advocate, Pitcairn, iii.
162.
219 History of the Kennedies, 27.
220 Letter above cited.
221 The resemblance of this case to the phenomena of what is
called electro-biology will be apparent.
222 The original documents regarding these trials are given in full
in the Spalding Club Miscellany, vol. i. Aberdeen. 1841.
223 William Shakspere, a Biography. 1843.
224 Chronicle of the Cid, translated by Robert Southey, pp. 75-83.
225 Near Cramond Island.
226 Fr. Bon aller, an entertainment at the commencement of a
journey.
227 Genealogical Deduction of Kilravock Family, written in 1683-
4.
228 Letter of Thomas Mallison, Aberdeen, June 28, 1597.
Spalding Club Misc., ii. lx.
229 Mait. Club Misc., i. 89.
230 He held a privy-council on the 4th November, and
occasionally during the month till the 29th, at Dumfries.
231 Calderwood.
232 ‘... that fearful eclipse of the sun which continued the space
of two hours, so fearful that that Saturday is yet called by the
people the Black Saturday; a prognostic, as the times give
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like