Skip to content

Array

An indexed numerical container whose shape, data type and axis meanings determine how positions, scenarios and calculations fit together.

An array is a data structure that stores elements at indexed positions. In quantitative Python, the usual numerical implementation is a NumPy ndarray: its shape gives the length of each axis, and one data type, or dtype, describes the storage format of its elements. An array can hold positions for many scenarios, but it does not know which axis represents scenarios or which numbers are shares. That meaning belongs to the calculation's contract. NumPy's array reference describes the underlying container.

Read the axes before doing arithmetic

Consider these fictional share positions, with alternative scenarios in rows and assets in columns:
Asset AAsset B
Scenario 01020
Scenario 13040
The shape is (2, 2): two scenarios and two assets. There are two dimensions and four elements. NumPy indexes from zero, so positions[1, 0] selects Scenario 1's Asset A position: 30 shares.
import numpy as np positions = np.array([[10, 20], [30, 40]], dtype=np.int64) print(positions.shape, positions.ndim, positions.size) # (2, 2) 2 4 print(positions[1, 0]) # 30 print(positions.sum(axis=1).tolist()) # [30, 70] print(positions.sum(axis=0).tolist()) # [40, 60]
sum(axis=1) reduces the asset axis, leaving one number per scenario: 10 + 20 = 30 shares and 30 + 40 = 70 shares. These are total share counts across the two assets, not portfolio market values; valuing the holdings also requires the corresponding prices. The sum documentation specifies how the selected axis is removed.
sum(axis=0) instead adds across scenarios, giving 40 and 60. The arithmetic is valid, but adding positions from alternative scenarios does not describe a portfolio held in either scenario. A shape check cannot catch that mistake. Transposing this square array also leaves its shape at (2, 2) while exchanging the axes' meanings.
Fictional share positions: Scenario 0 holds 10 shares of Asset A and 20 of Asset B; Scenario 1 holds 30 and 40. Reducing asset axis 1 gives scenario share counts of 30 and 70.
Fictional share positions: Scenario 0 holds 10 shares of Asset A and 20 of Asset B; Scenario 1 holds 30 and 40. Reducing asset axis 1 gives scenario share counts of 30 and 70.
Original fictional example. Rows are alternative scenarios; columns are assets. Reducing axis 1 leaves one share count per scenario. Share counts are not market values.

What shape leaves out

A two-dimensional array can represent a mathematical matrix, but its operations still need to express the intended mathematics. For ordinary numeric ndarrays, * multiplies element by element; @ performs matrix multiplication. NumPy documents these as distinct operations. Broadcasting can make differently shaped operands compatible without checking whether their asset orders or units agree.
The example explicitly uses int64 for whole-share counts. The dtype specifies a numeric representation, not a business unit: it cannot distinguish shares from cents. Choose suitable types and ranges for the calculation; fixed-width integer arithmetic can overflow. Floating-point storage also introduces rounding error. See NumPy's dtype reference and the integer-overflow note in its sum reference.
Nor does a new array variable necessarily own new data. A basic slice such as positions[0, :] is a view into the original array. An explicit .copy() of this numeric slice creates independent storage:
import numpy as np positions = np.array([[10, 20], [30, 40]], dtype=np.int64) first = positions[0, :] separate = first.copy() first[0] = 11 print(positions.tolist()) # [[11, 20], [30, 40]] print(separate.tolist()) # [10, 20]
Changing the view changes the original Scenario 0 holding. The copy retains its earlier values. Array views can also be noncontiguous in memory, so “array” does not guarantee a contiguous layout. NumPy's copies and views guide explains the distinction.
Examples checked with Python 3.14.4 and NumPy 2.5.1. To continue into slicing, weighted returns and broadcasting, use the NumPy lesson: its preview is public; the full lesson requires membership.

Sources

Last updated
  • python
  • numpy
  • arrays
  • data types
  • axis semantics
← Back to the glossaryBack to top ↑