ZetCode

Julia Statistics Tutorial

last modified November 18, 2026

The Statistics standard library contains the summary functions needed to describe a set of numbers: averages, spread, percentiles, and the relationship between two variables. This tutorial works through them with small fixed vectors so that every result can be checked by hand.

A population is the whole group we care about, while a sample is the part we actually measured. Julia's variance and covariance functions assume a sample by default. All examples are standalone main.jl files, verified with Julia 1.13.4.

Mean and Median

The arithmetic mean is the sum of the values divided by their count. The median is the middle value after sorting; it is less sensitive to extreme observations.

main.jl
using Statistics

data = [4, 7, 2, 9, 3]
println(mean(data))
println(median(data))

The five values add up to 25, so the mean is 5.0. Sorted, they are [2, 3, 4, 7, 9] and the middle element is 4.0. Both functions return a floating-point value even though the input vector contains integers.

$ julia main.jl
5.0
4.0

Median of an Even-Sized Sample

When there is no single middle element, the median is the average of the two central values.

main.jl
using Statistics

println(median([1, 2, 3, 4]))

The two central values are 2 and 3, so their average is 2.5. This convention is why median returns a float for integer data.

$ julia main.jl
2.5

Sums and Products

Two reductions that live in Base rather than in the Statistics library are used constantly in summaries: sum adds values and prod multiplies them.

main.jl
data = [1, 2, 3, 4, 5]
println(sum(data))
println(prod(data))
println(sum(data; init=100))

The sum is 15 and the product is 120. The init keyword supplies a starting value, so the last line evaluates 100 plus the sum of the elements. This is also the value returned for an empty collection.

$ julia main.jl
15
120
115

Variance and Standard Deviation

Variance measures spread as the average squared distance from the mean. The standard deviation is its square root and is expressed in the same units as the data.

main.jl
using Statistics

data = [2, 4, 4, 4, 5, 5, 7, 9]
println(var(data))
println(var(data; corrected=false))
println(std(data))
println(std(data; corrected=false))

These eight numbers have a mean of 5 and a total squared deviation of 32. By default var applies Bessel's correction: it divides by n - 1, which is 7, to give an unbiased estimate of the population variance from a sample. With corrected=false it divides by n, giving 4.0. The std function takes the square root of the corresponding variance.

$ julia main.jl
4.571428571428571
4.0
2.138089935299395
2.0

Quantiles

A quantile is the value below which a given fraction of the data falls. The median is the quantile at 0.5; the quartiles are the quantiles at 0.25 and 0.75.

main.jl
using Statistics

data = [1, 2, 3, 4, 5]
println(quantile(data, 0.0))
println(quantile(data, 0.5))
println(quantile(data, 1.0))
println(quantile(data, [0.25, 0.75]))

The fractions 0.0 and 1.0 return the smallest and largest values. For the other fractions shown here the result coincides with an element of the vector; for probabilities between elements, quantile interpolates between neighbouring sorted values. Passing a vector of probabilities returns a vector of quantiles.

$ julia main.jl
1.0
3.0
5.0
[2.0, 4.0]

Covariance and Correlation

Covariance says whether two variables move together: positive when they rise in step, negative when one rises as the other falls. Correlation rescales covariance into the range from -1 to 1, making it comparable across data sets.

main.jl
using Statistics

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
println(cor(x, y))
println(cov(x, y))
println(cov(x, y; corrected=false))

Here y is exactly twice x, so the correlation is 1.0, the largest possible value. The covariance is 5.0 with Bessel's correction applied and 4.0 without it, just as for the variance.

$ julia main.jl
1.0
5.0
4.0

Negative Correlation

A perfectly decreasing relationship produces the opposite extreme.

main.jl
using Statistics

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
println(cor(x, y))
println(cov(x, y))

The correlation is -1.0 and the corrected covariance is -2.5. A correlation near zero would indicate no linear relationship, even if the variables are related in some other way.

$ julia main.jl
-1.0
-2.5

Summarizing Matrix Columns and Rows

Many statistics functions accept a dims keyword to reduce an array along one axis rather than over the whole array.

main.jl
using Statistics

m = [1 2 3; 4 5 6]
println(mean(m))
println(mean(m; dims=1))
println(dropdims(mean(m; dims=2); dims=2))

Without dims the mean of all six entries is 3.5. With dims=1 we get one value per column: 2.5, 3.5, and 4.5. With dims=2 we get one value per row; dropdims removes the leftover length-one dimension so the result prints as an ordinary vector. Matrices and indexing are covered in the arrays and linear algebra tutorials.

$ julia main.jl
3.5
[2.5 3.5 4.5]
[2.0, 5.0]

Missing Values

Real data often contains gaps. Julia marks them with the special value missing, and skipmissing filters such entries out before a summary is computed.

main.jl
using Statistics

data = [1, missing, 3, 5]
println(mean(skipmissing(data)))
println(sum(skipmissing(data)))

The vector has three known values, 1, 3, and 5. Their mean is 3.0 and their sum is 9. Without skipmissing, mean would propagate the missing value instead of ignoring it.

$ julia main.jl
3.0
9

Best Practices for Statistics

A few points help keep summaries meaningful.

Source

Julia Statistics standard library documentation

Julia Missing Values documentation

In this article we have used the Statistics standard library to compute means, medians, variances, standard deviations, quantiles, covariance, and correlation, together with sums, products, and summaries of matrix dimensions. Data frames and their column operations are covered in Julia dataframes; numeric functions such as rounding are described in Julia math, and arrays in Julia arrays.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.