#lang racket/base
(require racket/contract)
(provide/contract
(make-binomial (-> real? procedure?))
(make-beta (-> natural-number/c natural-number/c procedure?))
(make-cumulative-beta (-> natural-number/c natural-number/c procedure?))
(chebyshev-integral (-> natural-number/c natural-number/c procedure?)))
(define (make-binomial x)
(lambda ()(if (< (random) x) #t #f)))
(define (make-beta t f)
(let [(norm ((chebyshev-integral t f) 1))]
(lambda (x)
(/ (* (expt x t)
(expt (- 1 x) f))
norm))))
(define (make-cumulative-beta t f)
(let* [(f (chebyshev-integral t f))
(norm (f 1))]
(lambda (x) (/ (f x) norm))))
(define (chebyshev-integral p q)
(let* [(f (cond
[(= p 0) (let [(q+1 (add1 q))]
(lambda (x)
(- (/ (expt (- 1 x) q+1)
q+1))))]
[(= q 0) (let [(p+1 (add1 p))]
(lambda (x)
(/ (expt x p+1) p+1)))]
[else
(let [(p+1 (add1 p))]
(lambda (x)
(/ (+ (* (expt (- 1 x) q)
(expt x p+1))
(* q
(apply (chebyshev-integral p+1 (sub1 q))
(list x))))
p+1)))]))
(f0 (f 0))]
(lambda (x) (- (f x) f0))))