Misplaced Pages

Spectral test

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Three-dimensional plot of 100,000 values generated with RANDU. Each point represents 3 consecutive pseudorandom values. It is clearly seen that the points fall in 15 two-dimensional planes.

The spectral test is a statistical test for the quality of a class of pseudorandom number generators (PRNGs), the linear congruential generators (LCGs). LCGs have a property that when plotted in 2 or more dimensions, lines or hyperplanes will form, on which all possible outputs can be found. The spectral test compares the distance between these planes; the further apart they are, the worse the generator is. As this test is devised to study the lattice structures of LCGs, it can not be applied to other families of PRNGs.

According to Donald Knuth, this is by far the most powerful test known, because it can fail LCGs which pass most statistical tests. The IBM subroutine RANDU LCG fails in this test for 3 dimensions and above.

Let the PRNG generate a sequence u 1 , u 2 , {\displaystyle u_{1},u_{2},\dots } . Let 1 / ν t {\displaystyle 1/\nu _{t}} be the maximal separation between covering parallel planes of the sequence { ( u n + 1 : n + t ) n = 0 , 1 , } {\displaystyle \{(u_{n+1:n+t})\mid n=0,1,\dots \}} . The spectral test checks that the sequence ν 2 , ν 3 , ν 4 , {\displaystyle \nu _{2},\nu _{3},\nu _{4},\dots } does not decay too quickly.

Knuth recommends checking that each of the following 5 numbers is larger than 0.01. μ 2 = π ν 2 2 / m , μ 3 = 4 3 π ν 3 3 / m , μ 4 = 1 2 π 2 ν 4 4 / m , μ 5 = 8 15 π 2 ν 5 5 / m , μ 6 = 1 6 π 3 ν 6 6 / m , {\displaystyle {\begin{aligned}\mu _{2}&=\pi \nu _{2}^{2}/m,&\mu _{3}&={\frac {4}{3}}\pi \nu _{3}^{3}/m,&\mu _{4}&={\frac {1}{2}}\pi ^{2}\nu _{4}^{4}/m,\\&&\mu _{5}&={\frac {8}{15}}\pi ^{2}\nu _{5}^{5}/m,&\mu _{6}&={\frac {1}{6}}\pi ^{3}\nu _{6}^{6}/m,\end{aligned}}} where m {\displaystyle m} is the modulus of the LCG.

Figures of merit

Knuth defines a figure of merit, which describes how close the separation 1 / ν t {\displaystyle 1/\nu _{t}} is to the theoretical minimum. Under Steele & Vigna's re-notation, for a dimension d {\displaystyle d} , the figure f d {\displaystyle f_{d}} is defined as f d ( m , a ) = ν d / ( γ d 1 / 2 m d ) , {\displaystyle f_{d}(m,a)=\nu _{d}/\left(\gamma _{d}^{1/2}{\sqrt{m}}\right),} where a , m , ν d {\displaystyle a,m,\nu _{d}} are defined as before, and γ d {\displaystyle \gamma _{d}} is the Hermite constant of dimension d. γ d 1 / 2 m d {\displaystyle \gamma _{d}^{1/2}{\sqrt{m}}} is the smallest possible interplane separation.

L'Ecuyer 1991 further introduces two measures corresponding to the minimum of f d {\displaystyle f_{d}} across a number of dimensions. Again under re-notation, M d + ( m , a ) {\displaystyle {\mathcal {M}}_{d}^{+}(m,a)} is the minimum f d {\displaystyle f_{d}} for a LCG from dimensions 2 to d {\displaystyle d} , and M d ( m , a ) {\displaystyle {\mathcal {M}}_{d}^{*}(m,a)} is the same for a multiplicative congruential pseudorandom number generator (MCG), i.e. one where only multiplication is used, or c = 0 {\displaystyle c=0} . Steele & Vigna note that the f d {\displaystyle f_{d}} is calculated differently in these two cases, necessitating separate values. They further define a "harmonic" weighted average figure of merit, H d + ( m , a ) {\displaystyle {\mathcal {H}}_{d}^{+}(m,a)} (and H d ( m , a ) {\displaystyle {\mathcal {H}}_{d}^{*}(m,a)} ).

Examples

A small variant of the infamous RANDU, with x n + 1 = 65539 x n mod 2 29 {\displaystyle x_{n+1}=65539\,x_{n}{\bmod {2}}^{29}} has:

d 2 3 4 5 6 7 8
ν
d
536936458 118 116 116 116
μd 3.14 10 10 10 0.02
fd 0.520224 0.018902 0.084143 0.207185 0.368841 0.552205 0.578329

The aggregate figures of merit are: M 8 ( 65539 , 2 29 ) = 0.018902 {\displaystyle {\mathcal {M}}_{8}^{*}(65539,2^{29})=0.018902} , H 8 ( 65539 , 2 29 ) = 0.330886 {\displaystyle {\mathcal {H}}_{8}^{*}(65539,2^{29})=0.330886} .

George Marsaglia (1972) considers x n + 1 = 69069 x n mod 2 32 {\displaystyle x_{n+1}=69069\,x_{n}{\bmod {2}}^{32}} as "a candidate for the best of all multipliers" because it is easy to remember, and has particularly large spectral test numbers.

d 2 3 4 5 6 7 8
ν
d
4243209856 2072544 52804 6990 242
μd 3.10 2.91 3.20 5.01 0.017
fd 0.462490 0.313127 0.457183 0.552916 0.376706 0.496687 0.685247

The aggregate figures of merit are: M 8 ( 69069 , 2 32 ) = 0.313127 {\displaystyle {\mathcal {M}}_{8}^{*}(69069,2^{32})=0.313127} , H 8 ( 69069 , 2 32 ) = 0.449578 {\displaystyle {\mathcal {H}}_{8}^{*}(69069,2^{32})=0.449578} .

Steele & Vigna (2020) provide the multipliers with the highest aggregate figures of merit for many choices of m = 2 and a given bit-length of a. They also provide the individual f d {\displaystyle f_{d}} values and a software package for calculating these values. For example, they report that the best 17-bit a for m = 2 is:

  • For an LCG (c ≠ 0), 0x1dab5 (121525). M 8 + = 0.6403 {\displaystyle {\mathcal {M}}_{8}^{+}=0.6403} , H 8 + = 0.6588 {\displaystyle {\mathcal {H}}_{8}^{+}=0.6588} .
  • For an MCG (c = 0), 0x1e92d (125229). M 8 = 0.6623 {\displaystyle {\mathcal {M}}_{8}^{*}=0.6623} , H 8 = 0.7497 {\displaystyle {\mathcal {H}}_{8}^{*}=0.7497} .

Additional illustration

Despite the fact that both relations pass the Chi-squared test, the first LCG is less random than the second, as the range of values it can produce by the order it produces them in is less evenly distributed.

References

  1. ^ Calculated using software from Steele & Vigna (2020), program "mspect" (src/spect.cpp, multiplicative mode).
  2. Calculated from ν
    d reported by Marsaglia.
  1. Williams, K. B.; Dwyer, Jerry (1 Aug 1996), "Testing Random Number Generators, Part 2", Dr. Dobb's Journal, retrieved 26 Jan 2012.
  2. Marsaglia, George (September 1968). "Random Numbers Fall Mainly in the Planes" (PDF). PNAS. 61 (1): 25–28. Bibcode:1968PNAS...61...25M. doi:10.1073/pnas.61.1.25. PMC 285899. PMID 16591687.
  3. Jain, Raj. "Testing Random-Number Generators (Lecture)" (PDF). Washington University in St. Louis. Retrieved 2 December 2016.
  4. ^ Knuth, Donald E. (1981), "3.3.4: The Spectral Test", The Art of Computer Programming volume 2: Seminumerical algorithms (2nd ed.), Addison-Wesley.
  5. IBM, System/360 Scientific Subroutine Package, Version II, Programmer's Manual, H20-0205-1, 1967, p. 54.
  6. International Business Machines Corporation (1968). "IBM/360 Scientific Subroutine Package (360A-CM-03X) Version III" (PDF). Stan's Library. II. White Plains, NY: IBM Technical Publications Department: 77. doi:10.3247/SL2Soft08.001. Scientific Application Program H20-0205-3.
  7. ^ Steele, Guy L. Jr.; Vigna, Sebastiano (February 2022) . "Computationally easy, spectrally good multipliers for congruential pseudorandom number generators" (PDF). Software: Practice and Experience. 52 (2): 443–458. arXiv:2001.05304. doi:10.1002/spe.3030. Associated software and data at https://github.com/vigna/CPRNG.
  8. L'Ecuyer, Pierre (January 1999). "Tables of Linear Congruential Generators of Different Sizes and Good Lattice Structure" (PDF). Mathematics of Computation. 68 (225): 249–260. Bibcode:1999MaCom..68..249L. CiteSeerX 10.1.1.34.1024. doi:10.1090/S0025-5718-99-00996-5. Be sure to read the Errata as well.
  9. Marsaglia, GEORGE (1972-01-01), Zaremba, S. K. (ed.), "The Structure of Linear Congruential Sequences", Applications of Number Theory to Numerical Analysis, Academic Press, pp. 249–285, ISBN 978-0-12-775950-0, retrieved 2024-01-29

Further reading

Category: