ACCFIN5277 Chap.10 Return Statistics with NumPy Arrays
Return Statistics with NumPy Arrays
Return Statistics with NumPy Arrays joins the financial statistics block to the first programming sessions. A NumPy array stores a whole series of prices or returns as one object, so a mean, a standard deviation or a percentile is a single call applied to every element at once.
The course pairs that convenience with a discipline: leave the original data file untouched, load a copy, and write every transformation in code so the result can be rerun. The chapter treats an array as the coded form of the vectors in the linear algebra notes, where the expectation of a vector of observations equals the mean times a vector of ones.
It then works through one year of daily market returns from the course index file, reporting the centre, spread, extremes and percentiles, and shows how the choice between dividing by the sample size or by one less changes the standard deviation in the fourth significant figure.
Further sections cover seeded random draws for reproducible simulations, special matrices, stacking and reshaping, and the correlation between two closely related index series.
What this chapter covers
- 01
Arrays as coded vectors of observations
- 02
Mean, standard deviation, minimum, maximum and percentiles
- 03
Population versus sample divisor with ddof
- 04
Histograms and the shape of daily returns
- 05
Seeded random numbers for reproducible simulation
- 06
Special matrices, stacking, reshaping and padding
Worked example · free
Two standard deviations from five returns
- 1Mean = (0.01 - 0.02 + 0.005 + 0.03 - 0.01) / 5 = 0.003, or 0.30%.
- 1Squared deviations from 0.003 sum to 0.00148.
- 1Divide by 5 for 0.000296 and take the root: 1.7205%. This is what np.std gives by default.
- 2Divide by 4 for 0.00037 and take the root: 1.9235%. This is np.std with ddof=1, the sample statistic.
Key terms
- Array shape
- The tuple giving the length of each dimension of an array, such as (250,) for one series or (250, 2) for two.
- Random seed
- A starting value for the random generator that makes a simulated sequence repeat exactly on every run.
- Percentile
- The value below which a stated percentage of observations falls, returned in NumPy by np.percentile.
- Correlation coefficient
- A number between minus one and one measuring how closely two series move together, given by np.corrcoef.
Return Statistics with NumPy Arrays FAQ
Why load data into NumPy instead of working in a spreadsheet?
The course points out that code keeps the original file untouched, works on a copy in memory and records every step, so the full chain from input to output can be rerun. A spreadsheet edit is easy to make and hard to audit. In an assessment marked on numerical precision, being able to reproduce a figure exactly is worth more than speed.
What is the ddof argument and when do I need it?
It sets how many degrees of freedom are subtracted from the count in the variance denominator. The default of zero divides by the number of observations; ddof=1 divides by one less and gives the usual sample estimate. For 250 daily returns the difference is small but visible in the fourth significant figure, enough to matter under a tight tolerance.
Why does setting a seed matter for simulations?
Without a seed, every run draws fresh random numbers, so a simulated price path or portfolio result changes each time and cannot be checked. Fixing the seed makes the draws identical on every run, provided the calls happen in the same order. That lets you, a classmate or a marker reproduce the exact figures you report.
How do vstack, hstack and reshape differ?
Vertical stacking places arrays on top of each other and adds rows, horizontal stacking places them side by side and adds columns, and reshape rearranges the existing elements into new dimensions without changing their order. Reshape fills each row completely before starting the next, so check the result on a small labelled array before trusting it on data.
What does a histogram of daily returns tell me that the mean does not?
It shows the whole distribution: where most days cluster, how wide the typical range is, and whether rare extreme days sit far out in the tails. In the 2025 index data most days lie close to zero, yet a handful of early April sessions moved by more than three per cent, which a single average conceals entirely.
Assessment move
Rebuild the 2025 statistics from the course index file yourself, starting with the row count and the first and last dates, then the mean, both standard deviations and the percentiles. Recompute the mean by summing and dividing to confirm the call. Practise shape reasoning on tiny labelled arrays: predict the result of each reshape and stack on paper, then run it.
Finally, write a short seeded simulation and confirm it prints identical numbers on two separate runs. Keep every tested routine in one personal notebook so the portfolio work starts from code you already trust.
Working through Return Statistics with NumPy Arrays in ACCFIN5277? Sia is AskSia’s AI Finance tutor — ask any ACCFIN5277 Return Statistics with NumPy Arrays question and get a clear, step-by-step explanation grounded in how ACCFIN5277 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.