racket/collects/math/private/functions/incomplete-gamma/gamma-normal.rkt
Neil Toronto f2dc2027f6 Initial math library commit. The history for these changes is preserved
in the original GitHub fork:

  https://github.com/ntoronto/racket

Some things about this are known to be broken (most egregious is that the
array tests DO NOT RUN because of a problem in typed/rackunit), about half
has no coverage in the tests, and half has no documentation. Fixes and
docs are coming. This is committed now to allow others to find errors and
inconsistency in the things that appear to be working, and to give the
author a (rather incomplete) sense of closure.
2012-11-16 11:39:51 -07:00

29 lines
1.1 KiB
Racket

#lang typed/racket/base
(require "../../../flonum.rkt"
"../../distributions/impl/normal-cdf.rkt")
(provide flgamma-normal)
;; Temme's normal approximation for regularized incomplete gamma functions
;; This is much better than moment-matching or Wilson-Hilferty
(: flgamma-normal (Float Float Any Any -> Float))
(define (flgamma-normal k x log? upper?)
(define l (fl/ x k))
(define norm-x
(cond [(or (l . fl< . epsilon.0) (l . fl> . (fl/ 1.0 epsilon.0)))
;; Avoid under-/overflow in calculating norm-x by doing it in log space
(define log-l (fl- (fllog x) (fllog k)))
(define l-1 (flexpm1 log-l))
(define l-1-sign (flsgn l-1))
(define log-n (fl* 0.5 (fl+ (fllog 2.0) (fllog (fl- l-1 log-l)))))
(fl* l-1-sign (flexp (fl+ log-n (fl* 0.5 (fllog k)))))]
[else
(define n (fl* (flsgn (fl- l 1.0)) (flsqrt (fl* 2.0 (fl- (fl- l 1.0) (fllog l))))))
(fl* n (flsqrt k))]))
(let ([norm-x (if upper? (- norm-x) norm-x)])
(cond [log? (standard-flnormal-log-cdf norm-x)]
[else (standard-flnormal-cdf norm-x)])))