Skip to content

Demonstration for Figaro 3.1 improvement

brassel edited this page May 26, 2015 · 1 revision

Here is a simple example which demonstrates the improvements achieved with Figaro version 3.1. The aim of the code is to compute the probability of drawing a pair from pack of poker cards. (As you might know, the result should be 3/51 ~= 0.058823529) Here is a simple representation for the cards.

case class Card(ord:Int) {
  // ordinal of the card's rank, 12 is Ace, 11 is King ...
  val rankOrd : Int = (ord / 4) % 13
  // ordinal of the card's suit, 0 is clubs, 1 is diamonds ...
  val suitOrd : Int = ord % 4
}

With this Card(0) represents the 2 of clubs, Card(1) the 2 of diamonds, and finally Card(51) the ace of spades.

To define our problem we can model drawing two cards like this.

def numberOfCards = 52

def twoCards = {
  def drawCard() = Apply( Uniform(0 until numberOfCards : _*), { i:Int => Card(i) }) 
  val pair = ^^(drawCard(), drawCard())
  pair.setCondition{ cc : (Card,Card) => cc._1 != cc._2 }
  pair
}

Having modelled drawing the cards we can make our query thus.

val p = VariableElimination.probability(twoCards,{ cc : (Card,Card) => 
  cc._1.rankOrd == cc._2.rankOrd 
})
println("probability of two random cards having the same rank is " + p)

Figaro 3.0 would not be able to compute the answer within sufferable time and memory constraints. In Figaro 3.1 the system prints probability of two random cards having the same rank is 0.058823529411764566 in virtually no time at all.