I am attempting to add discrete uniform distributions to math/distributions.
I have however ran into a type error, I can't fix. Any hints are welcome.
The error:
Type Checker: type mismatch
expected: Discrete-Uniform-Dist
given: (discrete-uniform-dist-struct Real Integer) in: ((inst
discrete-uniform-dist-struct Real Integer) pdf sample cdf inv-cdf min max
median a b)
#lang typed/racket
(require math/distributions racket/flonum math/flonum math/base)
(require math/private/distributions/utils)
; from private/distributions/utils.rkt
(define-real-dist: discrete-uniform-dist Discrete-Uniform-Dist
discrete-uniform-dist-struct ([a : Real] [b : Real]))
(: discrete-uniform : (case-> (-> Discrete-Uniform-Dist)
(-> Integer Discrete-Uniform-Dist)
(-> Integer Integer Discrete-Uniform-Dist)))
(define discrete-uniform
(case-lambda
[() (discrete-uniform 0 1)]
[(b) (discrete-uniform 0 b)]
[(a b) (let ([a (flfloor (fl a))] [b (flfloor (fl b))])
(let ([a (flmin a b)] [b (flmax a b)])
(define pdf (opt-lambda: ([v : Real] [log? : Any #f])
(let ([p (fl/ 1. (fl+ (fl- b a) 1.))])
(if log? (fllog p) p))))
(: sample : (case-> (-> Integer)
(-> Integer (Listof Integer))))
(define sample (let ([a (exact-floor a)] [b (exact-floor
b)])
(case-lambda
[() (random-integer a b)]
[([n : Integer])
(let loop ([i 0])
(if (= i n) '() (cons
(random-integer a b)
(loop (+ i
1)))))])))
(define cdf (opt-lambda: ([v : Real] [log? : Any #f]
[1-p? : Any #f])
(let* ([v (flfloor (fl v))]
[p (cond [(fl< v a) 0.]
[(fl>= v b) 1.]
[else (fl/ (fl+ (fl-
v a) 1.)
(fl+ (fl-
b a) 1.))])])
(cond
[log? (fllog p)]
[1-p? (fl- 1. p)]
[else p]))))
(define inv-cdf (opt-lambda: ([u : Real] [log? : Any #f]
[1-p? : Any #f])
(let* ([u (flfloor (fl u))]
[u (cond
[log? (flexp u)]
[1-p? (fl+ 1. u)]
[else u])])
(exact-floor
(cond [(<= 0. u 1.) (fl+ a (flfloor (*
u (fl+ (fl- b a) 1.))))]
[else 0.])))))
(define min (exact-floor a))
(define max (exact-floor b))
(define median (delay (exact-floor (fl/ (fl+ a b) 2.))))
((inst discrete-uniform-dist-struct Real Integer)
pdf sample cdf inv-cdf min max median a b)))]))
(define X-dist (discrete-uniform 1 11))
(distribution? X-dist)
(sample X-dist)
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.