Skip to content

Instantly share code, notes, and snippets.

@andresilva
Last active January 1, 2016 20:39
Show Gist options
  • Save andresilva/8198607 to your computer and use it in GitHub Desktop.
Save andresilva/8198607 to your computer and use it in GitHub Desktop.
Monty Hall Problem using PFP
import scalaz.syntax.monad._
import spire.math._
import spire.implicits._
import pfp.Distribution._
// data Outcome = Win | Lose
sealed trait Outcome
case object Win extends Outcome
case object Lose extends Outcome
// firstChoice :: Dist Outcome
// firstChoice = uniform [Win,Lose,Lose]
def firstChoice = uniform[Outcome, Rational].apply(Win :: Lose :: Lose :: Nil)
// switch :: Trans Outcome
// switch Win = certainly Lose
// switch Lose = certainly Win
def switch(o: Outcome) = o match {
case Win => certainly[Outcome, Rational](Lose)
case Lose => certainly[Outcome, Rational](Win)
}
firstChoice.plot
//
// Win 1/3 #################################
// Lose 2/3 ##################################################################
//
(firstChoice >>= switch).plot
//
// Win 2/3 ##################################################################
// Lose 1/3 #################################
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment