SlideShare a Scribd company logo
Probabilistic Programming in Scala

           Guy Van den Broeck
       guy.vandenbroeck@cs.kuleuven.be

          Computer Science Department
          Katholieke Universiteit Leuven


           September 13, 2011
What is a PPL?

     Probabilistic graphical models (Bayesian networks) important in
     machine learning, statistics, robotics, vision, biology, neuroscience,
     artificial intelligence (AI) and cognitive science.
     Probabilistic Programming Languages unify general purpose
     programming with probabilistic modeling
     Examples
          Functional, extending Scheme (Church) or Scala (Figaro,
          FACTORIE, ScalaPPL)
          Logical, extending Prolog (ProbLog, PRISM, BLOG, Dyna)
          Extending C# (Infer.NET)
     Tasks
          Compute probabilities, most likely assignments given observations
          Learn parameters and programs
     Applications in natural language processing, computer vision,
     machine learning, bioinformatics, probabilistic planning, seismic
     monitoring, . . .
PPLs: The Easy Case

  Idea behind functional PPLs
  Any function (A, B, . . .) => R can also operate on probability
  distributions (Distr [A], Distr [B], . . .) => Distr [R]

  Starting Point
  Many library functions operate on Booleans:
       &&, ||, !, ==, !=
       exists, forall

  Idea
    1. Make a version of those functions that operates on distributions and
       returns a Distr[Boolean].
    2. Extend with probabilistic data structures.
    3. Use them to model complex probability distributions.
PPLs: The Easy Case

  Idea behind functional PPLs
  Any function (A, B, . . .) => R can also operate on probability
  distributions (Distr [A], Distr [B], . . .) => Distr [R]

  Starting Point
  Many library functions operate on Booleans:
       &&, ||, !, ==, !=
       exists, forall

  Idea
    1. Make a version of those functions that operates on distributions and
       returns a Distr[Boolean].
    2. Extend with probabilistic data structures.
    3. Use them to model complex probability distributions.
PPLs: The Easy Case

  Idea behind functional PPLs
  Any function (A, B, . . .) => R can also operate on probability
  distributions (Distr [A], Distr [B], . . .) => Distr [R]

  Starting Point
  Many library functions operate on Booleans:
       &&, ||, !, ==, !=
       exists, forall

  Idea
    1. Make a version of those functions that operates on distributions and
       returns a Distr[Boolean].
    2. Extend with probabilistic data structures.
    3. Use them to model complex probability distributions.
PPLs: The Easy Case

  Idea behind functional PPLs
  Any function (A, B, . . .) => R can also operate on probability
  distributions (Distr [A], Distr [B], . . .) => Distr [R]

  Starting Point
  Many library functions operate on Booleans:
       &&, ||, !, ==, !=
       exists, forall

  Idea
    1. Make a version of those functions that operates on distributions and
       returns a Distr[Boolean].
    2. Extend with probabilistic data structures.
    3. Use them to model complex probability distributions.
PPLs: The Easy Case

  Idea behind functional PPLs
  Any function (A, B, . . .) => R can also operate on probability
  distributions (Distr [A], Distr [B], . . .) => Distr [R]

  Starting Point
  Many library functions operate on Booleans:
       &&, ||, !, ==, !=
       exists, forall

  Idea
    1. Make a version of those functions that operates on distributions and
       returns a Distr[Boolean].
    2. Extend with probabilistic data structures.
    3. Use them to model complex probability distributions.
Examples
Boolean formulae


      Random Variables are objects (each object independent)
      val a = Flip(0.3)
      val b = Flip(0.6)

      BooleanDistr has member functions that build Formula objects.
      val xor = a && !b || !a && b

      Run inference on BooleanDistr
      println("Probability = " + xor.probability())


      Probability = 0.54
Boolean formulae


      Random Variables are objects (each object independent)
      val a = Flip(0.3)
      val b = Flip(0.6)

      BooleanDistr has member functions that build Formula objects.
      val xor = a && !b || !a && b

      Run inference on BooleanDistr
      println("Probability = " + xor.probability())


      Probability = 0.54
Boolean formulae


      Random Variables are objects (each object independent)
      val a = Flip(0.3)
      val b = Flip(0.6)

      BooleanDistr has member functions that build Formula objects.
      val xor = a && !b || !a && b

      Run inference on BooleanDistr
      println("Probability = " + xor.probability())


      Probability = 0.54
Boolean formulae


      Random Variables are objects (each object independent)
      val a = Flip(0.3)
      val b = Flip(0.6)

      BooleanDistr has member functions that build Formula objects.
      val xor = a && !b || !a && b

      Run inference on BooleanDistr
      println("Probability = " + xor.probability())


      Probability = 0.54
2-state weather HMM

  abstract class Timestep {
    def rainy: BooleanDistr
    def umbrella = If(rainy, Flip(0.9), Flip(0.1))
  }

  object StartState extends Timestep {
    val rainy = Flip(0.2)
  }

  class SuccessorState(predecessor: Timestep) extends Timestep {
    val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1))
  }


  var timestep: Timestep = StartState
  for(i <- 1 until 2000) timestep = new SuccessorState(timestep)


  println("Probability = " + timestep.umbrella.probability())
2-state weather HMM

  abstract class Timestep {
    def rainy: BooleanDistr
    def umbrella = If(rainy, Flip(0.9), Flip(0.1))
  }

  object StartState extends Timestep {
    val rainy = Flip(0.2)
  }

  class SuccessorState(predecessor: Timestep) extends Timestep {
    val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1))
  }


  var timestep: Timestep = StartState
  for(i <- 1 until 2000) timestep = new SuccessorState(timestep)


  println("Probability = " + timestep.umbrella.probability())
2-state weather HMM

  abstract class Timestep {
    def rainy: BooleanDistr
    def umbrella = If(rainy, Flip(0.9), Flip(0.1))
  }

  object StartState extends Timestep {
    val rainy = Flip(0.2)
  }

  class SuccessorState(predecessor: Timestep) extends Timestep {
    val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1))
  }


  var timestep: Timestep = StartState
  for(i <- 1 until 2000) timestep = new SuccessorState(timestep)


  println("Probability = " + timestep.umbrella.probability())
Probabilistic Data Structures



      Probabilistic List: objects are in the list with a certain probability,
      given by a BooleanDistr
      trait ListDistr[T] extends Distribution[List[T]]{
          def forall(f: T => BooleanDistr) : BooleanDistr
          def exists(f: T => BooleanDistr) : BooleanDistr
      }

      Replace all Boolean by BooleanDistr in member functions
      Many possibilities (Set,Tree,. . . )
Probabilistic Graphs
       Viral marketing
       Learning biological pathways
       Spread of influence in social networks

   class Person {
     val influencedFriends = new ListDistr[Person]
     def influences(target: Person): BooleanDistr = {
         if(target == this) True
         else friends.exists(_.influences(target))
     }
   }


   val p1,p2,p3,p4,p5,p6 = new Person

   val influence1to2 = Flip(0.9)
   n1.influencedFriends += (influence1to2, p2)
   n2.influencedFriends += (influence1to2, p1)
   ...


   println("Probability = " + p1.influences(p4).probability())
Probabilistic Graphs
       Viral marketing
       Learning biological pathways
       Spread of influence in social networks

   class Person {
     val influencedFriends = new ListDistr[Person]
     def influences(target: Person): BooleanDistr = {
         if(target == this) True
         else friends.exists(_.influences(target))
     }
   }


   val p1,p2,p3,p4,p5,p6 = new Person

   val influence1to2 = Flip(0.9)
   n1.influencedFriends += (influence1to2, p2)
   n2.influencedFriends += (influence1to2, p1)
   ...


   println("Probability = " + p1.influences(p4).probability())
Probabilistic Graphs
       Viral marketing
       Learning biological pathways
       Spread of influence in social networks

   class Person {
     val influencedFriends = new ListDistr[Person]
     def influences(target: Person): BooleanDistr = {
         if(target == this) True
         else friends.exists(_.influences(target))
     }
   }


   val p1,p2,p3,p4,p5,p6 = new Person

   val influence1to2 = Flip(0.9)
   n1.influencedFriends += (influence1to2, p2)
   n2.influencedFriends += (influence1to2, p1)
   ...


   println("Probability = " + p1.influences(p4).probability())
Probabilistic Graphs
       Viral marketing
       Learning biological pathways
       Spread of influence in social networks

   class Person {
     val influencedFriends = new ListDistr[Person]
     def influences(target: Person): BooleanDistr = {
         if(target == this) True
         else friends.exists(_.influences(target))
     }
   }


   val p1,p2,p3,p4,p5,p6 = new Person

   val influence1to2 = Flip(0.9)
   n1.influencedFriends += (influence1to2, p2)
   n2.influencedFriends += (influence1to2, p1)
   ...


   println("Probability = " + p1.influences(p4).probability())
Probabilistic Values
      Model any discrete distribution
      class ValDistr[T] extends Distribution[T]{
        def ==(v: T): BooleanDistr
        def map[R](f: T => ValDistr[R]): ValDistr[R]
      }

      Apply any deterministic function to ValDistr arguments
      def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R]
      def Apply[A,B,R](f: (A,B) => R) ...
      ...


   Example
      Two dice: probability that their sum is 8?
      val die1 = Uniform(1 to 6)
      val die2 = Uniform(1 to 6)

      val sum = Apply(_+_)(die1,die2)

      println("Probability = " + (sum == 8).probability())
Probabilistic Values
      Model any discrete distribution
      class ValDistr[T] extends Distribution[T]{
        def ==(v: T): BooleanDistr
        def map[R](f: T => ValDistr[R]): ValDistr[R]
      }

      Apply any deterministic function to ValDistr arguments
      def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R]
      def Apply[A,B,R](f: (A,B) => R) ...
      ...


   Example
      Two dice: probability that their sum is 8?
      val die1 = Uniform(1 to 6)
      val die2 = Uniform(1 to 6)

      val sum = Apply(_+_)(die1,die2)

      println("Probability = " + (sum == 8).probability())
Probabilistic Values
      Model any discrete distribution
      class ValDistr[T] extends Distribution[T]{
        def ==(v: T): BooleanDistr
        def map[R](f: T => ValDistr[R]): ValDistr[R]
      }

      Apply any deterministic function to ValDistr arguments
      def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R]
      def Apply[A,B,R](f: (A,B) => R) ...
      ...


   Example
      Two dice: probability that their sum is 8?
      val die1 = Uniform(1 to 6)
      val die2 = Uniform(1 to 6)

      val sum = Apply(_+_)(die1,die2)

      println("Probability = " + (sum == 8).probability())
3-state weather HMM

  object Sunny extends Weather
  object Foggy extends Weather
  object Rainy extends Weather

  abstract class   Timestep {
    def weather:   ValDistr[Weather]
    def umbrella   = weather.map{
      case Sunny   => Flip(0.1)
      case Foggy   => Flip(0.3)
      case Rainy   => Flip(0.8)
    }
  }

  object StartState extends Timestep {
    val weather = ValDistr((0.3,Sunny), (0.3,Foggy), (0.4,Rainy))
  }

  class SuccessorState(predecessor: Timestep) extends Timestep {
    val weather = predecessor.weather.map{
      case Sunny => ValDistr((0.8,Sunny), (0.15,Foggy), (0.05,Rainy))
      case Foggy => ValDistr((0.05,Sunny), (0.3,Foggy), (0.65,Rainy))
      case Rainy => ValDistr((0.15,Sunny), (0.35,Foggy), (0.5,Rainy))
    }
  }
Why Scala for Probabilistic Programming?



      Probabilistic programming as a library
          No separate compiler or VM
      mixin DSL
      Higher-order functions and generics
          Pass existing deterministic code to probabilistic model
          Memoization for efficient inference
      Object-oriented easy to model probabilistic databases
      Other advantages carry over to the probabilistic case
Thanks

More Related Content

PDF
Beginners python cheat sheet - Basic knowledge
 
PPTX
Introduction to Monads in Scala (2)
PDF
Python Cheat Sheet
PDF
Pybelsberg — Constraint-based Programming in Python
PPTX
Python data structures
PDF
First few months with Kotlin - Introduction through android examples
PDF
ScalaMeter 2014
PDF
Introduction to Scala
Beginners python cheat sheet - Basic knowledge
 
Introduction to Monads in Scala (2)
Python Cheat Sheet
Pybelsberg — Constraint-based Programming in Python
Python data structures
First few months with Kotlin - Introduction through android examples
ScalaMeter 2014
Introduction to Scala

What's hot (20)

PDF
PPTX
Kotlin standard
PDF
Going bananas with recursion schemes for fixed point data types
PDF
Recursion schemes in Scala
PDF
Scala. Introduction to FP. Monads
PDF
Hammurabi
PDF
Monads and Monoids by Oleksiy Dyagilev
PDF
Google TensorFlow Tutorial
PPTX
Kotlin class
PPT
Scala presentation by Aleksandar Prokopec
PDF
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
PDF
An Introduction to Scala (2014)
PPTX
Intro to Python (High School) Unit #3
PDF
Chaco Step-by-Step
PDF
7 Habits For a More Functional Swift
PPT
Introduction to matlab
PDF
TensorFlow Tutorial
PDF
Fp in scala part 2
PDF
Killing The Unit test talk
PDF
Haskell in the Real World
Kotlin standard
Going bananas with recursion schemes for fixed point data types
Recursion schemes in Scala
Scala. Introduction to FP. Monads
Hammurabi
Monads and Monoids by Oleksiy Dyagilev
Google TensorFlow Tutorial
Kotlin class
Scala presentation by Aleksandar Prokopec
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
An Introduction to Scala (2014)
Intro to Python (High School) Unit #3
Chaco Step-by-Step
7 Habits For a More Functional Swift
Introduction to matlab
TensorFlow Tutorial
Fp in scala part 2
Killing The Unit test talk
Haskell in the Real World
Ad

Viewers also liked (9)

PPT
Site 2010
PDF
CSS Lessons Learned the Hard Way (Beyond Tellerand)
PDF
Debugging and Tuning Mobile Web Sites with Modern Web Browsers
PDF
Hourglass Interfaces for C++ APIs - CppCon 2014
PPTX
Realtime Learning: Using Triggers to Know What the ?$# is Going On
PPTX
Challenges of Predicting User Engagement
PPTX
Capturing the Mirage: Machine Learning in Media and Entertainment Industries
PDF
Innovation Driven Procurement
PPTX
Real time machine learning
Site 2010
CSS Lessons Learned the Hard Way (Beyond Tellerand)
Debugging and Tuning Mobile Web Sites with Modern Web Browsers
Hourglass Interfaces for C++ APIs - CppCon 2014
Realtime Learning: Using Triggers to Know What the ?$# is Going On
Challenges of Predicting User Engagement
Capturing the Mirage: Machine Learning in Media and Entertainment Industries
Innovation Driven Procurement
Real time machine learning
Ad

Similar to Probabilistic Programming in Scala (20)

PDF
Fast and Simple Statistics with Scala
PDF
Mathematical Background for Artificial Intelligence
PPTX
ScalaCheck
PPT
Probability statistics assignment help
PDF
Introduction à Scala - Michel Schinz - January 2010
PPT
presentation on Fandamental of Probability
PPT
original
PDF
Probabilistic Programming: Why, What, How, When?
PPT
Probability_Review.ppt
PPT
Probability_Review.ppt
PPT
Probability_Review.ppt
PPT
ppt
PPT
ppt
PDF
Scala or functional programming from a python developer's perspective
PPT
Probability_Review.ppt
PPT
Probability_Review.ppt
PPT
Probability_Review.ppt
PPT
Probability_Review.ppt for your knowledg
PPT
Probability Review for beginner to be used for
PPT
Probability_Review HELPFUL IN STATISTICS.ppt
Fast and Simple Statistics with Scala
Mathematical Background for Artificial Intelligence
ScalaCheck
Probability statistics assignment help
Introduction à Scala - Michel Schinz - January 2010
presentation on Fandamental of Probability
original
Probabilistic Programming: Why, What, How, When?
Probability_Review.ppt
Probability_Review.ppt
Probability_Review.ppt
ppt
ppt
Scala or functional programming from a python developer's perspective
Probability_Review.ppt
Probability_Review.ppt
Probability_Review.ppt
Probability_Review.ppt for your knowledg
Probability Review for beginner to be used for
Probability_Review HELPFUL IN STATISTICS.ppt

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
A Presentation on Artificial Intelligence
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
Chapter 3 Spatial Domain Image Processing.pdf
Programs and apps: productivity, graphics, security and other tools
sap open course for s4hana steps from ECC to s4
Dropbox Q2 2025 Financial Results & Investor Presentation
A Presentation on Artificial Intelligence
NewMind AI Weekly Chronicles - August'25-Week II
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
A comparative analysis of optical character recognition models for extracting...
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation

Probabilistic Programming in Scala

  • 1. Probabilistic Programming in Scala Guy Van den Broeck guy.vandenbroeck@cs.kuleuven.be Computer Science Department Katholieke Universiteit Leuven September 13, 2011
  • 2. What is a PPL? Probabilistic graphical models (Bayesian networks) important in machine learning, statistics, robotics, vision, biology, neuroscience, artificial intelligence (AI) and cognitive science. Probabilistic Programming Languages unify general purpose programming with probabilistic modeling Examples Functional, extending Scheme (Church) or Scala (Figaro, FACTORIE, ScalaPPL) Logical, extending Prolog (ProbLog, PRISM, BLOG, Dyna) Extending C# (Infer.NET) Tasks Compute probabilities, most likely assignments given observations Learn parameters and programs Applications in natural language processing, computer vision, machine learning, bioinformatics, probabilistic planning, seismic monitoring, . . .
  • 3. PPLs: The Easy Case Idea behind functional PPLs Any function (A, B, . . .) => R can also operate on probability distributions (Distr [A], Distr [B], . . .) => Distr [R] Starting Point Many library functions operate on Booleans: &&, ||, !, ==, != exists, forall Idea 1. Make a version of those functions that operates on distributions and returns a Distr[Boolean]. 2. Extend with probabilistic data structures. 3. Use them to model complex probability distributions.
  • 4. PPLs: The Easy Case Idea behind functional PPLs Any function (A, B, . . .) => R can also operate on probability distributions (Distr [A], Distr [B], . . .) => Distr [R] Starting Point Many library functions operate on Booleans: &&, ||, !, ==, != exists, forall Idea 1. Make a version of those functions that operates on distributions and returns a Distr[Boolean]. 2. Extend with probabilistic data structures. 3. Use them to model complex probability distributions.
  • 5. PPLs: The Easy Case Idea behind functional PPLs Any function (A, B, . . .) => R can also operate on probability distributions (Distr [A], Distr [B], . . .) => Distr [R] Starting Point Many library functions operate on Booleans: &&, ||, !, ==, != exists, forall Idea 1. Make a version of those functions that operates on distributions and returns a Distr[Boolean]. 2. Extend with probabilistic data structures. 3. Use them to model complex probability distributions.
  • 6. PPLs: The Easy Case Idea behind functional PPLs Any function (A, B, . . .) => R can also operate on probability distributions (Distr [A], Distr [B], . . .) => Distr [R] Starting Point Many library functions operate on Booleans: &&, ||, !, ==, != exists, forall Idea 1. Make a version of those functions that operates on distributions and returns a Distr[Boolean]. 2. Extend with probabilistic data structures. 3. Use them to model complex probability distributions.
  • 7. PPLs: The Easy Case Idea behind functional PPLs Any function (A, B, . . .) => R can also operate on probability distributions (Distr [A], Distr [B], . . .) => Distr [R] Starting Point Many library functions operate on Booleans: &&, ||, !, ==, != exists, forall Idea 1. Make a version of those functions that operates on distributions and returns a Distr[Boolean]. 2. Extend with probabilistic data structures. 3. Use them to model complex probability distributions.
  • 9. Boolean formulae Random Variables are objects (each object independent) val a = Flip(0.3) val b = Flip(0.6) BooleanDistr has member functions that build Formula objects. val xor = a && !b || !a && b Run inference on BooleanDistr println("Probability = " + xor.probability()) Probability = 0.54
  • 10. Boolean formulae Random Variables are objects (each object independent) val a = Flip(0.3) val b = Flip(0.6) BooleanDistr has member functions that build Formula objects. val xor = a && !b || !a && b Run inference on BooleanDistr println("Probability = " + xor.probability()) Probability = 0.54
  • 11. Boolean formulae Random Variables are objects (each object independent) val a = Flip(0.3) val b = Flip(0.6) BooleanDistr has member functions that build Formula objects. val xor = a && !b || !a && b Run inference on BooleanDistr println("Probability = " + xor.probability()) Probability = 0.54
  • 12. Boolean formulae Random Variables are objects (each object independent) val a = Flip(0.3) val b = Flip(0.6) BooleanDistr has member functions that build Formula objects. val xor = a && !b || !a && b Run inference on BooleanDistr println("Probability = " + xor.probability()) Probability = 0.54
  • 13. 2-state weather HMM abstract class Timestep { def rainy: BooleanDistr def umbrella = If(rainy, Flip(0.9), Flip(0.1)) } object StartState extends Timestep { val rainy = Flip(0.2) } class SuccessorState(predecessor: Timestep) extends Timestep { val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1)) } var timestep: Timestep = StartState for(i <- 1 until 2000) timestep = new SuccessorState(timestep) println("Probability = " + timestep.umbrella.probability())
  • 14. 2-state weather HMM abstract class Timestep { def rainy: BooleanDistr def umbrella = If(rainy, Flip(0.9), Flip(0.1)) } object StartState extends Timestep { val rainy = Flip(0.2) } class SuccessorState(predecessor: Timestep) extends Timestep { val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1)) } var timestep: Timestep = StartState for(i <- 1 until 2000) timestep = new SuccessorState(timestep) println("Probability = " + timestep.umbrella.probability())
  • 15. 2-state weather HMM abstract class Timestep { def rainy: BooleanDistr def umbrella = If(rainy, Flip(0.9), Flip(0.1)) } object StartState extends Timestep { val rainy = Flip(0.2) } class SuccessorState(predecessor: Timestep) extends Timestep { val rainy = If(predecessor.rainy, Flip(0.5), Flip(0.1)) } var timestep: Timestep = StartState for(i <- 1 until 2000) timestep = new SuccessorState(timestep) println("Probability = " + timestep.umbrella.probability())
  • 16. Probabilistic Data Structures Probabilistic List: objects are in the list with a certain probability, given by a BooleanDistr trait ListDistr[T] extends Distribution[List[T]]{ def forall(f: T => BooleanDistr) : BooleanDistr def exists(f: T => BooleanDistr) : BooleanDistr } Replace all Boolean by BooleanDistr in member functions Many possibilities (Set,Tree,. . . )
  • 17. Probabilistic Graphs Viral marketing Learning biological pathways Spread of influence in social networks class Person { val influencedFriends = new ListDistr[Person] def influences(target: Person): BooleanDistr = { if(target == this) True else friends.exists(_.influences(target)) } } val p1,p2,p3,p4,p5,p6 = new Person val influence1to2 = Flip(0.9) n1.influencedFriends += (influence1to2, p2) n2.influencedFriends += (influence1to2, p1) ... println("Probability = " + p1.influences(p4).probability())
  • 18. Probabilistic Graphs Viral marketing Learning biological pathways Spread of influence in social networks class Person { val influencedFriends = new ListDistr[Person] def influences(target: Person): BooleanDistr = { if(target == this) True else friends.exists(_.influences(target)) } } val p1,p2,p3,p4,p5,p6 = new Person val influence1to2 = Flip(0.9) n1.influencedFriends += (influence1to2, p2) n2.influencedFriends += (influence1to2, p1) ... println("Probability = " + p1.influences(p4).probability())
  • 19. Probabilistic Graphs Viral marketing Learning biological pathways Spread of influence in social networks class Person { val influencedFriends = new ListDistr[Person] def influences(target: Person): BooleanDistr = { if(target == this) True else friends.exists(_.influences(target)) } } val p1,p2,p3,p4,p5,p6 = new Person val influence1to2 = Flip(0.9) n1.influencedFriends += (influence1to2, p2) n2.influencedFriends += (influence1to2, p1) ... println("Probability = " + p1.influences(p4).probability())
  • 20. Probabilistic Graphs Viral marketing Learning biological pathways Spread of influence in social networks class Person { val influencedFriends = new ListDistr[Person] def influences(target: Person): BooleanDistr = { if(target == this) True else friends.exists(_.influences(target)) } } val p1,p2,p3,p4,p5,p6 = new Person val influence1to2 = Flip(0.9) n1.influencedFriends += (influence1to2, p2) n2.influencedFriends += (influence1to2, p1) ... println("Probability = " + p1.influences(p4).probability())
  • 21. Probabilistic Values Model any discrete distribution class ValDistr[T] extends Distribution[T]{ def ==(v: T): BooleanDistr def map[R](f: T => ValDistr[R]): ValDistr[R] } Apply any deterministic function to ValDistr arguments def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R] def Apply[A,B,R](f: (A,B) => R) ... ... Example Two dice: probability that their sum is 8? val die1 = Uniform(1 to 6) val die2 = Uniform(1 to 6) val sum = Apply(_+_)(die1,die2) println("Probability = " + (sum == 8).probability())
  • 22. Probabilistic Values Model any discrete distribution class ValDistr[T] extends Distribution[T]{ def ==(v: T): BooleanDistr def map[R](f: T => ValDistr[R]): ValDistr[R] } Apply any deterministic function to ValDistr arguments def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R] def Apply[A,B,R](f: (A,B) => R) ... ... Example Two dice: probability that their sum is 8? val die1 = Uniform(1 to 6) val die2 = Uniform(1 to 6) val sum = Apply(_+_)(die1,die2) println("Probability = " + (sum == 8).probability())
  • 23. Probabilistic Values Model any discrete distribution class ValDistr[T] extends Distribution[T]{ def ==(v: T): BooleanDistr def map[R](f: T => ValDistr[R]): ValDistr[R] } Apply any deterministic function to ValDistr arguments def Apply[A,R](f: (A) => R)(a: ValDistr[A]): ValDistr[R] def Apply[A,B,R](f: (A,B) => R) ... ... Example Two dice: probability that their sum is 8? val die1 = Uniform(1 to 6) val die2 = Uniform(1 to 6) val sum = Apply(_+_)(die1,die2) println("Probability = " + (sum == 8).probability())
  • 24. 3-state weather HMM object Sunny extends Weather object Foggy extends Weather object Rainy extends Weather abstract class Timestep { def weather: ValDistr[Weather] def umbrella = weather.map{ case Sunny => Flip(0.1) case Foggy => Flip(0.3) case Rainy => Flip(0.8) } } object StartState extends Timestep { val weather = ValDistr((0.3,Sunny), (0.3,Foggy), (0.4,Rainy)) } class SuccessorState(predecessor: Timestep) extends Timestep { val weather = predecessor.weather.map{ case Sunny => ValDistr((0.8,Sunny), (0.15,Foggy), (0.05,Rainy)) case Foggy => ValDistr((0.05,Sunny), (0.3,Foggy), (0.65,Rainy)) case Rainy => ValDistr((0.15,Sunny), (0.35,Foggy), (0.5,Rainy)) } }
  • 25. Why Scala for Probabilistic Programming? Probabilistic programming as a library No separate compiler or VM mixin DSL Higher-order functions and generics Pass existing deterministic code to probabilistic model Memoization for efficient inference Object-oriented easy to model probabilistic databases Other advantages carry over to the probabilistic case