~ubuntu-branches/ubuntu/precise/r-cran-stabledist/precise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
## This R package is free software; you can redistribute it and/or
## modify it under the terms of the GNU Library General Public
## License as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.
##
## This R package is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Library General Public License for more details.
##
## You should have received a copy of the GNU Library General
## Public License along with this library; if not, write to the
## Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
## MA  02111-1307  USA


################################################################################
## FUNCTIONS:		 DESCRIPTION:
##  dstable		  Returns density for stable DF
##  pstable		  Returns probabilities for stable DF
##  qstable		  Returns quantiles for stable DF
##  rstable		  Returns random variates for stable DF
## UTILITY FUNCTION	 DESCRIPTION:
##  .integrate2  	  Integrates internal functions for *stable
################################################################################


##==============================================================================

### MM TODO:

## 0) All d, p, q, q -- have identical parts
##  a) 'parametrization' (pm) check
##  b) checking, alpha, beta,
##  c) subdivisions etc {all but rstable}
## ---	to do: "Fix" these in dstable(), then copy/paste to others

##==============================================================================

##' @title omega() according to Lambert & Lindsey (1999), p.412
##' @param gamma
##' @param alpha
##' @return
.om <- function(gamma,alpha)
    if(alpha == 1) (2/pi)*log(gamma) else tan(pi*alpha/2)

dstable <- function(x, alpha, beta,
		    gamma = 1, delta = 0, pm = 0, log = FALSE,
		    tol = 16*.Machine$double.eps, subdivisions = 1000)
{
    ## Original implemented by Diethelm Wuertz;
    ## Changes for efficiency and accuracy by Martin Maechler

    ## Description:
    ##	 Returns density for stable DF

    ## Details:
    ##	 The function uses the approach of J.P. Nolan for general
    ##	 stable distributions. Nolan derived expressions in form
    ##	 of integrals based on the charcteristic function for
    ##	 standardized stable random variables. These integrals
    ##	 can be numerically evaluated.

    ## Arguments:
    ##	 alpha = index of stability, in the range (0,2]
    ##	 beta  = skewness, in the range [-1, 1]
    ##	 gamma = scale, in the range (0, infinity)
    ##	 delta = location, in the range (-infinity, +infinity)
    ##	 param = type of parmeterization

    ## Note: S+ compatibility no longer considered (explicitly)

    ## Parameter Check:
    ## NB: (gamma, delta) can be *vector*s (vectorized along x)
    stopifnot( 0 < alpha, alpha <= 2, length(alpha) == 1,
	      -1 <= beta, beta	<= 1, length(beta) == 1,
	      0 <= gamma, length(pm) == 1, pm %in% 0:2,
	      tol > 0, subdivisions > 0)

    ## Parameterizations:
    if (pm == 1) {
	delta <- delta + beta*gamma * .om(gamma,alpha)
    } else if (pm == 2) {
	delta <- delta - alpha^(-1/alpha)*gamma*stableMode(alpha, beta)
	gamma <- alpha^(-1/alpha) * gamma
    } ## else pm == 0

    ## Shift and Scale:
    x <- (x - delta) / gamma
    ans <-
	## Special Cases:
	if (alpha == 2) {
	    dnorm(x, mean = 0, sd = sqrt(2))
	} else if (alpha == 1 && beta == 0) {
	    dcauchy(x)
	} else {

	    ## General Case
	    if (alpha != 1) { ## 0 < alpha < 2	&  |beta| <= 1	from above
		tanpa2 <- tan(pi*alpha/2)
		zeta <- -beta * tanpa2
		theta0 <- atan(beta * tanpa2) / alpha
		## Loop over all x values:
		unlist(lapply(x, function(z)
                              .fct1(z, zeta, alpha=alpha, theta0 = theta0,
                                    tol=tol, subdivisions=subdivisions)))
	    }
	    ## Special Case alpha == 1	and  -1 <= beta <= 1 (but not = 0) :
	    else { ## (alpha == 1)  and	 0 < |beta| <= 1  from above
		## Loop over all x values:
		unlist(lapply(x, function(z) {
		    if (z >= 0) {
			.fct2( z , beta, tol=tol, subdivisions=subdivisions)
		    } else {
			.fct2(-z, -beta, tol=tol, subdivisions=subdivisions)
		    }
		}))
	    }
	}

    ans <- ans/gamma
    ## Return:
    if (log) log(ans) else ans
}

## ------------------------------------------------------------------------------

.fct1 <- function(x, zeta, alpha, theta0, tol, subdivisions,
                  verbose = getOption("dstable.debug", default=FALSE))
{
    ## -- Integrand for dstable() --

    x.m.zet <- abs(x - zeta)
    f.zeta <- function()
        gamma(1+1/alpha)*cos(theta0) / (pi*(1+zeta^2)^(1/(2*alpha)))

    ## Modified: originally was  if (z == zeta),
    ## then (D.W.)   if (x.m.zet < 2 * .Machine$double.eps)
    ## then (M.M.)   if (x.m.zet <= 1e-5 * abs(x))
    if(x.m.zet < 1e-10)
        return(f.zeta())
    ## the real check should be about the feasibility of g() below, or its integration

    if(x < zeta) theta0 <- -theta0

    ## constants ( independent of integrand g1(th) = g*exp(-g) ):
    ## zeta <- -beta * tan(pi*alpha/2)
    ## theta0 <- (1/alpha) * atan( beta * tan(pi*alpha/2))
    ## x.m.zet <- abs(x - zeta)
    a_1 <- alpha - 1
    cat0 <- cos(at0 <- alpha*theta0)

    g <- function(th) (cat0 * cos(th) * (x.m.zet/sin(at0+alpha*th))^alpha)^(1/a_1)  * cos(at0+ a_1*th)

    ## Function to Integrate:
    g1 <- function(th) ## and (xarg, alpha, beta) => (zeta,at0,g0,.....)
    {
        ## g1 = g(.) exp(-g(.))
        v <- g(th)
        ## making sure that we don't get   Inf * 0  =>  NaN
	r <- v * exp(-v)
	if(any(L <- v > 1000)) ## actually, v > -(.Machine$double.min.exp * log(2)) == 708.396...
	    r[L] <- 0
	r
    }

    c2 <- ( alpha / (pi*abs(a_1)*x.m.zet) )
    ## Result = c2 * \int_{-t0}^{pi/2} g1(u) du
    ## where however, g1(.) may look to be (almost) zero almost everywhere and just have a small peak
    ## ==> Split the integral into two parts of two intervals  (t0, t_max) + (t_max, pi/2)

    ##  However, this may still be bad, e.g., for dstable(71.61531, alpha=1.001, beta=0.6),
    ##  or  dstable(1.205, 0.75, -0.5)
    ##   the 2nd integral is "completely wrong" (basically zero, instead of ..e-5)
    ## FIXME --- Lindsey uses "Romberg" integration -- maybe we must split even more


    g. <- if(alpha >= 1) g(-theta0+ 1e-6*abs(theta0)) else g(pi/2 -1e-6*(pi/2))
    if(is.na(g.) || identical(g., 0))# g() is not usable
	return(f.zeta())

    ## We know that the maximum of g1(.) is = exp(-1) = 0.3679  "at" g(.) == 1
    ## find that by uniroot :
    ur <- uniroot(function(th) g(th) - 1, lower = -theta0, upper = pi/2, tol = tol)
    theta2 <- ur$root

    ## now, because the peak may be extreme, we find two more intermediate values
    ## NB: Max = 0.3679 ==> 1e-4 is a very small fraction of that
    ## to the left:
    th1 <- uniroot(function(th) g1(th) - 1e-4, lower = -theta0, upper = theta2,
		   tol = tol)$root
    if((do4 <- g1(pi/2) < 1e-4))
        ## to the right:
        th3 <- uniroot(function(th) g1(th) - 1e-4, lower = theta2, upper = pi/2,
                       tol = tol)$root

    Int <- function(a,b)
        .integrate2(g1, lower = a, upper = b,
                    subdivisions=subdivisions, rel.tol= tol, abs.tol= tol)

    r1 <- Int(-theta0, th1)
    r2 <- Int(         th1, theta2)
    if(do4) {
        r3 <- Int(          theta2, th3)
        r4 <- Int(                  th3, pi/2)
    } else {
        r3 <- Int(          theta2,      pi/2)
        r4 <- 0
    }
    if(verbose)
        cat(sprintf(".fct1(%11g,%10g,..): c2*sum(r[1..4])= %11g*(%9.4g + %9.4g + %9.4g + %9.4g)= %g\n",
                    x,zeta, c2, r1,r2,r3,r4, c2*(r1+r2+r3+r4)))
    c2*(r1+r2+r3+r4)
}


## ------------------------------------------------------------------------------

##' is only used when alpha == 1 (!)
.fct2 <- function(xarg, beta, tol, subdivisions)
{
    ## Integration:

    i2b <- 1/(2*beta)
    p2b <- pi*i2b # = pi/(2 beta)
    pi2 <- pi/2
    g. <- exp(-p2b*xarg)

    ## Function to Integrate; x is a non-sorted vector!
    g2 <- function(x) { ## and (xarg, beta)
	g <- p2b+ x # == g'/beta where g' := pi/2 + beta*x
	v <- g / (p2b*cos(x)) * exp(g*tan(x))
	g <- g. * v
	gval <- g * exp(-g)
	if(any(ina <- is.na(gval))) gval[ina] <- 0 ## replace NA at pi/2
	gval
    }

    theta2 <- optimize(g2, lower = -pi2, upper = pi2,
		       maximum = TRUE, tol = tol)$maximum
    r1 <- .integrate2(g2, lower = -pi2, upper = theta2,
		     subdivisions = subdivisions,
		     rel.tol = tol, abs.tol = tol)
    r2 <- .integrate2(g2, lower = theta2, upper = pi2,
		     subdivisions = subdivisions,
		     rel.tol = tol, abs.tol = tol)
    abs(i2b)*(r1 + r2)
}

### ------------------------------------------------------------------------------


pstable <- function(q, alpha, beta, gamma = 1, delta = 0, pm = 0,
		    tol = 16*.Machine$double.eps, subdivisions = 1000)
{
    ## A function implemented by Diethelm Wuertz

    ## Description:
    ##	 Returns probability for stable DF

    x <- q
    ## Parameter Check:
    ## NB: (gamma, delta) can be *vector*s (vectorized along x)
    stopifnot( 0 < alpha, alpha <= 2, length(alpha) == 1,
	      -1 <= beta, beta	<= 1, length(beta) == 1,
	      0 <= gamma, length(pm) == 1, pm %in% 0:2,
	      tol > 0, subdivisions > 0)

    ## Parameterizations:
    if (pm == 1) {
	delta <- delta + beta*gamma * .om(gamma,alpha)
    } else if (pm == 2) {
	delta <- delta - alpha^(-1/alpha)*gamma*stableMode(alpha, beta)
	gamma <- alpha^(-1/alpha) * gamma
    } ## else pm == 0

    ## Shift and Scale:
    x <- (x - delta) / gamma

    ## Return directly
    ## ------  first, special cases:
    if (alpha == 2) {
	pnorm(x, mean = 0, sd = sqrt(2))
    } else if (alpha == 1 && beta == 0) {
	pcauchy(x)
    } else {

	## General Case
	if (alpha != 1) { ## 0 < alpha < 2	&  |beta| <= 1	from above
	    tanpa2 <- tan(pi*alpha/2)
	    zeta <- -beta * tanpa2
	    theta0 <- atan(beta * tanpa2) / alpha

	    ## Loop over all x values:
	    unlist(lapply(x, function(z) {
		z.m.zet <- abs(z - zeta)

		if (z.m.zet < 2 * .Machine$double.eps)
		    ## FIXME? same problem as dstable
		    (1/2- theta0/pi)
		else if (z > zeta)
		    .FCT1(z.m.zet, alpha=alpha, theta0= theta0,
			  tol = tol, subdivisions = subdivisions)
		else ## (z < zeta)
		    1 - .FCT1(z.m.zet, alpha=alpha, theta0= -theta0,
			      tol = tol, subdivisions = subdivisions)
	    }))
	}
	## Special Case alpha == 1	and  -1 <= beta <= 1 (but not = 0) :
	else { ## (alpha == 1)	and	 0 < |beta| <= 1  from above
	    ## Loop over all x values:
	    unlist(lapply(x, function(z) {
		if (beta >= 0) {
		    .FCT2(xarg = z, beta = beta,
			  tol = tol, subdivisions = subdivisions)
		} else {
		    1 - .FCT2(xarg = -z, beta = -beta,
			      tol = tol, subdivisions = subdivisions)
		}
	    }))
	}
    }
}

## ------------------------------------------------------------------------------

.FCT1 <- function(x.m.zeta, alpha, theta0, tol, subdivisions)
{
    a_1 <- alpha - 1
    aa1 <- alpha/a_1
    cat0 <- cos(at0 <- alpha*theta0)
    g0 <- x.m.zeta^aa1

    ## Function to integrate:
    G1 <- function(x) { ## (xarg, alpha, beta)
	v <- (cat0*cos(x))^(1/a_1) * sin(at0+ alpha*x)^-aa1 * cos(at0+a_1*x)
	exp(-(g0 * v))
    }

    theta2 <- optimize(G1, lower = -theta0, upper = pi/2,
		       maximum = TRUE, tol = tol)$maximum
    c1 <- if(alpha < 1) 1/2 - theta0/pi else 1
    c3 <- sign(1-alpha)/pi
    r1 <- .integrate2(G1, lower = -theta0,
		      upper = theta2, subdivisions = subdivisions,
		      rel.tol = tol, abs.tol = tol)
    r2 <- .integrate2(G1, lower = theta2,
		      upper = pi/2, subdivisions = subdivisions,
		      rel.tol = tol, abs.tol = tol)
    c1 + c3*(r1+r2)
}

## ------------------------------------------------------------------------------

.FCT2 <- function(xarg, beta, tol, subdivisions)
{
    i2b <- 1/(2*beta)
    p2b <- pi*i2b # = pi/(2 beta)
    pi2 <- pi/2
    g. <- exp(-p2b*xarg)

    ## Function to Integrate; x is a non-sorted vector!
    G2 <- function(x) { ## and (xarg, beta)
	g <- p2b+ x # == g'/beta where g' := pi/2 + beta*x
	v <- g / (p2b*cos(x)) * exp(g*tan(x))
	g <- g. * v
	gval <- exp(-g)
	if(any(ina <- is.na(gval))) gval[ina] <- 0 ## replace NA at pi/2
	gval
    }

    ## Integration:
    theta2 <- optimize(G2, lower = -pi2, upper = pi2,
		       maximum = TRUE, tol = tol)$maximum
    r1 <- .integrate2(G2, lower = -pi2,
		     upper = theta2, subdivisions = subdivisions,
		     rel.tol = tol, abs.tol = tol)
    r2 <- .integrate2(G2, lower = theta2,
		     upper = pi2, subdivisions = subdivisions,
		     rel.tol = tol, abs.tol = tol)
    (r1+r2)/pi
}

### ------------------------------------------------------------------------------


qstable <- function(p, alpha, beta, gamma = 1, delta = 0, pm = 0,
                    tol = .Machine$double.eps^0.25, maxiter = 1000,
                    integ.tol = 1e-7, subdivisions = 200)
{
    ## A function implemented by Diethelm Wuertz

    ## Description:
    ##	 Returns quantiles for stable DF

    ## Parameter Check:
    ## NB: (gamma, delta) can be *vector*s (vectorized along x)
    stopifnot( 0 < alpha, alpha <= 2, length(alpha) == 1,
	      -1 <= beta, beta	<= 1, length(beta) == 1,
	      0 <= gamma, length(pm) == 1, pm %in% 0:2,
	      tol > 0, subdivisions > 0)

    ## Parameterizations:
    if (pm == 1) {
	delta <- delta + beta*gamma * .om(gamma,alpha)
    } else if (pm == 2) {
	delta <- delta - alpha^(-1/alpha)*gamma*stableMode(alpha, beta)
	gamma <- alpha^(-1/alpha) * gamma
    } ## else pm == 0

    result <-
	## Special Cases:
	if (alpha == 2)
	    qnorm(p, mean = 0, sd = sqrt(2))
	else if (alpha == 1 && beta == 0)
	    qcauchy(p)
	else if (abs(alpha-1) < 1) { ## -------------- 0 < alpha < 2 ---------------
            .froot <- function(x, p) {
                pstable(q = x, alpha=alpha, beta=beta, pm = 0,
                        tol=integ.tol, subdivisions=subdivisions) - p
            }
            ## Calculate:
            unlist(lapply(p, function(pp) {
                if (beta < 0) {
                    xmin <- -(1-pp)/pp
                    ## xmax = pp/(1-pp)
                    if (pp < 0.5) {
                        xmax <- qnorm(pp, mean = 0, sd = sqrt(2))
                    } else {
                        xmax <- qcauchy(pp)
                    }
                }
                if (beta > 0 ) {
                    ## xmin = -(1-pp)/pp
                    if (pp < 0.5) {
                        xmin <- qcauchy(pp)
                    } else {
                        xmin <- qnorm(pp, mean = 0, sd = sqrt(2))
                    }
                    xmax <- pp/(1-pp)
                }
                if (beta == 0 ) {
                    ## xmin = -(1-pp)/pp
                    if (pp < 0.5) {
                        xmin <- qcauchy(pp)
                    } else {
                        xmin <- qnorm(pp, mean = 0, sd = sqrt(2))
                    }
                    ## xmax = pp/(1-pp)
                    if (pp < 0.5) {
                        xmax <- qnorm(pp, mean = 0, sd = sqrt(2))
                    } else {
                        xmax <- qcauchy(pp)
                    }
                }
                root <- NA
                counter <- 0
                while (is.na(root)) {
                    root <- .unirootNA(.froot, interval = c(xmin, xmax), p = pp,
                                       tol=tol, maxiter=maxiter)
                    counter <- counter + 1
                    xmin <- xmin-2^counter
                    xmax <- xmax+2^counter
                }
                root
            }))
        }

    ## Result:
    result * gamma + delta
}

