random-distributions/gaussian-tail.ss
;;; PLT Scheme Science Collection
;;; random-distributions/gaussian-tail.ss
;;; Copyright (c) 2004 M. Douglas Williams
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 2.1 of the License, or (at your option) any later version.
;;;
;;; This library 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
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free
;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
;;; 02111-1307 USA.
;;;
;;; -------------------------------------------------------------------
;;;
;;; This module implements the Gaussian tail distribution.  It is based
;;; on the Random Number Distributions.
;;;
;;; Version  Date      Description
;;; 1.0.0    09/28/04  Marked as ready for Release 1.0.  Added
;;;                    contracts for functions.  (Doug Williams)

(module gaussian-tail mzscheme
  
  (require (lib "contract.ss"))
  
  (provide/contract
   (random-gaussian-tail
    (case-> (-> random-source? real? real? (>=/c 0.0) real?)
            (-> real? real? (>=/c 0.0) real?)))
   (random-unit-gaussian-tail
    (case-> (-> random-source? real? real?)
            (-> real? real?)))
   (gaussian-tail-pdf
    (-> real? real? real? (>=/c 0.0) (>=/c 0.0)))
   (unit-gaussian-tail-pdf
    (-> real? real? (>=/c 0.0))))
  
  (require "../math.ss")
  (require "../random-source.ss")
  (require "../special-functions/error.ss")
  (require "gaussian.ss")
  
  ;; Gaussian tail distribution

  ;; random-gaussian-tail: random-source x real x real x real -> real
  ;; random-gaussian-tail" real x real x real -> real
  ;;
  ;; Returns a gaussian random variable larger than a.  This
  ;; implementation does one-sided upper-tailed deviates.
  (define random-gaussian-tail
    (case-lambda
      ((r a mu sigma)
       (let ((s (/ (- a mu) sigma)))
         (if (<= s 1.0)
             ;; For small s, use a direct rejection method.  The limit
             ;; s <= 1 can be adjusted to optimize overall effeciency.
             (let ((x 0.0))
               (let loop ()
                 (set! x (random-unit-gaussian r))
                 (if (< x s) 
                     (loop)))
               (+ mu (* x sigma)))
             ;; Use the "supertail" deviates from the last two steps
             ;; of Marsaglia's rectangle-wedge-tail method, as described
             ;; in Knuth, v2, 3rd ed, pp 123-128.  (See also exercise 11,
             ;; p139, and the solution, p586.)
             (let ((u 0.0)
                   (v 0.0)
                   (x 0.0))
               (let loop ()
                 (set! u (random-uniform r))
                 (set! v (random-uniform r))
                 ;; Note: v > 0.0
                 (set! x (sqrt (- (* s s) (* 2.0 (log v)))))
                 (if (> (* x u) s)
                     (loop)))
               (+ mu (* x sigma))))))
      ((a mu sigma)
       (random-gaussian-tail (current-random-source) a mu sigma))))
  
  ;; random-unit-gaussian-tail: random-source x real -> real
  ;; random-unit-gaussian-tail: real -> real
  (define random-unit-gaussian-tail
    (case-lambda
      ((r a)
       (if (not (random-source? r))
           (raise-type-error 'random-unit-gaussian-tail
                             "random-stream" r))
       (if (not (real? a))
           (raise-type-error 'random-unit-gaussian-tail
                             "real" a))
       (random-gaussian-tail r a 0.0 1.0))
      ((a)
       (random-unit-gaussian-tail (current-random-source) a))))
  
  ;; gaussian-tail-pdf: real x real x real x real -> real
  ;;
  ;; This function computes the probability density p(x) at x from the
  ;; upper tail of a Gaussian distribution with mean mu and standard
  ;; deviation sigma.
  (define (gaussian-tail-pdf x a mu sigma)
    (if (not (real? x))
        (raise-type-error 'gaussian-tail-pdf
                          "real" x))
    (if (not (real? a))
        (raise-type-error 'gaussian-tail-pdf
                          "real" a))
    (if (not (real? sigma))
        (raise-type-error 'gaussian-tail-pdf
                          "real" sigma))
    (if (< x a)
        0
        (let ((N 0.0)
              (p 0.0)
              (u (/ (- x mu) sigma))
              (f (erfc (/ (- a mu) (* (sqrt 2.0) sigma)))))
          (set! N (* 0.5 f))
          (set! p (* (/ 1.0 (* N (sqrt (* 2.0 pi)) sigma))
                     (exp (/ (* (- u) u) 2.0))))
          p)))
  
  ;; unit-gaussian-tail-pdf: real x real -> real
  (define (unit-gaussian-tail-pdf x a)
    (if (not (real? x))
        (raise-type-error 'unit-gaussian-tail-pdf
                          "real" x))
    (if (not (real? a))
        (raise-type-error 'unit-gaussian-tail-pdf
                          "real" a))
    (gaussian-tail-pdf x a 0.0 1.0))
  
)