The previous post commented that although the digits in the decimal representation of π are not random, it is sometimes useful to think of them as random. Similarly, it is often useful to think of prime numbers as being randomly distributed.

If prime numbers were samples from a random variable, it would be natural to look into the mean and variance of that random variable. We can’t just compute the mean of all primes, but we can compute the mean and variance of all primes less than an upper bound *x*.

Let *M*(*x*) be the mean of all primes less than *x* and let *V*(*x*) be the corresponding variance. Then we have the following asymptotic results:

*M*(*x*) ~ *x* / 2

and

*V*(*x*) ~ *x*²/12.

We can investigate how well these limiting results fit for finite *x* with the following Python code.

from sympy import sieve def stats(x): s = 0 ss = 0 count = 0 for p in sieve.primerange(x): s += p ss += p**2 count += 1 mean = s / count variance = ss/count - mean**2 return (mean, variance)

So, for example, when *x* = 1,000 we get a mean of 453.14, a little less than the predicted value of 500. We get a variance of 88389.44, a bit more than the predicted value of 83333.33.

When *x* = 1,000,000 we get closer to values predicted by the limiting formula. We get a mean of 478,361, still less than the prediction of 500,000, but closer. And we get a variance of 85,742,831,604, still larger than the prediction 83,333,333,333, but again closer. (Closer here means the ratios are getting closer to 1; the absolute difference is actually getting larger.)

## Taylor’s law

Taylor’s law is named after ecologist Lionel Taylor (1924–2007) who proposed the law in 1961. Taylor observed that variance and mean are often approximately related by a power law independent of sample size, that is

*V*(*x*) ≈ *a* *M*(*x*)^{b}

independent of *x*.

Taylor’s law is an empirical observation in ecology, but it is a theorem when applied to the distribution of primes. According to the asymptotic results above, we have *a* = 1/3 and *b* = 2 in the limit as *x* goes to infinity. Let’s use the code above to look at the ratio

*V*(*x*) / *a* *M*(*x*)^{b}

for increasing values of *x*.

If we let *x* = 10^{k} for *k* = 1, 2, 3, …, 8 we get ratios

0.612, 1.392, 1.291, 1.207, 1.156, 1.124, 1.102, 1.087

which are slowly converging to 1.

## Related posts

- How long does it take to find large primes?
- Refinements to the prime number theorem
- Prime numbers, phone numbers, and the normal distribution

Reference: Joel E. Cohen. Statistics of Primes (and Probably Twin Primes) Satisfy Taylor’s Law from Ecology. The American Statistician, Vol. 70, No. 4 (November 2016), pp. 399–404

Once again, the heuristic ‘every number is prime’ comes out with the right answers.