A portfolio may have hundreds of assets and thousands of scenarios. NumPy applies numerical operations to those collections as arrays. This lesson teaches shape, slicing, weighted returns and broadcasting through small examples. Match asset order before calculating: a correct shape alone does not guarantee a correct portfolio dot product. pandas adds labels to help preserve that meaning.
Learning objectives. By the end of this note you should be able to:
- construct and inspect a numerical array;
- select rows and columns;
- replace a scalar loop with vectorised arithmetic;
- compute a weighted portfolio return.
In this lesson
- Arrays carry shape and type
- Vectorised calculations
- Broadcasting
- Broadcasting scales columns
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or browse the Python and Data Primer syllabus.