Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ping, or, Bayesian random bits #36

Open
lukego opened this issue Mar 24, 2023 · 0 comments
Open

Ping, or, Bayesian random bits #36

lukego opened this issue Mar 24, 2023 · 0 comments

Comments

@lukego
Copy link
Owner

lukego commented Mar 24, 2023

Here is a random post of stuff that I have in my Emacs buffers today.

Cute type declarations:

(deftype R   (&rest args) "Real number."    `(double-float ,@args))
(deftype R[] ()           "Real vector."    '(simple-array R (*)))
(deftype P   ()           "Probability."    '(R 0d0 1d0))
(deftype L   ()           "Log-likelihood." '(R * #.(log 1)))

Example of DEFMODEL macro to define statistical models for Bayesian parameter inference and model selection:

(defmodel line (x y &param 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:

(defun line (&key n-particles observations (jitter-scales '(0.01 0.1 0.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 👍

@lukego lukego changed the title Ping... Ping, or, Bayesian random bits Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant