Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language...

64
Probabilistic Programming Probabilistic Programming Lecture #4: Probabilistic GCL Joost-Pieter Katoen RWTH Lecture Series on Probabilistic Programming 2018 Joost-Pieter Katoen Probabilistic Programming 1/31

Transcript of Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language...

Page 1: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming

Probabilistic Programming

Lecture #4: Probabilistic GCL

Joost-Pieter Katoen

RWTH Lecture Series on Probabilistic Programming 2018

Joost-Pieter Katoen Probabilistic Programming 1/31

Page 2: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Overview

1 Probabilistic Guarded Command Language

2 Operational semantics

3 Recursion

Joost-Pieter Katoen Probabilistic Programming 2/31

Page 3: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Dijkstra’s guarded command language: Syntax

Z skip empty statementZ diverge divergenceZ x := E assignmentZ prog1 ; prog2 sequential compositionZ if (G) prog1 else prog2 choiceZ prog1 [] prog2 non-deterministic choiceZ while (G) prog iteration

Joost-Pieter Katoen Probabilistic Programming 3/31

Page 4: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Elementary pGCL ingredients

Z Program variables x " Vars whose values are fractional numbers

Z Arithmetic expressions E over the program variables

Z Boolean expressions G (guarding a choice or loop) over the programvariables

Z A distribution expression µ ⇥ � � Dist(Q)Z A probability expression p ⇥ � � [0, 1] = Q

Joost-Pieter Katoen Probabilistic Programming 4/31

2X fly

x -y

u Co ) =Dist C Q )

I

p- -

x

Page 5: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Probabilistic GCL: Syntax

Kozen McIver Morgan

Z skip empty statementZ diverge divergenceZ x := E assignmentZ x :r= mu random assignment (x ⇥ ⌅µ)Z prog1 ; prog2 sequential compositionZ if (G) prog1 else prog2 choiceZ prog1 [p] prog2 probabilistic choice

Z while (G) prog iteration

Conditioning in the form of observe-statements omitted for now.

Joost-Pieter Katoen Probabilistic Programming 5/31

/P - I

p -Iotx

4=2 X Eh

p= I p= I5

--

Page 6: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Let’s start simple

x := 0 [0.5] x := 1;y := -1 [0.5] y := 0

This program admits four runs and yields the outcome:

Pr[x =0, y =0] = Pr[x =0, y =�1] = Pr[x =1, y =0] = Pr[x =1, y =�1] = 1/4

Joost-Pieter Katoen Probabilistic Programming 6/31

D flip a fair coin

A flip a fair cosh

- - - -

Page 7: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

A loopy program

For 0 < p < 1 an arbitrary probability:

bool c := true;int i := 0;while (c) {

i++;(c := false [p] c := true)

}

The loopy program models a geometric distribution with parameter p.

Pr[i = N] = (1�p)N�1 � p for N > 0

Joost-Pieter Katoen Probabilistic Programming 7/31

I flip a biased

- - coin

Page 8: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

On termination

bool c := true;int i := 0;while (c) {

i++;(c := false [p] c := true)

}

This program does not always terminate. It almost surely terminates.

Joost-Pieter Katoen Probabilistic Programming 8/31

=Pr { one non - terminating run } = O

Page 9: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

The good, the bad, and the ugly

Joost-Pieter Katoen Probabilistic Programming 9/31

Page 10: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

The good, the bad, and the ugly

Joost-Pieter Katoen Probabilistic Programming 9/31

Page 11: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Duelling cowboys

int cowboyDuel(float a, b) {int t := A [0.5] t := B;bool c := true;while (c) {

if (t = A) {(c := false [a] t := B);

} else {(c := false [b] t := A);

}}return t;

}

Joost-Pieter Katoen Probabilistic Programming 10/31

B

fhitting prob .

of cowboyA

Ll

I←

← turn

{a survivor

Page 12: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Probabilistic Guarded Command Language

Random assignments

The random assignment x ⇥ ⌅µ works as follows:

1. evaluate distribution expression µ in the current program state s2. sample from the resulting probability distribution µ(s) yielding value v with

probability µ(s)(v )3. assign the value v to the variable x .

For denoting distribution expressions, we use the bra–ket notation.

12 � [aã + 1

3 � [bã + 16 � [cã

denotes the distribution µ with µ(a) = 1/2, µ(b) = 1/3, and µ(c) = 1/6. The supportset of µ equals { a, b, c }Examples on the black board.

Joost-Pieter Katoen Probabilistic Programming 11/31

X : = unit Et . . 2*54

① ye 2

unit G . . D

unit Er . . zy ) ( s ) ( s ) =I

t

gsafe .

- - -

Page 13: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

X in I E xti ) t 'z Ex - t )

equivalent

Xi -- xt ' Et ] Xi - x - s

-

× : = 1¥,

Ixtn ) t III. Ex - i >

-X = 2

scx ) -- 2 I → x .

- =xt7

3- → xi=x - s

Page 14: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Overview

1 Probabilistic Guarded Command Language

2 Operational semantics

3 Recursion

Joost-Pieter Katoen Probabilistic Programming 12/31

Page 15: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Why formal semantics matters

Z Unambiguous meaning to all programs

Z Basis for proving correctnessZ of programsZ of program transformationsZ of program equivalenceZ of static analysisZ of compilersZ . . . . . .

Joost-Pieter Katoen Probabilistic Programming 13/31

Page 16: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

The inventors of semantics

Tony Hoare Robert W. Floyd Gordon Plotkin

Christopher Strachey Dana ScottJoost-Pieter Katoen Probabilistic Programming 14/31

ng 8N

rgb ?

ng 6g

Page 17: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Approaches to semantics

Z Operational semantics: (developed by Plotkin)Z The meaning of a program in terms of how it executes on an abstract

machine.Z Useful for modelling the execution behaviour of a program.

Z Axiomatic semantics: (developed by Floyd and Hoare)Z Provides correctness assertions for each program construct.Z Useful for verifying that a program’s computed results are correct with

respect to the specification.

Z Denotational semantics: (developed by Strachey and Scott)Z Provides a mapping of language constructs onto mathematical objects.Z Useful for obtaining an abstract insight into the working of a program.

Today: operational semantics of pGCL in terms of Markov chains.

Later: denotational semantics of pGCL in terms of weakest preconditions.

Joost-Pieter Katoen Probabilistic Programming 15/31

Page 18: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Approaches to semantics

Z Operational semantics: (developed by Plotkin)Z The meaning of a program in terms of how it executes on an abstract

machine.Z Useful for modelling the execution behaviour of a program.

Z Axiomatic semantics: (developed by Floyd and Hoare)Z Provides correctness assertions for each program construct.Z Useful for verifying that a program’s computed results are correct with

respect to the specification.

Z Denotational semantics: (developed by Strachey and Scott)Z Provides a mapping of language constructs onto mathematical objects.Z Useful for obtaining an abstract insight into the working of a program.

Today: operational semantics of pGCL in terms of Markov chains.

Later: denotational semantics of pGCL in terms of weakest preconditions.

Joost-Pieter Katoen Probabilistic Programming 15/31

{ pre } p L post )

Page 19: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Approaches to semantics

Z Operational semantics: (developed by Plotkin)Z The meaning of a program in terms of how it executes on an abstract

machine.Z Useful for modelling the execution behaviour of a program.

Z Axiomatic semantics: (developed by Floyd and Hoare)Z Provides correctness assertions for each program construct.Z Useful for verifying that a program’s computed results are correct with

respect to the specification.

Z Denotational semantics: (developed by Strachey and Scott)Z Provides a mapping of language constructs onto mathematical objects.Z Useful for obtaining an abstract insight into the working of a program.

Today: operational semantics of pGCL in terms of Markov chains.

Later: denotational semantics of pGCL in terms of weakest preconditions.

Joost-Pieter Katoen Probabilistic Programming 15/31

Page 20: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Approaches to semantics

Z Operational semantics: (developed by Plotkin)Z The meaning of a program in terms of how it executes on an abstract

machine.Z Useful for modelling the execution behaviour of a program.

Z Axiomatic semantics: (developed by Floyd and Hoare)Z Provides correctness assertions for each program construct.Z Useful for verifying that a program’s computed results are correct with

respect to the specification.

Z Denotational semantics: (developed by Strachey and Scott)Z Provides a mapping of language constructs onto mathematical objects.Z Useful for obtaining an abstract insight into the working of a program.

Today: operational semantics of pGCL in terms of Markov chains.

Later: denotational semantics of pGCL in terms of weakest preconditions.

