Represents a weighted probability distribution. Supports a tree-structure for weights with Weighted<Weighted<A>>

  enum Coin { H; T; }

  // Heads and Tails equally likely
  var fairCoin = [H, T].uniform();
  var fairFlips = [
      for(i in 0...10)
          fairCoin.draw(Math.random)
  ]; // ex: [T,H,H,T,T,H,T,T,H,T]

  // Tails much more likely than Heads
  var unfairCoin = [H.with(0.25), T.with(0.75)];
  var unfairFlips = [
      for(i in 0...10)
          unfairCoin.draw(Math.random)
  ]; // ex: [T,T,T,T,H,T,H,T,T,H]

Static methods

staticasArray(this:Iterable<Pair<A, Float>>):Array<Pair<A, Float>>

staticasWeighted<A>(t:A):Weighted<A>

Lifts a single value into a weighted distribution

staticdraw<A>(dist:Weighted<A>, randomGen:() ‑> Float):A

Draws a single result from a weighted set. randomGen should be a function that generates a random value between 0 and 1, e.g. Math.random
Clamps the output of randomGen between 0.0 and 1.0

staticdrawIndex<A>(dist:Weighted<A>, randomGen:() ‑> Float):Int

Draws a result from a weighted set and returns its index. randomGen should be a function that generates a random value between 0 and 1, e.g. Math.random
Clamps the output of randomGen between 0.0 and 1.0

staticflatMap<A, B>(dist:Weighted<A>, fn:A ‑> Weighted<B>):Weighted<B>

Projects each element of a weighted distribution of A to a distribution of type B, and flattens the results
Monadic bind/flatMap

staticflatten<A>(dists:Weighted<Weighted<A>>):Weighted<A>

Flattens one layer of a probability tree by multiplying weights together

@:fromstaticfromIterable<A>(value:Iterable<Pair<A, Float>>):Weighted<A>

staticmap<A, B>(dist:Weighted<A>, fn:A ‑> B):Weighted<B>

Maps the A elements of a weighted distribution to a type B using fn

[123.with(1)].map(x -> '$x') == ['123'.with(1)];

staticnormalize<A>(dist:Weighted<A>):Weighted<A>

Normalizes large weights to numbers between 0 and 1

['abc'.with(450), 'def'.with(50)].normalize() == ['abc'.with(0.9), 'def'.with(0.1)];

staticshow<A>(dist:Weighted<A>):String

Pretty-prints the weights in a distribution

statictoString(this:Iterable<Pair<A, Float>>):String

staticuniform<A>(vals:Iterable<A>):Weighted<A>

Creates an even set of weights from a list of elements