fine-grained semantics for probabilistic programs · termination, the semantics of each expression...

36
Fine-Grained Semantics for Probabilistic Programs Benjamin Bichsel Timon Gehr Martin Vechev

Upload: others

Post on 22-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Fine-Grained Semanticsfor Probabilistic Programs

Benjamin Bichsel

TimonGehr

Martin Vechev

Page 2: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Probabilistic models

NetworkingSecurity

Machine Learning

RoboticsRandomized Algorithms

Page 3: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Probabilistic programming

x := uniform(0,1);

y := uniform(0,1);

return x+y;

Probability density function Cumulative distribution function

Page 4: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Observations

x := uniform(0,1);

y := uniform(0,1);

observe(x+y>=1);

return x+y;

Probability density function Cumulative distribution function

0.5

Page 5: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Partial functions

x := uniform(-1,1);

x = sqrt(x);

return x;

Probability density function Cumulative distribution function

0.5

Page 6: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Loops

x := 2;

while x>0 {

if x<4 {

if flip(0.5) {

x++;

} else {

x--;

}

}

}

return x;

2 3 410

1/2 1/2 1/2

1/2 1/2 1/2

1return 0

start

0.5

Page 7: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Interaction of exceptions

x := 0;

while 1 {

x = x/x;

}

return x;

Better:

Page 8: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Interaction of exceptions II

x := 0;

while x==0 {

x = flip(0.5);

observe(x==0);

}

return x;Better:

Page 9: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Interaction of exceptions III

x := 10;

d := gauss(0,1);

observe(d>=-0.5);

while x>=0 {

x-=d;

}

return x;

Better:

Page 10: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Existing denotational semantics

Page 11: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Our work – Denotational semantics

Explicitly distinguish exceptions:

• non-termination ( )

• errors ( )

• observation failure ( )

Our semantics also support• Mixing continuous and discrete distributions• Arrays• The score primitive

Page 12: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Benefits

• Deeper understanding of probabilistic semantics

• Establish correctness of probabilistic solvers (e.g. PSI)

• More efficient normalization in probabilistic solvers

• Generalize to arbitrary number of exceptions

Page 13: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Measure theory

0

1

Page 14: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Measure theory

0

1

Page 15: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics

x := uniform(-1,1);

x = sqrt(x);

return x;lif

t

lift

lift

lift

sqrt(uniform(-1,1))

Page 16: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

sqrt(uniform(-1,1))

Denotational semantics - Constants

Page 17: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

sqrt(uniform(-1,1))

Denotational semantics - Constants

Page 18: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

sqrt(uniform(-1,1))

Denotational semantics - Constants

Dirac delta

Page 19: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Constants

sqrt(uniform(-1,1))

Page 20: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Functions

sqrt(uniform(-1,1))

Page 21: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Functions

sqrt(uniform(-1,1))

Page 22: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Functions

sqrt(uniform(-1,1))

Page 23: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Functions

sqrt(uniform(-1,1))

Page 24: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Functions

sqrt(uniform(-1,1))

Page 25: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Composition

sqrt(uniform(-1,1))

Page 26: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Loops

n := 0;

while !flip(0.5) {

n++;

}

Page 27: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Loops

n := 0;

while !flip(0.5) {

n++;

}

Page 28: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Loops

n := 0;

while !flip(0.5) {

n++;

}

Page 29: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Denotational semantics - Loops

n := 0;

while !flip(0.5) {

n++;

}

Page 30: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Probability kernel

Theorem: The semantics of each expression and each statement is a probability kernel.

Page 31: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Properties of Semantics - Commutativity

F() {

while 1 {

skip;

}

return 0;

}

Page 32: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Properties of Semantics - Associativity

Proof: Relies on associativity of the product of kernels and composition of kernels.

Page 33: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Score primitive

x := gauss(0,1);

score( );

return x;

Page 34: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Score primitive vs non-termination

i := 0;

while 1 {

if i==0 {

score(2);

} else {

score(0.5);

}

i = 1-i;

}

Page 35: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Score primitive – s-finite kernels

Theorem: After adding the score primitive and abolishing non-termination, the semantics of each expression and each statement is an s-finite kernel.

Informally:

Page 36: Fine-Grained Semantics for Probabilistic Programs · termination, the semantics of each expression and each statement is an s-finite kernel. Informally: Features of probabilistic

Features of probabilistic programminglanguages

observations

errors

mix discrete and continuous

higher-order

score

loops recursion

non-determinism