Course contents
Fast math on arrays
NumPy gives Python the fast numerical array, letting you do arithmetic on a whole series of prices at once, vectorised, instead of looping. It is the engine under pandas and the right tool for number-heavy work.
- Create NumPy arrays and do vectorised arithmetic
- Compute returns on an array of prices without a loop
- Use basic array functions like mean and standard deviation
Back in the loops chapter you computed daily returns by stepping through prices one at a time. That works, but for number-heavy work there is a better way, and it is what professionals actually use. NumPy lets you do arithmetic on a whole array of numbers at once, an approach called vectorisation, which is both faster to run and far cleaner to read. It is also the engine underneath pandas, so understanding it makes the next chapter easier.
Arithmetic on a whole array
A NumPy array looks like a list but behaves like a single mathematical object: multiply it, subtract it, or divide it, and the operation applies to every element at once, with no loop. This program computes the daily returns of a week of prices in a single expression.
# NumPy does arithmetic on a whole array at once, no loop needed.
import numpy as np
closes = np.array([1380, 1402, 1395, 1410, 1425], dtype=float)
returns = (closes[1:] - closes[:-1]) / closes[:-1] * 100 # every day at once
print("Daily returns (%):", np.round(returns, 2))
print("Average:", round(returns.mean(), 3), "%")
print("Volatility (std):", round(returns.std(), 3), "%")Daily returns (%): [ 1.59 -0.5 1.08 1.06] Average: 0.809 % Volatility (std): 0.785 %
The key line computes the returns for the whole array at once: (closes[1:] - closes[:-1]) / closes[:-1] * 100 subtracts each price from the next and divides, across the entire series, giving the four daily returns [1.59, -0.5, 1.08, 1.06], exactly the numbers the loop produced earlier but without any loop. Then returns.mean() gives the average daily return, about 0.809%, and returns.std() gives the standard deviation of those returns, about 0.785%, which you will soon recognise as a measure of volatility. Two array slices and a division did what a loop did before, in one readable line.
Why vectorisation matters
Two things make this worth the new tool. The first is speed: NumPy performs these operations in fast, low-level code, so computing returns across ten years of prices for hundreds of stocks is quick, where a plain Python loop would crawl. The second, which matters even more day to day, is clarity: (closes[1:] - closes[:-1]) / closes[:-1] reads as a single mathematical idea, the return series, rather than a loop whose intent you have to reconstruct line by line. As your calculations grow, vectorised code stays legible while looped code sprawls.
NumPy arrays also come with a large kit of ready functions, mean, std, min, max, sum, cumsum, and many more, each applying to the whole array at once. You will rarely use NumPy entirely on its own, because pandas, the next chapter, wraps it in a friendlier, labelled form. But pandas is built on NumPy, and the vectorised thinking you practised here, operating on whole series rather than one element at a time, is exactly how you will work with pandas too.
What to carry forward
NumPy's array applies arithmetic to a whole series at once, so a week, or a decade, of daily returns comes from a single vectorised expression rather than a loop, and it brings ready functions like mean and std. It is faster and clearer than looping and is the foundation pandas is built on, so the habit of thinking in whole series carries straight into the next chapter.
That next chapter meets pandas, which wraps NumPy's speed in labelled Series and DataFrames, the programmable spreadsheet at the heart of all your data work.