Joost-Pieter Katoen Probabilistic Programming 15/31

00

- -

Page 21: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Approaches to semantics

Z Operational semantics: (developed by Plotkin)Z The meaning of a program in terms of how it executes on an abstract

machine.Z Useful for modelling the execution behaviour of a program.

Z Axiomatic semantics: (developed by Floyd and Hoare)Z Provides correctness assertions for each program construct.Z Useful for verifying that a program’s computed results are correct with

respect to the specification.

Z Denotational semantics: (developed by Strachey and Scott)Z Provides a mapping of language constructs onto mathematical objects.Z Useful for obtaining an abstract insight into the working of a program.

Today: operational semantics of pGCL in terms of Markov chains.

Later: denotational semantics of pGCL in terms of weakest preconditions.

Joost-Pieter Katoen Probabilistic Programming 15/31

- -

Page 22: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Structural operational semantics: ingredients

Z Variable valuation s ⇥ Vars � Q maps each program variable onto avalue (here: rational numbers)

Z Expression valuation1, let [[E ]] denote the valuation of expression E

Z Configuration (aka: state) ÖP, sã denotes thatZ program P is about to be executed (aka: program counter)Z and the current variable valuation equals s.

Z Transition rules for the execution of commands: ÖP, sã ∫ ÖP ¨, s ¨ãtransition rules are written as

premiseconclusion

where the premise is omitted if it is vacuously true.

1Here, we will not go into the details of this (simple) part.Joost-Pieter Katoen Probabilistic Programming 16/31

s C x ) = Is Cy) = n

p GCL- MC

program

Page 23: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Structural operational semantics: ingredients

Z Variable valuation s ⇥ Vars � Q maps each program variable onto avalue (here: rational numbers)

Z Expression valuation1, let [[E ]] denote the valuation of expression E

Z Configuration (aka: state) ÖP, sã denotes thatZ program P is about to be executed (aka: program counter)Z and the current variable valuation equals s.

Z Transition rules for the execution of commands: ÖP, sã ∫ ÖP ¨, s ¨ãtransition rules are written as

premiseconclusion

where the premise is omitted if it is vacuously true.

1Here, we will not go into the details of this (simple) part.Joost-Pieter Katoen Probabilistic Programming 16/31

0scx ) - Zz E=zx2tySly ) - I

FED ( s ) = 2. (E)"

+1

Page 24: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Structural operational semantics: ingredients

Z Variable valuation s ⇥ Vars � Q maps each program variable onto avalue (here: rational numbers)

Z Expression valuation1, let [[E ]] denote the valuation of expression E

Z Configuration (aka: state) ÖP, sã denotes thatZ program P is about to be executed (aka: program counter)Z and the current variable valuation equals s.

Z Transition rules for the execution of commands: ÖP, sã ∫ ÖP ¨, s ¨ãtransition rules are written as

premiseconclusion

where the premise is omitted if it is vacuously true.

1Here, we will not go into the details of this (simple) part.Joost-Pieter Katoen Probabilistic Programming 16/31

MC

Page 25: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Structural operational semantics: ingredients

Z Variable valuation s ⇥ Vars � Q maps each program variable onto avalue (here: rational numbers)

Z Expression valuation1, let [[E ]] denote the valuation of expression E

Z Configuration (aka: state) ÖP, sã denotes thatZ program P is about to be executed (aka: program counter)Z and the current variable valuation equals s.

Z Transition rules for the execution of commands: ÖP, sã ∫ ÖP ¨, s ¨ãtransition rules are written as

premiseconclusion

where the premise is omitted if it is vacuously true.

1Here, we will not go into the details of this (simple) part.Joost-Pieter Katoen Probabilistic Programming 16/31

Page 26: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Recall: Markov chains

A Markov chain (MC) is a triple (�, ‡I , P) with:Z � being a countable set of statesZ ‡I " � the initial state, andZ P ⇥ � � Dist(�) the transition probability function

where Dist(�) is a discrete probability measure on �.

Joost-Pieter Katoen Probabilistic Programming 17/31

- -

Page 27: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Operational semantics

Aim: Model the behaviour of a pGCL program P by the MC [[P ]].Approach:

Z Take states of the formZ ÖQ, sã with program Q or ⇤, and variable valuation s ⇥ Vars � QZ Ösinkã models program termination (successful or violated observation)

