SlideShare a Scribd company logo
1 of 26
Download to read offline
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

What's hot

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
krishna_093
 

What's hot (20)

ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
Kotlin standard
Kotlin standardKotlin standard
Kotlin standard
 
Going bananas with recursion schemes for fixed point data types
Going bananas with recursion schemes for fixed point data typesGoing bananas with recursion schemes for fixed point data types
Going bananas with recursion schemes for fixed point data types
 
Recursion schemes in Scala
Recursion schemes in ScalaRecursion schemes in Scala
Recursion schemes in Scala
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Google TensorFlow Tutorial
Google TensorFlow TutorialGoogle TensorFlow Tutorial
Google TensorFlow Tutorial
 
Kotlin class
Kotlin classKotlin class
Kotlin class
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3
 
Chaco Step-by-Step
Chaco Step-by-StepChaco Step-by-Step
Chaco Step-by-Step
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional Swift
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
TensorFlow Tutorial
TensorFlow TutorialTensorFlow Tutorial
TensorFlow Tutorial
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Killing The Unit test talk
Killing The Unit test talkKilling The Unit test talk
Killing The Unit test talk
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 

Viewers also liked

Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014
Stefanus Du Toit
 
Real time machine learning
Real time machine learningReal time machine learning
Real time machine learning
Vinoth Kannan
 

Viewers also liked (9)

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

Similar to Probabilistic Programming in Scala

pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
Ti1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismTi1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: Polymorphism
Eelco Visser
 

Similar to Probabilistic Programming in Scala (20)

From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Monadologie
MonadologieMonadologie
Monadologie
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Ti1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismTi1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: Polymorphism
 
Poly-paradigm Java
Poly-paradigm JavaPoly-paradigm Java
Poly-paradigm Java
 
The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3Applied machine learning for search engine relevance 3
Applied machine learning for search engine relevance 3
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

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