4 Derived Stochastic Forms and Functions
This section describes features derived from the primitive stochastic functions.
4.1 Sampling Convenience Functions
The stochastic functions in this section, sometimes called elementary stochastic procedures (ERPs), are convenience functions equivalent to calling sample on an appropriate distribution.
procedure
p : (real-in 0 1) = 1/2
procedure
(binomial count p) → exact-nonnegative-integer?
count : exact-nonnegative-integer? p : (real-in 0 1)
procedure
(categorical weights) → exact-nonnegative-integer?
weights : (vectorof (>=/c 0))
procedure
n : exact-positive-integer?
procedure
(geometric [p]) → exact-nonnegative-integer?
p : (real-in 0 1) = 1/2
procedure
(poisson mean) → exact-nonnegative-integer?
mean : (>/c 0)
(sample (bernoulli-dist p)) (sample (binomial-dist count p)) (sample (categorical-dist weights)) (sample (categorical-dist (make-vector n (/ n)))) (sample (geometric-dist p)) (sample (poisson-dist mean))
procedure
a : (>/c 0) b : (>/c 0)
procedure
mode : real? scale : (>/c 0) = 1
procedure
(exponential mean) → real?
mean : (>/c 0)
procedure
shape : (>/c 0) = 1 scale : (>/c 0) = 1
procedure
mean : real? = 0 scale : (>/c 0) = 1
procedure
mean : real? = 0 stddev : (>/c 0) = 1
procedure
scale : (>/c 0) shape : (>/c 0)
procedure
(uniform hi) → real? hi : real? (uniform lo hi) → real? lo : real? hi : real?
(sample (beta-dist a b)) (sample (cauchy-dist mode scale)) (sample (exponential-dist mean)) (sample (gamma-dist shape scale)) (sample (logistic-dist mean scale)) (sample (normal-dist mean stddev)) (sample (pareto-dist scale shape)) (sample (uniform-dist lo hi))
procedure
(multi-normal mean cov) → col-matrix?
mean : col-matrix? cov : square-matrix?
procedure
(wishart n V) → square-matrix?
n : real? V : square-matrix?
procedure
(inverse-wishart n Vinv) → square-matrix?
n : real? Vinv : square-matrix?
(sample (multi-normal-dist mean cov)) (sample (wishart-dist n V)) (sample (inverse-wishart-dist n Vinv))
procedure
weighted-vals : (listof (cons/c any/c (>=/c 0)))
procedure
vals : (non-empty-listof any/c)
weights : (non-empty-listof (>/c 0)) = (make-vector (vector-length vals) 1)
(sample (make-discrete-dist weighted-vals)) (sample (make-discrete-dist* vals weights))
4.2 Observations
syntax
(observe observable-expr value-expr)
The observe form raises an error if observable-expr does not contain a call to sample in a suitable position and if observable-expr produces a value not equal to value-expr. See also observe/fail.
A observable context (OC) is (currently) defined as follows:
OC | = | [ ] | ||
| | (+ expr ... OC) | |||
| | (cons OC expr) | |||
| | (cons expr OC) | |||
| | (reverse OC) |
Support for other invertible built-in functions will be added in the future.
> (observe (+ 10 (normal 0 1)) 11.5) 11.5
> (observe (cons (bernoulli) (normal 0 1)) (cons 0 0.2)) '(0 . 0.2)
> (observe (build-list 3 (lambda (i) (bernoulli))) '(1 1 0)) '(1 1 0)
> (observe 3 3) eval:6:0: observe: expression is not observable;
it does not sample in an observable context
at: 3
in: (observe 3 3)
> (observe (+ (normal 0 1) 10) 11.5) eval:7:0: observe: expression is not observable;
it does not sample in an observable context
at: (+ (normal 0 1) 10)
in: (observe (+ (normal 0 1) 10) 11.5)
Note: because of floating-point imprecision, the result of observable-expr may not be exactly equal to value-expr.
procedure
(observe/fail v) → void?
v : any/c (observe/fail v1 v2) → void? v1 : any/c v2 : any/c
4.3 Laziness via Memoization
syntax
(pdelay body ...+)
Use pdelay to make a random choice lazy when it may not be relevant to all execution paths.
Examples: | ||||||||||||||
|
syntax
(deflazy id expr)
Examples: | |||||||
|
(define fun-id (mem (lambda (arg-id ...) body ...))) (define fun-id (mem (lambda (arg-id ... . rest-arg-id) body ...)))
4.4 Indexed Tables
syntax
(table ([var-id sequence-expr] ...+) maybe-lazy body ...+)
(table (var-id ...+) body ...+)
maybe-lazy =
| #:lazy
sequence-expr : sequence?
In the first form, the table is finite, and it is eagerly populated with an entry for every combination of elements from each sequence-expr. (Each sequence-expr must be finite.) If a finite table is addressed with indexes that do not occur in sequence-expr, an error is raised.
Examples: | |||||||||
|
If the #:lazy keyword appears after the variable binding sequence, then the table’s entries are not evaluated until they are looked up (see also pdelay).
Examples: | ||||||||
|
In the second form, the table is conceptually infinite, and it is lazily populated as entries are requested. This form is equivalent to (mem (lambda (var-id ...) body ...)).