Z Take initial state ‡I = ÖP, sã where s fulfils the initial conditionsZ Transition relation � is the smallest relation satisfying the SOS rules

on the next slidesZ Where transition probabilities equal to one are omitted

Joost-Pieter Katoen Probabilistic Programming 18/31

O-

< t,

s > a

•( P

,s ) mum > I

,

s 7^-3 C sink >

thymus diverging behaviours

Page 28: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Operational semantics

Aim: Model the behaviour of a pGCL program P by the MC [[P ]].Approach:

Z Take states of the formZ ÖQ, sã with program Q or ⇤, and variable valuation s ⇥ Vars � QZ Ösinkã models program termination (successful or violated observation)

Z Take initial state ‡I = ÖP, sã where s fulfils the initial conditions

Z Transition relation � is the smallest relation satisfying the SOS ruleson the next slides

Z Where transition probabilities equal to one are omitted

Joost-Pieter Katoen Probabilistic Programming 18/31

Page 29: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Operational semantics

Aim: Model the behaviour of a pGCL program P by the MC [[P ]].Approach:

Z Take states of the formZ ÖQ, sã with program Q or ⇤, and variable valuation s ⇥ Vars � QZ Ösinkã models program termination (successful or violated observation)

Z Take initial state ‡I = ÖP, sã where s fulfils the initial conditionsZ Transition relation � is the smallest relation satisfying the SOS rules

on the next slidesZ Where transition probabilities equal to one are omitted

Joost-Pieter Katoen Probabilistic Programming 18/31

Page 30: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (1)

Öskip, sã � Ö⇤, sã Ödiverge, sã � Ödiverge, sã

Ö⇤, sã � Ösinkã Ösinkã � ÖsinkãÖx ⇥= E , sã � Ö⇤, s[x ⇥= s([[E ]])]ã

µ(s)(v ) = a > 0Öx ⇥ ⌅µ, sã a��� Ö⇤, s[x ⇥= v ]ãÖP[ p]Q, sã � µ with µ(ÖP, sã) = p and µ(ÖQ, sã) = 1�p

Joost-Pieter Katoen Probabilistic Programming 19/31

a a

-

Page 31: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (1)

Öskip, sã � Ö⇤, sã Ödiverge, sã � Ödiverge, sãÖ⇤, sã � Ösinkã Ösinkã � Ösinkã

Öx ⇥= E , sã � Ö⇤, s[x ⇥= s([[E ]])]ãµ(s)(v ) = a > 0Öx ⇥ ⌅µ, sã a��� Ö⇤, s[x ⇥= v ]ã

ÖP[ p]Q, sã � µ with µ(ÖP, sã) = p and µ(ÖQ, sã) = 1�p

Joost-Pieter Katoen Probabilistic Programming 19/31

1 I

Page 32: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (1)

Öskip, sã � Ö⇤, sã Ödiverge, sã � Ödiverge, sãÖ⇤, sã � Ösinkã Ösinkã � ÖsinkãÖx ⇥= E , sã � Ö⇤, s[x ⇥= s([[E ]])]ã

µ(s)(v ) = a > 0Öx ⇥ ⌅µ, sã a��� Ö⇤, s[x ⇥= v ]ãÖP[ p]Q, sã � µ with µ(ÖP, sã) = p and µ(ÖQ, sã) = 1�p

Joost-Pieter Katoen Probabilistic Programming 19/31

P Falu ale E in s

2×2 t y-

I = {u ifEat

value so ×

s ( z ) if x I 2-

Page 33: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (1)

Öskip, sã � Ö⇤, sã Ödiverge, sã � Ödiverge, sãÖ⇤, sã � Ösinkã Ösinkã � ÖsinkãÖx ⇥= E , sã � Ö⇤, s[x ⇥= s([[E ]])]ã

µ(s)(v ) = a > 0Öx ⇥ ⌅µ, sã a��� Ö⇤, s[x ⇥= v ]ã

ÖP[ p]Q, sã � µ with µ(ÖP, sã) = p and µ(ÖQ, sã) = 1�p

Joost-Pieter Katoen Probabilistic Programming 19/31

OO

f Sly ) = 2

X inn X : = unit Er . . ay ]-

V =3 a = Ig

Page 34: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (1)

Öskip, sã � Ö⇤, sã Ödiverge, sã � Ödiverge, sãÖ⇤, sã � Ösinkã Ösinkã � ÖsinkãÖx ⇥= E , sã � Ö⇤, s[x ⇥= s([[E ]])]ã

µ(s)(v ) = a > 0Öx ⇥ ⌅µ, sã a��� Ö⇤, s[x ⇥= v ]ãÖP[ p]Q, sã � µ with µ(ÖP, sã) = p and µ(ÖQ, sã) = 1�p

Joost-Pieter Katoen Probabilistic Programming 19/31

to

C- Dist CE )

Page 35: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (2)

ÖP, sã � µÖP; Q, sã � ‹with ‹(ÖP ¨; Q ¨, s ¨ã) = µ(ÖP ¨, s ¨ã) where ⇤; Q = Q

s Ï GÖif (G){P} else {Q}, sã � ÖP, sã s /Ï GÖif (G){P} else {Q}, sã � ÖQ, sãs Ï GÖwhile(G){P}, sã � ÖP; while (G){P}, sã s /Ï GÖwhile(G){P}, sã � Ö⇤, sã

Joost-Pieter Katoen Probabilistic Programming 20/31

( X :=E,

s ) → A,

s'

✓E Dist CE) ✓

--

- -

i - -

O

I

( P,

s ) -4C P'

,s

' ) ( P, D → Chs ' )

- -

( P ;Q ,s ) ( P

'

;Q ,s

' ) ( P ;Q,s ) → ( I;nQ,s7=Q

Page 36: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (2)

ÖP, sã � µÖP; Q, sã � ‹with ‹(ÖP ¨; Q ¨, s ¨ã) = µ(ÖP ¨, s ¨ã) where ⇤; Q = Q

s Ï GÖif (G){P} else {Q}, sã � ÖP, sã s /Ï GÖif (G){P} else {Q}, sã � ÖQ, sã

s Ï GÖwhile(G){P}, sã � ÖP; while (G){P}, sã s /Ï GÖwhile(G){P}, sã � Ö⇤, sã

Joost-Pieter Katoen Probabilistic Programming 20/31

-

SCD = } Gee X 72gS KG

Sly ) =3s G six ) > 2 Sb )

⇒ § > 2- I

s # G

Page 37: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Transition rules (2)

ÖP, sã � µÖP; Q, sã � ‹with ‹(ÖP ¨; Q ¨, s ¨ã) = µ(ÖP ¨, s ¨ã) where ⇤; Q = Q

s Ï GÖif (G){P} else {Q}, sã � ÖP, sã s /Ï GÖif (G){P} else {Q}, sã � ÖQ, sãs Ï GÖwhile(G){P}, sã � ÖP; while (G){P}, sã s /Ï GÖwhile(G){P}, sã � Ö⇤, sã

Joost-Pieter Katoen Probabilistic Programming 20/31

Page 38: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Example

Joost-Pieter Katoen Probabilistic Programming 21/31

Page 39: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Xiao C I ) x : - n ; yin unit Er . . no ]- -

P Q

( P,

s > s x : - o,

s )-

SP ; Q,

s ) s x .to ; Q,

s )

= S'

(x :-O,

s > Is at,

FEET>

-Sxieoso > Is Ct ; Q,

s Tx : -03 )-

= Q

unif En . . no ] ( s' ) ( 6) = It 7 O

-

( y in unif a . . no ] ,so > ¥-3 S I

,s

'

Ey .

. =D )-

S' Cx ) = O

Sky ) - b

Page 40: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

to

+SP ; Q

,s > Zz

← →

SXi=o;Q,

s ) ( x 7510 ,'s )

it :.

.

( d ; Q ,s Ex : -03 )

. .

,

ditto

-

wx C t,

@Ex :-D ) Ey -

- no ] )

hit( sink )

On

what changes if

x : -0 C }3x ; yin wife ..

x ]

Page 41: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

P :

:-X: -0 ; Cien ; while (c) { Xie a - x ;

Cin ft ) - Ot

⇒ . i )x :

# tr

SP ,s )

t( . Cien ; . . . .

,S Ex :-D )

t

swhileCd-.is

I s

::÷÷÷:← ¥

( while. . .

,x⇒

,⇐ o ) C while

,

^ f s×

?.

&,

Xen,

c- o ) → C sink ) !

Page 42: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Duelling cowboys

