
R version 3.3.2 RC (2016-10-26 r71594) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ####	d|ensity
> ####	p|robability (cumulative)
> ####	q|uantile
> ####	r|andom number generation
> ####
> ####	Functions for  ``d/p/q/r''
> 
> .ptime <- proc.time()
> F <- FALSE
> T <- TRUE
> 
> options(warn = 2)
> ##      ======== No warnings, unless explicitly asserted via
> assertWarning <- tools::assertWarning
> 
> ###-- these are identical in ./arith-true.R ["fixme": use source(..)]
> opt.conformance <- 0
> Meps <- .Machine $ double.eps
> xMax <- .Machine $ double.xmax
> options(rErr.eps = 1e-30)
> rErr <- function(approx, true, eps = getOption("rErr.eps", 1e-30))
+ {
+     ifelse(Mod(true) >= eps,
+ 	   1 - approx / true, # relative error
+ 	   true - approx)     # absolute error (e.g. when true=0)
+ }
> ## Numerical equality: Here want "rel.error" almost always:
> All.eq <- function(x,y) {
+     all.equal.numeric(x,y, tolerance = 64*.Machine$double.eps,
+                       scale = max(0, mean(abs(x), na.rm=TRUE)))
+ }
> if(!interactive())
+     set.seed(123)
> 
> ## The prefixes of ALL the PDQ & R functions
> PDQRinteg <- c("binom", "geom", "hyper", "nbinom", "pois","signrank","wilcox")
> PDQR <- c(PDQRinteg, "beta", "cauchy", "chisq", "exp", "f", "gamma",
+ 	  "lnorm", "logis", "norm", "t","unif","weibull")
> PQonly <- c("tukey")
> 
> ###--- Discrete Distributions --- Consistency Checks  pZZ = cumsum(dZZ)
> 
> ##for(pre in PDQRinteg) { n <- paste("d",pre,sep=""); cat(n,": "); str(get(n))}
> 
> ##__ 1. Binomial __
> 
> ## Cumulative Binomial '==' Cumulative F :
> ## Abramowitz & Stegun, p.945-6;  26.5.24  AND	26.5.28 :
> n0 <- 50; n1 <- 16; n2 <- 20; n3 <- 8
> for(n in rbinom(n1, size = 2*n0, p = .4)) {
+     for(p in c(0,1,rbeta(n2, 2,4))) {
+ 	for(k in rbinom(n3, size = n,  prob = runif(1)))
+ 	    ## For X ~ Bin(n,p), compute 1 - P[X > k] = P[X <= k] in three ways:
+ 	    stopifnot(all.equal(       pbinom(0:k, size = n, prob = p),
+ 				cumsum(dbinom(0:k, size = n, prob = p))),
+ 		      all.equal(if(k==n || p==0) 1 else
+ 				pf((k+1)/(n-k)*(1-p)/p, df1=2*(n-k), df2=2*(k+1)),
+ 				sum(dbinom(0:k, size = n, prob = p))))
+     }
+ }
> 
> ##__ 2. Geometric __
> for(pr in seq(1e-10,1,len=15)) # p=0 is not a distribution
+     stopifnot(All.eq((dg <- dgeom(0:10, pr)),
+ 		     pr * (1-pr)^(0:10)),
+ 	      All.eq(cumsum(dg), pgeom(0:10, pr)))
> 
> 
> ##__ 3. Hypergeometric __
> 
> m <- 10; n <- 7
> for(k in 2:m) {
+     x <- 0:(k+1)
+     stopifnot(All.eq(phyper(x, m, n, k), cumsum(dhyper(x, m, n, k))))
+ }
> 
> ##__ 4. Negative Binomial __
> 
> ## PR #842
> for(size in seq(0.8,2, by=.1))
+     stopifnot(all.equal(cumsum(dnbinom(0:7, size, .5)),
+ 			       pnbinom(0:7, size, .5)))
> stopifnot(All.eq(pnbinom(c(1,3), .9, .5),
+ 		 c(0.777035760338812, 0.946945347071519)))
> 
> ##__ 5. Poisson __
> 
> stopifnot(dpois(0:5,0)		 == c(1, rep(0,5)),
+ 	  dpois(0:5,0, log=TRUE) == c(0, rep(-Inf, 5)))
> 
> ## Cumulative Poisson '==' Cumulative Chi^2 :
> ## Abramowitz & Stegun, p.941 :	 26.4.21 (26.4.2)
> n1 <- 20; n2 <- 16
> for(lambda in rexp(n1))
+     for(k in rpois(n2, lambda))
+ 	stopifnot(all.equal(1 - pchisq(2*lambda, 2*(1+ 0:k)),
+ 			    pp <- cumsum(dpois(0:k, lambda=lambda)),
+ 			    tolerance = 100*Meps),
+ 		  all.equal(pp, ppois(0:k, lambda=lambda), tolerance = 100*Meps),
+ 		  all.equal(1 - pp, ppois(0:k, lambda=lambda, lower.tail = FALSE)))
> 
> 
> ##__ 6. SignRank __
> for(n in rpois(32, lam=8)) {
+     x <- -1:(n + 4)
+     stopifnot(All.eq(psignrank(x, n), cumsum(dsignrank(x, n))))
+ }
> 
> ##__ 7. Wilcoxon (symmetry & cumulative) __
> is.sym <- TRUE
> for(n in rpois(5, lam=6))
+     for(m in rpois(15, lam=8)) {
+ 	x <- -1:(n*m + 1)
+ 	fx <- dwilcox(x, n, m)
+ 	Fx <- pwilcox(x, n, m)
+ 	is.sym <- is.sym & all(fx == dwilcox(x, m, n))
+ 	stopifnot(All.eq(Fx, cumsum(fx)))
+     }
> stopifnot(is.sym)
> 
> 
> ###-------- Continuous Distributions ----------
> 
> ##---  Gamma (incl. central chi^2) Density :
> x <- round(rgamma(100, shape = 2),2)
> for(sh in round(rlnorm(30),2)) {
+     Ga <- gamma(sh)
+     for(sig in round(rlnorm(30),2))
+ 	stopifnot(all.equal((d1 <- dgamma(	 x,   shape = sh, scale = sig)),
+                             (d2 <- dgamma(x/sig, shape = sh, scale = 1) / sig),
+                             tolerance = 1e-14)## __ad interim__ was 1e-15
+                   ,
+                   All.eq(d1, (d3 <- 1/(Ga * sig^sh) * x^(sh-1) * exp(-x/sig)))
+                   )
+ }
> 
> stopifnot(pgamma(1,Inf,scale=Inf) == 0)
> ## Also pgamma(Inf,Inf) == 1 for which NaN was slightly more appropriate
> assertWarning(stopifnot(
+     is.nan(c(pgamma(Inf,  1,scale=Inf),
+              pgamma(Inf,Inf,scale=Inf)))))
> scLrg <- c(2,100, 1e300*c(.1, 1,10,100), 1e307, xMax, Inf)
> stopifnot(pgamma(Inf, 1, scale=xMax) == 1,
+           pgamma(xMax,1, scale=Inf) == 0,
+           all.equal(pgamma(1e300, 2, scale= scLrg, log=TRUE),
+                     c(0, 0, -0.000499523968713701, -1.33089326820406,
+                       -5.36470502873211, -9.91015144019122,
+                       -32.9293385491433, -38.707517174609, -Inf),
+                     tolerance = 2e-15)
+           )
> 
> p <- 7e-4; df <- 0.9
> stopifnot(
+ abs(1-c(pchisq(qchisq(p, df),df)/p, # was 2.31e-8 for R <= 1.8.1
+         pchisq(qchisq(1-p, df,lower=FALSE),df,lower=FALSE)/(1-p),# was 1.618e-11
+         pchisq(qchisq(log(p), df,log=TRUE),df, log=TRUE)/log(p), # was 3.181e-9
+         pchisq(qchisq(log1p(-p),df,log=T,lower=F),df, log=T,lower=F)/log1p(-p)
+         )# 32b-i386: (2.2e-16, 0,0, 3.3e-16); Opteron: (2.2e-16, 0,0, 2.2e-15)
+     ) < 1e-14
+ )
> 
> ##-- non central Chi^2 :
> xB <- c(2000,1e6,1e50,Inf)
> for(df in c(0.1, 1, 10))
+     for(ncp in c(0, 1, 10, 100)) stopifnot(pchisq(xB, df=df, ncp=ncp) == 1)
> stopifnot(all.equal(qchisq(0.025,31,ncp=1,lower.tail=FALSE),# inf.loop PR#875
+                     49.7766246561514, tolerance = 1e-11))
> for(df in c(0.1, 0.5, 1.5, 4.7, 10, 20,50,100)) {
+     xx <- c(10^-(5:1), .9, 1.2, df + c(3,7,20,30,35,38))
+     pp <- pchisq(xx, df=df, ncp = 1) #print(pp)
+     dtol <- 1e-12 *(if(2 < df && df <= 50) 64 else if(df > 50) 20000 else 501)
+     stopifnot(all.equal(xx, qchisq(pp, df=df, ncp=1), tolerance = dtol))
+ }
> 
> ## p ~= 1 (<==> 1-p ~= 0) -- gave infinite loop in R <= 1.8.1 -- PR#6421
> psml <- 2^-(10:54)
> q0 <- qchisq(psml,    df=1.2, ncp=10, lower.tail=FALSE)
> q1 <- qchisq(1-psml, df=1.2, ncp=10) # inaccurate in the tail
> p0 <- pchisq(q0, df=1.2, ncp=10, lower.tail=FALSE)
> p1 <- pchisq(q1, df=1.2, ncp=10, lower.tail=FALSE)
> iO <- 1:30
> stopifnot(all.equal(q0[iO], q1[iO], tolerance = 1e-5),# 9.86e-8
+           all.equal(p0[iO], psml[iO])) # 1.07e-13
> 
> ##--- Beta (need more):
> 
> ## big a & b (PR #643)
> stopifnot(is.finite(a <- rlnorm(20, 5.5)), a > 0,
+           is.finite(b <- rlnorm(20, 6.5)), b > 0)
> pab <- expand.grid(seq(0,1,by=.1), a, b)
> p <- pab[,1]; a <- pab[,2]; b <- pab[,3]
> stopifnot(all.equal(dbeta(p,a,b),
+                     exp(pab <- dbeta(p,a,b, log = TRUE)), tolerance = 1e-11))
> sp <- sample(pab, 50)
> if(!interactive())
+ stopifnot(which(isI <- sp == -Inf) ==
+               c(3, 11, 15, 20, 22, 23, 30, 39, 42, 43, 46, 47, 49),
+           all.equal(range(sp[!isI]), c(-2906.123981, 2.197270387))
+           )
> 
> 
> ##--- Normal (& Lognormal) :
> 
> stopifnot(
+     qnorm(0) == -Inf, qnorm(-Inf, log = TRUE) == -Inf,
+     qnorm(1) ==  Inf, qnorm( 0,   log = TRUE) ==  Inf)
> 
> assertWarning(stopifnot(
+     is.nan(qnorm(1.1)),
+     is.nan(qnorm(-.1))))
> 
> x <- c(-Inf, -1e100, 1:6, 1e200, Inf)
> stopifnot(
+     dnorm(x,3,s=0) == c(0,0,0,0, Inf, 0,0,0,0,0),
+     pnorm(x,3,s=0) == c(0,0,0,0,  1 , 1,1,1,1,1),
+     dnorm(x,3,s=Inf) == 0,
+     pnorm(x,3,s=Inf) == c(0, rep(0.5, 8), 1))
> 
> ## 3 Test data from Wichura (1988) :
> stopifnot(
+     all.equal(qnorm(c( 0.25,  .001, 1e-20)),
+ 	      c(-0.6744897501960817, -3.090232306167814, -9.262340089798408),
+ 	      tolerance = 1e-15)
+   , ## extreme tail -- available on log scale only:
+     all.equal(qnorm(-1e5, log = TRUE), -447.1974945)
+ )
> 
> z <- rnorm(1000); all.equal(pnorm(z),  1 - pnorm(-z), tolerance = 1e-15)
[1] TRUE
> z <- c(-Inf,Inf,NA,NaN, rt(1000, df=2))
> z.ok <- z > -37.5 | !is.finite(z)
> for(df in 1:10) stopifnot(all.equal(pt(z, df), 1 - pt(-z,df), tolerance = 1e-15))
> 
> stopifnot(All.eq(pz <- pnorm(z), 1 - pnorm(z, lower=FALSE)),
+           All.eq(pz,		    pnorm(-z, lower=FALSE)),
+           All.eq(log(pz[z.ok]), pnorm(z[z.ok], log=TRUE)))
> y <- seq(-70,0, by = 10)
> cbind(y, "log(pnorm(y))"= log(pnorm(y)), "pnorm(y, log=T)"= pnorm(y, log=TRUE))
       y log(pnorm(y)) pnorm(y, log=T)
[1,] -70          -Inf   -2455.1676378
[2,] -60          -Inf   -1805.0135607
[3,] -50          -Inf   -1254.8313611
[4,] -40          -Inf    -804.6084420
[5,] -30  -454.3212440    -454.3212440
[6,] -20  -203.9171554    -203.9171554
[7,] -10   -53.2312852     -53.2312852
[8,]   0    -0.6931472      -0.6931472
> y <- c(1:15, seq(20,40, by=5))
> cbind(y, "log(pnorm(y))"= log(pnorm(y)), "pnorm(y, log=T)"= pnorm(y, log=TRUE),
+       "log(pnorm(-y))"= log(pnorm(-y)), "pnorm(-y, log=T)"= pnorm(-y, log=TRUE))
       y log(pnorm(y)) pnorm(y, log=T) log(pnorm(-y)) pnorm(-y, log=T)
 [1,]  1 -1.727538e-01   -1.727538e-01      -1.841022        -1.841022
 [2,]  2 -2.301291e-02   -2.301291e-02      -3.783184        -3.783184
 [3,]  3 -1.350810e-03   -1.350810e-03      -6.607726        -6.607726
 [4,]  4 -3.167174e-05   -3.167174e-05     -10.360101       -10.360101
 [5,]  5 -2.866516e-07   -2.866516e-07     -15.064998       -15.064998
 [6,]  6 -9.865877e-10   -9.865876e-10     -20.736769       -20.736769
 [7,]  7 -1.279865e-12   -1.279813e-12     -27.384307       -27.384307
 [8,]  8 -6.661338e-16   -6.220961e-16     -35.013437       -35.013437
 [9,]  9  0.000000e+00   -1.128588e-19     -43.628149       -43.628149
