Blang

Built-in random variables

The interfaces RealVar and IntVar are automatically imported. As for most random variables, they can be either latent (unobserved, sampled), or fixed (conditioned upon).

RealVar and IntVar are closely related to Java's Double and Integer but the former have implementations allowing mutability for the purpose of efficient sampling. Java's Double and Integer are automatically converted back and forth to RealVar and IntVar (some kind of generalization of auto-boxing).

Blang's linear algebra is based on xlinear which is in turn based on a portfolio of established libraries.

The basic classes there are Matrix, DenseMatrix and SparseMatrix. Blang/XBase allows operator overloading, so you write expressions likes matrix1 * matrix2, 2.0 * matrix, etc. Vectors do not have a distinct type, they are just 1-by-n or n-by-1 matrix. Standard operations are supported using unsurprising syntaxes, e.g. identity(100_000), ones(3,3) matrix.norm, matrix.sum, matrix.readOnlyView, matrix.slice(1, 3, 0, 2), matrix.cholesky, etc. See xlinear for more info.

xlinear is augmented in Blang with the following types:

DenseSimplex: Vector of entries summing to one.

DenseTransitionMatrix: Matrix where each row is a DenseSimplex.

If a variable is only declared, as in random RealVar myVariable or param RealVar myVariable, then it will be initialized using the command line arguments with prefix model.myVariable. Use --help to see the list of arguments. How to customize this behaviour is describe here.

If a variable is provided with a default value, as in random RealVar myVariable ?: fixedReal(42.0) or param RealVar myVariable ?: { /* init block */ }, then the initialization block will be used whenever no command line arguments are provided for this variable. The following are useful for creating initialization blocks:

StaticUtils: Automatically statically imported in Blang meaning can call "StaticUtils::function(..)" as just "function(..)".

ExtensionUtils: Automatically imported as extension methods, meaning functions f(a, b, ..) can be called as a.f(b, ...).

Collections of random variables

As hinted in StaticUtils above, simple collections of random variables can be achieved using Java built-in List objects. However in more complex scenarios we need random variables indexed by several plates.

Plate: in the following, K is the type indexing the replicates, typically an Integer or String. We assume these indices are not random variables.

Plated: a random variable or parameter of type T enclosed in one or more Plates.

Generation of random variables

For random generation, Blang uses bayonet.distributions.Random, a replacement for java.util.Random which by default uses under the hood the Math Commons implementation of MarsenneTwister and is compatible both with Java and Math Commons random types.

To generate specific distributions, use blang.distributions.Generators, the methods of which are automatically imported in Blang model as static extensions, meaning for example to generate a gamma you can just write rand.gamma(shape, rate).

Generators: Various random number generators.