File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11(ns gen.distribution
22 " Collection of protocols and functions for working with primitive
33 distributions."
4- (:require [gen.dynamic.choice-map :as cm]
4+ (:require [gen.distribution.math.log-likelihood :as ll]
5+ [gen.dynamic.choice-map :as cm]
56 [gen.generative-function :as gf]
67 [gen.dynamic.trace :as trace])
78 #? (:clj
184185 v)`"
185186 [ctor encode decode]
186187 (comp #(->Encoded % encode decode) ctor))
188+
189+ (defn delta-distribution
190+ " Deterministic distribution"
191+ [x]
192+ (reify
193+ Sample
194+ (sample [_] x)
195+
196+ LogPDF
197+ (logpdf [_ v] (ll/delta x v))))
198+
199+ (def delta
200+ (->GenerativeFn delta-distribution))
Original file line number Diff line number Diff line change 211211 [& args]
212212 {:clj-kondo/lint-as 'clojure.core/fn}
213213 (apply gen-body args))
214+
215+ (defmacro let-traced
216+ " Similar to `clojure.core/let`, but wraps all values in [[trace!]] calls
217+ addressed via the binding symbol.
218+
219+ Example usage:
220+
221+ ```clojure
222+ (def func
223+ (gen []
224+ (let-traced [a (gen.distribution/delta \" face\" )
225+ b (gen.distribution/delta \" cake\" )]
226+ (str a \" ,\" b))))
227+
228+ (into {} (gf/simulate func []))
229+ ;; => {:a \" face\" :b \" cake\" }
230+ ```"
231+ [bindings & body]
232+ (let [bents (partition 2 bindings)]
233+ (assert (every? symbol? (map first bents)))
234+ `(let ~(into []
235+ (mapcat (fn [[sym expr]]
236+ [sym `(trace! (quote ~sym) ~@expr)]))
237+ bents)
238+ ~@body)))
You can’t perform that action at this time.
0 commit comments