WebPPL - CheatSheet
last revision 2016/12/15 - feedback is welcome: claus.moebus(at)uni-oldenburg.de -
When working through the WebPPL-Tutorial Probabilistic Models of Cognition there came up a desire for a handy cheetsheat.
My cheetsheat is based on:
1. the WebPPL doc
Operators
operand1 + operand2 { + operandi } …
- computes the sum of the operands (eventually after conversion of operands to numerical operands)
- or cocatenates strings (eventually after conversion of operands to string operands)
- Examples:
1 + 1 + 2 ==> 4 // numeric addition
false + false + true + true ==> 2 // addition after conversion
"my name is " + "Claus" ==> my name is Claus // cocatenation of strings
7 + " is my favorite number" ==> 7 is my favorite number // concatenation after conversion
operand1 == operand2
- tests the equality of the operands (eventually after conversion of operands) without respect to types
- Examples:
- 3 == 3 ==> true // equality test with type conversion
- “3” == 3 ==> true // equality test with type conversion
- “3” === 3 ==> false // equality test without type conversion
- true == 1 ==> true // equality test with type conversion
- true === 1 ==> false // equality test without type conversion
operand1 && operand2
- boolean and operator
- Examples:
- true && false ==> false
operand1 || operand2
- boolean or operator
- Examples:
- true || false ==> true
boolean_expr ? expr1 : expr2
- evaluates a conditional expression: evaluates expr1 if boolean_expr is true, otherwise evaluates expr2
- Examples:
true ? (3 * 1) : (5 + 4) ==> 3
false ? (3 * 1) : (5 + 4) ==> 9
(1 == 2) ? false :
(2 == 3) ? false :
(3 == 4) ? false :
(3 == 3) ? true : false ==> true
[{operand ,}]
- creates an array
- Examples:
[] ==> []
A
B
C
Categorical({ps: array_of_probs, vs: array_of_values})
- generates a categorical distribution object over elements of vs with P(vsi) proportional to psi
- array_of_probs : array or vector of probabilities (can be unnormalized) (>0)
- array_of_values: support (array of values)
- Examples:
Categorical({ps:[1/3,1/3,1/3], vs: ["a","b","c"]}) ==> <distribution>
print(Categorical({ps:[1/3,1/3,1/3], vs: ["a","b","c"]})) ==>
==> {"probs":[0.3333333333333333,0.3333333333333333,0.3333333333333333],"support":["a","b","c"]}
D
Date.now()
- JS-function, outputs milliseconds since 1st January 1970 00:00:00 worldtime (UTC)
E
F
flip()
- generates o n e sample from the Bernoulli distribution
- is a paremeterless procedure (= thunk)
- Examples:
flip() ==> false
flip() ==> false
flip() ==> true
G
H
I
if(boolean-expr) {expr1} else {expr2}
Infer({options}, model)
- generates a proper conditional distribution object
- options : {option} or {option, options}
- option : method: ‘forward’, samples: integer || method:‘enumerate’ || …
- model : parameterless function (= thunk) || thunk denoting symbol
- Example:
Infer({method: 'enumerate'}, function() {return flip() + flip()}) ==> <distribution>
print(Infer({method: 'enumerate'}, function() {return flip() + flip()})) ==>
==> {"probs":[0.24999999999999994,0.49999999999999994,0.24999999999999994],"support":[0,1,2]}
J
K
L
N
O
P
Math.PI
- value of pi
- Example:
- Math.PI ==> 3.141592653589793
Math.pow(base, exponent)
- computes base^exponent
- Example:
- Math.pow(3,2) ==> 9
R
repeat(nsamples, thunk)
- generates nsamples from a distribution hidden in a thunk or a thunk-denoting symbol
- Examples:
repeat(3,function() {flip()}) ==> [true,false,false] // thunk
repeat(3,flip) ==> [true,false,true] // thunk-denoting symbol
S
sample(distribution-object)
- draws a sample from the distribution-object , which is either a primitive distribution or a distribution obtained as * the result of marginal inference.
- Examples:
- sample(Categorical({ps:[1/3,1/3,1/3], vs: [“a”,“b”,“c”]})) ==> b
- sample(Categorical({ps:[1/3,1/3,1/3], vs: [“a”,“b”,“c”]})) ==> a
T
U
V
var varname = expression
- defines a new variable and binds it to the value of expression
- Examples:
var x = 3 * 6 ; x ==> 18
var sumFlips = function() { return flip() + flip() + flip() } ; sumFlips() ==> {0,1,2,3}
var y = 5; y = y * 5 ==> error !
var x = 3; var x = 5; var x = x + 6: var x = x * 5; x ==> 55
viz(array_of_values, options)
- displays a histogram of array_of_values
- options : {option} or {option, options}
- option : xLabel: String || xLabel: String || …
- Example:
viz([true,false,true,true,false]) ==> <<false,0.40>,<true,0.60>>
viz([true,false,true,true,false],{xLabel: 'Boolean'}) ==> labelled histogram