Skip to content

Instantly share code, notes, and snippets.

@RichardKelley
Last active October 10, 2017 04:39
Show Gist options
  • Save RichardKelley/c36e8b3858bc29c23f18ecd564203821 to your computer and use it in GitHub Desktop.
Save RichardKelley/c36e8b3858bc29c23f18ecd564203821 to your computer and use it in GitHub Desktop.
Probit Function in Racket. This gives quantiles of the standard normal, which ends up being trickier to implement than you might guess. We follow the "quantile mechanics" approach of Steinbrecher and Shaw: http://www.homepages.ucl.ac.uk/~ucahwts/lgsnotes/EJAM_Quantiles.pdf.
#lang racket
(define (sum-to n term)
(letrec ((S (lambda (acc k)
(cond
[(eq? k 0) (+ (term 0) acc)]
[else (S (+ acc (term k)) (- k 1))]))))
(S 0.0 n)))
(define vs '())
(define lookup
(lambda (v)
(cdr (assoc v vs))))
(define extend
(lambda (k v)
(set! vs (cons (cons k v)
vs))))
(define (W k)
(cond
[(eq? k 0) 1.0]
[(assoc k vs) (lookup k)]
[else
(let ([new-v (* (/ pi 4.0)
(sum-to (- k 1) (lambda (j) (/ (* (W j) (W (- k (+ j 1))))
(* (+ j 1) (+ (* 2 j) 1))))))])
(extend k new-v)
new-v)]))
(define probit
(lambda (p)
(letrec ((S (lambda (acc k)
(cond
[(eq? k 0) (* (+ acc (* (/ (W 0)
(+ (* 2 0) 1))
(expt (- (* 2 p) 1)
(+ (* 2 0) 1))))
(sqrt (/ pi 2)))]
[else
(S (+ acc (* (/ (W k)
(+ (* 2 k) 1))
(expt (- (* 2 p) 1)
(+ (* 2 k) 1))))
(- k 1))]))))
(S 0.0 100))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment