6 Utilities
6.1 Models: Lightweight Modules
syntax
(defmodel model-name body ...+)
Every name defined within the model is exported, but an export bound to unknown syntax (eg, a macro definition) raises an error if used. Names bound with defmem and deflazy may be exported and used safely. Names imported via open-model are automatically re-exported.
Examples: | |||||||||||||||||||||||||||
|
Models can be used to define reusable generative models:
> (defmodel true-strength ; type Person = Symbol ; type Match = (list Team Team) ; strength : Person -> Real (defmem (strength p) (normal 10 2)) ; lazy? : Person -> Boolean (define (lazy? p) (flip 0.1)) ; pulling-power : Person -> Real (define (pulling-power p) (if (lazy? p) (/ (strength p) 2.0) (strength p))) ; team-pulling-power : (Listof Person) -> Real (define (team-pulling-power t) (for/sum ([p (in-list t)]) (pulling-power p))) ; team1-wins? : Match -> Boolean (define (team1-wins? m) (> (team-pulling-power (car m)) (team-pulling-power (cadr m)))) (observe/fail (team1-wins? '((james david) (brian john))) #t))
The generative model can then be combined with different queries:
> (sampler->discrete-dist (mh-sampler (open-model true-strength) (> (strength 'james) (strength 'brian))) 1000) (discrete-dist [#f 0.219] [#t 0.781])
> (sampler->discrete-dist (mh-sampler (open-model true-strength) (team1-wins? '((james david) (bob andrew)))) 1000) (discrete-dist [#f 0.385] [#t 0.615])
syntax
(defmodel+ model-name body ...)
Examples: | ||||||||||||||||
|
syntax
(open-model model-name)
6.2 Lazy Structures
syntax
(lazy-struct name-id (field-id ...))
In addition to the standard names (name, name?, name-field ...), the name strict-make-name is bound to a strict constructor procedure.
Printing, equal? comparison, and hashing all automatically force all fields.
Instances of lazy structs can be destructured with match using one of the following patterns:
(name-id #:strict field-pattern ...) Forces each field and matches it against field-pattern.
(name-id #:thunk field-var-id ...) Binds each field-var-id to a thunk that when applied forces the corresponding field.
Examples: | ||||||||||||||||||||||
|
6.3 Statistical Utilities
struct
(struct statistics (dim n mean cov))
dim : exact-positive-integer? n : exact-positive-integer? mean : col-matrix? cov : matrix?
procedure
(real-vector-like? v) → boolean?
v : any/c
procedure
(samples->statistics samples) → statistics?
samples : (vectorof real-vector-like?)
procedure
(sampler->statistics s n [ f #:burn burn #:thin thin]) → statistics? s : sampler? n : exact-positive-integer? f : (-> any/c real-vector-like?) = values burn : exact-nonnegative-integer? = 0 thin : exact-nonnegative-integer? = 0
(samples->statistics (generate-samples s n f #:burn burn #:thin thin))
Example: | |||||||
|
procedure
(samples->mean+variance rvs) →
real? real? rvs : (vectorof real?)
procedure
(sampler->mean+variance sampler n [ f #:burn burn #:thin thin]) →
real? real? sampler : sampler? n : exact-positive-integer? f : (-> any/c real?) = values burn : exact-nonnegative-integer? = 0 thin : exact-nonnegative-integer? = 0
(samples->mean+variance (generate-samples s n f #:burn burn #:thin thin))
Example: | ||||
|
procedure
(samples->mean rvs) → any/c
rvs : (vectorof any/c)
procedure
(sampler->mean sampler n [ f #:burn burn #:thin thin]) → any/c sampler : weighted-sampler? n : exact-positive-integer? f : (-> any/c any/c) = values burn : exact-nonnegative-integer? = 0 thin : exact-nonnegative-integer? = 0
Examples: | ||||
|
6.4 Utilities for Testing and Comparing Distributions
procedure
(samples->KS samples dist) → real?
samples : (vectorof real?) dist : dist?
Examples: | ||||||||||||
|
procedure
(discrete-dist-error dist1 dist2) → (>=/c 0)
dist1 : discrete-dist? dist2 : discrete-dist?
Example: | |||||
|
In the example above, 1/10 of the probability mass of 'A in the first distribution would have to be shifted to 'B to transform the first distribution into the second.
6.5 Arrays and Matrices
This language provides a wrapped version of the array and matrix types defined in math/array and math/matrix. Unlike the version provided by those libraries, this library’s array and matrix types are specialized to real numbers, and they are simpler and faster to use in untyped code.
Most of the functions listed in the documentation for math/array and math/matrix have corresponding monomorphic-wrapped functions exported by this language. In addition, the following functions and special forms are provided.
procedure
v : any/c
procedure
(mutable-array? v) → boolean?
v : any/c
procedure
v : any/c
procedure
(square-matrix? v) → boolean?
v : any/c
procedure
(row-matrix? v) → boolean?
v : any/c
procedure
(col-matrix? v) → boolean?
v : any/c
struct
contents : (math:Array typed:Real)
struct
contents : (math:Mutable-Array typed:Real)
procedure
(Array-contents a) → (math:Array typed:Real)
a : array?
syntax
(array #[#[...] ...])
syntax
(mutable-array #[#[...] ...])
syntax
(matrix [[element-expr ...] ...])
syntax
(col-matrix [element-expr ...])
syntax
(row-matrix [element-expr ...])
syntax
(for/matrix numrows-expr numcols-expr (for-clause ...) body ...+)
syntax
(for*/matrix numrows-expr numcols-expr (for-clause ...) body ...+)
procedure
(matrix11->value matrix?) → real?
matrix? : m
procedure
(array->immutable-array array?) → array?
array? : a
procedure
(make-mutable-matrix m n fill) → matrix?
m : exact-nonnegative-integer? n : exact-nonnegative-integer? fill : real?
procedure
(matrix-set! m i j value) → void?
m : matrix? i : exact-nonnegative-integer? j : exact-nonnegative-integer? value : real?
procedure
(matrix-symmetric? m) → boolean?
m : matrix?
procedure
(array-sqrt/nan a) → array?
a : array?
procedure
(array-sqrt/err a) → array?
a : array?
procedure
(matrix-cholesky m) → matrix?
m : matrix?
If the Cholesky decomposition cannot be calculated, the function raises an error.
6.6 Miscellaneous Utilities
procedure
(probability? v) → boolean?
v : any/c
procedure
thunk : (-> any/c) n : exact-nonnegative-integer?
Example: | ||
|
procedure
value-or-pred : (or/c (-> any/c) any/c)
procedure
(indicator/value value) → (-> any/c (or/c 1 0))
value : any/c
procedure
(indicator/predicate pred) → (-> any/c (or/c 1 0))
pred : (-> any/c boolean?)
Examples: | ||||||||||||||||||
|
procedure
samples : vector? weights : (vectorof (>=/c 0)) n : exact-nonnegative-integer? = (vector-length samples) algorithm : (or/c 'multinomial 'residual #f) = 'multinomial