Skip to content

Instantly share code, notes, and snippets.

@k1m190r

k1m190r/mcmc.apl Secret

Last active May 2, 2021 13:18
Show Gist options
  • Save k1m190r/36481a394ab94b69f027f9005f3b1b59 to your computer and use it in GitHub Desktop.
Save k1m190r/36481a394ab94b69f027f9005f3b1b59 to your computer and use it in GitHub Desktop.
mcmc←{
⍝ dist mcmc proposals
⍝ dist - PMF/PDF function (monadic)
⍝ proposals - random proposals for dist()
proposals←⍵
N←⊃⍴⍵ ⍝ number of proposals
dist←⍺⍺ ⍝ pmf/pdf function
coins←?N⍴0 ⍝ random coin tosses
⍝ accepted states initialized to first proposal
states←N⍴⊃proposals
Ni←N-1 ⍝ number of iters
⍝ iterate Ni times starting from 2
_←({i←⍵
curr←states[i-1]
move←proposals[i]
p_curr←dist curr
p_move←dist move
accept←1+coins[i]≤1⌊p_move÷p_curr
⍝ accept 1=False, 2=True
states[i]←(curr move)[accept]
i+1
}⍣Ni)2
states
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment