Sutes of Tests DIEHARD TESTS (Marsagla, 985 See http://stat.fsu.edu/~geo/dehard.html NIST Test sute- 6 tests on the sequences of bts http://csrc.nst.gov/rng/ Test U0 Includes the above tests. http://www.ro.umontreal.ca/~lecuyer/
Numercal Recpes Provdes C or Fortran Code for a number of numercal routnes ncludng random number generators http:\\lb-www.lanl.gov/numercal/bookcpdf.html
Increasng perod of generatorshufflng
Eample of shufflng
Sums or dfferences mod(m (see Fshman Sect. 7.3
Generatng non-unform random varates.
The nverse transform
Inverse transform for a Contnuous Dstrbuton Suppose we wsh to generate a random varable from a dstrbuton wth probablty densty functon f ( /, 0 < < Then the c.d.f. s F( Inverse / 4, for 0 < <.. F ( U U so s U where U s U[0,] the requred generator.
Eample: Inverse transform for a dscrete dstrbuton Consder the dstrbuton wth probablty functon P[ ] /0, for,,3,4. Then the nverse transform method results n the generator F ( U 3 4 U <.. U <.3.3 U <.6.6 U
Graph of c.d.f
Eponental dstrbuton generator.
j Geometrc Dstrbuton Generator Geometrc( f ( The F ( Then, F(j - ( cumulatve solvng < U p p ( log( - U log( - p Dstrbut p F(j p, for dstrbut on functon s [ ], where [] for an nteger gves where on has,,... j denotes probablt y functon denotes such that the celng. nteger part.
Bnomal generators Bnomal(n,p, n small -nverse transform or use n I( U < p Bnomal(n,p, n large, p small nf{ ; + G > n}, where G acceptance-rejecton log(- U log(- p geometrc( p
Cauchy
What f ncrements n a tme seres returns have a Cauchy dstrbuton???? plot(0+cumsum(.00./tan(p*rand(,0000
Generators n Matlab and R(Splus
Acceptance Rejecton
Acceptance-rejecton (cont
Acceptance-rejecton Suppose a probablty densty f( has c.d.f. that s NOT easly nverted. We cannot use nverse transform. How do we use the above theorem to generate from f(? Generate a pont at random (unformly under the graph of f( and output the coordnate (how? Pck the pont at random unformly n some larger regon and use t only f t s under f(
Eample Generate U[a,b] Generate Y U[0,c] If Y<f(, then accept (.e. output otherwse generate new values of,y and contnue untl one s accepted.
Acceptance-Rejecton: Dscrete Random varables Want to generate such that P[ ] We are able easly to generatey wth P[ Y ] q. p Suppose there s some constant c such that c. q Method GenerateY, say Accept ths value (.e. put wth probablty Otherwse generate new Y. p. p / cq.
Eample: acceptance rejecton for dscrete random varables E.g. generate r.v. wth probablty functon P[ k p q 0.98. ] c k 00k,,,...,00 where Here q 9.3 for all 00 y<-celng(00*runf(00000; <-y[runf(00000</y]; hst(, freq FALSE, breaks 50, p k,...,00 and, p cq.
When to use acceptance-rejecton (dscrete or contnuous The c.d.f. F( s ether hard to obtan or hard to nvert. We know the p.d.f. f( We can fnd another p.d.f. g( such that It s easy (e.g. by nverse transform to generate from random varables wth p.d.f. g(. Ideally cg( close to f( (.e. c close to f all ( cg ( for and some c.
. 3 45 where ( ( have we 0, ( gven by p.d.f. that wth Notce,0 ( 6 45 ( from p.d.f. r.v. a Generate 4 / < < < < c cg f g f Eample
Effcency of Acceptance- Rejecton Algorthm Probablty that we accept on each teraton(area under f(/(area under cg( /c Probablty we need to generate n ponts before generatng one accepted n ( c c Ths s a geometrc dstrbuton wth probablty p/c Epected number of tmes through loop to generate one s c.
Eample. Gamma(3/, Dstrbuton Want to generate gamma random varable by acceptance rejecton. f ( Γ (3/ / e, > 0 Try domnatng wth an eponental dstrbuton g( λe λ, > 0. f ( cg( f c ma( f ( / g(. For effcent method, we may choose λ so that c ma( f ( / g( s small. The best value of λ /3.(gves an eponental wth the same mean, 3/, as ths gamma dstrbuton.
Bvarate normal p.d.f. f (, y ep( π + y
The Bo-Muller transformaton for normal random varables
The Bo-Muller transformaton for normal random varables (graph
Bo-Muller generator Generate R ln( U and Θ π U, whereu, U ndep U[0,]. Put R cos( Θ, Y R sn( Θ Then (, Y ndependent N(0,
Bo-Muller & lattce Structure Can use nverse transform to machne precson: P.J. Acklam (003 http://home.onlne.no/~pjacklam/notes/nvnorm/
. Generate. If 3. Let R Y Z Z Z Marsagla Polar Normal generator + Z RZ RZ + + Z Z Z, Z >, GO TO. ln( Z,, ndep. U[-,], Then (, Y are ndep N(0, + Z
Generatng Dependent Normal ( Put ndependent N(0,., Generate. where,, but wth correlaton(,,, Normal(, Suppose we want Y Y ρ ρ σ µ σ µ ρ ρ σ µ + + + < <
Obtan the sngular value decomposton of the covarance matr so that Σ AA' for some matr A Defne AY + µ wherey`s vector of ndependent N(0, Then s mvnorm( µ,aa' Σ rmvnorm <- functon(n, meanrep(0, nrow(sgma, sgmadag(length(mean{ f(nrow(sgma! ncol(sgma{stop("sgma must be a square matr" } f(length(mean! nrow(sgma{ stop("mean and sgma have nonconformng sze" } ev <- egen(sgma, sym TRUE$values f (!all(ev > -sqrt(.machne$double.eps * abs(ev[] warnng("sgma s numercally not postve defnte" sgsvd <- svd(sgma retval <- t(sgsvd$v %*% (t(sgsvd$u * sqrt(sgsvd$d retval <- matr(rnorm(n * ncol(sgma, nrow n %*% retval retval <- sweep(retval,, mean, "+" retval} (R code from http://lb.stat.cmu.edu/r/cran/
The lognormal dstrbuton
Composton: for mtures of dstrbutons. Suppose a c.d.f. takes the form n j ( for and wth c.d.f.'s > 0 wth (. Frst choose random I,,... n wth probabltes p p F p F and then generate from the c.d.f. F p I (.
Eample: Composton 4 / / 4 4 3 set 0.4, If., Generate (, ( 0.6 0.4 (,0.4 0.8 ( U else U U U U F F F f < + < < +
λ Posson(. Use Posson process; for small nf{ n; n + U < e λ or nverse transform } λ For large use acceptance-rejecton λ
Mean squared error for medan of Cauchy n R n <- 0 nsm <- 50000 t<-proc.tme([] thetahat <- double(nsm for ( n :nsm { <- rcauchy(n thetahat[] <- medan( } mean(thetahat^ cat("calculaton took", (proc.tme([]-t, "seconds.\n" hst(thetahat, freq FALSE, breaks 00 curve(dnorm(, sd sqrt(mean(thetahat^, add TRUE curve(dnorm(, sd sqrt( / (4 * n * dcauchy(0^, add TRUE, col "red" 50000 smulatons of a medan from a Cauchy sample of sze 0. The red curve s the normal appromaton wth se estmated usng the cauchy densty; the black curve uses the standard devaton of the estmates.
Gamma( dstrbuton 0 0, 0,, ( ( / > > Γ β α β α α β α e f β α, freedom of wth n degrees - squared ch called s Gamma,, / f and Ep(/ s then, If var(, (, Gam( s., Gam s then, are ndependen t Gam( If β α β α β α β α β α β α β α n E c c ( n n : Propertes
Some gamma p.d.f.
Gamma tmes Beta
Gamma dstrbuton generators (cont when α >, agan use acceptance rejecton (domnate wth a functon lke g( cp ( c + p p for a sutable constant c or f α small (e.g..6 add ndependen t Gamma(0.6, + Gamma(,
Gamma dstrbuton generators. when α β α s ln( U a small,. When α <, β, use acceptance - rejecton wth eα g( e + α e e + α and c eαγ( α α U,, nteger are U[0,] <
Beta Dstrbuton for a and b small
Logstc Ft to Normal f logt vs. Probt b e ( + e ( a/ b ( ( a/ b
Symmetrc stable laws (specal cases. Normal dstrbuton (alpha. Cauchy dstrbuton (alpha 3. For most normal-lke data,.5<alpha< Lke the normal dstrbuton, the sum of ndependent stable(alpha random varables s stable(alpha.
Symmetrc Stable dstrbutons f c ( παc k 0 ( k k Γ( α + (k! ( y c k In the case c generate usng
Symmetrc stable random walk, alpha.7
Smulatng the Law of large numbers. z<-:0000 <-cumsum(rnorm(0000,0,/z; <-cumsum(rstable(0000,.7/z; # must defne rstable 3<-cumsum(rstable(0000,.3/z; 4<-cumsum(rt(0000,/z; plot(z,, l,col red lnes(z,,col green lnes(z,3,col blue lnes(z,4, col black"