gen_rs/gfi.rs
1/// Representation of the probabilistic execution of a `GenFn`.
2#[derive(Clone)]
3pub struct Trace<Args,Data,Ret> {
4 /// Input arguments to the `GenFn`.
5 pub args: Args,
6
7 /// Random variables sampled by the `GenFn`.
8 pub data: Data,
9
10 /// The return value of the `GenFn`.
11 /// Always `Some(v)` if the `Trace` is returned by a GFI method.
12 pub retv: Option<Ret>,
13
14 /// The log joint probability of all the data `log[p(data; args)]`.
15 pub logp: f64
16}
17
18
19impl<Args: 'static,Data: 'static,Ret: 'static> Trace<Args,Data,Ret> {
20 /// Create a `Trace` with a `Some(retv)`.
21 pub fn new(args: Args, data: Data, retv: Ret, logp: f64) -> Self {
22 Trace { args, data, retv: Some(retv), logp }
23 }
24
25 /// Set `self.retv` to `Some(v)`.
26 pub fn set_retv(&mut self, v: Ret) { self.retv = Some(v); }
27}
28
29
30/// Interface for functions that support the standard inference library.
31///
32/// Implementation follows closely to the Generative Function Interface (GFI), as specified in:
33///
34/// > Gen: A General-Purpose Probabilistic Programming System with Programmable Inference.
35/// > Cusumano-Towner, M. F.; Saad, F. A.; Lew, A.; and Mansinghka, V. K.
36/// > In Proceedings of the 40th ACM SIGPLAN Conference on Programming Language
37/// > Design and Implementation (PLDI ‘19).
38///
39/// Any function that implements `GenFn` can use the standard inference library
40/// to perform Bayesian inference to generate fair samples from the posterior distribution.
41///
42/// `p(trace) ~ p( . | constraints)`
43///
44/// This terminology may be slightly unusual to users from other languages;
45/// `data` refers to all random variables, and `constraints` more precisely
46/// refers to a subset of the data that we observe.
47pub trait GenFn<Args,Data,Ret> {
48
49 /// Execute the generative function and return a sampled trace.
50 fn simulate(&self, args: Args) -> Trace<Args,Data,Ret>;
51
52 /// Execute the generative function consistent with `constraints`.
53 /// Return the log probability
54 /// `log[p(t; args) / q(t; constraints, args)]`
55 fn generate(&self, args: Args, constraints: Data) -> (Trace<Args,Data,Ret>, f64);
56
57 /// Update a trace
58 fn update(&self,
59 trace: Trace<Args,Data,Ret>,
60 args: Args,
61 diff: GfDiff,
62 constraints: Data // Data := forward choices
63 ) -> (Trace<Args,Data,Ret>, Data, f64); // Data := backward choices
64
65 /// Call a generative function and return the output.
66 fn call(&self, args: Args) -> Ret {
67 self.simulate(args).retv.unwrap()
68 }
69
70 /// Use a generative function to propose some data.
71 fn propose(&self, args: Args) -> (Data, f64) {
72 let trace = self.simulate(args);
73 (trace.data, trace.logp)
74 }
75
76 /// Assess the conditional probability of some proposed `constraints` under a generative function.
77 fn assess(&self, args: Args, constraints: Data) -> f64 {
78 let (_, weight) = self.generate(args, constraints);
79 weight
80 }
81
82}
83
84
85/// Flag that gives information about the type of incremental difference a generative
86/// function can expect to a `Trace`'s arguments during an update.
87///
88/// Can be used to increase efficiency for example in particle filter procedures.
89#[derive(Debug,Clone)]
90pub enum GfDiff {
91 /// No change to input arguments.
92 NoChange,
93
94 /// An unknown change to input arguments.
95 Unknown,
96
97 /// An incremental change to input arguments.
98 ///
99 /// Generally means the `trace` has a vector-valued
100 /// `data` field that is being pushed to.
101 Extend
102}