About the Author 13
Acknowledgements 15
1. Python for Fearful Beginners………………….17
1.1. Your New Python Flight Manual 17
1.2. Python for Quantitative People 21
1.3. Installing Python 25
1.3.1. Python, Officially 25
1.3.2. Python via Anaconda (recommended) 29
1.4. Using Python 31
1.4.1. Interactive Mode 31
1.4.2. Writing .py Codes 31
1.4.3. Integrated Developments Environments (IDEs) 32
PyCharm 32
PyDev in Eclipse 34
Spyder 37
Rodeo 38
Other IDEs 39
2. Fundamentals of Python………………………41
2.1. Introduction to Mathematics 41
2.1.1. Numbers, Arithmetic, and Logic 41
Integers, Floats, Comments 41
Computations Powered by Python 3.5 43
N-base Number Conversion 44
Strings 45
Booleans 46
If-Elif-If 46
Comparison and Assignment Operators 47
Precedence in Python Arithmetics 48
2.1.2. Import, i.e. "Beam me up, Scotty!" 49
2.1.3. Built-In Exceptions 51
2.1.4. math Module 55
2.1.5. Rounding and Precision 56
2.1.6. Precise Maths with decimal Module 58
2.1.7. Near-Zero Maths 62
2.1.8. fractions and Approximations of Numbers 64
2.1.9. Formatting Numbers for Output 66
2.2. Complex Numbers with cmath Module 71
2.2.1. Complex Algebra 71
2.2.2. Polar Form of z and De Moivre’s Theorem 73
2.2.3. Complex-valued Functions 75
References and Further Studies 77
2.3. Lists and Chain Reactions 79
2.3.1. Indexing 81
2.3.2. Constructing the Range 82
2.3.3. Reversed Order 83
2.3.4. Have a Slice of List 85
2.3.5. Nesting and Messing with List’s Elements 86
2.3.6. Maths and statistics with Lists 88
2.3.7. More Chain Reactions 94
2.3.8. Lists and Symbolical Computations with
sympy Module 98
2.3.9. List Functions and Methods 102
Further Reading 104
2.4. Randomness Built-In 105
2.4.1. From Chaos to Randomness Amongst the Order 105
2.4.2. True Randomness 106
2.4.3. Uniform Distribution and K-S Test 110
2.4.4. Basic Pseudo-Random Number Generator 114
Detecting Pseudo-Randomness with re and
collections Modules 115
2.4.5. Mersenne Prime Numbers 123
2.4.6. Randomness of random. Mersenne Twister. 126
Seed and Functions for Random Selection 129
Random Variables from Non-Random Distributions 131
2.4.7. urandom 132
References 133
Further Reading 133
2.5. Beyond the Lists 135
2.5.1. Protected by Tuples 135
Data Processing and Maths of Tuples 135
Methods and Membership 137
Tuple Unpacking 138
Named Tuples 139
2.5.2. Uniqueness of Sets 139
2.5.3. Dictionaries, i.e. Call Your Broker 141
2.6. Functions 145
2.6.1. Functions with a Single Argument 146
2.6.2. Multivariable Functions 147
References and Further Studies 149
3. Fundamentals of NumPy for Quants………….151
3.1. In the Matrix of NumPy 151
Note on matplotlib for NumPy 153
3.2. 1D Arrays 155
3.2.1. Types of NumPy Arrays 155
Conversion of Types 156
Verifying 1D Shape 156
More on Type Assignment 157
3.2.2. Indexing and Slicing 157
Basic Use of Boolean Arrays 158
3.2.3. Fundamentals of NaNs and Zeros 159
3.2.4. Independent Copy of NumPy Array 160
3.2.5. 1D Array Flattening and Clipping 161
3.2.6. 1D Special Arrays 163
Array—List—Array 164
3.2.7. Handling Infs 164
3.2.8. Linear and Logarithmic Slicing 165
3.2.9. Quasi-Cloning of Arrays 166
3.3. 2D Arrays 167
3.3.1. Making 2D Arrays Alive 167
3.3.2. Dependent and Independent Sub-Arrays 169
3.3.3. Conditional Scanning 170
3.3.4. Basic Engineering of Array Manipulation 172
3.4. Arrays of Randomness 177
3.4.1. Variables, Well Shook 177
Normal and Uniform 177
Randomness and Monte-Carlo Simulations 179
3.4.2. Randomness from Non-Random Distributions 183
3.5. Sample Statistics with scipy.stats Module 185
3.5.1. Downloading Stock Data from Yahoo! Finance 186
3.5.2. Distribution Fitting. PDF. CDF. 187
3.5.1. Finding Quantiles. Value-at-Risk. 189
3.6. 3D, 4D Arrays, and N-dimensional Space 193
3.6.1. Life in 3D 194
3.6.2. Embedding 2D Arrays in 4D, 5D, 6D 196
3.7. Essential Matrix and Linear Algebra 203
3.7.1. NumPy’s ufuncs: Acceleration Built-In 203
3.7.2. Mathematics of ufuncs 205
3.7.3. Algebraic Operations 208
Matrix Transpositions, Addition, Subtraction 208
Matrix Multiplications 209
@ Operator, Matrix Inverse, Multiple Linear Regression 210
Linear Equations 213
Eigenvectors and Principal Component Analysis (PCA)
for N-Asset Portfolio 215
3.8. Element-wise Analysis 223
3.8.1. Searching 223
3.8.2. Searching, Replacing, Filtering 225
3.8.3. Masking 227
3.8.4. Any, if Any, How Many, or All? 227
3.8.5. Measures of Central Tendency 229
Appendixes……………………………………….231
A. Recommended Style of Python Coding 231
B. Date and Time 232
C. Replace VBA with Python in Excel 232
D. Your Plan to Master Python in Six Months 233