University of Glasgow · FACULTY OF FINANCE

ACCFIN5277 Chap.10 Return Statistics with NumPy Arrays

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 12 · ACCFIN5277

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.

In this chapter

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

Q [5 marks]. Five daily returns are 1%, -2%, 0.5%, 3% and -1%. Compute the mean and the standard deviation with divisor 5 and with divisor 4, and say which one np.std returns by default. The marks shown are an original practice weighting, not a university allocation.
The strip shows twelve values in their stored order, and the two grids show the same values after reshape(3, 4) and reshape(4, 3); the shading proves that NumPyThe strip shows twelve values in their stored order, and the two grids show the same values after reshape(3, 4) and reshape(4, 3); the shading proves that NumPy fills each row completely before moving to the next.112a, shape (12,)112reshape(3, 4)112reshape(4, 3)rows fill first
The strip shows twelve values in their stored order, and the two grids show the same values after reshape(3, 4) and reshape(4, 3); the shading proves that NumPy fills each row completely before moving to the next.
  • 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.
The mean is 0.30%. The standard deviation is 1.7205% with divisor 5, the NumPy default, and 1.9235% with divisor 4, the sample version requested by ddof=1.
Sia tip — With only five observations the two divisors differ by about 12%, so state ddof explicitly every time.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your ACCFIN5277 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
ACCFIN5277 · Finance Programming in Python and Databases - independent study guide on the AskSia Library. More University of Glasgow subjects · Microeconomics across all universities