Skip to content
AI-grafen
CBuilderLab· about 40 min· in the browser

Lab: mean, median and standard deviation by hand

Implement the three basic measures yourself, without libraries, and understand why the median withstands outliers.

Theory

Mean = sum/count. Median = the middle value after sorting (the mean of the two middle values for an even count). Standard deviation (population) = the square root of the mean of the squared deviations.

Sub-tasks

  1. medel (mean), median — Implement medel and median.
  2. std — Implement the population standard deviation std.

The starter code

runs in your browser
def medel(x):
    # TODO
    ...


def median(x):
    # TODO: sortera först; jämnt antal → medel av de två mittersta
    ...


def std(x):
    # TODO: populationsstandardavvikelse: sqrt(medel av (xi - medel)^2)
    ...

You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.

Try the diagnosticCreate a free account

Expected results

median([7,2,9,4,11]) → 7; median([1,2,3,4]) → 2.5; std([2,4,4,4,5,5,7,9]) → 2.0.

Common mistakes

  • Forgets to sort before the median.
  • Uses the sample variance (n−1) — here it should be n.
  • Mixes up variance and standard deviation (the square root is missing).