Basic Prerequisites: Quantitative Research and Numerical Methods. Comfort with Python syntax; no performance background required. (these are the folder-level prerequisites for pages 02–06; page 01 states its own, smaller, entry requirements)


1. Intuition & Practical Objective

The Python quant stack is the research-and-prototyping layer that front-ends the compiled core (High-Performance C++ for Trading). It is where alpha is born: a researcher loads ticks into a DataFrame, expresses a signal as a NumPy expression, and backtests it - all in a morning. The stack’s entire design thesis is one sentence: make the researcher’s time cheap by pushing the computer’s work into C-level loops you don’t write yourself.

This folder is a hub. It (a) gives the stack lookup table below (job #1 of this pillar: know which tool does which job and where the traps are), and (b) routes you to six sub-pages that walk from raw intuition through vectorization, the pandas pitfalls, JIT acceleration, failure modes, and the modern alternatives.

The one-sentence essence. “Python gives you a thin scripting layer over two invisible C runtimes - NumPy’s ndarray (contiguous typed buffers) and pandas’ DataFrame (labelled, index-aligned) - and your only job is to express computations so those C runtimes do whole-array work in C, never element-by-element work in interpreted Python. Every performance rule here is a corollary of that sentence.”

The stack at a glance (job #1). Each tool is an answer to a specific pain, and each has a known trap:

ToolJob in the stackThe trap to remember
NumPyContiguous typed arrays + whole-array math, broadcasting, linear algebraFancy indexing copies; slices are views; dtype overflows silently (int32)
pandasLabelled, index-aligned tabular data & time-seriesChained assignment loses writes; object dtype kills speed; apply re-enters Python
numbaJIT-compiles a Python for loop to machine code at first callFirst call pays a compile cost; the GIL means threads don’t help CPU-bound Python
vectorbtNumpy/numba-accelerated vectorized backtesting engineVectorized = memory-heavy; huge grids blow RAM (see 06 · Advanced Extensions)
polars / ArrowColumnar, SIMD, lazy, copy-free modern alternativeStreaming/lazy needs materialization at .collect(); not a drop-in for every pandas idiom
GILThe CPython global interpreter lockOne Python thread runs at a time for CPU-bound code → use processes or release it via C/numpy/numba

2. Mathematical Ground Truth & Lookups

Why vectorization wins - the speedup model. An elementwise operation on an array of length costs, per element, either interpreted Python overhead (a few hundred nanoseconds: bytecode dispatch, boxing, attribute lookups) or C-loop overhead (a few nanoseconds). The theoretical vectorization speedup for a homogeneous array operation is

where is the one-time dispatch cost (negligible for large ). Because is typically 50–150×, a well-vectorized expression is 1–2 orders of magnitude faster than the equivalent Python loop - confirmed by the verified runs below.

Vectorization for backtests (the financial payoff). A vectorized backtest treats the whole price path as an array and the strategy as a sequence of whole-array transforms: returns , a signal computed by rolling window, positions , then equity . Computing the compounded equity from position-adjusted returns :

which is a single np.log1p + cumsum + exp chain - a handful of C-level passes over the data instead of a bar-by-bar Python loop run in the interpreter.

The lookup numbers (each reproduced by a verified script in §3 / the sub-pages):

QuantityValue (verified)
Python loop vs NumPy elementwise (5M elems)82.8× faster (620 ms → 7.5 ms)
Python double-loop vs NumPy broadcasting (1.2M cells)122× faster (0.28 s → 0.002 s)
Python loop vs numba @njit (2M dot)165.7× faster; numba within ~2–3× of numpy (np.dot 0.69 ms vs njit 1.96 ms)
GIL: two threads on CPU-bound workspeedup ≈ 1.0× (serialized) - vs 1.9× for two processes
pandas chained assignment df[mask]["col"]=vwrite silently lost (SettingWithCopyWarning), value stays unchanged
pandas object vs float64 column arithmetic47× slower (0.08 ms → 3.93 ms); ~4× the memory
pandas groupby+apply (Python) vs native .mean()3.0× slower
polars vs pandas groupby-sum (5M rows)polars 9.1× faster (5.1 ms vs 45.1 ms)
int32 overflow 2e9+2e9silently −294,967,296

Critical caveat. These are measured-on-this-box magnitudes, not portable guarantees; relative ratios (loop vs vectorized, threads vs processes) are robust, absolute times vary with hardware, NumPy/Pandas version, and machine load. Always re-measure before quoting a number in a research note.


3. Computational Implementation - the vectorization & stack model

This hub ships one self-contained, stdlib+numpy benchmark that ties the whole folder together: the loop-vs-vectorized speedup that motivates the entire stack, reproduced exactly.

The same ~two-orders-of-magnitude gap is why the stack exists: you cannot hand-write the research in interpreted Python, but you can write NumPy expressions that run the same math in C. Everything else in the folder is about making that gap work reliably (NumPy), comfortably on tabular/time-series data (pandas), faster when you must loop (numba), and at industrial scale (polars/Arrow) - and about the traps that silently destroy the gains.


4. Failure Modes & First-Principles Breakdowns

Hub signposts - the folder’s fault analysis lives in 05 · Failure Modes & Practice. In one line each:

  1. Hidden copies - fancy-indexing a NumPy array, or slicing without .loc in pandas, returns a copy; writes to it vanish. “The column I updated is still zero” is almost always this.
  2. dtype traps - object columns, integer overflow (int32), and float-cents arithmetic quietly corrupt results at 8× the cost.
  3. The GIL - CPU-bound pure-Python code cannot use threads; only processes (or releasing the GIL via NumPy/numba) scale past one core.
  4. apply and elementwise loops - any place you re-enter interpreted Python per element throws away the C-level speedup that is the whole point of the stack.

5. References

  • McKinney, Wes: Python for Data Analysis (3rd ed., O’Reilly, 2022)
  • Gorelick & Ozsvald: High Performance Python (2nd ed., O’Reilly, 2020)
  • Hilpisch, Yves: Python for Finance (2nd ed., 2018) and Python for Algorithmic Trading (2020)
  • VectorBT: Vectorized Backtesting with VectorBT + official docs (vectorbt.dev)
  • QuantEcon - Numba chapter (quantecon.org)

6. Connected Graph Bridges

Beginner: start at 01 · Practitioner: start at 05