You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Example of DEFMODEL macro to define statistical models for Bayesian parameter inference and model selection:
(defmodel line (x y ¶m m c σ)
"Linear relationship between X and Y with Gaussian noise of constant scale: y = m*x + c + N(0,σ) Infers parameters M (gradient), C (intercept), and σ (standard deviation.)"
(gaussian-log-likelihood (+ c (* m x)) σ y))
Macroexpansion of the above into a bespoke Sequential Monte Carlo simulation:
(defunline (&key n-particles observations (jitter-scales '(0.010.10.5)))
"Linear relationship between X and Y with Gaussian noise of constant scale: y = m*x + c + N(0,σ) Infers parameters M (gradient), C (intercept), and σ (standard deviation.)"
(let ((#:m (make-array (list n-particles) :element-type'r))
(#:c (make-array (list n-particles) :element-type'r))
(#:σ (make-array (list n-particles) :element-type'r)))
(labels ((log-likelihood (m c σ x y)
(gaussian-log-likelihood (+ c (* m x)) σ y))
(particle-log-likelihood (i x y)
(log-likelihood (aref#:m i) (aref#:c i) (aref#:σ i) x y))
(respawn! (parents)
(reorder! parents #:m #:c #:σ))
(jitter! (metropolis-accept?)
(loop for stddev in jitter-scales
do (loop for i below n-particles
for m = (aref#:m i)
for c = (aref#:c i)
for σ = (aref#:σ i)
for ll.old = (partial #'log-likelihood m c σ)
for #:m.p = (add-noise m stddev)
for #:c.p = (add-noise c stddev)
for #:σ.p = (add-noise σ stddev)
for ll.new = (partial #'log-likelihood #:m.p
#:c.p #:σ.p)
when (funcall metropolis-accept? ll.old ll.new)
do (setf (aref#:m i) #:m.p
(aref#:c i) #:c.p
(aref#:σ i) #:σ.p))))
(add-noise (x stddev)
(+ x (* stddev (gaussian-random)))))
(smc/likelihood-tempering n-particles observations :log-likelihood#'particle-log-likelihood :respawn!#'respawn! :jitter!#'jitter!))))
I am having fun 👍
The text was updated successfully, but these errors were encountered:
lukego
changed the title
Ping...
Ping, or, Bayesian random bits
Mar 24, 2023
Here is a random post of stuff that I have in my Emacs buffers today.
Cute type declarations:
Example of
DEFMODEL
macro to define statistical models for Bayesian parameter inference and model selection:Macroexpansion of the above into a bespoke Sequential Monte Carlo simulation:
I am having fun 👍
The text was updated successfully, but these errors were encountered: