probl-engine 0.1.0

Internal to Probl, with no stable API: values, distributions and the world-set interpreter. Use the `probl` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Identity-preserving continuous outcomes during enumeration.
//!
//! A draw is an affine function of one latent variable. Evidence is a set of
//! intervals in that variable's CDF coordinates. Worlds own their restrictions;
//! values and closures remain immutable, and calls return restrictions alongside
//! their results. Independent latent combinations require sampling for now.

use crate::continuous::Family;
use crate::dist::Budget;
use crate::error::{OpError, OpResult};
use crate::value::{Closure, Value, family_key, float_key};
use probl_syntax::ast::BinOp;
use std::collections::BTreeMap;
use std::sync::Arc;

pub type Constraints = Arc<BTreeMap<u64, Domain>>;

/// Sorted, disjoint CDF intervals. Endpoints have zero probability.
#[derive(Clone, Debug, PartialEq)]
pub struct Domain(pub Vec<(f64, f64)>);
impl Domain {
    pub fn full() -> Self {
        Self(vec![(0.0, 1.0)])
    }
    pub fn mass(&self) -> f64 {
        self.0.iter().map(|(a, b)| b - a).sum()
    }
    pub fn intersect(&self, other: &Self) -> Self {
        let (mut i, mut j) = (0, 0);
        let mut out = Vec::new();
        while i < self.0.len() && j < other.0.len() {
            let (a, b) = self.0[i];
            let (c, d) = other.0[j];
            if a.max(c) < b.min(d) {
                out.push((a.max(c), b.min(d)));
            }
            if b < d {
                i += 1;
            } else {
                j += 1;
            }
        }
        Self(out)
    }
    pub fn complement(&self) -> Self {
        let mut out = Vec::new();
        let mut lo = 0.0;
        for &(a, b) in &self.0 {
            if lo < a {
                out.push((lo, a));
            }
            lo = b;
        }
        if lo < 1.0 {
            out.push((lo, 1.0));
        }
        Self(out)
    }
    fn below(p: f64) -> Self {
        if p > 0.0 { Self(vec![(0.0, p)]) } else { Self(vec![]) }
    }
    fn key(&self) -> Vec<(u64, u64)> {
        self.0.iter().map(|(a, b)| (float_key(*a), float_key(*b))).collect()
    }
}
impl Eq for Domain {}
impl std::hash::Hash for Domain {
    fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
        self.key().hash(h);
    }
}
impl Ord for Domain {
    fn cmp(&self, b: &Self) -> std::cmp::Ordering {
        self.key().cmp(&b.key())
    }
}
impl PartialOrd for Domain {
    fn partial_cmp(&self, b: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(b))
    }
}

#[derive(Clone, Debug)]
pub struct Analytic {
    pub id: u64,
    pub family: Family,
    pub domain: Domain,
    pub scale: f64,
    pub offset: f64,
}
impl Analytic {
    pub fn new(id: u64, family: Family) -> Self {
        Self {
            id,
            family,
            domain: Domain::full(),
            scale: 1.0,
            offset: 0.0,
        }
    }
    pub fn key(&self) -> impl Ord + std::hash::Hash + use<> {
        (
            self.id,
            family_key(&self.family),
            self.domain.key(),
            float_key(self.scale),
            float_key(self.offset),
        )
    }
    pub fn value(self) -> OpResult<Value> {
        if !self.scale.is_finite() || !self.offset.is_finite() {
            return Err(unsupported("non-finite affine coefficients"));
        }
        if self.scale == 0.0 {
            Ok(Value::Float(self.offset))
        } else {
            Ok(Value::Analytic(Arc::new(self)))
        }
    }
    pub fn cdf(&self, x: f64) -> f64 {
        let p = self.family.cdf((x - self.offset) / self.scale);
        let below = self.domain.intersect(&Domain::below(p)).mass() / self.domain.mass();
        (if self.scale > 0.0 { below } else { 1.0 - below }).clamp(0.0, 1.0)
    }
    pub fn pdf(&self, x: f64) -> f64 {
        let x = (x - self.offset) / self.scale;
        let u = self.family.cdf(x);
        if self.domain.0.iter().any(|(a, b)| *a <= u && u <= *b) {
            self.family.pdf(x) / (self.scale.abs() * self.domain.mass())
        } else {
            0.0
        }
    }
    pub fn quantile(&self, q: f64) -> f64 {
        let mut left = q * self.domain.mass();
        let n = self.domain.0.len();
        for i in 0..n {
            let (a, b) = self.domain.0[if self.scale > 0.0 { i } else { n - 1 - i }];
            if left <= b - a || i + 1 == n {
                let u = if self.scale > 0.0 { a + left } else { b - left };
                return self.scale * self.family.quantile(u.clamp(a, b)) + self.offset;
            }
            left -= b - a;
        }
        f64::NAN
    }
    pub fn moments(&self) -> (f64, f64) {
        let (mean, variance) = if self.domain == Domain::full() {
            (self.family.mean(), self.family.variance())
        } else {
            let parts: Vec<_> = self
                .domain
                .0
                .iter()
                .map(|&(a, b)| {
                    let (m, v) = self
                        .family
                        .interval_moments(self.family.quantile(a), self.family.quantile(b));
                    (m, v, (b - a) / self.domain.mass())
                })
                .collect();
            let mean = parts.iter().map(|(m, _, p)| m * p).sum::<f64>();
            let variance = parts.iter().map(|(m, v, p)| p * (v + (m - mean).powi(2))).sum();
            (mean, variance)
        };
        (self.scale * mean + self.offset, self.scale * self.scale * variance)
    }

    pub fn sd(&self) -> f64 {
        if self.domain == Domain::full() {
            self.scale.abs() * self.family.sd()
        } else {
            libm::sqrt(self.moments().1)
        }
    }
}

#[derive(Clone, Debug)]
pub struct Event {
    pub draw: Analytic,
    pub yes: Domain,
}
impl Event {
    pub fn key(&self) -> impl Ord + std::hash::Hash + use<> {
        (self.draw.key(), self.yes.key())
    }
    pub fn probability(&self) -> f64 {
        (self.draw.domain.intersect(&self.yes).mass() / self.draw.domain.mass()).clamp(0.0, 1.0)
    }
    pub fn value(self) -> Value {
        let p = self.probability();
        if p == 0.0 || p == 1.0 {
            Value::Bool(p == 1.0)
        } else {
            Value::Event(Arc::new(self))
        }
    }
    pub fn restrict(&self, yes: bool, context: &mut Constraints) -> f64 {
        let prior = &self.draw.domain;
        let domain = prior.intersect(&if yes { self.yes.clone() } else { self.yes.complement() });
        let p = domain.mass() / prior.mass();
        Arc::make_mut(context).insert(self.draw.id, domain);
        p.clamp(0.0, 1.0)
    }
}

pub fn unsupported(what: &str) -> OpError {
    OpError::unsupported(format!("{what} isn't supported for analytic continuous draws yet"))
        .help("use `@mode sample(runs: 10_000)` for this operation")
}

pub fn binary(op: BinOp, a: &Value, b: &Value) -> OpResult<Value> {
    use BinOp::*;
    let (mut x, reverse, other) = match (a, b) {
        (Value::Analytic(x), b) => ((**x).clone(), false, b),
        (a, Value::Analytic(x)) => ((**x).clone(), true, a),
        _ => return Err(unsupported("this operation")),
    };
    let (scale, offset) = match other {
        Value::Analytic(y) if x.id == y.id => (y.scale, y.offset),
        Value::Analytic(_) => return Err(unsupported("combining independent continuous draws")),
        v => (
            0.0,
            v.as_f64()
                .filter(|v| v.is_finite())
                .ok_or_else(|| unsupported("this operand"))?,
        ),
    };
    match op {
        Add => {
            x.scale += scale;
            x.offset += offset;
        }
        Sub => {
            x.scale -= scale;
            x.offset -= offset;
            if reverse {
                x.scale = -x.scale;
                x.offset = -x.offset;
            }
        }
        Mul if scale == 0.0 => {
            x.scale *= offset;
            x.offset *= offset;
        }
        Div if !reverse && scale == 0.0 => {
            if offset == 0.0 {
                return Err(OpError::new("division by zero"));
            }
            x.scale /= offset;
            x.offset /= offset;
        }
        Eq | Ne | Lt | Le | Gt | Ge => {
            x.scale -= scale;
            x.offset -= offset;
            if reverse {
                x.scale = -x.scale;
                x.offset = -x.offset;
            }
            if !x.scale.is_finite() || !x.offset.is_finite() {
                return Err(unsupported("non-finite affine coefficients"));
            }
            if x.scale == 0.0 {
                return Ok(Value::Bool(match op {
                    Eq => x.offset == 0.0,
                    Ne => x.offset != 0.0,
                    Lt => x.offset < 0.0,
                    Le => x.offset <= 0.0,
                    Gt => x.offset > 0.0,
                    _ => x.offset >= 0.0,
                }));
            }
            if op == Eq || op == Ne {
                return Ok(Value::Bool(op == Ne));
            }
            let p = x.family.cdf(-x.offset / x.scale);
            if !p.is_finite() {
                return Err(unsupported("this numerically unstable comparison"));
            }
            let below = Domain::below(p);
            let yes = if matches!(op, Lt | Le) == (x.scale > 0.0) {
                below
            } else {
                below.complement()
            };
            return Ok(Event { draw: x, yes }.value());
        }
        _ => return Err(unsupported("nonlinear arithmetic")),
    }
    x.value()
}

pub fn logic(and: bool, a: &Value, b: &Value) -> OpResult<Value> {
    let (event, other) = match (a, b) {
        (Value::Event(e), b) | (b, Value::Event(e)) => (e, b),
        _ => return Err(unsupported("this logical operation")),
    };
    let mut e = (**event).clone();
    match other {
        Value::Bool(x) => {
            if *x != and {
                return Ok(Value::Bool(*x));
            }
        }
        Value::Event(other) if e.draw.id == other.draw.id => {
            e.yes = if and {
                e.yes.intersect(&other.yes)
            } else {
                e.yes.complement().intersect(&other.yes.complement()).complement()
            };
        }
        _ => {
            return Err(unsupported(
                "combining this event with another probability or independent draw",
            ));
        }
    }
    Ok(e.value())
}

/// Scan through aggregate values too: symbolic numbers must never become
/// categorical keys or be compared by their internal identity as user data.
pub fn contains(v: &Value) -> bool {
    match v {
        Value::Analytic(_) | Value::Event(_) => return true,
        Value::List(_) | Value::Map(_) | Value::Bag(_) | Value::Record(_) | Value::Dist(_) | Value::Closure(_) => {}
        _ => return false,
    }
    let mut pending = vec![v];
    while let Some(v) = pending.pop() {
        match v {
            Value::Analytic(_) | Value::Event(_) => return true,
            Value::List(v) => pending.extend(v.iter()),
            Value::Map(v) => pending.extend(v.iter().flat_map(|(k, v)| [k, v])),
            Value::Bag(v) => pending.extend(v.keys()),
            Value::Record(v) => pending.extend(v.fields.iter().map(|(_, v)| v)),
            Value::Dist(d) => pending.extend(d.outcomes.iter().map(|(v, _)| v)),
            Value::Closure(c) => pending.extend(c.captured.iter()),
            _ => {}
        }
    }
    false
}

/// Latents reachable through an immutable value (including closure captures).
pub fn collect_ids(v: &Value, ids: &mut std::collections::BTreeSet<u64>) {
    let mut pending = vec![v];
    while let Some(v) = pending.pop() {
        match v {
            Value::Analytic(a) => {
                ids.insert(a.id);
            }
            Value::Event(e) => {
                ids.insert(e.draw.id);
            }
            Value::List(v) => pending.extend(v.iter()),
            Value::Map(v) => pending.extend(v.iter().flat_map(|(k, v)| [k, v])),
            Value::Bag(v) => pending.extend(v.keys()),
            Value::Record(v) => pending.extend(v.fields.iter().map(|(_, v)| v)),
            Value::Dist(d) => pending.extend(d.outcomes.iter().map(|(v, _)| v)),
            Value::Closure(c) => pending.extend(c.captured.iter()),
            _ => {}
        }
    }
}

/// Snapshot a value under this world's posterior without mutating aliases.
pub fn resolve(v: &Value, context: &Constraints, budget: &mut Budget) -> OpResult<Value> {
    if context.is_empty() || !contains(v) {
        return Ok(v.clone());
    }
    resolve_at(v, context, budget, 0)
}
fn resolve_at(v: &Value, c: &Constraints, b: &mut Budget, depth: usize) -> OpResult<Value> {
    b.work(1)?;
    if depth > 64 {
        return Err(OpError::limit("analytic value nesting exceeds the limit of 64"));
    }
    let draw = |x: &Analytic| {
        let mut x = x.clone();
        if let Some(d) = c.get(&x.id) {
            x.domain = x.domain.intersect(d);
        }
        x
    };
    let mut child = |v: &Value| resolve_at(v, c, b, depth + 1);
    Ok(match v {
        Value::Analytic(x) => draw(x).value()?,
        Value::Event(e) => Event {
            draw: draw(&e.draw),
            yes: e.yes.clone(),
        }
        .value(),
        Value::List(xs) => Value::list(xs.iter().map(&mut child).collect::<OpResult<_>>()?),
        Value::Record(r) => crate::ops::make_record(
            r.ty.clone(),
            r.fields
                .iter()
                .map(|(k, v)| Ok((k.clone(), child(v)?)))
                .collect::<OpResult<_>>()?,
        ),
        Value::Map(xs) => Value::map(
            xs.iter()
                .map(|(k, v)| Ok((k.clone(), child(v)?)))
                .collect::<OpResult<_>>()?,
        ),
        Value::Closure(f) => Value::Closure(Arc::new(Closure {
            func: f.func,
            captured: f.captured.iter().map(&mut child).collect::<OpResult<_>>()?,
        })),
        Value::Dist(d) => {
            let pairs = d
                .outcomes
                .iter()
                .map(|(v, p)| Ok((child(v)?, *p)))
                .collect::<OpResult<_>>()?;
            crate::ops::combine(pairs, d.missing, b)?
        }
        // Analytic keys and bags are rejected when constructed.
        _ => v.clone(),
    })
}