Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 45 ยท Numerical Methods

Statistics and Random Numbers

Generate random numbers with rand, randn and rng, summarize data, measure correlation and covariance, and run a Monte Carlo estimate, with Python stand-ins.

Statistics asks what a pile of numbers looks like: where is the middle, how spread out is it, and do two measurements move together? Random numbers go the other way. They let you build artificial data, test an idea on thousands of made-up cases, or estimate a quantity you cannot compute by hand. MATLAB and Octave have a small, fast core for both jobs: rand, randn, randi, rng, mean, std, corrcoef, cov and histcounts.

MATLAB and Octave are not available in your browser here, so the MATLAB code is read-only. Every example has a runnable Python stand-in using NumPy and matplotlib that follows the same steps. One honest difference: a seeded generator produces different numbers in MATLAB, in Octave and in NumPy, so the exact digits you see in the stand-ins will not match a real MATLAB session. The statistics of the results (the mean near 50, a correlation near 0.9) will.

Random numbers in three flavors

rand Uniform values between 0 and 1.
randn Normal values, mean 0, spread 1.
randi Random whole numbers in a range.
1 / 2

Random numbers and seeding

rand(m, n) returns an m-by-n matrix of values drawn uniformly from the interval between 0 and 1. randn(m, n) draws from the standard normal distribution (the bell curve with mean 0 and standard deviation 1). randi(k, m, n) gives whole numbers from 1 to k. To get other normal distributions, scale and shift: 50 + 10 * randn(1000, 1) has mean about 50 and standard deviation about 10.

MATLAB
u = rand(1, 5)               % five uniform values in (0, 1)
z = randn(3)                 % 3-by-3 standard normal values
d = randi(6, 1, 10)          % ten dice rolls, whole numbers 1..6
x = 50 + 10 * randn(1000, 1) % 1000 values, mean ~50, std ~10

These numbers are only pseudo-random: a deterministic algorithm produces them, starting from a seed. If you set the seed first, you get the same sequence again, which is what you want for a report, a bug hunt or a test.

MATLAB
rng(42)                      % seed the generator
a = rand(1, 3);
rng(42)                      % same seed ...
b = rand(1, 3);              % ... same numbers
isequal(a, b)                % 1 (true)

rng('shuffle') seeds from the clock so every run differs. In MATLAB, rng controls rand, randn and randi together. Older Octave versions seeded with rand('seed', 42) and randn('state', 42); recent Octave versions accept rng as well, but check your version if a script must run in both.

Python Seeding gives repeatable randomness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Summarizing a sample

For a vector, mean, median, std, var, min and max return a single number. For a matrix they work down each column by default, so a data matrix with one variable per column is summarized in one call. Pass a dimension to change that: mean(A, 2) averages along each row. These were covered in Built-in Math and Statistics; here is the extra piece, the histogram.

histcounts(x, edges) counts how many values fall in each bin. Older code uses hist(x, nbins), which Octave still supports. histogram(x) in MATLAB draws the bars directly; in Octave you use hist(x) or bar with the counts.

MATLAB
x = 50 + 10 * randn(1000, 1);
mean(x), std(x), median(x)
edges = 20:10:80;
counts = histcounts(x, edges)    % how many values per 10-wide bin
histogram(x, 20)                 % MATLAB: draw the histogram
xlabel('value'), ylabel('count')

std divides by n - 1 by default (the sample standard deviation), and so does var. Pass 1 as the second argument to divide by n instead: std(x, 1). NumPy's std divides by n unless you write ddof=1, so the stand-ins below set ddof=1 to match MATLAB.

Python Histogram and summary of a normal sample

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Correlation and covariance

Covariance measures whether two variables rise and fall together; its size depends on the units. Correlation rescales covariance to a number between -1 and 1, so it can be compared: 1 means a perfect rising line, -1 a perfect falling line, and 0 means no straight-line relationship.

MATLAB
x = randn(200, 1);
y = 2*x + 0.5*randn(200, 1);   % y follows x, plus noise
R = corrcoef(x, y)             % 2-by-2 matrix, ones on the diagonal
r = R(1, 2)                    % the correlation, close to 0.97
C = cov(x, y)                  % 2-by-2 covariance matrix

