Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/stdlib/ext/dist-ext.mc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ let betabinLogPmf:Int -> Float -> Float -> Int -> Float = lam n. lam a. lam b. l
let betabinPmf = lam n:Int. lam a: Float. lam b: Float. lam x:Int.
exp (betabinLogPmf n a b x)

-- Reciprocal (or log uniform) distribution
let reciprocalSample : Float -> Float -> Float = lam a. lam b.
let logSample = uniformContinuousSample (log a) (log b) in
exp logSample

let reciprocalPdf : Float -> Float -> Float -> Float = lam a. lam b. lam x.
if geqf x a then
if leqf x b then divf 1.0 (mulf (log (divf b a)) x)
else 0.
else 0.

let reciprocalLogPdf : Float -> Float -> Float -> Float = lam a. lam b. lam x.
if geqf x a then
if leqf x b then negf (log (mulf (log (divf b a)) x))
else negf inf
else negf inf

-- Seed
external externalSetSeed ! : Int -> ()
let setSeed : Int -> () = lam seed.
Expand Down Expand Up @@ -322,6 +339,13 @@ utest exp (betabinLogPmf 5 1. 1. 3) with 0.166666666667 using _eqf in
utest betabinPmf 5 1. 1. 3 with 0.166666666667 using _eqf in
utest betabinSample 20 1. 1. with 0 using intRange 0 20 in

-- Testing Reciprocal
utest reciprocalLogPdf 1. 2. 1.5 with -0.038952187526 using _eqf in
utest reciprocalLogPdf 1. 2. 2.5 with negf inf using leqf in
utest exp (reciprocalLogPdf 4. 6. 5.) with 0.493260692475 using _eqf in
utest reciprocalPdf 4. 6. 5. with 0.493260692475 using _eqf in
utest reciprocalSample 0.5 0.7 with 0.5 using floatRange 0.5 0.7 in

-- Testing seed
utest setSeed 0; uniformSample (); uniformSample ()
with setSeed 0; uniformSample (); uniformSample () in
Expand Down
Loading