;;; PLT Scheme Science Collection ;;; random-distributions/beta.ss ;;; Copyright (c) 2004-2007 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 modules provides the implementation of the beta distribution. ;;; It is based on the Random Number Distributions in the GNU ;;; Scientific Library (GSL). ;;; ;;; Version Date Description ;;; 1.0.0 09/28/04 Marked as ready for Release 1.0. Added ;;; contracts for functions. (Doug Williams) ;;; 1.1.0 02/08/06 Added cdf. (Doug Williams) ;;; 2.0.0 11/19/07 Added unchecked versions of functions and ;;; getting ready for PLT Scheme 4.0 (Doug Williams) (module beta mzscheme (require (lib "contract.ss")) (provide (rename random-beta unchecked-random-beta) (rename beta-pdf unchecked-beta-pdf) (rename beta-cdf unchecked-beta-cdf)) (provide/contract (random-beta (case-> (-> random-source? real? real? (real-in 0.0 1.0)) (-> real? real? (real-in 0.0 1.0)))) (beta-pdf (-> real? real? real? (>=/c 0.0))) (beta-cdf (-> real? real? real? (real-in 0.0 1.0)))) (require "../random-source.ss") (require "../special-functions/gamma.ss") (require "gamma.ss") (require "cdf-beta-inc.ss") ;; random-beta: random-source x real x real -> real ;; random-beta: real x real -> real ;; These functions return a random variate from the beta distribution ;; with parameters a and b. (define random-beta (case-lambda ((r a b) (let ((x1 (unchecked-random-gamma r a 1.0)) (x2 (unchecked-random-gamma r b 1.0))) (/ x1 (+ x1 x2)))) ((a b) (random-beta (current-random-source) a b)))) ;; beta-pdf: real x real x real -> real ;; This function computes the probability density for the beta ;; distribution with parameters a and b. (define (beta-pdf x a b) (if (or (< x 0.0) (> x 1.0)) 0.0 (let ((gab (unchecked-lngamma (+ a b))) (ga (unchecked-lngamma a)) (gb (unchecked-lngamma b))) (* (exp (- gab ga gb)) (expt x (- a 1.0)) (expt (- 1.0 x) (- b 1.0)))))) ;; beta-cdf: real x real x real -> real ;; This function computes the cummulative probability density for ;; the beta distribution with parameters a and b. (define (beta-cdf x a b) (cond ((< x 0.0) 0.0) ((>= x 1.0) 1.0) (else (beta-inc-axpy 1.0 0.0 a b x)))) )