corrcoef always returns a matrix; the number you want sits off the diagonal, in R(1, 2). Given a data matrix with one variable per column, corrcoef(A) and cov(A) return the whole table at once. Be careful with cov(x, y): current MATLAB returns the 2-by-2 covariance matrix, but some older releases of MATLAB and Octave treated the two inputs differently (Octave once returned a single cross-covariance number). Writing cov([x y]) with the variables as columns gives the matrix everywhere.

Correlation is not causation, and it only sees straight-line patterns: y = x.^2 on a symmetric range has a correlation near 0 even though y is completely determined by x. Always plot before you trust the number.

Python Correlation, covariance and a scatter plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Distributions beyond uniform and normal

Core MATLAB gives you rand, randn, randi and randperm (a random shuffle of 1:n). Everything else, such as normpdf, normcdf, binornd, poissrnd, ttest or fitdist, comes from the Statistics and Machine Learning Toolbox, a paid add-on. Octave's statistics package (installed with pkg install -forge statistics and activated with pkg load statistics) provides most of the same names, and some are in core Octave too. Check availability with exist('normpdf').

If you only have the core, you can build many distributions from rand by hand. The inverse transform trick turns uniform values into exponential ones: if u is uniform on (0, 1), then -log(u) / lambda is exponentially distributed with rate lambda. A coin flip is rand < 0.5; the sum of twelve uniform values minus 6 is approximately normal (the central limit theorem at work).

MATLAB
lambda = 0.5;
e = -log(rand(1e5, 1)) / lambda;   % exponential samples
mean(e)                            % close to 1/lambda = 2
flips = rand(1, 1000) < 0.5;       % logical coin flips
sum(flips)                         % about 500 heads

Monte Carlo: answering questions by sampling

A Monte Carlo method estimates a quantity by simulating many random trials and averaging. A classic example is estimating pi. Scatter random points in the unit square; the fraction that lands inside the quarter circle x^2 + y^2 < 1 approaches pi/4, because that is the ratio of the areas.

MATLAB
n = 1e6;
p = rand(n, 2);                    % n random points in the unit square
inside = sum(p(:,1).^2 + p(:,2).^2 < 1);
pi_est = 4 * inside / n            % about 3.14

Notice there is no loop: the points are one matrix and the test is one array expression (see Vectorization). The error shrinks like 1/sqrt(n), so a hundred times more points buys only ten times more accuracy. Monte Carlo is rarely the fastest way to a one-dimensional integral, but it wins in high dimensions and for systems too tangled for formulas.

Python Estimating pi with random points

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why call rng(42) at the top of a script?

    Pseudo-random numbers come from a deterministic algorithm. Fixing the seed gives the same sequence each run, which helps with reports and debugging.
  2. 2. How do you draw 1000 values with mean 50 and standard deviation 10?

    randn gives mean 0 and standard deviation 1. Multiplying by 10 scales the spread and adding 50 shifts the center.
  3. 3. What does corrcoef(x, y) return for two vectors?

    corrcoef returns the correlation matrix. The entry off the diagonal is the correlation between the two variables.
  4. 4. You estimate pi with 10,000 random points and want ten times the accuracy. Roughly how many points do you need?

    Monte Carlo error shrinks like 1/sqrt(n). Ten times less error needs a hundred times more samples.
  5. 5. Which functions come from the Statistics and Machine Learning Toolbox rather than core MATLAB?

    rand, randn, randi, randperm, mean and std are core. Distribution functions such as normpdf and tests such as ttest need the toolbox (or Octave's statistics package).

Summary

  • rand, randn and randi generate uniform, normal and integer random values; scale and shift to get other means and spreads.
  • rng(seed) makes a run repeatable; the same seed gives different digits in MATLAB, Octave and NumPy.
  • mean, std and var work down columns by default, and std/var divide by n - 1 unless told otherwise.
  • corrcoef returns a matrix, and correlation only measures straight-line relationships, so plot your data.
  • Distribution functions live in the Statistics Toolbox or Octave's statistics package; the core can build many from rand.
  • Monte Carlo estimates by averaging random trials, with error shrinking like 1/sqrt(n).

Next: Signals and the FFT.