Vous êtes sur la page 1sur 24

Outline

Ce este Programarea probabilistă (PP)?


O scurtă introducere ı̂n Figaro

Programare probabilistă: Introducere

Dorel Lucanu

Faculty of Computer Science


Alexandru Ioan Cuza University, Ia’si, Romania
dlucanu@info.uaic.ro

PA 2016/2017

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Ce este Programarea probabilistă (PP)?

O scurtă introducere ı̂n Figaro

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Plan

Ce este Programarea probabilistă (PP)?

O scurtă introducere ı̂n Figaro

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Un prim răspuns intuitiv


O modalitate de a crea sisteme care să ajute la luarea de decizii
ı̂ntr-un context incert.
Rat, ionamentul probabilistic combină cunos, tint, ele despre o anumită
situat, ie cu legile probabilităt, ilor pentru a determina factori
neobservabili care sunt necesari luării de decizii.
Exemplu:
I decizie: dacă un nou produs realizat va fi lansat pe piat, ă
I context incert: economia, piat, a, competit, ia
I factori neobservabil de determinat: dacă va avea succes, ce
venituri va aduce, ce pierderi va produce ı̂n caz de es, ec
I cunos, tint, e: experient, ă, statistici, informat, ii despre produsele
competitoare

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Probabilist sau probabilistic?

DEX 2009
probabilist: 2. Adj. Care foloses, te calculul probabilităt, ilor.
probabilistic: adj. 1. Cu caracter de probabilitate. 2. Referitor la
teoria probabilităt, ilor, la calculul probabilităt, ilor.

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Sistem de rat, ionament probabilist pentru prezicerea


6rezulatului unui corner la fotbal
Probabilistic (fig.
programming in
CHAPTER 1 a nutshell1.2, p. 6)

2. You supply knowledge


about the preconditions
of this particular
1. You encode your knowledge corner kick.
Probabilistic
about corner kicks and reasoning system
the relevant factors.
Corner-kick model

Evidence
• Tall center forward
• Inexperienced goalie
• Strong wind
Inference algorithm
Query
• Will a goal be scored?

Answer
5. The system returns the • Yes: 20%; No: 80%
3. You tell the system
answer as probabilities. what outcome you
want to know.
4. The system uses the inference
algorithm to predict the outcome.

Figure 1.2 How a probabilistic reasoning system predicts the outcome of a corner kick

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Termeni fundamentali

I cunos, tint, e generale


I model probabilist
I evident, ă
I interogare
I inferent, ă

D. Lucanu LPP: Introducere


ent goalie for the next game. The conclusions Outline themselves are framed probabilistically,
like the probability of differentprobabilistă
Ce este Programarea skill levels(PP)?
of the goalie.
O scurtă introducere ı̂n Figaro
The relationship between the model, the information you provide, and the
answers to queries is well defined mathematically by the laws of probability. The pro-
Componentele de bază ale unui sistem de rationament
cess of using the model to answer queries based on the evidence is called probabilistic
inference, or simply inference. Fortunately, computer algorithms have been developed
,
probabilist (fig. 1.3, p. 7)
that do the math for you and make all the necessary calculations automatically. These
algorithms are called inference algorithms.
Figure 1.3 summarizes what you’ve learned.

Probabilistic The evidence contains


The probabilistic specific information
model expresses reasoning system
about a situation.
general knowledge
about a situation. Probabilistic model

The inference
algorithm uses Evidence
the model to
answer queries, Inference algorithm
given evidence. Queries

The answers to Answer


queries are framed The queries express the
as probabilities of things that will help you
different outcomes. make a decision.

Figure 1.3 The basic components of a probabilistic reasoning system

In a nutshell, what we’ve just discussed are the constituents


D. Lucanu
of a probabilistic reason-
LPP: Introducere
Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Ttrei moduri de a rat, iona probabilist

1. prezicerea evenimentelor viitoare


2. determinarea (inferarea) cauzelor uni eveniment
3. ı̂nvăt, area din evenimentele trecute pentru a prezice mai bine
ı̂n viitor

D. Lucanu LPP: Introducere


she’s poorly skilled? Figure 1.4 shows how to use a probabilistic reasoning sys-
Outline
tem to
Ceanswer this question.
este Programarea The model
probabilistă (PP)? is the same corner-kick model you used
O scurtă introducere ı̂n Figaro
before to predict whether a goal would be scored. (This is a useful property of
probabilistic reasoning: the same model that can be used to predict a future
result can be used after the fact to infer what caused that result.) The evidence
Modifcând interogarea si evidenta, se determină de ce a a
here is the same as before, together, , that a goal was scored. The
with the fact
query is the skill level of the goalie, and the answer provides the probability of
fost ı̂nscris un gol (fig. 1.4, p. 8)
various skill levels.

The evidence contains


knowledge of both
preconditions and
outcomes.

Probabilistic
reasoning system
Evidence
• Preconditions
Corner-kick model
° Tall center forward
° Inexperienced goalie
° Strong wind
• Outcome
° A goal was scored
The inference Inference algorithm
algorithm reasons Query
about the query, • How good is the goalie?
given all known
preconditions
and outcomes.
Answer
• Poor: 50%; Average: 30%;
Good: 20% The query is a possible
cause of the outcome.

Figure 1.4 By altering the query and evidence, the system can now infer why a goal was
scored.
D. Lucanu LPP: Introducere
feature of probabilistic reasoning, whichOutline
I’ll repeat throughout the book: the
directionCeofeste Programarea
reasoning probabilistăfollow
doesn’t necessarily (PP)? the direction of the model.
O scurtă introducere ı̂n Figaro
■ Learn from past events to better predict future events. Now fast-forward another 10
minutes. The same team has won another corner kick. Everything is similar to
before in this new situation—tall center forward, inexperienced goalie—but
Luând ı̂n considerare evident, a ultimului corner, sistemul de
now the wind has died down. Using probabilistic reasoning, you can use what
happened in the previous kick to help you predict what will happen on the next
rat, ionament probabilist poate prezice mai bine ce se va
kick. Figure 1.5 shows how to do this. The evidence includes all evidence from
last time (making a note that it was from last time), as well as the new informa-
ı̂ntâmpla la următorul corner (fig. 1.5, p. 9)
tion about the current situation. In answering the query about whether a goal

The evidence contains knowledge of both


preconditions and outcomes of previous
situations, as well as preconditions of
the current situation.

Evidence
Probabilistic • Previous corner kick
reasoning system ° Tall center forward
° Inexperienced goalie
Corner-kick model ° Strong wind
° A goal was scored
The inference • Current corner kick
algorithm reasons ° Tall center forward
about the previous ° Inexperienced goalie
situations to infer ° Weak wind
factors (like the skill level Inference algorithm
of the goalie) that hold Query
in the new situation. • Will a goal be scored
this time?

The algorithm uses


these factors to improve Answer
its prediction of the • Yes: 25%; No: 75%
outcome of the new The query is an outcome
situation. of the new situation.

Figure 1.5 By taking into account evidence from the outcome of the last corner kick, the
probabilistic reasoning system can produce a better prediction
D. Lucanu of the next LPP:
corner Introducere
kick.
Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Învăt, area unui model probabilist (mai bun) (fig. 1.6, p.11)
What is probabilistic programming? 11

The evidence about previous


situations constitutes the data.
Probabilistic
reasoning system

Original corner-
kick model Past experience 1
• Tall center forward
• Inexperienced goalie
• Strong wind
Past experience 2
Past experience 3
The learning algorithm Learning algorithm Etc.
uses the original model
and the data to produce
a new model. Before
The evidence is provided
After about a new situation.
New corner-
kick model

New evidence
• Tall center forward
• Inexperienced goalie
• Weak wind
The inference algorithm Inference algorithm
uses the new model to
answer queries about Query
the new situation. • Will a goal be scored
this time?

Answer
• Yes: 15%; No: 85%

Figure 1.6 You can use a learning algorithm to learn a new model based on a set of experiences.
This new model can then be used for future inferences.
D. Lucanu LPP: Introducere
ure 1.3 to highlight the differences between the two systems. The main change is that
Outline
Ce este Programarea probabilistă (PP)?
models are expressed as programs
O scurtă introducereinı̂naFigaro
programming language rather than as a
mathematical construct like a Bayesian network. As a result of this change, evidence,
queries, and answers all apply to variables in the program. Evidence might specify par-
Relatia dintre sistemele de rationament probabilist s, i
ticular
, values for program variables, queries ask ,for the values of program variables,
and answers are probabilities of different values of the query variables. In addition, a
sistemele de programare probabilistă (fig. 1.6, p.12)
probabilistic programming system typically comes with a suite of inference algorithms.
These algorithms apply to programs written in the language.

The model is expressed


as a program in a The evidence relates
programming to values of program
language rather Figaro
variables.
than a mathematical
construct. Probabilistic model

The probabilistic
programming system Evidence
provides a suite of
Inference algorithm
inference algorithms
that apply to models Queries
written in the language.

The answers are Answer


probabilities of different
values of the query variables. The queries are
for values of other
program variables.

Figure 1.7 A probabilistic programming system is a probabilistic reasoning system that uses a
programming language to represent probabilistic models.

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Un program probabilist defines, te un proces care generează


date de ies, ire aleatorii pentru o intrare dată (fig. 1.8, p.
14)
14 Probabilistic programming in a nutshell
CHAPTER 1

1. A probabilistic program Probabilistic


describes a process that programming system
randomly generates
outputs, given inputs. Corner-kick program

No goal

• Tall center forward Goal!!!


• Inexperienced goalie Random execution
• Strong wind No goal

Near miss

2. Given the inputs, the 3. The probability of an output


program can be randomly is the probability of execution
executed many times to paths that result in that
produce different outputs. output. In this example the
probability of a goal is ¼.

Figure 1.8 A probabilistic program defines a process of randomly generating outputs, given inputs.
D. Lucanu LPP: Introducere
Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

De ce PP?

I expresivitate
”Probabilistic reasoning + Turing-complete = probabilistic
programming”
I rat, ionament probabilist mai bun
I simulare mai bună (execut, ie + analiză)

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Plan

Ce este Programarea probabilistă (PP)?

O scurtă introducere ı̂n Figaro

D. Lucanu LPP: Introducere


Some of the available systems are listed with a brief description in appendix B. A sec-
Outline
Ce este Programarea probabilistă (PP)?
ondary goal, however, O is to give
scurtă you hands-on
introducere experience with creating practical prob-
ı̂n Figaro
abilistic programs, and provide you with tools you can use right away. For that reason,
a lot of the examples are made concrete in Figaro code.
Cum Figaro utilizează Scala pentru a defini un sistem de
Figaro, which is open source and maintained on GitHub, has been under develop-
ment since 2009. It’s implemented as a Scala library. Figure 1.9 shows how Figaro uses
programare probabilist (fig. 1.9, p. 18)
The probabilistic model
takes the form of a set of
Figaro data structures
called elements. Figaro

Elements correspond Probabilistic model


to variables in your
model, like the height Figaro
of the center forward elements
or the outcome of the The evidence is information
corner kick. Scala
about the values of elements.

You write Scala code to


create these elements.
Inference algorithm
You perform inference Evidence
by running one of Figaro’s Figaro
inference algorithms on algorithms
your model, using Scala Queries
the evidence.

Inference is invoked by
a Scala function call.
Answers The queries request information
The answers provide the about the values of elements.
probabilities of different
values of elements.
D. Lucanu LPP: Introducere
Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Avantajele ı̂ncorporării unui limbaj probabilist ı̂ntr-unul de


programare general

I evident, a poate fi descrisă ı̂n limbajul gazdă


I rezultatele date sistem pot fi utilizate ı̂n programe
I se poate utiliza cod general ı̂n programe probabiliste
I se pot utiliza tehnici de programare generală ı̂n scrierea de
programe probabiliste

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

De ce Scala

I este un limbaj funct, ional


I este un limbaj OO

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Avantaje ale sistemului Figaro independente de Scala

I poate reprezenta o gamă largă de modele probabiliste;


I un cadru bogat pentru descrierea evident, elor prin condit, ii s, i
constrângeri;
I include un set important algoritmi de inferent, ă;
I poate reprezenta s, i rat, iona despre modele care se modifică ı̂n
timp;
I poate include decizii explicite ı̂n modelele sale s, i permite
inferarea de decizii optime.

D. Lucanu LPP: Introducere


greeting that depends on the weather. This happens two days in a row. Also, the
Outline
weather on the second
Ce este day is dependent
Programarea probabilistăon the first day: the second day is more likely
(PP)?
to be sunny if the firstO day is sunny.
scurtă These
introducere English language statements can be quanti-
ı̂n Figaro
fied numerically by the numbers in table 1.1.

Hello World probabilistic


Table 1.1 Quantifying the probabilities in the Hello World example

Today’s weather

Sunny 0.2

Not sunny 0.8

Today’s greeting

If today’s weather is sunny “Hello, world!” 0.6

“Howdy, universe!” 0.4

If today’s weather isn’t sunny “Hello, world!” 0.2

“Oh no, not again” 0.8

Tomorrow’s weather

If today’s weather is sunny Sunny 0.8

Not sunny 0.2

If today’s weather isn’t sunny Sunny 0.05

Not sunny 0.95

Tomorrow’s greeting

If tomorrow’s weather is sunny “Hello, world!” 0.6

“Howdy, universe!” 0.4

If tomorrow’s weather isn’t sunny “Hello, world!” 0.2

“Oh no, not again” 0.8


D. Lucanu LPP: Introducere
Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Hello World ı̂n Java

p. 22-23

D. Lucanu LPP: Introducere


rules of inference to write this code. Even if you have this knowledge, writing
Outline
this code correctly
Ce este is difficult.
Programarea Testing
probabilistă whether you have the right answer is also
(PP)?
O scurtă introducere ı̂n Figaro
difficult. And this is an extremely simple example. For a complex application, it
would be impractical to create reasoning code in this way.
Hello
NowWorld ı̂n Scala/Figaro
let’s look at the Figaro code.1/2
Listing 1.2 Hello World in Figaro

import com.cra.figaro.language.{Flip, Select}


import com.cra.figaro.library.compound.If Import Figaro
import com.cra.figaro.algorithm.factored.VariableElimination
constructs

object HelloWorld {
val sunnyToday = Flip(0.2)
val greetingToday = If(sunnyToday,
Select(0.6 -> "Hello, world!", 0.4 -> "Howdy, universe!"),
Select(0.2 -> "Hello, world!", 0.8 -> "Oh no, not again")) Define the
val sunnyTomorrow = If(sunnyToday, Flip(0.8), Flip(0.05)) model
val greetingTomorrow = If(sunnyTomorrow,
Select(0.6 -> "Hello, world!", 0.4 -> "Howdy, universe!"),
Select(0.2 -> "Hello, world!", 0.8 -> "Oh no, not again"))

def predict() {
val result = VariableElimination.probability(greetingToday, Predict
today’s
"Hello, world!")
greeting using
println("Today’s greeting is \"Hello, world!\" " +
an inference
"with probability " + result + ".")
algorithm
}

D. Lucanu LPP: Introducere


Outline
Ce este Programarea probabilistă (PP)?
O scurtă introducere ı̂n Figaro

Hello World ı̂n Figaro 2/2


Introducing Figaro: a probabilistic programming language 25

def infer() { Use an inference


greetingToday.observe("Hello, world!") algorithm to infer
val result = VariableElimination.probability(sunnyToday, true) today’s weather,
println("If today's greeting is \"Hello, world!\", today’s " + given the observation
"weather is sunny with probability " + result + ".") that today’s greeting
} is “Hello, world!”

def learnAndPredict() {
greetingToday.observe("Hello, world!") Learn from
val result = VariableElimination.probability(greetingTomorrow, observing that
"Hello, world!") today’s greeting is
println("If today's greeting is \"Hello, world!\", " +
“Hello, world!” to
predict tomorrow’s
"tomorrow's greeting will be \"Hello, world!\" " +
greeting using an
"with probability " + result + ".")
inference algorithm
}

def main(args: Array[String]) {


predict() Main method
infer() that performs
learnAndPredict() all the tasks
}
}

I’ll wait until the next chapter to explain this code in detail. For now, I want to point
out that it solves the two problems with the Java code. First, the model definition
describes exactly the structure of the model, in correspondence with table 1.1. You
define four variables: sunnyToday, greetingToday
D. Lucanu , LPP: Introducere , and greeting-
sunnyTomorrow