[10,] 10  0.000000e+00   -7.619853e-24     -53.231285       -53.231285
[11,] 11  0.000000e+00   -1.910660e-28     -63.824934       -63.824934
[12,] 12  0.000000e+00   -1.776482e-33     -75.410673       -75.410673
[13,] 13  0.000000e+00   -6.117164e-39     -87.989720       -87.989720
[14,] 14  0.000000e+00   -7.793537e-45    -101.563034      -101.563034
[15,] 15  0.000000e+00   -3.670966e-51    -116.131385      -116.131385
[16,] 20  0.000000e+00   -2.753624e-89    -203.917155      -203.917155
[17,] 25  0.000000e+00  -3.056697e-138    -316.639408      -316.639408
[18,] 30  0.000000e+00  -4.906714e-198    -454.321244      -454.321244
[19,] 35  0.000000e+00  -1.124911e-268    -616.975101      -616.975101
[20,] 40  0.000000e+00    0.000000e+00           -Inf      -804.608442
> ## Symmetry:
> y <- c(1:50,10^c(3:10,20,50,150,250))
> y <- c(-y,0,y)
> for(L in c(FALSE,TRUE))
+     stopifnot(identical(pnorm(-y, log= L),
+ 			pnorm(+y, log= L, lower=FALSE)))
> 
> ## Log norm
> stopifnot(All.eq(pz, plnorm(exp(z))))
> 