Skip to content

NumPy

The Python numerical-computing library that turns arrays of prices and scenarios into calculations, with explicit choices about shape, type and financial meaning.

NumPy is a Python library for numerical computing with multidimensional arrays. It supplies operations for arithmetic, reductions, linear algebra and random sampling, so a quant can express a calculation over many prices or scenarios in a few readable steps. Python is the language; NumPy is a separately installed package imported into a Python program. Its central numerical container is the ndarray, discussed in Array. NumPy's introduction describes the library's scope.

From prices to a result

Consider one fictional asset observed at three successive, equally spaced times, with prices of USD 100, 110 and 99 per share. Assume no distributions or corporate actions. The adjacent simple return is rt=Pt/Pt−1−1r_t=P_t/P_{t-1}-1.

import numpy as np prices = np.array([100.0, 110.0, 99.0], dtype=np.float64) returns = prices[1:] / prices[:-1] - 1 print(returns.shape, returns.dtype) # (2,) float64 print(np.round(returns, 4)) # [ 0.1 -0.1] print(f"Mean period return: {returns.mean():.1%}") # Mean period return: 0.0% print(f"Buy-and-hold return: {np.prod(1 + returns) - 1:.1%}") # Buy-and-hold return: -1.0%
Tested with Python 3.14.4 and NumPy 2.5.1. The two slices pair 110 with 100 and 99 with 110; division and subtraction act element by element. This is vectorisation: the numerical loop sits inside the library rather than appearing as a Python loop in the example. The quickstart develops these operations and reductions.
Three fictional prices in chronological order: USD 100, 110 and 99 per share. Adjacent ratios give plus 10 percent and minus 10 percent. Mean period return is zero percent, but compounding gives a minus 1 percent buy-and-hold return.
Three fictional prices in chronological order: USD 100, 110 and 99 per share. Adjacent ratios give plus 10 percent and minus 10 percent. Mean period return is zero percent, but compounding gives a minus 1 percent buy-and-hold return.
Original fictional example. Three prices produce two period returns. The arithmetic mean describes the average period; compounding describes the change in wealth.
The arithmetic mean is zero, but a share bought for USD 100 ends worth USD 99: P2/P0−1=−0.01P_2/P_0-1=-0.01. Choosing a library function is only half the work; choosing the quantity that answers the financial question matters just as much. np.round changes the displayed return values here; the subsequent calculations use the unrounded returns array.

What the library buys you

NumPy provides a common numerical interface across a research workflow: transform data, aggregate results and pass arrays into other scientific tools. A payoff calculation can apply np.maximum across simulated terminal prices; a portfolio calculation can combine a return matrix with weights using @; a risk calculation can reduce a scenario axis. None of these operations supplies the financial interpretation of an axis or verifies that asset order matches the weights.
For labelled observations and joins, pandas is often a useful next layer. SciPy supplies further scientific algorithms. NumPy remains a numerical building block within that wider toolkit. The official pandas overview and SciPy introduction describe those roles.

Four checks before trusting a calculation

  • Shape and meaning. shape gives axis lengths, not dates or instrument names. Three prices yielded two returns above. Broadcasting can combine different shapes under explicit compatibility rules; compatible shapes still need compatible meanings. Check observation order, alignment and missing-data policy before arithmetic. See the broadcasting rules.
  • Data type. An array has one dtype; numeric dtypes have finite range and precision. float64 supports approximate real-number calculations, not exact decimal accounting. Fixed-width integers can overflow. An object-dtype array holds references to Python objects and does not carry the same storage or performance assumptions as a plain numeric array. See data types and the ndarray reference.
  • Shared memory. Basic slicing can give a view sharing the original storage; changing that view can change the source. An array view need not be contiguous. Use an explicit copy when independent numerical storage is needed, and account for that allocation. The copies and views guide explains the distinction.
  • Actual cost. Compiled numeric operations often reduce Python overhead, but speed depends on size, dtype, layout and temporary allocations. Vectorisation is not a universal speed guarantee; broadcasting can create a large result. np.vectorize is a convenience wrapper whose implementation is essentially a loop, rather than a way to compile an arbitrary Python function. See the vectorize reference.
For a guided next step, the owned NumPy lesson develops shape, slicing, weighted returns and broadcasting. The public page is a preview; the full Scientific Python lesson requires membership.

Sources

Last updated
  • python
  • numpy
  • numerical computing
  • vectorisation
  • returns
← Back to the glossaryBack to top ↑