🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Statistics from Scratch

Statistics from scratch is the practice of computing summary values directly from fixed data. Each value below comes from the numbers themselves, with no hidden step.

The mean of four numbers is 5.0

The list holds 2.0, 4.0, 6.0, and 8.0. The mean balances them at the middle value.

using Statistics
data = [2.0, 4.0, 6.0, 8.0]
println(mean(data))

Spread grows with the gaps from the mean

The same list spreads around 5.0. Variance and standard deviation turn that spread into numbers.

using Statistics
data = [2.0, 4.0, 6.0, 8.0]
println(round(var(data), digits=6))
println(round(std(data), digits=6))

Two lists in perfect step correlate at 1.0

The second list is exactly twice the first list. Their correlation is the highest possible value.

using Statistics
x = [1.0, 2.0, 3.0, 4.0]
y = [2.0, 4.0, 6.0, 8.0]
println(cor(x, y))

Example

using Statistics
data = [3.0, 5.0, 7.0, 9.0, 11.0]
println(mean(data))
println(var(data))
println(round(std(data), digits=6))

Key points

  • Mean: the mean is the sum of the numbers divided by how many there are.
  • Variance: the variance measures how far numbers spread from the mean.
  • Standard deviation: the standard deviation is the square root of the variance.
  • Correlation: the correlation measures how closely two lists move together.
  • Range of correlation: a correlation of 1.0 means two lists move in perfect step.

In this example, a five-number list is summarized in three lines. The first line prints the mean. The second line prints the variance. The third line prints the standard deviation, rounded to six digits.

Note

the Statistics functions here work on plain lists of numbers.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

What is the mean of 2.0, 4.0, 6.0, and 8.0?