int cowboyDuel(float a, b) {int t := A [0.5] t := B;bool c := true;while (c) {

if (t = A) {(c := false [a] t := B);

} else {(c := false [b] t := A);

}}return t;

}

This (parametric) MC is finite. Once we count the number of shots before one of thecowboys dies, the MC becomes countably infinite.

Joost-Pieter Katoen Probabilistic Programming 22/31

Page 43: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Duelling cowboys

int cowboyDuel(float a, b) {int t := A [0.5] t := B;bool c := true;while (c) {

if (t = A) {(c := false [a] t := B);

} else {(c := false [b] t := A);

}}return t;

}

11 A 0

4 A 0

6 A 1

a

! 2 • •H--+13 B •

3 A * I 8

V.>--'-r-L....I

B I

1- b

4 A 1 8 B I

5A l \ 581

\ 6 A l 4 B I

1 - a

6 /\

b

8 B 1

4 B 0

I I B 0

This (parametric) MC is finite. Once we count the number of shots before one of thecowboys dies, the MC becomes countably infinite.

Joost-Pieter Katoen Probabilistic Programming 22/31

±ft

2 § z← c

: O.OI O O

%OO

.

Page 44: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Duelling cowboys

int cowboyDuel(float a, b) {int t := A [0.5] t := B;bool c := true;while (c) {

if (t = A) {(c := false [a] t := B);

} else {(c := false [b] t := A);

}}return t;

}

11 A 0

4 A 0

6 A 1

a

! 2 • •H--+13 B •

3 A * I 8

V.>--'-r-L....I

B I

1- b

4 A 1 8 B I

5A l \ 581

\ 6 A l 4 B I

1 - a

6 /\

b

8 B 1

4 B 0

I I B 0

This (parametric) MC is finite.

Once we count the number of shots before one of thecowboys dies, the MC becomes countably infinite.

Joost-Pieter Katoen Probabilistic Programming 22/31

Page 45: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Duelling cowboys

int cowboyDuel(float a, b) {int t := A [0.5] t := B;bool c := true;while (c) {

if (t = A) {(c := false [a] t := B);

} else {(c := false [b] t := A);

}}return t;

}

11 A 0

4 A 0

6 A 1

a

! 2 • •H--+13 B •

3 A * I 8

V.>--'-r-L....I

B I

1- b

4 A 1 8 B I

5A l \ 581

\ 6 A l 4 B I

1 - a

6 /\

b

8 B 1

4 B 0

I I B 0

This (parametric) MC is finite. Once we count the number of shots before one of thecowboys dies, the MC becomes countably infinite.

Joost-Pieter Katoen Probabilistic Programming 22/31

int EE O

itt

it

Page 46: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Playing with geometric distributions

Z X is a random variable, geometrically distributed with parameter pZ Y is a random variable, geometrically distributed with parameter q

Q: generate a sample x , say, according to the random variable X � Y

int XminY1(float p, q){ // 0 <= p, q <= 1int x := 0;bool flip := false;while (not flip) { // take a sample of X to increase x

(x +:= 1 [p] flip := true);}flip := false;while (not flip) { // take a sample of Y to decrease x

(x -:= 1 [q] flip := true);}return x; // a sample of X-Y

}

Joost-Pieter Katoen Probabilistic Programming 23/31

Page 47: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Playing with geometric distributions

Z X is a random variable, geometrically distributed with parameter p

Z Y is a random variable, geometrically distributed with parameter qQ: generate a sample x , say, according to the random variable X � Y

int XminY1(float p, q){ // 0 <= p, q <= 1int x := 0;bool flip := false;while (not flip) { // take a sample of X to increase x

(x +:= 1 [p] flip := true);}flip := false;while (not flip) { // take a sample of Y to decrease x

(x -:= 1 [q] flip := true);}return x; // a sample of X-Y

}

Joost-Pieter Katoen Probabilistic Programming 23/31

Page 48: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Playing with geometric distributions

Z X is a random variable, geometrically distributed with parameter pZ Y is a random variable, geometrically distributed with parameter q

Q: generate a sample x , say, according to the random variable X � Y

int XminY1(float p, q){ // 0 <= p, q <= 1int x := 0;bool flip := false;while (not flip) { // take a sample of X to increase x

(x +:= 1 [p] flip := true);}flip := false;while (not flip) { // take a sample of Y to decrease x

(x -:= 1 [q] flip := true);}return x; // a sample of X-Y

}

Joost-Pieter Katoen Probabilistic Programming 23/31

Page 49: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Playing with geometric distributions

Z X is a random variable, geometrically distributed with parameter pZ Y is a random variable, geometrically distributed with parameter q

Q: generate a sample x , say, according to the random variable X � Y

int XminY1(float p, q){ // 0 <= p, q <= 1int x := 0;bool flip := false;while (not flip) { // take a sample of X to increase x

(x +:= 1 [p] flip := true);}flip := false;while (not flip) { // take a sample of Y to decrease x

(x -:= 1 [q] flip := true);}return x; // a sample of X-Y

}

Joost-Pieter Katoen Probabilistic Programming 23/31

Page 50: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Playing with geometric distributions

Z X is a random variable, geometrically distributed with parameter pZ Y is a random variable, geometrically distributed with parameter q

Q: generate a sample x , say, according to the random variable X � Y

int XminY1(float p, q){ // 0 <= p, q <= 1int x := 0;bool flip := false;while (not flip) { // take a sample of X to increase x

(x +:= 1 [p] flip := true);}flip := false;while (not flip) { // take a sample of Y to decrease x

(x -:= 1 [q] flip := true);}return x; // a sample of X-Y

}

Joost-Pieter Katoen Probabilistic Programming 23/31

Page 51: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

An alternative program

int XminY2(float p, q){int x := 0;bool flip := false;(flip := false [0.5] flip := true); // flip a fair coinif (not flip) {

while (not flip) { // sample X to increase x(x +:= 1 [p] flip := true);

}} else {

flip := false; // reset flipwhile (not flip) { // sample Y to decrease x

x -:= 1;(skip [q] flip := true);

}}

return x; // a sample of X-Y}

Joost-Pieter Katoen Probabilistic Programming 24/31

Page 52: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Program equivalence: X � Y

int XminY1(float p, q){int x, c := 0, 1;while (c) {

(x +:= 1 [p] c := 0);}c := 1;while (c) {

(x -:= 1 [q] c := 0);}return x;

}

int XminY2(float p, q){int x := 0;(c := 0 [0.5] c := 1);if (c) {

while (c) {(x +:= 1 [p] c := 0);

}} else {

c := 1;while (c) {

x -:= 1;(skip [q] c := 0);

}}

return x;}

The probability that x = k for some k " Z coincides for both programs if and only ifq = 1

2�p .

Joost-Pieter Katoen Probabilistic Programming 25/31

Page 53: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Program equivalence: X � Y

int XminY1(float p, q){int x, c := 0, 1;while (c) {

(x +:= 1 [p] c := 0);}c := 1;while (c) {

(x -:= 1 [q] c := 0);}return x;

}

int XminY2(float p, q){int x := 0;(c := 0 [0.5] c := 1);if (c) {

while (c) {(x +:= 1 [p] c := 0);

}} else {

c := 1;while (c) {

x -:= 1;(skip [q] c := 0);

}}

return x;}

The probability that x = k for some k " Z coincides for both programs if and only ifq = 1

2�p .

Joost-Pieter Katoen Probabilistic Programming 25/31

Page 54: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Program equivalence: X � Y

int XminY1(float p, q){int x, c := 0, 1;while (c) {

(x +:= 1 [p] c := 0);}c := 1;while (c) {

(x -:= 1 [q] c := 0);}return x;

}

int XminY2(float p, q){int x := 0;(c := 0 [0.5] c := 1);if (c) {

while (c) {(x +:= 1 [p] c := 0);

}} else {

c := 1;while (c) {

x -:= 1;(skip [q] c := 0);

}}

return x;}

The probability that x = k for some k " Z coincides for both programs if and only ifq = 1

2�p .

Joost-Pieter Katoen Probabilistic Programming 25/31

Page 55: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Program equivalence: X � Y

int XminY1(float p, q){int x, c := 0, 1;while (c) {

(x +:= 1 [p] c := 0);}c := 1;while (c) {

(x -:= 1 [q] c := 0);}return x;

}

int XminY2(float p, q){int x := 0;(c := 0 [0.5] c := 1);if (c) {

while (c) {(x +:= 1 [p] c := 0);

}} else {

c := 1;while (c) {

x -:= 1;(skip [q] c := 0);

}}

return x;}

The probability that x = k for some k " Z coincides for both programs if and only ifq = 1

2�p .Joost-Pieter Katoen Probabilistic Programming 25/31

Page 56: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

The outcome of a pGCL program

Unlike a deterministic program, a pGCL program P has not an output for a giveninput. Instead, it yields a unique probability distribution over its final states.

In fact, this is a sub-distribution (probability mass at most one), as with a certainprobability P may diverge.

Let P be a pGCL program and s an input state. Then the distribution over finalstates obtained by running P starting in s is given by Pr(s Ï É Ö⇤, �ã).

Joost-Pieter Katoen Probabilistic Programming 26/31

Page 57: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

The outcome of a pGCL program

Unlike a deterministic program, a pGCL program P has not an output for a giveninput. Instead, it yields a unique probability distribution over its final states.

In fact, this is a sub-distribution (probability mass at most one), as with a certainprobability P may diverge.

Let P be a pGCL program and s an input state. Then the distribution over finalstates obtained by running P starting in s is given by Pr(s Ï É Ö⇤, �ã).

Joost-Pieter Katoen Probabilistic Programming 26/31

- -

Stys>Stx ) = .

Sly ) = .

W

Page 58: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

The outcome of a pGCL program

Unlike a deterministic program, a pGCL program P has not an output for a giveninput. Instead, it yields a unique probability distribution over its final states.

In fact, this is a sub-distribution (probability mass at most one), as with a certainprobability P may diverge.

Let P be a pGCL program and s an input state. Then the distribution over finalstates obtained by running P starting in s is given by Pr(s Ï É Ö⇤, �ã).

Joost-Pieter Katoen Probabilistic Programming 26/31

d. s >

fr : I → To , n ]

I MCD = s

TE Z

E meds a

re Z

Page 59: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

The outcome of a pGCL program

Unlike a deterministic program, a pGCL program P has not an output for a giveninput. Instead, it yields a unique probability distribution over its final states.

In fact, this is a sub-distribution (probability mass at most one), as with a certainprobability P may diverge.

Let P be a pGCL program and s an input state. Then the distribution over finalstates obtained by running P starting in s is given by Pr(s Ï É Ö⇤, �ã).

Joost-Pieter Katoen Probabilistic Programming 26/31

-

Page 60: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Operational semantics

Reachability probabilities

If the MC [[P ]] of pGCL program P has finitely many states, reachabilityprobabilities can be obtained in an automated manner. This applies to the

cowboy example for given probabilities a and b.

The same holds for expected rewards, e.g., the expected number of steps untiltermination of a finite-state program P.

Joost-Pieter Katoen Probabilistic Programming 27/31

± 's

Page 61: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Recursion

Overview

1 Probabilistic Guarded Command Language

2 Operational semantics

3 Recursion

Joost-Pieter Katoen Probabilistic Programming 28/31

Page 62: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Recursion

Probabilistic GCL with recursion: Syntax

Z skip empty statementZ diverge divergenceZ x := E assignmentZ x :r= mu random assignment (x ⇥ ⌅µ)Z prog1 ; prog2 sequential compositionZ if (G) prog1 else prog2 choiceZ prog1 [p] prog2 probabilistic choiceZ while (G) prog iterationZ P = prog process definition

Z call P process invocation

Recursion does not increase the expressive power, but is often convenient.

Joost-Pieter Katoen Probabilistic Programming 29/31

8

Page 63: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Recursion

Pushdown Markov chains

Pushdown Markov chain

A pushdown Markov chain D is a tuple (�, ‡I , �, “0, �) where:Z � is a countable set of (control) statesZ ‡I " � is the initial (control) stateZ � is a finite stack alphabetZ “0 " � is the bottom-of-the-stack symbolZ � ⇥ � ✓ � � Dist(�) ✓ (� \ { “0 }ò is the probability transition relation

Joost-Pieter Katoen Probabilistic Programming 30/31

T t -

stackstack

symbol

Page 64: Probabilistic Programming€¦ · Probabilistic Programming Probabilistic Guarded Command Language Random assignments The random assignment x ⇥ ⌅µ works as follows: 1. evaluate

Probabilistic Programming Recursion

Recursion: pushdown Markov chains

Joost-Pieter Katoen Probabilistic Programming 31/31

→← → → O %D.

Pie --

y

prog = call P