## ------------------------------------------------------------------------------


rstable <- function(n, alpha, beta, gamma = 1, delta = 0, pm = 0)
{
    ## Description:
    ##	 Returns random variates for stable DF

    ## slightly amended along  nacopula::rstable1

    ## Parameter Check:
    ## NB: (gamma, delta) can be *vector*s (vectorized along x)
    stopifnot( 0 < alpha, alpha <= 2, length(alpha) == 1,
	      -1 <= beta, beta	<= 1, length(beta) == 1,
	      0 <= gamma, length(pm) == 1, pm %in% 0:2)

    ## Parameterizations:
    if (pm == 1) {
	delta <- delta + beta*gamma * .om(gamma,alpha)
    } else if (pm == 2) {
	delta <- delta - alpha^(-1/alpha)*gamma*stableMode(alpha, beta)
	gamma <- alpha^(-1/alpha) * gamma
    } ## else pm == 0

    ## Calculate uniform and exponential distributed random numbers:
    theta <- pi * (runif(n)-1/2)
    w <- -log(runif(n))

    result <-
        ## If alpha is equal 1 then:
        if (alpha == 1 & beta == 0) {
            rcauchy(n)
            ## Otherwise, if alpha is different from 1:
        } else {
            ## FIXME: learn from nacopula::rstable1R()
	    b.tan.pa <- beta*tan(pi*alpha/2)
	    theta0 <- atan(b.tan.pa) / alpha ## == \theta_0
            c <- (1+b.tan.pa^2)^(1/(2*alpha))
	    a.tht <- alpha*(theta+theta0)
            r <- ( c*sin(a.tht)/
                  (cos(theta))^(1/alpha) ) *
                      (cos(theta-a.tht)/w)^((1-alpha)/alpha)
            ## Use Parametrization 0:
            r - b.tan.pa
        }

    ## Result:
    result * gamma + delta
}


## ------------------------------------------------------------------------------


##' Numerically Integrate -- basically the same as R's	integrate()
##' --------------------- main difference: no errors, but warnings
.integrate2 <- function(f, lower, upper, subdivisions, rel.tol, abs.tol, ...)
{
    ## Originally implemented by Diethelm Wuertz -- without *any* warnings

    if (class(version) != "Sversion") {
	## R:
	f <- match.fun(f)
	ff <- function(x) f(x, ...)
	wk <- .External("call_dqags", ff,
			rho = environment(), as.double(lower),
			as.double(upper), as.double(abs.tol),
			as.double(rel.tol), limit = as.integer(subdivisions),
			PACKAGE = "base")[c("value","ierr")]
	iErr <- wk[["ierr"]]
	if(iErr == 6) stop("the input is invalid")
	if(iErr > 0)
            ## NB:  "roundoff error ..." happens many times
	    warning(switch(iErr + 1, "OK",
			   "maximum number of subdivisions reached",
			   "roundoff error was detected",
			   "extremely bad integrand behaviour",
			   "roundoff error is detected in the extrapolation table",
			   "the integral is probably divergent"))
	wk[[1]]

    } else {
	## SPlus:
	integrate(f, lower, upper, subdivisions, rel.tol, abs.tol, ...)[[1]]
    }
}


################################################################################