Skip to content

Commit

Permalink
feat: delta distribution for deterministic vals
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Oct 2, 2023
1 parent a7d8668 commit afb41bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/gen/distribution.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns gen.distribution
"Collection of protocols and functions for working with primitive
distributions."
(:require [gen.dynamic.choice-map :as cm]
(:require [gen.distribution.math.log-likelihood :as ll]
[gen.dynamic.choice-map :as cm]
[gen.generative-function :as gf]
[gen.dynamic.trace :as trace])
#?(:clj
Expand Down Expand Up @@ -184,3 +185,16 @@
v)`"
[ctor encode decode]
(comp #(->Encoded % encode decode) ctor))

(defn delta-distribution
"Deterministic distribution"
[x]
(reify
Sample
(sample [_] x)

LogPDF
(logpdf [_ v] (ll/delta x v))))

(def delta
(->GenerativeFn delta-distribution))
17 changes: 16 additions & 1 deletion test/gen/distribution_test.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns gen.distribution-test
(:require [com.gfredericks.test.chuck.clojure-test :refer [checking]]
[clojure.test :refer [is testing]]
[clojure.test :refer [deftest is testing]]
[clojure.test.check.generators :as gen]
[gen.diff :as diff]
[gen.distribution :as dist]
[gen.dynamic :as dynamic :refer [gen]]
[gen.dynamic.choice-map :as choice-map]
[gen.generative-function :as gf]
[gen.trace :as trace]
Expand Down Expand Up @@ -182,3 +183,17 @@
"Inside the bounds, log-l*range == 1.0")
(is (= ##-Inf log-l)
"Outside the bounds, (log 0.0)")))))

(deftest deterministic-tests
(checking "(log of the) Beta function is symmetrical"
[v gen/any-equatable]
(let [f (gen [] (dynamic/trace! :k dist/delta v))
{:keys [trace weight]} (gf/generate f [])]
(is (= {:k v} (into {} trace))
"The delta distribution always samples its argument.")

(is (= 0.0 weight)
"The weight is always 0.0 == Log(1)")))

(testing "delta distribution"
(delta-tests dist/delta-distribution)))

0 comments on commit afb41bd

Please sign in to comment.