SLIDE 1 PMAF: AN ALGEBRAIC FRAMEWORK FOR STATIC ANALYSIS OF PROBABILISTIC PROGRAMS
Di Wang1, Jan Hoffmann1, Thomas Reps2
1 Carnegie Mellon University 2 University of Wisconsin; GrammaTech, Inc.
SLIDE 2
PROBABILISTIC PROGRAMS
Draw random data from distributions Condition control-flow at random
SLIDE 3 PROBABILISTIC PROGRAMS
b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi; tick(1.0)
return (b1, b2)
True randomness Distributions on executions
SLIDE 4
BAYESIAN NETWORKS
Conditional distributions Query about the posterior
Pollution Smoker Cancer Xray Res Dyspnea
SLIDE 5
BAYESIAN NETWORKS
Conditional distributions Query about the posterior
Pollution Smoker Cancer Xray Res Dyspnea Prob[Cancer ∣ Smoker ∧ Xray Res] = ?
SLIDE 6
BAYESIAN NETWORKS AS PROB. PROG.
p b1 b2 0.6 0.5 0.7 p b1 b2 0.6 0.5 0.7
SLIDE 7
if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi
BAYESIAN NETWORKS AS PROB. PROG.
p b1 b2 0.6 0.5 0.7 p b1 b2 0.6 0.5 0.7
SLIDE 8 if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi
BAYESIAN NETWORKS AS PROB. PROG.
b1 b2 && ret p b1 b2 0.5 0.7 0.6 0.5 0.7 b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do ; tick(1.0)
return (b1, b2) p b1 b2 0.6 0.5 0.7
SLIDE 9 if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi
BAYESIAN NETWORKS AS PROB. PROG.
b1 b2 && ret p b1 b2 0.5 0.7 0.6 0.5 0.7 b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do ; tick(1.0)
return (b1, b2) p b1 b2 0.6 0.5 0.7
Query: probability that b1 and b2 are both false?
SLIDE 10 if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi
BAYESIAN NETWORKS AS PROB. PROG.
b1 b2 && ret p b1 b2 0.5 0.7 0.6 0.5 0.7 b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do ; tick(1.0)
return (b1, b2) p b1 b2 0.6 0.5 0.7
Query: expected termination time?
SLIDE 11
SAMPLING-BASED TECHNIQUES
Simulation & frequency count Flexible & universal Potentially unsound & inefficient
sample approximate
SLIDE 12
SAMPLING-BASED TECHNIQUES
Simulation & frequency count Flexible & universal Potentially unsound & inefficient
sample approximate What about static analysis?
SLIDE 13 ABSTRACT INTERPRETATION
Cousot et al. proposed Probabilistic Abstract Interpretation1 Sound, flexible, and universal
1 P
. Cousot and M. Monerau. Probabilistic Abstract Interpretation. In ESOP’12.
SLIDE 14 ABSTRACT INTERPRETATION
Cousot et al. proposed Probabilistic Abstract Interpretation1 Sound, flexible, and universal Their concrete semantics resolves probabilities prior to nondeterminism
1 P
. Cousot and M. Monerau. Probabilistic Abstract Interpretation. In ESOP’12.
SLIDE 15 ABSTRACT INTERPRETATION
Cousot et al. proposed Probabilistic Abstract Interpretation1 Sound, flexible, and universal Their concrete semantics resolves probabilities prior to nondeterminism Sometimes desirable to revolve nondeterminism prior to probabilities
1 P
. Cousot and M. Monerau. Probabilistic Abstract Interpretation. In ESOP’12.
SLIDE 16
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi * denotes nondeterministic choice tick(q) increases by q T
SLIDE 17 COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
with prob. 1 4
* denotes nondeterministic choice tick(q) increases by q T
SLIDE 18
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi * denotes nondeterministic choice tick(q) increases by q T
SLIDE 19 COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
with prob. 1 4
* denotes nondeterministic choice tick(q) increases by q T
SLIDE 20
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi * denotes nondeterministic choice tick(q) increases by q T
SLIDE 21 COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
with prob. 1 4
* denotes nondeterministic choice tick(q) increases by q T
SLIDE 22
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi * denotes nondeterministic choice tick(q) increases by q T
SLIDE 23 COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
with prob. 1 4
* denotes nondeterministic choice tick(q) increases by q T
SLIDE 24
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi * denotes nondeterministic choice tick(q) increases by q T
SLIDE 25
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
Their concrete semantics yields
𝔽[T] ∈ 1 4 ⋅ {1} + 1 4 ⋅ {2} + 1 4 ⋅ {1,2} + 1 4 ⋅ {1,2} = {1.25,1.5,1.75} * denotes nondeterministic choice tick(q) increases by q T
SLIDE 26
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
Their concrete semantics yields
𝔽[T] ∈ 1 4 ⋅ {1} + 1 4 ⋅ {2} + 1 4 ⋅ {1,2} + 1 4 ⋅ {1,2} = {1.25,1.5,1.75} * denotes nondeterministic choice Identical! tick(q) increases by q T
SLIDE 27
COUSOT ET AL.’S SEMANTICS
if * then if prob(0.5) then tick(1.0) else tick(2.0) fi else if prob(0.5) then tick(1.0) else tick(2.0) fi fi
while our semantics yields Their concrete semantics yields
𝔽[T] ∈ 1 4 ⋅ {1} + 1 4 ⋅ {2} + 1 4 ⋅ {1,2} + 1 4 ⋅ {1,2} = {1.25,1.5,1.75}
𝔽[T] = 1.5
* denotes nondeterministic choice Identical! tick(q) increases by q T
SLIDE 28 CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF
Recursion Unstructured control-flow Divergence Nondeterminism …
SLIDE 29
CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF
SLIDE 30
CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF Design Prove Implement
SLIDE 31
CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF
SLIDE 32
Bayesian Inference Markov Decision Problem
CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF Existing
SLIDE 33
Expectation-Invariant Analysis Bayesian Inference Markov Decision Problem
CONTRIBUTIONS
A denotational semantics with nondeterminism resolved first An algebraic framework for interprocedural dataflow analysis of first-order probabilistic programs
PMAF Existing New
SLIDE 34 EXAMPLE ANALYSES
Our framework can be instantiated to prove: the probability that b1 and b2 are both false at the end of the program = 0.15 the expected termination time (ticks) = 5/6
b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi; tick(1.0)
return (b1, b2)
SLIDE 35
OVERVIEW
Motivation The Algebraic Framework Hyper-Graph Analysis Evaluation
SLIDE 36 THE ALGEBRAIC FRAMEWORK
Any static analysis method performs reasoning in some space of program properties and property operations
Program Properties Actions
skip x := x + 5 b ~ Bernoulli(0.4) tick(1.0) …
Sequencing Cond.-choice Prob.-choice Nondet.-choice Semantic Function
SLIDE 37 THE ALGEBRAIC FRAMEWORK
skip x := x + 5 b ~ Bernoulli(0.4) tick(1.0) …
Concrete Operations Abstract Operations Actions Concrete Semantics Abstract Semantics Concrete Semantic Function Abstract Semantic Function Sound Abstraction
SLIDE 38 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
SLIDE 39 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
SLIDE 40 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
Program properties and approximation order
SLIDE 41 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
Program properties and approximation order Sequencing, cond.-choice, prob.-choice, and nondet.-choice
SLIDE 42 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
Program properties and approximation order Sequencing, cond.-choice, prob.-choice, and nondet.-choice The bottom element and the identity element
SLIDE 43 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
Program properties and approximation order Sequencing, cond.-choice, prob.-choice, and nondet.-choice
SLIDE 44 THE ALGEBRAIC FRAMEWORK
Characterize program properties and property
- perations by algebraic laws
⟨M, ⊑ , ⊗ ,φ ⋄ , p ⊕ , ⋓ , ⊥ , 1⟩
Program properties and approximation order Sequencing, cond.-choice, prob.-choice, and nondet.-choice a p ⊕ b = b 1−p ⊕ a (a ⊗ b) ⊗ c = a ⊗ (b ⊗ c) a ⊗ 1 = 1 ⊗ a = a a ⋓ a = a ⋯
SLIDE 45
OVERVIEW
Motivation The Algebraic Framework Hyper-Graph Analysis Evaluation
SLIDE 46 PROGRAM SEMANTICS
Control-flow graphs Reason about paths Paths are independent
[n=1] [n!=1] [n%2==0] [n%2!=0] i:=i+1 n:=n/2 n:=3*n+1
SLIDE 47
PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
SLIDE 48 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] [n%2==0] n:=n/2
SLIDE 49 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] [n%2==0] n:=n/2 [n!=1] prob(0.6) prob(0.6) n:=n/2 n:=n/2 n:=n+1
SLIDE 50 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] [n%2==0] n:=n/2 [n!=1] prob(0.6) prob(0.6) n:=n/2 n:=n/2 n:=n+1
n may be a random value
SLIDE 51 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] [n%2==0] n:=n/2 [n!=1] prob(0.6) prob(0.6) n:=n/2 n:=n/2 n:=n+1
n may be a random value random control-flow
SLIDE 52 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] prob(0.6) prob(0.6) n:=n/2 n:=n/2 n:=n+1
n may be a random value random control-flow
Nondeterminism is modeled by collections of such distributions
SLIDE 53 PROGRAM SEMANTICS
Reason about distributions over paths Paths are not independent
[n!=1] prob(0.6) prob(0.6) n:=n/2 n:=n/2 n:=n+1
n may be a random value random control-flow
Nondeterminism is modeled by collections of such distributions
Resolve nondeterminism first!
SLIDE 54 PROGRAM SEMANTICS
prob(0.6) true false
SLIDE 55 PROGRAM SEMANTICS
Control-flow hyper-graphs Branching are hyper-edges
b1 ~ Bernoulli(0.5); b2 ~ Bernoulli(0.7); while (b1 && b2) do if prob(0.6) then b1 ~ Bernoulli(0.5) else b2 ~ Bernoulli(0.7) fi; tick(1.0)
return (b1, b2)
b1,b2~B(0.5),B(0.7) [b1&&b2] false true ret prob(0.6) true false b1~B(0.5) b2~B(0.7) tick(1.0)
SLIDE 56 HYPER-GRAPH ANALYSIS
Forward assertions The semantics of a node is a summary of computation that continues from the node
b1,b2~B(0.5),B(0.7) [b1&&b2] false true ret prob(0.6) true false b1~B(0.5) b2~B(0.7) tick(1.0)
SLIDE 57 HYPER-GRAPH ANALYSIS
Forward assertions The semantics of a node is a summary of computation that continues from the node
E.g. the semantics of the node is
b1,b2~B(0.5),B(0.7) [b1&&b2] false true ret prob(0.6) true false b1~B(0.5) b2~B(0.7) tick(1.0)
λ(𝖼𝟤, 𝖼𝟥) . if 𝖼𝟥 then 1 7 [𝖼𝟤′ = 𝖴, 𝖼𝟥′ = 𝖦] + 6 7 [𝖼𝟤′ = 𝖦, 𝖼𝟥′ = 𝖴] else 1 2 [𝖼𝟤′ = 𝖴, 𝖼𝟥′ = 𝖦] + 1 2 [𝖼𝟤′ = 𝖦, 𝖼𝟥′ = 𝖦]
SLIDE 58 The hyper-graph analysis is formulated by an equation system
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
SLIDE 59 The hyper-graph analysis is formulated by an equation system
S[0]=seq[b1,b2~B(0.5),B(0.7)](S[1])
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
SLIDE 60 The hyper-graph analysis is formulated by an equation system
S[0]=seq[b1,b2~B(0.5),B(0.7)](S[1])
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
S[1]=cond[b1&&b2](S[2],S[3])
SLIDE 61 The hyper-graph analysis is formulated by an equation system
S[0]=seq[b1,b2~B(0.5),B(0.7)](S[1])
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
S[1]=cond[b1&&b2](S[2],S[3]) S[2]=prob[0.6](S[4],S[5])
SLIDE 62 The hyper-graph analysis is formulated by an equation system
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
S[0]=seq[b1,b2~B(0.5),B(0.7)](S[1]) S[1]=cond[b1&&b2](S[2],S[3]) S[2]=prob[0.6](S[4],S[5])
SLIDE 63 The hyper-graph analysis is formulated by an equation system
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
S[0]=seq[b1,b2~B(0.5),B(0.7)](S[1]) S[1]=cond[b1&&b2](S[2],S[3]) S[2]=prob[0.6](S[4],S[5])
Use the semantic algebra to interpret seq, cond, prob
SLIDE 64 The hyper-graph analysis is formulated by an equation system
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
Use the semantic algebra to interpret seq, cond, prob
S[0] = [𝖼𝟤, 𝖼𝟥 ∼ 𝖢(𝟣 . 𝟨), 𝖢(𝟣 . 𝟪)] ⊗ S[1] S[1] = S[2]𝖼𝟤&&𝖼𝟥 ⋄ S[3] S[2] = S[4]0.6 ⊕ S[5]
SLIDE 65 The hyper-graph analysis is formulated by an equation system
HYPER-GRAPH ANALYSIS
1
b1,b2~B(0.5),B(0.7)
3 2
[b1&&b2] false true
7
ret
5
prob(0.6)
4 6
true false b1~B(0.5) b2~B(0.7) tick(1.0)
S[0] = [𝖼𝟤, 𝖼𝟥 ∼ 𝖢(𝟣 . 𝟨), 𝖢(𝟣 . 𝟪)] ⊗ S[1] S[1] = S[2]𝖼𝟤&&𝖼𝟥 ⋄ S[3] S[2] = S[4]0.6 ⊕ S[5]
If using abstract semantics, we obtain an equation system for static analysis
SLIDE 66
OVERVIEW
Motivation The Algebraic Framework Hyper-Graph Analysis Evaluation
SLIDE 67
INSTANTIATIONS
PMAF
SLIDE 68
INSTANTIATIONS
PMAF Bayesian Inference Markov Decision Problem Existing
SLIDE 69
INSTANTIATIONS
PMAF Bayesian Inference Markov Decision Problem Existing Interprocedural analyzers! New
SLIDE 70
INSTANTIATIONS
PMAF Bayesian Inference Markov Decision Problem Existing
SLIDE 71
INSTANTIATIONS
PMAF Bayesian Inference Markov Decision Problem Existing Expectation-Invariant Analysis New
SLIDE 72
INSTANTIATIONS
PMAF Bayesian Inference Markov Decision Problem Existing Expectation-Invariant Analysis New Prove invariants among initial values and expected final values
SLIDE 73 PROBABILISTIC MODEL ANALYSES
Benchmark collected from PReMo1 Achieve the same precision
Markov Decision Problem (Table 2)
Program #loc time (sec) binary10 184 0.03 loop 10 0.03 quicksort7 109 0.03 recursive 13 0.03 student 43 0.03
Bayesian Inference (Table 2)
Program #loc time (sec) compare 17 2.22 dice 12 0.02 eg1 10 0.02 eg2 16 0.01 recursive 14 0.01
1 D. Wojtczak and K. Etessami. PReMo - Probabilistic Recursive Models analyzer. Available at groups.inf.ed.ac.uk/premo/.
SLIDE 74 EXPECTATION-INVARIANT ANALYSIS
Benchmark collected from the literature1,2 and also handcrafted by us Derive expectation invariants as least as precise as them in most case
Expectation-Invariant Analysis (Table 1)
Program #loc time (sec) Expectation Invariants binom-update 14 0.06 E[4x’-n’]=4x-n, E[x’]<=x+1/4 eg 8 0.89 E[x’+y’]=x+y+4, E[z’]=1/4z+3/4 recursive 13 0.37 E[x’]=x+9 mot-ex 16 0.06 E[2x’-y’]=2x-y, E[4x’-3c’]=4x-3c, E[x’]<=x+3/4
1 A. Chakarov and S. Sankaranarayanan. Expectation Invariants for Probabilistic Loops as Fixed Points. In SAS’14. 2 J.-P
. Katoen, A. K. McIver, L. A. Meinicke, and C. C. Morgan. Linear-Invariant Generation for Probabilistic Programs. In SAS’10.
SLIDE 75
SUMMARY
PMAF Design Prove Implement
SLIDE 76
SUMMARY
PMAF Hyper-Graph Semantics Design Prove Implement
SLIDE 77
SUMMARY
PMAF Bayesian Inference Markov Decision Problem Expectation-Invariant Analysis Hyper-Graph Semantics Instantiations Design Prove Implement
SLIDE 78
SUMMARY
PMAF Bayesian Inference Markov Decision Problem Expectation-Invariant Analysis Hyper-Graph Semantics Instantiations Design Prove Implement Limitations: Only first-order programs No function pointers Not Galois connections
SLIDE 79
SUMMARY
PMAF Bayesian Inference Markov Decision Problem Expectation-Invariant Analysis Hyper-Graph Semantics Instantiations Design Prove Implement Limitations: Only first-order programs No function pointers Not Galois connections Future work: Higher-order programs More efficient algorithm New instantiations