Skip to content

Commit

Permalink
drawing random variates
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenjw committed Aug 24, 2022
1 parent 7c85854 commit 65f2f1d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
7 changes: 7 additions & 0 deletions dex/Reminders.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ dex --backend llvm-mc script myScript.dx
dex --backend llvm-cuda script myScript.dx



make install
make tests
make run-tests/linalg-tests
make run-examples/regression


```
3 changes: 3 additions & 0 deletions dex/pmmh/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ repl:
sandpit:
dex --lib-path $(LIBPATH) script sandpit.dx

random:
dex --lib-path $(LIBPATH) script random-tests.dx


# eof
21 changes: 5 additions & 16 deletions dex/pmmh/lv.dx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
import djwutils -- load up my Dex utils from the logreg repo


-- Random exponentials
def rande (k: Key) : Float =
neg $ log $ rand k

def rande_vec {n} [Ix n] (k:Key) : n=>Float =
for i. rande (ixkey k i)

def randExp (r: Float) (k: Key) : Float =
(rande k) / r


-- Transition kernel for the LV process
def stepLV (th: Fin 3=>Float) (x: Fin 2=>Int) (t: Float) (k: Key) : (Fin 2=>Int) =
yield_state x \xs.
with_state 0.0 \ts.
with_state k \ks.
with_state k \ks.
iter \_.
cx = get xs
ct = get ts
Expand All @@ -32,7 +21,7 @@ def stepLV (th: Fin 3=>Float) (x: Fin 2=>Int) (t: Float) (k: Key) : (Fin 2=>Int)
then
1.0e99
else
ct + (randExp h0 k0)
ct + (exponential h0 k0)
if (nt >= t)
then
Done ()
Expand All @@ -58,10 +47,10 @@ k = new_key 42
th = [1.0, 0.005, 0.6]
stepLV th [50, 100] 2.0 k

kern = \k x. stepLV th x 2.0 k
kern k [50, 100]
kern = \x k. stepLV th x 2.0 k
kern [50, 100] k

ts = markov_chain k [50, 100] kern 50
ts = markov_chain [50, 100] kern 50 k
tsf = for i. for j. i_to_f ts.i.j
tsv = to_tsv tsf
tsv
Expand Down
41 changes: 41 additions & 0 deletions dex/pmmh/random-tests.dx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- random-tests.dx
-- understanding and testing Dex random number generation

import djwutils

k = new_key 42

-- single draws

rand k
randn k
bern 0.5 k
poisson 5.0 k
binom 50 0.5 k

exponential 2.0 k
gaussian 5.0 2.0 k

g = gaussian 5.0 2.0
[k1, k2] = split_key k
g k
g k1
g k2

-- drawing vectors

x: (Fin 10)=>Float = randn_vec k
x

rand_vec 10 randn k
rand_vec 8 (poisson 5.0) k
rand_vec 15 (binom 20 0.5) k
rand_vec 15 (binom 20 0.5) k1
rand_vec 15 (binom 20 0.5) k2




-- eof


0 comments on commit 65f2f1d

Please sign in to comment.