TExcelTAD
Home › Julia › Julia Statistics

Julia Statistics

The Statistics standard library module provides the basic descriptive statistics: mean, median, variance, standard deviation, quantiles, correlation and covariance. More advanced tools (distributions, hypothesis tests, regression) live in packages such as StatsBase, Distributions and GLM.

julia · try it yourself
using Statistics

scores = [72, 85, 90, 66, 85, 94, 78]
println("mean:   ", round(mean(scores), digits=2))
println("median: ", median(scores))
println("var:    ", round(var(scores), digits=2))
println("std:    ", round(std(scores), digits=2))
println("range:  ", extrema(scores))
mean:   81.43
median: 85.0
var:    99.29
std:    9.96
range:  (66, 94)

sum, maximum, minimum and extrema are in Base; mean, median, var, std and friends need using Statistics.

Sample vs population

var and std compute the sample variance by default, dividing by n − 1. Pass corrected=false for the population version (dividing by n):

julia · try it yourself
using Statistics

x = [2, 4, 4, 4, 5, 5, 7, 9]
println(var(x), " ", std(x))
println(var(x, corrected=false), " ", std(x, corrected=false))
m = mean(x)
println(sum((x .- m) .^ 2) / length(x))
4.571428571428571 2.138089935299395
4.0 2.0
4.0

Quantiles

julia · try it yourself
using Statistics

times = [12.1, 15.4, 9.8, 22.0, 14.3, 18.7, 11.2, 13.5]
println(quantile(times, 0.5))
println(quantile(times, [0.25, 0.5, 0.75]))
q1, q3 = quantile(times, [0.25, 0.75])
println("IQR = ", round(q3 - q1, digits=3))
println(median(times) == quantile(times, 0.5))
13.9
[11.875, 13.9, 16.225]
IQR = 4.35
true

Working with matrices: the dims keyword

For a matrix, statistics are computed over all elements unless you pass dims: dims=1 gives one value per column, dims=2 one per row.

julia · try it yourself
using Statistics

# rows = students, columns = tests
marks = [70 80 90
         60 75 85
         95 85 75]
println(mean(marks))
println(mean(marks, dims=1))            # per test
println(vec(mean(marks, dims=2)))       # per student
println(round.(std(marks, dims=1), digits=2))
79.44444444444444
[75.0 80.0 83.33333333333333]
[80.0, 73.33333333333333, 85.0]
[18.03 5.0 7.64]

Correlation and covariance

julia · try it yourself
using Statistics

hours = [1, 2, 3, 4, 5, 6]
score = [52, 55, 61, 64, 70, 74]
println(round(cor(hours, score), digits=4))
println(round(cov(hours, score), digits=4))

# Simple linear regression from the formulas
b = cov(hours, score) / var(hours)
a = mean(score) - b * mean(hours)
println("score ≈ ", round(a, digits=2), " + ", round(b, digits=2), " × hours")
0.9962
15.8
score ≈ 46.87 + 4.51 × hours

cor of a matrix returns the correlation matrix between its columns.

Standardising data

julia · try it yourself
using Statistics

heights = [160.0, 172.0, 181.0, 168.0, 174.0]
z = (heights .- mean(heights)) ./ std(heights)
println(round.(z, digits=3))
println(round(mean(z), digits=10), " ", round(std(z), digits=10))
println(round.(middle(heights)), " ", mean(skipmissing([1, missing, 3])))
[-1.42, 0.129, 1.291, -0.387, 0.387]
0.0 1.0
170.0 2.0

skipmissing lets statistics ignore missing values in real-world data; middle is the midpoint of the minimum and maximum.

A quick summary function

julia · try it yourself
using Statistics, Printf

function summarize(name, v)
    @printf("%-8s n=%d  mean=%.2f  sd=%.2f  min=%.1f  median=%.1f  max=%.1f\n",
            name, length(v), mean(v), std(v), minimum(v), median(v), maximum(v))
end

summarize("north", [12.0, 15.5, 11.0, 14.0])
summarize("south", [22.5, 19.0, 24.0, 21.5, 20.0])
north    n=4  mean=13.12  sd=2.02  min=11.0  median=13.0  max=15.5
south    n=5  mean=21.40  sd=1.98  min=19.0  median=21.5  max=24.0

Test yourself

Which module must you load to use mean?

  1. Base
  2. Statistics
  3. LinearAlgebra
  4. Math

What does std(x) divide by by default?

  1. n
  2. n - 1
  3. n + 1
  4. sqrt(n)

For a matrix M, what does mean(M, dims=1) return?

  1. One mean per row
  2. One mean per column
  3. The overall mean
  4. The first element