Skip to content

Commit 1a8f1a6

Browse files
Add reciprocal distribution (#953)
* Add reciprocal distribution * Fix bug in sampling * Fix tests and remove comments
1 parent c9aa581 commit 1a8f1a6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/stdlib/ext/dist-ext.mc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ let betabinLogPmf:Int -> Float -> Float -> Int -> Float = lam n. lam a. lam b. l
203203
let betabinPmf = lam n:Int. lam a: Float. lam b: Float. lam x:Int.
204204
exp (betabinLogPmf n a b x)
205205

206+
-- Reciprocal (or log uniform) distribution
207+
let reciprocalSample : Float -> Float -> Float = lam a. lam b.
208+
let logSample = uniformContinuousSample (log a) (log b) in
209+
exp logSample
210+
211+
let reciprocalPdf : Float -> Float -> Float -> Float = lam a. lam b. lam x.
212+
if geqf x a then
213+
if leqf x b then divf 1.0 (mulf (log (divf b a)) x)
214+
else 0.
215+
else 0.
216+
217+
let reciprocalLogPdf : Float -> Float -> Float -> Float = lam a. lam b. lam x.
218+
if geqf x a then
219+
if leqf x b then negf (log (mulf (log (divf b a)) x))
220+
else negf inf
221+
else negf inf
222+
206223
-- Seed
207224
external externalSetSeed ! : Int -> ()
208225
let setSeed : Int -> () = lam seed.
@@ -322,6 +339,13 @@ utest exp (betabinLogPmf 5 1. 1. 3) with 0.166666666667 using _eqf in
322339
utest betabinPmf 5 1. 1. 3 with 0.166666666667 using _eqf in
323340
utest betabinSample 20 1. 1. with 0 using intRange 0 20 in
324341

342+
-- Testing Reciprocal
343+
utest reciprocalLogPdf 1. 2. 1.5 with -0.038952187526 using _eqf in
344+
utest reciprocalLogPdf 1. 2. 2.5 with negf inf using leqf in
345+
utest exp (reciprocalLogPdf 4. 6. 5.) with 0.493260692475 using _eqf in
346+
utest reciprocalPdf 4. 6. 5. with 0.493260692475 using _eqf in
347+
utest reciprocalSample 0.5 0.7 with 0.5 using floatRange 0.5 0.7 in
348+
325349
-- Testing seed
326350
utest setSeed 0; uniformSample (); uniformSample ()
327351
with setSeed 0; uniformSample (); uniformSample () in

0 commit comments

Comments
 (0)