randtoolbox.test {randtoolbox}R Documentation

Tests for random number generation

Description

The Gap test, the Frequency test, the Serial test, the Poker test and the Marsaglia test.

Usage

gap.test(u, lower = 0, upper = 1/2, echo = TRUE)
freq.test(u, seq = 0:15, echo = TRUE)
serial.test(u , d = 8, echo = TRUE)
poker.test(u , d = 5, echo = TRUE)
mars.test(u, d = 3, echo = TRUE)

Arguments

u sample of random numbers in ]0,1[.
lower numeric for the lower bound, default 0.
upper numeric for the upper bound, default 1/2.
echo logical to plot detailed results, default TRUE
seq a vector of contiguous integers, default 0:15.
d a numeric for the dimension, see details.

Details

In all the tests, we consider a vector u, realisation of i.i.d. uniform random variables U1... Un.

The gap test works on the 'gap' variables defined as

1 if lower <= Ui <= upper, 0 otherwise.

Let p the probability that Gi equals to one. Then we compute the length of zero gaps and denote by nj the number of zero gaps of length j. The chi-squared statistic is given by

S = sum_{j=0}^m (n_j - n p_j)^2/[n p_j],

where pj stands for the probability the length of zero gaps equals to j ((1-p)^2 p^j ) and m the max number of lengths (at least ceiling( ( log( 10^(-3) ) - log( p ) ) / log( 1 - p ) ).

The frequency test works on a serie seq of ordered contiguous integers (s_1, ...,s_d), where s_j in Z. From the sample u, we compute observed integers as

d_i = floor( u_i * ( s_d + 1 ) + s_1 ),

(i.e. d_i are uniformely distributed in {s_1, ...,s_d}). The expected number of integers equals to j is m=n/(s_d - s_1+1). Finally, the chi-squared statistic is

S = sum_{j=1}^d (card(d_i=s_j) - m)^2/m .

The serial test computes a serie of integer pairs (p_i,p_{i+1}) from the sample u with p_i = floor(u_i d) (u must have an even length). Let n_j be the number of pairs such that j=p_i d + p_{i+1}. If d=2, we count the number of pairs equals to 00, 01, 10 and 11. Since all the combination of two elements in {0, ..., d-1} are equiprobable, the chi-squared statistic is

S = sum_{j=1}^d [n_j - n/(2 d^2)]^2/[n/(2 d^2)].

The poker test computes a serie of 'hands' in {0, ..., d-1} from the sample h_i = floor(u_i d) (u must have a length dividable by d). Let n_j be the number of 'hands' with (exactly) j different cards. The probability is

p_j = C_d^j * (d-1)! / (d^(j-1)*(d-j+1)!) * (j/d)^(d-j),

where C_d^j denotes d!/[j!(d-j)!]. Finally the chi-squared statistic is

S = sum_{j=1}^d [n_j - np_j/d ]^2/[np_j/d].

The Marsaglia test works on a sequence of triplets x,y,z of uniform i.i.d. random variables. The triplet is build from the vector u. The nomber of permutation among the components of a triplet is 3!=6, i.e. x<y<z, x<z<y, y<x<z, y<z<x, z<x<y and z<y<x. The Marsaglia test computes the empirical of the different permutations as well as the theoretical one n/6 where n is the number of triplets. Finally the chi-squared statistic is

S = sum_{j=1}^6 [n_j - n/6 ]^2/[n/6].

Value

a list with the following components :
statistic the value of the chi-squared statistic.
p.value the p-value of the test.
observed the observed counts.
expected the expected counts under the null hypothesis.
residuals the Pearson residuals, (observed - expected) / sqrt(expected).

Author(s)

Christophe Dutang.

References

Planchet F., Jacquemin J. (2003), L'utilisation de methodes de simulation en assurance. Bulletin Francais d'Actuariat, vol. 6, 11, 3-69. (available online)

See Also

ks.test for the Kolmogorov Smirnov test and acf for the autocorrelation function.

Examples

# (1) the gap test
#
gap.test(runif(1000))
print( gap.test( runif(1000000), echo=FALSE ) )

# (2) the frequency test
#
freq.test(runif(1000))
print( freq.test( runif(1000000), echo=FALSE) )

# (3) the serial test
#
serial.test(runif(1000))
print( serial.test( runif(1000000), d=2, e=FALSE) )

# (4) the poker test
#

#hands of 5 'cards'
poker.test(runif(50000))
#hands of 4 'cards'
poker.test(runif(40000), 4)

# (5) the Marsaglia test
#

# mersenne twister
mars.test(runif(6000))
# torus
mars.test(torus(6000))

[Package randtoolbox version 1.00 Index]