bp with probt

Upload: mitch-alighieri

Post on 02-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 BP With ProBT

    1/64

    Bayesian Programming

    with ProBT c

    and some dice

    Juan Manuel Ahuactzin Kamel Mekhnacha

    Pierre Bessiere Emmanuel Mazer

    July 2012

  • 8/10/2019 BP With ProBT

    2/64

    2

  • 8/10/2019 BP With ProBT

    3/64

    Chapter 1

    Variables

    Program 1:Multiple type variables sets.

    1 /*========================================================= ==============2 * Product :

    3 * File : multipleTypes.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : 2002-Oct-30 19:29

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This file gives an example to define multiple type variables

    14 * conjunctions

    15 *--------------------------------------------------------- --------------

    16 */

    17

    18 #include

    19

    20

    21 using namespace std;

    22

    23

    24 int main()

    25 {

    26 //Definig types

    27 plIntegerType rank_type(0,10);

    28 plIntegerType age_type(0,120);

    29 plRealType weight_type(40.0,140.0);

    30 plRealType height_type(0.2,2.30, 200);

    31

    32 //Defining single type variables conjunctions

    33 plSymbol rank("rank",rank_type);

    34 plSymbol age("age",age_type);

    3

  • 8/10/2019 BP With ProBT

    4/64

    4 CHAPTER 1. VARIABLES

    35 plSymbol height("height",height_type);

    36 plSymbol weight("weight",weight_type);

    37 plArray ancestors_height("anc_height",height_type,1,2);

    38

    39 //Defining a multiple type variable

    40 plVariable physical_info(weight^height^ancestors_height);

    41 cout

  • 8/10/2019 BP With ProBT

    5/64

    5

    Program 2:Grouping variables.

    1 /*========================================================= ==============

    2 * Product :

    3 * File : groupingVars.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : 2002-Oct-30 19:29

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This is a simple example showing how to group a set of

    14 * variables. We assume having n sensors giving a lecture of a moving

    15 * object. A lectures is composed by an angle, a distance and a

    16 * velocity in x and y. A set of Left an right lectures is also

    17 * constructed. We assume that odd sensors are left sensors and that

    18 * even sensors are right sensors

    19 * *-----------------------------------------------------------------------

    20 */21

    22 #include

    23

    24 using namespace std;

    25

    26 int main ()

    27 {

    28 /**********************************************************************

    29 Defining types and variables

    30 ***********************************************************************/

    31

    32 //Defining types

    33 plIntegerType angle_lecture(0,360);

    34 plRealType distance_lecture(0,100.0,50);

    35 plRealType velocity_lecture(-50,50,20);

    36

    37 const unsigned int n_sensors = 6;

    38

    39 //Defining a plArrays angle(n_sensors) for angles

    40 plArray angle("T",angle_lecture,1,n_sensors);

    41

    42 //Defining a plArrays distance(n_sensors) for distances

    43 plArray distance("D",distance_lecture,1,n_sensors);

    44

    45 //Defining a plArrays velocity(n_sensors, 2). The "2" corresponds to

    46 //the x and y components

    47 plArray velocity("V",velocity_lecture,2,n_sensors,2);

    48

    49 /**********************************************************************

    50 Joining variables conjunctions by means of the ^ operator

    51 ***********************************************************************/

    52

    53 unsigned int i;

  • 8/10/2019 BP With ProBT

    6/64

  • 8/10/2019 BP With ProBT

    7/64

    7

    Program 3:Variable values loops.

    1 /*========================================================= ==============

    2 * Product :

    3 * File : iterateValues.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : 2002-Oct-30 19:29

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program shows how to cover all the values of a conjunction of

    14 * discrete variables (i.e. creation of loops). Three examples

    15 * consisting in the more typical circumstances are shown.

    16 *--------------------------------------------------------- --------------

    17 */

    18

    19

    20 #include 21

    22 using namespace std;

    23

    24 int main ()

    25 {

    26 /**********************************************************************

    27 Defining the variable type and symbols

    28 ***********************************************************************/

    29

    30 plIntegerType minuteType(0,59);

    31 plIntegerType hourType(0,23);

    32

    33 plSymbol minute("minute",minuteType);

    34 plSymbol hour("hour",hourType);

    35

    36 plRealType temperature(-20,40,100);

    37 plRealType humidity(0.0,1.0,25);

    38 plRealType speed(0,70,20);

    39

    40 plSymbol hi("Hi",temperature);

    41 plSymbol lo("Lo",temperature);

    42 plSymbol wind_speed("wind_speed",speed);

    43 plSymbol wind_humidity("wind_humidity",humidity);

    44

    45 /**********************************************************************

    46 First example: A loop for all the variables in the plValues

    47 with the order of iteration given by the order used at the creation

    48 of the plValues.

    49 ***********************************************************************/

    50

    51 cout

  • 8/10/2019 BP With ProBT

    8/64

    8 CHAPTER 1. VARIABLES

    54

    55 time.reset(); // Reset all values in "time"

    56 do

    57 cout

  • 8/10/2019 BP With ProBT

    9/64

    Chapter 2

    Distributions

    2.1 Unconditional distributions

    9

  • 8/10/2019 BP With ProBT

    10/64

    10 CHAPTER 2. DISTRIBUTIONS

    Program 4:Throwing a die.

    1 /*======================================================= ================

    2 * Product :

    3 * File : die.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : 2002-Oct-30 19:29

    6 *

    7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program simulates throwing a symmetric 6 sides die

    14 *----------------------------------------------------------- ------------

    15 */

    16

    17 #include

    18

    19 using namespace std;

    2021 int main()

    22 {

    23 /**********************************************************************

    24 Defining the variable type, a symbol and values.

    25 **********************************************************************/

    26

    27 plIntegerType Points_type(1,6); // Type for Points [1,2,...,6]

    28 plSymbol Points("Points",Points_type); // Variable set for Points

    29 plValues values(Points); // Values for Points

    30

    31 /**********************************************************************

    32 Definig P(Die) = uniform

    33 **********************************************************************/

    34

    35 plUniform P_Points(Points); // Distribution of the variable space

    36

    37 /**********************************************************************

    38 Displaying the defined data

    39 **********************************************************************/

    40

    41 cerr

  • 8/10/2019 BP With ProBT

    11/64

    2.2. CONDITIONAL DISTRIBUTIONS 11

    54

    55 for (i=0;i

  • 8/10/2019 BP With ProBT

    12/64

    12 CHAPTER 2. DISTRIBUTIONS

    31 plSymbol Points("Points",Points_type);// Variable set for Points

    32 plSymbol Die("Die",Die_type); // Variable set for the Die

    33 plValues values(Points Die); // Values for Points and Die

    34

    35 /**********************************************************************

    36 Definig P(Points | Die)

    37 **********************************************************************/

    3839 // Distributions of dice 1,3 and 4

    40 plProbValue distDie1[6] = {0.3, 0.2, 0.1, 0.1, 0.2, 0.1};

    41 plProbValue distDie3[6] = {0.4, 0.15, 0.1, 0.15, 0.1, 0.1};

    42 plProbValue distDie4[6] = {0.2, 0.2, 0.1, 0.1, 0.35, 0.05};

    43

    44 // The conditional distribution P(Points | Die)

    45 plDistributionTable P_PointsKDie(Points,Die);

    46

    47 P_PointsKDie.push(1,plProbTable(Points,distDie1));

    48 P_PointsKDie.push(2,plUniform(Points)); // Die 2 is a symmetric die

    49 P_PointsKDie.push(3,plProbTable(Points,distDie3));

    50 P_PointsKDie.push(4,plProbTable(Points,distDie4));

    51

    52 // Printing the created objects

    53 cerr

  • 8/10/2019 BP With ProBT

    13/64

    Chapter 3

    Bayesian Networks

    13

  • 8/10/2019 BP With ProBT

    14/64

    14 CHAPTER 3. BAYESIAN NETWORKS

    Die

    Points

    0 1

    0.36 0.64

    Die P oints

    1 2 3 4 5 6

    0 0.3 0.2 0.1 0.1 0.2 0.1

    1 0.16 0.16 0.16 0.16 0.16 0.16

    Figure 3.1: The two dice Bayesian network.

    Program 6:Two dice..

    1 /*======================================================= ================

    2 * Product :

    3 * File : twoDice.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : Tue Mar 8 15:12:38 2005

    6 *

    7 *=========================================================== ============

    8 *(c) Copyright 2000-2004, Centre National de la Recherche Scientifique,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 *

    14 *

    15 *----------------------------------------------------------- ------------

    16 */

    17

    18

    19 #include

    20

    21 using namespace std;

    22

    23 int main() {

    24 /***************************************************** *****************

    25 VARIABLES SPECIFICATION

    26 ************************************************************ **********/

    27 plIntegerType Die_type(0,1); // Two dice 0 and 1

    28 plIntegerType Points_type(1,6); // Type for Points [1,2,...,6]

    29 plSymbol Die("Die",Die_type);

  • 8/10/2019 BP With ProBT

    15/64

    15

    twoDice() =

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Description

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Specification

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Relevant Variables:Die, P oints

    Decomposition:

    P(Die P oints| ) =P(Die| )P(Points| Die )

    Parametric Forms:P(Die| ) = 0 1

    0.36 0.64

    P(Points|Die ) =

    Die P oints

    1 2 3 4 5 6

    0 0.3 0.2 0.1 0.1 0.2 0.1

    1 0.16 0.16 0.16 0.16 0.16 0.16

    Identification:All tables provided by the user

    Question:P(Die|Points)

    Figure 3.2: The two dice Bayesian program.

    30 plSymbol Points("Points",Points_type);

    31

    32 /********************************************************** ************

    33 PARAMETRIC FORM SPECIFICATION

    34 ********************************************************** ************/

    35 // P(Die)

    36 plProbValue Die_table[] = {0.36, 0.64};

    37 plProbTable P_Die(Die, Die_table);

    38

    39 // Distributions of dice 0

    40 plProbValue distDie0[] = {0.3, 0.2, 0.1, 0.1, 0.2, 0.1};

    41

    42 // The conditional distribution P(Points | Die)

    43 plDistributionTable P_PointsKDie(Points,Die);

    44

    45 P_PointsKDie.push(0,plProbTable(Points,distDie0));

    46 P_PointsKDie.push(1,plUniform(Points)); // Die 1 is a symmetric die

    47

    48 /********************************************************** ************

    49 DECOMPOSITION

    50 ********************************************************** ************/

    51 // P(Die Points) = P(Die) P(Point | Die)

    52 plJointDistribution jd(Die^Points, P_Die*P_PointsKDie);

    53 jd.draw_graph("twoDice.fig");

    54 /********************************************************** ************

    55 PROGRAM QUESTION

    56 ********************************************************** ************/

    57 plCndDistribution question, compiled_question;

    58

  • 8/10/2019 BP With ProBT

    16/64

    16 CHAPTER 3. BAYESIAN NETWORKS

    59 jd.ask(question, Die, Points); // Compute P(Die | Points)

    60 question.compile(compiled_question);

    61 cout

  • 8/10/2019 BP With ProBT

    17/64

    17

    A B

    C D

    E

    P(A| )0 1

    0.4 0.6

    P(B| )0 1

    0.18 0.82

    P(C| )0 1

    0.75 0.25

    P(D|A B )A B D

    0 1

    0 0 0.6 0.4

    0 1 0.3 0.7

    1 0 0.1 0.9

    1 1 0.5 0.5

    P(E|C D )C D E

    0 1

    0 0 0.59 0.41

    0 1 0.25 0.75

    1 0 0.8 0.2

    1 1 0.35 0.65

    Figure 3.3: A simple Bayesian network.

  • 8/10/2019 BP With ProBT

    18/64

    18 CHAPTER 3. BAYESIAN NETWORKS

    BN() =

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Description

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Specification

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Relevant Variables:A,B,C,D ,E IB

    Decomposition:

    P(A B C D E | ) =P(A | )P(B| )P(C| )P(D|A B )P(E|C D )

    Parametric Forms:P(A | ) = {0.4, 0.6}P(B| ) = {0.18, 0.82}P(C| ) = {0.75, 0.25}

    P(D|A B ) =

    8>>>:

    0.6, 0.40.3, 0.70.1, 0.90.5, 0.5

    9>>=>>;

    P(E|C D ) =

    8>>>:

    0.59, 0.410.25, 0.750.80, 0.200.35, 0.65

    9>>=>>;

    Identification:All tables provided by the user

    Question:P(C B|[E=true] [D= false])

    Figure 3.4: The Bayesian program specification of Figure 3.3.

  • 8/10/2019 BP With ProBT

    19/64

    19

    Program 7:A simple bayesian network.

    1 /*========================================================= ==============

    2 * Product :

    3 * File : BayesianNetwork.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : 2004-Feb-05 15:51

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2000, Centre National de la Recherche Scientifique,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 *

    14 *

    15 *--------------------------------------------------------- --------------

    16 */

    17

    18 #include

    19 #include

    20 using namespace std;21

    22 int main()

    23 {

    24 /**********************************************************************

    25 VARIABLES SPECIFICATION

    26 **********************************************************************/

    27 plSymbol A("A",PL_BINARY_TYPE);

    28 plSymbol B("B",PL_BINARY_TYPE);

    29 plSymbol C("C",PL_BINARY_TYPE);

    30 plSymbol D("D",PL_BINARY_TYPE);

    31 plSymbol E("E",PL_BINARY_TYPE);

    32

    33 /**********************************************************************

    34 PARAMETRIC FORM SPECIFICATION

    35 **********************************************************************/

    36 // Specification of P(A)

    37 plProbValue tableA[] = {0.4, 0.6};

    38 plProbTable P_A(A, tableA);

    39

    40 // Specification of P(B)

    41 plProbValue tableB[] = {0.18, 0.82};

    42 plProbTable P_B(B, tableB);

    43

    44 // Specification of P(C)

    45 plProbValue tableC[] = {0.75, 0.25};

    46 plProbTable P_C(C, tableC);

    47

    48 // Specification of P(D | A B)

    49 plProbValue tableD_knowingA_B[] = {0.6, 0.4, // P(D | [A=f]^[B=f])

    50 0.3, 0.7, // P(D | [A=f]^[B=t])

    51 0.1, 0.9, // P(D | [A=t]^[B=f])

    52 0.5, 0.5}; // P(D | [A=t]^[B=t])

    53 plDistributionTable P_D(D,A^B,tableD_knowingA_B);

  • 8/10/2019 BP With ProBT

    20/64

    20 CHAPTER 3. BAYESIAN NETWORKS

    54

    55 // Specification of P(E | C D)

    56 plProbValue tableE_knowingC_D[] = {0.59, 0.41, // P(E | [C=f]^[D=f])

    57 0.25, 0.75, // P(E | [C=f]^[D=t])

    58 0.8, 0.2, // P(E | [C=t]^[D=f])

    59 0.35, 0.65}; // P(E | [C=t]^[D=t])

    60 plDistributionTable P_E(E,C^D,tableE_knowingC_D);

    6162 /**********************************************************************

    63 DECOMPOSITION

    64 **********************************************************************/

    65 plJointDistribution jd(A B^C D^E, P_A*P_B*P_C*P_D*P_E);

    66 jd.draw_graph("bayesian_network.fig");

    67 cout

  • 8/10/2019 BP With ProBT

    21/64

    21

    Gray Clouds(GC)

    Rain(R)

    Sprinkler(S)

    Wet Grass(WG)

    P(GC| )0 1

    0.5 0.5

    P(R|GC )GC R

    0 1

    0 0.8 0.2

    1 0.35 0.65

    P(S|GC )GC S

    0 1

    0 0.5 0.5

    1 0.9 0.1

    P(W G|S R )S R W G

    0 1

    0 0 1.0 0.0

    0 1 0.1 0.91 0 0.1 0.9

    1 1 0.01 0.99

    Figure 3.5: The water-sprinkler Bayesian network.

  • 8/10/2019 BP With ProBT

    22/64

    22 CHAPTER 3. BAYESIAN NETWORKS

    Exercise 6: A complicated situation for Peter.Peter will hold a party at home and he invited his best friends: Mary,

    Alice, John, Victor, and Bill. Peter would like to have everyone at the party.However, there are various factors that will influence the party attendance.

    1. Mary, Alice, John, and Bill will accept the invitation in function ofdifferent events.

    John is rainophobe, if he receives the invitation on a rainyday he will refuse it with a probability of 0.6. If he receives theinvitation on a non rainy day he will accept it with a probability0.9.

    Alice will accept a weekend invitation with a probability of 0.8and a weekday invitation with a probability of 0.4.

    Bill is a very busy man and will not accept a late invitation witha probability 0.7. He will accept an in time invitation with a

    probability of 0.95. Mary best friends are John and Alice. Nevertheless, from Marys

    point of view John and Alice are always fighting. Consequently,Mary will accept the invitation with a probability 0.05 if bothJohn and Alice accepted the invitation. She will accept with aprobability of 0.95 if John accepted but Alice didnt. Similarly, ifAlice accepted but John didnt she will accept with a probabilityof 0.85. If neither Alice nor John accepted then she will leavethat to chance by flipping a coin.

    2. Victor lives with Peter and does not need to accept or refuse the invi-

    tation. However, the day of the party he will stay at home dependingon Alice and Bill response. In fact there is some competition betweenBill and Victor for Alice, consequently if both of them accepted thenVictor would be present. If this is not the case, he will decide betweenstaying at home and going to a pub for watching a football game. Ifnone of them accepted then he will stay at home with a probability of0.7. If Alice accepted but Bill didnt then he will stays at home witha probability of 0.9. If Bill accepted and Alice didnt then he will goto the pub with a probability of 0.6.

    3. One thing is to accept or to refuse the invitation and another thing isto attend or not to the party. Except for Victor, everyone can change

    his or her mind. Peter will accept the attendance of a friend even

  • 8/10/2019 BP With ProBT

    23/64

    23

    if a he or she gave a negative response. In the past, Mary changedher mind 3% of the time. If Alice accepted an invitation then youcan be sure at 80% that she will be present, if she refused then theprobabilities for attendance and non-attendance are equals.

    4. John and Bill have extra constraints.

    The day of the party John will not come if it rains and if he didntaccept the invitation. However, in the past, when it was rainingand he accepted the invitation he showed up at the party 70% ofthe time. If the day of the party is not raining and he refused theinvitation then he will show up at the party with a probability of0.4; if he accepted then he will show up with a probability of 0.9.

    Bill works at the hospital. Hence, if the day of the party thereis an emergency he will not come. If no emergency is presentand he accepted the invitation he will come with a probability

    of 0.95; on the contrary, if he refused, then he will come with aprobability of 0.2.

    5. The probability of rain is 0.22.

    6. The probability of organizing the party at weekend is 0.28

    7. The probability of receiving a late invitation is 0.3.

    8. The probability of having an emergency at the hospital is 0.65

    For the previous description:

    i. Identify the variables of the problem

    ii. Write Bayesian program: Description + Question. Where the questionis the probability of presence of each of the Peters friends.

    iii. Draw the Bayesian network

    iv. Write the expressions for the following questions:

    (a) What is the probability that it was raining when John receivedthe invitation knowing that Mary didnt attend the party and

    that the party is on Monday?

  • 8/10/2019 BP With ProBT

    24/64

    24 CHAPTER 3. BAYESIAN NETWORKS

    (b) What is the probability that all Peters friends attend the partyknowing that it will take place on a sunny Saturday, knowingthat Bill received the invitation in time and no emergency waspresent at the hospital?

    (c) What is the probability that Alice, Victor and Bill attend the

    party?

    (d) Alice answer the phone at Peters house, Hi John. What isthe probability that Mary and Victor attended the party? (Note:No, no, no, John is not calling from a cell phone).

    v. Write the ProBT Bayesian program modeling the Peters problem andsolving the previous questions.

    Program 8:The addition of two dice.

    1 /*======================================================= ================

    2 * Product :

    3 * File : addTwoDice.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : 2008-Oct-02 17:27

    6 *

    7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program shows the use of external functions by means of C++

    14 * functions The construction of a functional dirac is also shown.

    15 * The program gives the possible values of two dice given the sum of

    16 * points.

    17 *----------------------------------------------------------- ------------

    18 */

    19

    20 #include

    21

    22 using namespace std;

    23

    24 void add_dice(plValues &addition, const plValues &die) {

    25 addition[0] = die[0]+die[1];

    26 }

    27

    28

    29 int main()

    30 {

    31 /***************************************************** *****************

    32 VARIABLES SPECIFICATION

  • 8/10/2019 BP With ProBT

    25/64

    25

    33 ********************************************************** ************/

    34 plIntegerType die_type(1,6); // Type for a die [1,2,...,6]

    35 plIntegerType dice_sum(2,12); // Type for the sum [2,3,...,12]

    36 plArray dice_set("Die",die_type,1,2); // Variable space for the dice

    37 plSymbol points("Addition",dice_sum); // Variable space for the addition

    38

    39 plValues result(dice_set^points); // Values storing the result of the

    40 // dice and the addition41 /********************************************************** ************

    42 PARAMETRIC FORM SPECIFICATION

    43 ********************************************************** ************/

    44 // Defining P(Die1 Die2) as an uniform distribution

    45 plUniform p_dice(dice_set);

    46

    47 // Defining P(Addition | Die1 Die2) = 1 if

    48 // Addition = add_dice(Die1,Die2) else 0

    49 plExternalFunction sum(points,dice_set,add_dice);

    50 plFunctionalDirac p_addition(points,dice_set,sum);

    51

    52 /********************************************************** ************

    53 DECOMPOSITION

    54 ********************************************************** ************/

    55 // Defining P(Die1 Die2 Addition) = P(Die1 Die2) P(Addition | Die1 Die2)

    56 plJointDistribution dice_jd(dice_set points,p_dice*p_addition);57

    58 /********************************************************** ************

    59 PROGRAM QUESTION

    60 ********************************************************** ************/

    61 plCndDistribution cnd_question;

    62 plDistribution question;

    63 int v;

    64

    65 // Get P(Die1 Die2 | Addition )

    66 dice_jd.ask(cnd_question,dice_set,points);

    67 coutv;

    68 result[points] = v;

    69

    70 // Get P(Die1 Die2 | Addition=v)

    71 cnd_question.instantiate(question,result);

    72 question.tabulate(cout,false);73

    74 return 0;

    75 }

    Exercise 7: Modify the program addTwoDice.cpp so that it accepts anumber n of dice as input? That is, write a program to compute

    P(Points|Die1Die2...Dien).

    Exercise 8: For the Peters party model introduce a new variable N

    representing the number of persons that attend the Party and compute:

  • 8/10/2019 BP With ProBT

    26/64

    26 CHAPTER 3. BAYESIAN NETWORKS

    1. the probability distribution for the number of persons that attend theparty (plot the distribution),

    2. the probability that it rained on the invitation day knowing that threepersons attended the party.

    Consider that your fellow has a box with m dice; secretly she takesN Mdice and throw them on the table. She lets you know that the sumof all points is s. What is the most probable number Nof dice.

    Program 9:How many dice do I have?.

    1 /*======================================================= ================

    2 * Product :

    3 * File : addDice3.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : 2002-Oct-30 19:29

    6 *7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program computes the more probable number of dice that where

    14 * thrown given that we know the total number of points

    15 *----------------------------------------------------------- ------------

    16 */

    17

    18 #include

    19

    20 using namespace std;

    21

    22 #define SQRT_DIV35_12 1.7078251

    23

    24 // User function for computing the mean

    25 void f_mean(plValues &mean, const plValues &n) {

    26 mean[0] = 7*n[0]/2;

    27

    28 }

    29

    30 // User function for computing the standard deviation

    31 void f_std(plValues &std, const plValues &n) {

    32 std[0] = SQRT_DIV35_12*sqrt(double(n[0]));

    33

    34 }

    35

    36 int main()

    37 {

    38 /***************************************************** *****************

    39 VARIABLES SPECIFICATION

  • 8/10/2019 BP With ProBT

    27/64

    27

    40 ********************************************************** ************/

    41 plIntegerType die_number(1,10); // Type for the # of dice [1,2,...,100]

    42 plIntegerType dice_sum(1,60); // Type for the sum [2,3,...,n*6]

    43 plSymbol points("Addition",dice_sum); // Variable space for the addition

    44 plSymbol number("n",die_number); // Variable space for the # of dice

    45

    46 plValues result(number^points); // Values storing the # of dice

    47 // and the addition48

    49 /********************************************************** ************

    50 PARAMETRIC FORM SPECIFICATION

    51 **********************************************************************/

    52 // P(number) = uniform(number)

    53 plUniform p_number(number);

    54

    55 // Create the external functions to compute the mean and the

    56 // standard deviation

    57 plExternalFunction f_mu(number,f_mean);

    58 plExternalFunction f_sigma(number,f_std);

    59

    60 // P(points | number) = CndBellShape(points,f_mu(number),f_sigma(number))

    61 plCndBellShape p_addition(points,number,f_mu,f_sigma);

    62

    63 /********************************************************** ************64 DECOMPOSITION

    65 **********************************************************************/

    66 // P(points number) = P(points | number) P(number)

    67 plJointDistribution dice_jd(number^points,p_number*p_addition);

    68

    69 /********************************************************** ************

    70 PROGRAM QUESTION

    71 **********************************************************************/

    72 plCndDistribution cnd_question;

    73 plDistribution question;

    74 int v;

    75

    76 // Get P(number | points)

    77 dice_jd.ask(cnd_question,number,points);

    78 coutv;80 result[points] = v;

    81

    82 // Get P(number | points = v)

    83 cnd_question.instantiate(question,result);

    84 plDistribution compiled_question;

    85

    86 question.compile(compiled_question);

    87 cout

  • 8/10/2019 BP With ProBT

    28/64

    28 CHAPTER 3. BAYESIAN NETWORKS

  • 8/10/2019 BP With ProBT

    29/64

    Chapter 4

    Mixture models

    Exercise 9: The population of a region on Equatorial Africa is composedof a non-Pygmy majority (84%) and a substantial Pygmy minority (16%).A non-pygmy adult has an average height of 1.55 meters with a standarddeviation of 7.5 cm. In contrast, a pygmy adult has an average height of1.30 meters with a standard deviation of 9.5 cm. The distribution of heightis assumed to be Gaussian.

    Compute P(Height) for this region of Equatorial Africa.

    29

  • 8/10/2019 BP With ProBT

    30/64

    30 CHAPTER 4. MIXTURE MODELS

  • 8/10/2019 BP With ProBT

    31/64

    Chapter 5

    Nave Bayes

    P(D|M1 M2) = P(D)P(M1 M2|D)

    P(M1 M2)

    P(D)P(M1 M2 |D) is the joint distribution P(D M1 M2) andP(M1 M2)is a constant without effect.

    P(D M1 M2) = P(D)P(M1 M2|D)

    = P(D)P(M1|D)P(M2|D M1).

    The naive conditional independence assumption consists in supposing thatM1 is conditionally independent ofM2. Other wise stated that:

    P(M2|D M

    1) =P(M

    2|D).

    The joint distribution of the same model with n sensors will be writenas:

    P(D M1 M2...Mn) = P(D)P(M1 M2...Mn|D)

    = P(D)P(M1|D)P(M2 ...Mn|D M1)

    = P(D)P(M1|D)P(M2|D M1)P(M3 ...Mn|D M1)...

    = P(D)P(M1|D)P(M2|D M1) . . . P (Mn|D M1 M2. . . M n).

    31

  • 8/10/2019 BP With ProBT

    32/64

    32 CHAPTER 5. NAIVE BAYES

    The assuming that each Mi is conditionally independent of every other Mjwith i =j leads to:

    P(D|M1 M2...Mn) P(D)n

    i=1

    P(Mi|D)

    Program 10:A sensor fusion program.

    1 /*======================================================= ================

    2 * Product :

    3 * File : sensors.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : Fri Nov 21 18:14:13 2008

    6 *

    7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program simulates reading the distance of and object with two

    14 * sensors. The two measures has a bellshape like distribution with

    15 * two different variances. The goal is to find the more probable real

    16 * distance

    17 *----------------------------------------------------------- ------------

    18 */

    19

    20 #define MIN_DIST 0

    21 #define MAX_DIST 100

    22 #define STD_DEV_1 10

    23 #define STD_DEV_2 15

    24

    25 //#define MIN_MES_1 0

    26 #define MIN_MES_1 (MIN_DIST-3*STD_DEV_1)

    27 #define MAX_MES_1 (MAX_DIST+3*STD_DEV_1)

    28

    29 //#define MIN_MES_2 0

    30 #define MIN_MES_2 (MIN_DIST-3*STD_DEV_2)

    31 #define MAX_MES_2 (MAX_DIST+3*STD_DEV_2)

    32

    33 #include

    34

    35 using namespace std;

    36

    37 int main ()

    38 {

    39 /**********************************************************************

    40 VARIABLES SPECIFICATION

    41 **********************************************************************/

    42 plIntegerType distance(MIN_DIST,MAX_DIST); // Distance type

  • 8/10/2019 BP With ProBT

    33/64

    33

    43 plIntegerType measure1(MIN_MES_1, // Measure type for sensor 1

    44 MAX_MES_1);

    45 plIntegerType measure2(MIN_MES_2, // Measure type for sensor 2

    46 MAX_MES_2);

    47

    48 plSymbol D("D",distance); // Real distance variable

    49 plSymbol M1("M1",measure1); // sensor 1 measure

    50 plSymbol M2("M2",measure2); // sensor 2 measure51

    52

    53 plVariable model_vars; // The variables of the model are the distance {D}

    54 model_vars = D^M1^M2; // union measures 1 and 2 {M1, M2}

    55

    56

    57 plValues model_values(model_vars); // A variable values for storing

    58 // our data

    59 /**********************************************************************

    60 PARAMETRIC FORM SPECIFICATION

    61 **********************************************************************/

    62 plUniform P_dist(D); // D has an uniform distribution

    63 plCndBellShape P_dist1(M1,D,STD_DEV_1); // M1 and M2 are bellshape like

    64 plCndBellShape P_dist2(M2,D,STD_DEV_2); // distribution with a

    65 // predefined variance

    6667 /**********************************************************************

    68 DECOMPOSITION

    69 **********************************************************************/

    70 // Defining the joint distribution P(D M1 M2) = P(D) P(M1|D) P(M2|D)

    71 plJointDistribution my_model(model_vars,P_dist*P_dist1*P_dist2);

    72

    73

    74 /**********************************************************************

    75 PROGRAM QUESTION

    76 **********************************************************************/

    77 //Ask P( D | M1 M2)

    78 plCndDistribution P_Cnd_dist;

    79 my_model.ask(P_Cnd_dist,D,M1^M2); // Get P( D | M1 M2)

    80

    81 // Read measures from sensor A and B, then get P( D | M1=va M1=va)

    8283 int v1, v2;

    84

    85 do {

    86 coutv1;

    88 }while (v1 < MIN_MES_1 || v1 > MAX_MES_1);

    89

    90 do {

    91 coutv2;

    93 }while (v2 < MIN_MES_2 || v2 > MAX_MES_2);

    94

    95 model_values[M1]=v1;

    96 model_values[M2]=v2;

    97

    98 plDistribution P_dist_after_measure;

  • 8/10/2019 BP With ProBT

    34/64

    34 CHAPTER 5. NAIVE BAYES

    99 P_Cnd_dist.instantiate(P_dist_after_measure,

    100 model_values); //Get P( D | M1=v1 M2=v2)

    101

    102 P_dist_after_measure.best(model_values); //Get the most probable distance

    103

    104 cout

  • 8/10/2019 BP With ProBT

    35/64

    35

    35 // Set the spamer dictionary from the indicated file

    36 spamer_dictionary my_dictionary(argv[1]);

    37

    38 /**********************************************************************

    39 VARIABLES SPECIFICATION

    40 **********************************************************************/

    41 plSymbol Spam("Spam",PL_BINARY_TYPE);

    42 plArray W("W",PL_BINARY_TYPE,1,my_dictionary.get_number_of_words());43

    44 /********************************************************* *************

    45 PARAMETRIC FORM SPECIFICATION

    46 **********************************************************************/

    47 // Construct P(Spam) from the the data on the dictionary

    48 plProbTable P_Spam(Spam,my_dictionary.spams);

    49

    50 // A tabable containing all the P(Wi | Spam)

    51 vector P_W(my_dictionary.get_number_of_words());

    52

    53 // A list containig the product of

    54 // P(W0 | Spam)*P(W1 | Spam)*...P(Wn | Spam)

    55 plComputableObjectList all_PW;

    56

    57 // Construct each of the P(Wi | Spam) from the data on the dictionary

    58 for (i=0; i

  • 8/10/2019 BP With ProBT

    36/64

    36 CHAPTER 5. NAIVE BAYES

    91

    92 // Instantiate and compile the question

    93 CndQuestion.instantiate(Question,W_values);

    94 Question.compile(CompiledQuestion);

    95

    96 // Display the result

    97 cout

  • 8/10/2019 BP With ProBT

    37/64

    Chapter 6

    Dynamic Bayesian

    Programming

    6.1 Bayesian filters

    Exercise 11: Use Program 9 to write a program that accepts a sequenceof throws of N dice with N 6. That is, your fellow takes the N diceand throw them t times. She let you know the number of points for each ofthe throws (without changing the number of dice). Write a ProBT programcomputing P(N| points1 points2 ... pointst).

    6.2 Hidden Markov Models

    For the previous exercise, consider that the number of dice from throw i

    to throw i + 1 could change according to a random hidden variable Action.The hiden variable Action indicates one of three cases: add one die, takeout one die or stay with the same number of dice. Given a sequence ofnumber of points (corresponding to throwing the dice t times) we wold liketo computes the more probable number of dice at throw 1, 2,...,t. Otherwisestated, P(Nt| ponts1 points2 ...pointsn) is to be computed. The bayesiannetwork representing this model is shown in Figure 6.1. The distributionof P(Nt| Nt1 Action ) and that of P(Action) are shown in Figure 6.2.Assume that the initial number of dice (N0) is 3.

    37

  • 8/10/2019 BP With ProBT

    38/64

    38 CHAPTER 6. DYNAMIC BAYESIAN PROGRAMMING

    Program 12:The dice Hidden Markov Model.

    1 /*======================================================= ================

    2 * Product :

    3 * File : diceHMM.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : 2008-Oct-02 16:16

    6 *

    7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * Given a sequence of number of points (corresponding to throwing the

    14 * dice t times). This program computes the more probable number of dice

    15 * at throw 1,2,...,t. The number of dice from throw i to throw i+1

    16 * could change according to a random hidden variable "Action". The hiden

    17 * variable "Action" indicates one of three cases: add one die, reduce one

    18 * die or stay with the same number of dice.

    19 *----------------------------------------------------------- ------------

    20 */21

    22 #include

    23

    24 using namespace std;

    25

    26 #define SQRT_DIV35_12 1.7078251

    27

    28 // User function for computing the mean

    29 void f_mean(plValues &mean, const plValues &n) {

    30 mean[0] = 7*n[0]/2;

    31

    32 }

    33

    34 // User function for computing the standard deviation

    35 void f_std(plValues &std, const plValues &n) {

    36 std[0] = SQRT_DIV35_12*sqrt(double(n[0]));

    37

    38 }

    39

    40 const unsigned int max_n = 6;

    41

    42 int main()

    43 {

    44 /***************************************************** *****************

    45 VARIABLES SPECIFICATION

    46 **********************************************************************/

    47 plIntegerType die_number(1,max_n); // Type for the maximum number

    48 // of dice [1,2,...,max_n]

    49 plIntegerType dice_sum(1,max_n*6); // Type for the sum [2,3,...,n*6]

    50 plIntegerType HiddenVarType (-1, 1); // The type for the hidden variable

    51

    52 plSymbol Points("Points",dice_sum); // Variable space for the addition

    53 plSymbol Previous_N("Prev_N",die_number);// Previous number of dice

  • 8/10/2019 BP With ProBT

    39/64

    6.2. HIDDEN MARKOV MODELS 39

    54 plSymbol Current_N("Curr_N",die_number); // Current number of dice

    55

    56 plSymbol Action("Action", HiddenVarType); // The hidden variable

    57

    58 plValues result(Current_N^Points); // Values storing the # of dice

    59 // and the addition

    60 /********************************************************** ************

    61 PARAMETRIC FORM SPECIFICATION62 **********************************************************************/

    63 // P(Prevoius_N) at the begining we start with 3 dice

    64 plProbValue tablePrevious[] = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0};

    65 plProbTable P_Previous_N(Previous_N, tablePrevious);

    66

    67 plProbValue tableAction[] = {0.333, 0.166, 0.5};

    68 plProbTable P_Action(Action, tableAction);

    69

    70

    71 plProbValue tableCurrent_N[] =

    72 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, // N=1 A = -1

    73 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, // N=1 A = 0

    74 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, // N=1 A = 1

    75 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, // N=2 A = -1

    76 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, // N=2 A = 0

    77 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, // N=2 A = 178 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, // N=3 A = -1

    79 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, // N=3 A = 0

    80 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // N=3 A = 1

    81 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, // N=4 A = -1

    82 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // N=4 A = 0

    83 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, // N=4 A = 1

    84 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // N=5 A = -1

    85 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, // N=5 A = 0

    86 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, // N=5 A = 1

    87 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, // N=6 A = -1

    88 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, // N=6 A = 0

    89 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, // N=6 A = 1

    90 };

    91 plDistributionTable P_Current_N(Current_N, Previous_N Action, tableCurrent_N);

    92

    93 cout

  • 8/10/2019 BP With ProBT

    40/64

    40 CHAPTER 6. DYNAMIC BAYESIAN PROGRAMMING

    110

    111 /***************************************************** *****************

    112 PROGRAM QUESTION

    113 ************************************************************ **********/

    114 plCndDistribution cnd_question;

    115 plDistribution question;

    116 int v;

    117118 // Get P(Current_N | Points)

    119 dice_jd.ask(cnd_question,Current_N,Points);

    120

    121 unsigned int n=0;

    122

    123 coutn;

    125

    126 do {

    127 n--;

    128 coutv;

    130 result[Points] = v;

    131

    132 // Get P(Current_N | Points = v)

    133 cnd_question.instantiate(question,result);134 plDistribution compiled_question;

    135

    136 question.compile(compiled_question);

    137 cout

  • 8/10/2019 BP With ProBT

    41/64

    6.2. HIDDEN MARKOV MODELS 41

    Actiont

    Nt1 Nt

    Pointst

    Figure 6.1: The dice Hidden Markov Model.

  • 8/10/2019 BP With ProBT

    42/64

    42 CHAPTER 6. DYNAMIC BAYESIAN PROGRAMMING

    P(Nt|Nt1 Action )Nt1 Action N t

    1 2 3 4 5 6

    1 -1 1 0 0 0 0 0

    1 0 1 0 0 0 0 0

    1 1 0 1 0 0 0 0

    2 -1 1 0 0 0 0 0

    2 0 0 1 0 0 0 0

    2 1 0 0 1 0 0 0

    3 -1 0 1 0 0 0 03 0 0 0 1 0 0 0

    3 1 0 0 0 1 0 0

    4 -1 0 0 1 0 0 0

    4 0 0 0 0 1 0 0

    4 1 0 0 0 0 1 0

    5 -1 0 0 0 1 0 0

    5 0 0 0 0 0 1 0

    5 1 0 0 0 0 0 1

    6 -1 0 0 0 0 1 0

    6 0 0 0 0 0 0 1

    6 1 0 0 0 0 0 1

    P(Action)-1 0 1

    0.333 0.166 0.5

    Figure 6.2: The distributionsP(Nt|Nt1 Action ) andP(Action) used forthe Bayesian Network on Figure 6.1.

  • 8/10/2019 BP With ProBT

    43/64

    Appendix A

    Answers to exercises

    Exercise 1: For each one of the cases, Line 20 of program dice.cpp mustbe changed as shown below:

    i. plArray dice_set("Die",die_type,1,2);

    ii. plArray dice_set("Die",die_type,1,5);

    iii. plArray dice_set("Die",die_type,2,3,5);

    iv. plArray dice_set("Die",die_type,3,3,5,2);

    Exercise 2: Add the following code at the end of the file twoDice.cpp:

    plValues PntsDieValues(Points^Die);

    PntsDieValues.reset();

    do{

    cout

  • 8/10/2019 BP With ProBT

    44/64

    44 APPENDIX A. ANSWERS TO EXERCISES

    P(Die=1 | Points=2)= 0.597015

    P(Die=0 | Points=3)= 0.252336

    P(Die=1 | Points=3)= 0.747664

    P(Die=0 | Points=4)= 0.252336

    P(Die=1 | Points=4)= 0.747664

    P(Die=0 | Points=5)= 0.402985P(Die=1 | Points=5)= 0.597015

    P(Die=0 | Points=6)= 0.252336

    P(Die=1 | Points=6)= 0.747664

    Output for Exercise 3:

    P(Die=0 | Points=1)= 0.108

    P(Die=1 | Points=1)= 0.106667

    P(Die=0 | Points=2)= 0.072

    P(Die=1 | Points=2)= 0.106667

    P(Die=0 | Points=3)= 0.036

    P(Die=1 | Points=3)= 0.106667P(Die=0 | Points=4)= 0.036

    P(Die=1 | Points=4)= 0.106667

    P(Die=0 | Points=5)= 0.072

    P(Die=1 | Points=5)= 0.106667

    P(Die=0 | Points=6)= 0.036

    P(Die=1 | Points=6)= 0.106667

    Exercise 4:

    plDistribution question2;

    jd.ask(question2, Points);

    cout

  • 8/10/2019 BP With ProBT

    45/64

    45

    Exercise 5:

    Program 13:The water-sprinkler program.

    1 /*========================================================= ==============

    2 * Product :3 * File : sprinkler.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : Fri Nov 21 18:06:08 2008

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This contains the water-sprinkler model

    14 *--------------------------------------------------------- --------------

    15 */

    16

    17 #include

    18 #include 19

    20 using namespace std;

    21

    22 int main() {

    23 /********************************************************** ************

    24 VARIABLES SPECIFICATION

    25 ********************************************************** ************/

    26 plSymbol GC("GC",PL_BINARY_TYPE);

    27 plSymbol R("R",PL_BINARY_TYPE);

    28 plSymbol WG("WG",PL_BINARY_TYPE);

    29 plSymbol S("S",PL_BINARY_TYPE);

    30

    31 /********************************************************** ************

    32 PARAMETRIC FORM SPECIFICATION

    33 ********************************************************** ************/

    34 plProbValue T_GC[] = {0.5, 0.5};

    35 plProbTable P_GC(GC,T_GC);

    36

    37 plProbValue T_R[] = {0.8, 0.2,

    38 0.35, 0.65};

    39 plDistributionTable P_R(R,GC,T_R);

    40

    41 plProbValue T_S[] = {0.5, 0.5,

    42 0.9, 0.1};

    43 plDistributionTable P_S(S,GC,T_S);

    44

    45 plProbValue T_WG[] = {1.0, 0.0,

    46 0.1, 0.9,

    47 0.1, 0.9,

    48 0.01, 0.99};

    49 plDistributionTable P_WG(WG,S^R,T_WG);

    50

    51 /********************************************************** ************

  • 8/10/2019 BP With ProBT

    46/64

    46 APPENDIX A. ANSWERS TO EXERCISES

    52 DECOMPOSITION

    53 ************************************************************ **********/

    54 plJointDistribution jd(GC^R^S WG, P_GC*P_R*P_S*P_WG);

    55

    56 /***************************************************** *****************

    57 PROGRAM QUESTION

    58 ************************************************************ **********/

    59 plCndDistribution question;60 plDistribution inst_question, comp_question;

    61

    62 jd.ask(question, GC, WG^S);

    63

    64 plValues evidence(WG^S);

    65 evidence[WG] = true;

    66 evidence[S] = false;

    67

    68 question.instantiate(inst_question, evidence);

    69 inst_question.compile(comp_question);

    70

    71 cout

  • 8/10/2019 BP With ProBT

    47/64

    47

    EEmergency

    JA John accepted

    MA Mary accepted

    AA Alice accepted

    BA Bill accepted

    JPJohn is present

    MPMary is present

    APAlice is present

    VPVictor is present

    BPBill is present

    ii. The Bayesian program for Peters party is show in Figure A.1

    PartyBN() =

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Description

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Specification

    8>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:

    Relevant Variables:RI, RP, We, LI, E, JA, MA, AA, BA, JP, MP, AP, VP, BP

    Decomposition:

    P(RI RP We LI E JA MA AA BA JP MP AP V P BP| ) =

    Decomposition

    P(RI| )P(RP| )P(We| )P(LI| )P(E| )P(JA |RI )P(BA |LI )P(AA |We )P(AP|AA )P(MA |JA AA )P(MP|MA )P(VP|AA BA )P(JP|JA RP )P(BP|BA E )

    Parametric Forms:See FigureA.2

    Identification:All tables provided by the user

    Question:

    P(JP MP AP VP BP| )

    Figure A.1: Peters party Bayesian program. The variables are Rain oninvitations day (RI),Rain on Partys day(RP),Weekend invitation(We),Late invitation (LI), Emergency (E), John accepted (JA), Mary accepted(MA), Alice accepted(AA),Bill accepted(BA), John is present(JP), Maryis present(MP),Alice is present(AP),Victor is present(VP),Bill is present(BP)

    iii. The Bayesian network is shown in Figure A.3

    iv. The expressions for the questions are:

  • 8/10/2019 BP With ProBT

    48/64

    48 APPENDIX A. ANSWERS TO EXERCISES

    P(RI| )0 1

    0.78 0.22

    P(RP| )0 1

    0.78 0.22

    P(We| )0 1

    0.72 0.28

    P(LI| )0 1

    0.7 0.3

    P(E| )0 1

    0.35 0.65

    P(JA|RI )RI JA

    0 1

    0 0.1 0.9

    1 0.6 0.4

    P(BA|LI )LI BA

    0 1

    0 0.05 0.95

    1 0.7 0.3

    P(AA|We )We AA

    0 1

    0 0.6 0.4

    1 0.2 0.8

    P(AP| AA )AA AP

    0 1

    0 0.5 0.5

    1 0.2 0.8

    P(MA|JA AA )JA AA MA

    0 1

    0 0 0.5 0.5

    0 1 0.15 0.85

    1 0 0.05 0.95

    1 1 0.95 0.05

    P(MP| MA )MA MP

    0 1

    0 0.97 0.03

    1 0.03 0.97

    P(VP| AA BA )AA BA VP

    0 1

    0 0 0.3 0.7

    0 1 0.6 0.41 0 0.1 0.9

    1 1 0.0 1.0

    P(JP| JA RP )JA RP JP

    0 1

    0 0 0.6 0.4

    0 1 1.0 0.01 0 0.1 0.9

    1 1 0.3 0.7

    P(BP| BA E )BA E BP

    0 1

    0 0 0.8 0.2

    0 1 1.0 0.01 0 0.05 0.95

    1 1 1.0 0.0

    Figure A.2: Parametrical forms of Peters party Bayesian program (SeeFigure A.1).

  • 8/10/2019 BP With ProBT

    49/64

    49

    Invitationon weekend

    It rains theday of the

    Urgency inthe hospital

    John is present Mary is present Alice is present Victor is present Bill is present

    Late invitation

    John Alice Bill

    Mary

    accepted accepted accepted

    accepted

    invitation

    It rains theday of the

    party

    Figure A.3: The Peters party Bayesian network.

    (a) What is the probability that it was raining when John receivedthe invitation knowing that Mary didnt attend the party andthat the party is on Monday?

    P(RI =true|MP=true We=false )

    (b) What is the probability that all Peters friends attend the party

    knowing that it will take place on a sunny Saturday, knowingthat Bill received the invitation in time and no emergency waspresent at the hospital?

    P(JP=true MP=true AP=true AP=true BP=true|RP=false We= true LI =false E=false)

    (c) What is the probability that Alice, Victor and Bill attend theparty?

    P(AP=true VP=true BP=true| )

  • 8/10/2019 BP With ProBT

    50/64

    50 APPENDIX A. ANSWERS TO EXERCISES

    (d) Alice answer the phone at Peters house, Hi John. What isthe probability that Mary and Victor attended the party? (Note:No, no, no, John is not calling from a cell phone).

    P(MP=true VP=true|AP=true JP=false )

    v. The ProBT Bayesian program modeling the Peters problem appearsbelow:

  • 8/10/2019 BP With ProBT

    51/64

    51

    Program 14:Peters problem .

    1 /*======================================================== ===============

    2 * Product :

    3 * File : PartyBN.cpp

    4 * Author : Juan-Manuel Ahuactzin5 * Creation : Wed Nov 26 16:54:59 2008

    6 *

    7 *======================================================== ===============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *======================================================== ===============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program solves the Peters party problem

    14 *-------------------------------------------------------- ---------------

    15 */

    16

    17 #include

    18 #include

    19

    20 using namespace std;21

    22 int main() {

    23

    24 /********************************************************* *************

    25 VARIABLES SPECIFICATION

    26 ********************************************************* *************/

    27 plSymbol RainOnInv("RainOnInv", PL_BINARY_TYPE);

    28 plSymbol RainOnParty("RainOnParty",PL_BINARY_TYPE);

    29 plSymbol WE_Invitation("WE_Invitation",PL_BINARY_TYPE);

    30 plSymbol LateInv("LateInv",PL_BINARY_TYPE);

    31 plSymbol Emergency("Emergency",PL_BINARY_TYPE);

    32 plSymbol JohnAccepted("JohnAccepted",PL_BINARY_TYPE);

    33 plSymbol MaryAccepted("MaryAccepted",PL_BINARY_TYPE);

    34 plSymbol AliceAccepted("AliceAccepted",PL_BINARY_TYPE);

    35 plSymbol BillAccepted("BillAccepted",PL_BINARY_TYPE);

    36 plSymbol JohnIsPresent("JohnIsPresent",PL_BINARY_TYPE);

    37 plSymbol MaryIsPresent("MaryIsPresent",PL_BINARY_TYPE);

    38 plSymbol AliceIsPresent("AliceIsPresent",PL_BINARY_TYPE);

    39 plSymbol VictorIsPresent("VictorIsPresent",PL_BINARY_TYPE);

    40 plSymbol BillIsPresent("BillIsPresent",PL_BINARY_TYPE);

    41

    42 /********************************************************* *************

    43 PARAMETRIC FORM SPECIFICATION

    44 ********************************************************* *************/

    45 plProbValue tableRain[] = {0.78, 0.22};

    46

    47 plProbTable P_RI(RainOnInv, tableRain);

    48 cout

  • 8/10/2019 BP With ProBT

    52/64

    52 APPENDIX A. ANSWERS TO EXERCISES

    54 cout

  • 8/10/2019 BP With ProBT

    53/64

    53

    110 plProbValue tableBP[] = {0.8, 0.2,

    111 1.0, 0.0,

    112 0.05, 0.95,

    113 1.0, 0.0};

    114 plDistributionTable P_BP(BillIsPresent, BillAccepted^Emergency, tableBP);

    115 cout

  • 8/10/2019 BP With ProBT

    54/64

    54 APPENDIX A. ANSWERS TO EXERCISES

    166 values[AliceIsPresent]=true;

    167 values[BillIsPresent]=true;

    168 values[VictorIsPresent]=true;

    169

    170 cout

  • 8/10/2019 BP With ProBT

    55/64

    55

    Exercise 7:

    Program 15:The sum of N dice.

    1 /*========================================================= ==============

    2 * Product :3 * File : addDice.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : 2002-Oct-30 19:29

    6 *

    7 *========================================================= ==============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *========================================================= ==============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program shows the use of external functions by means of C++ functions

    14 * The construction of a functional dirac is also shown.

    15 * The program gives the possible values of each of the dice given the sum of

    16 * points.

    17 *--------------------------------------------------------- --------------

    18 */19

    20 #include

    21

    22 using namespace std;

    23

    24 void add_dice(plValues &dice_addition, const plValues &die) {

    25 unsigned int n_dice; // Number of dice

    26 unsigned int sum=0; // sum of points

    27 unsigned int i;

    28

    29 n_dice = die.size(); // Get the number of dice

    30

    31 for(i=0;i

  • 8/10/2019 BP With ProBT

    56/64

    56 APPENDIX A. ANSWERS TO EXERCISES

    52 number_of_dice*6);// Sum type [n,n+1,...,n*6]

    53 plSymbol points("Addition",dice_sum); // Variable space for the addition

    54 plArray dice("Die",die_type,1,

    55 number_of_dice); // Variable space for the dice

    56

    57 plValues result(dice^points); // Values storing the result of the

    58 // dice and the addition

    5960 /**********************************************************************

    61 Writing the joint distribution

    62 ***********************************************************************/

    63

    64 // P(Die0 Die1 ... Die(n-1)) = Uniform(Die0 Die1 ... Die(n-1))

    65 plUniform p_dice(dice);

    66

    67 // Create the external function

    68 plExternalFunction sum(points,dice,add_dice);

    69

    70 // P(points=p | Die0=die0 Die1=die1 ... Die(n-1)=die(n-1)) =

    71 // 1 if points = add_dice(die0,die1,...,die(n-1)) else 0

    72 plFunctionalDirac p_addition(points,dice,sum);

    73

    74 // P(Die0 Die1...Die(n-1) points)=

    75 // P(Die0 Die1 ... Die(n-1))76 // P(points | Die0 Die1...Die(n-1))

    77 plJointDistribution dice_jd(dice^points,p_dice*p_addition);

    78

    79 /**********************************************************************

    80 Making a question P(Die0 Die1 ... Die(n-1)| points = v)

    81 ***********************************************************************/

    82

    83 plCndDistribution cnd_question;

    84 plDistribution question;

    85 int v;

    86

    87 // Get P(Die0 Die1 ... Die(n-1) | points )

    88 dice_jd.ask(cnd_question,dice,points);

    89 coutv;

    91 result[points] = v;92

    93 // Get P(Die0 Die1 ... Die(n-1) | points=v)

    94 cnd_question.instantiate(question,result);

    95

    96 /**********************************************************************

    97 Tabulate the result of the question

    98 ***********************************************************************/

    99

    100 cout

  • 8/10/2019 BP With ProBT

    57/64

    57

    Exercise 8: At the beginning of Program 14 (before the main) add thefollowing lines:

    void f_N_persons(plValues &tot_attended, const plValues &attended) {

    unsigned int i, n;

    // Count the total number of persons who attended the partyn = 0 ;

    for(i=0; i

  • 8/10/2019 BP With ProBT

    58/64

  • 8/10/2019 BP With ProBT

    59/64

    59

    0

    0.005

    0.01

    0.015

    0.02

    0.025

    0.03

    0.035

    0.04

    0.045

    40 60 80 100 120 140 160 180 200

    P(Height)

    Height

    P(Height) = { Sum_{Pygmy} {P(Pygmy)P(Height | Pygmy) } }

    Figure A.4: Output of Program 16: the distribution of P(Height) for theregion of Equatorial Africa described on Exercise 9.

    51 /********************************************************** ************

    52 PROGRAM QUESTION

    53 ********************************************************** ************/

    54 // Compute and plot P(Height)

    55 plDistribution question;

    56

    57 jd.ask(question, Height);

    58

    59 question.plot("Height.gplot");60 }

    The output of Program 16 is shown in Figure A.4.

    Exercise 10 A third sensor id added easily mainly by adding the followinglines:

    plIntegerType measure3(MIN_MES_3, // Measure type for sensor 3

    MAX_MES_3);

    plSymbol M3("M3",measure3); // sensor 3 measure

    model_vars = D^M1^M2^M3; // union measures 1,2 and 3 {M1, M2, M3}

  • 8/10/2019 BP With ProBT

    60/64

    60 APPENDIX A. ANSWERS TO EXERCISES

    plCndBellShape P_dist3(M3,D,STD_DEV_3); // distribution with a

    // predefined variance

    then in the joint distribution you must add:

    plJointDistribution my_model(model_vars,P_dist*P_dist1*P_dist2*P_d ist3);

    Exercise 11

    Program 17:The dice filter program.

    1 /*======================================================= ================

    2 * Product :

    3 * File : diceFilter.cpp

    4 * Author : Juan-Manuel Ahuactzin

    5 * Creation : 2008-Oct-02 16:16

    6 *7 *=========================================================== ============

    8 * (c) Copyright 2008, Probayes SAS,

    9 * all rights reserved

    10 *=========================================================== ============

    11 *

    12 *------------------------- Description ---------------------------------

    13 * This program computes the more probable number of dice given the

    14 * sequence of number of points corresponding to throwing the dice t

    15 * times.

    16 *----------------------------------------------------------- ------------

    17 */

    18

    19 #include

    20

    21 using namespace std;

    22

    23 #define SQRT_DIV35_12 1.7078251

    24

    25

    26 // User function for computing the mean

    27 void f_mean(plValues &mean, const plValues &n) {

    28 mean[0] = 7*n[0]/2;

    29 }

    30

    31 // User function for computing the standard deviation

    32 void f_std(plValues &std, const plValues &n) {

    33 std[0] = SQRT_DIV35_12*sqrt(double(n[0]));

    34 }

    35

    36 int main() {

    37 /***************************************************** *****************

    38 VARIABLES SPECIFICATION

    39 **********************************************************************/

  • 8/10/2019 BP With ProBT

    61/64

    61

    40 unsigned int max_n;

    41 coutmax_n;

    43

    44 plIntegerType die_number(1,max_n); // Type for the maximum number

    45 // of dice [1,2,...,max_n]

    46 plIntegerType dice_sum(1,max_n*6); // Type for the sum [2,3,...,n*6]

    47 plSymbol points("Addition",dice_sum); // Variable space for the addition48 plSymbol number("n",die_number); // Variable space for the

    49 // selected number of dice

    50

    51 plValues result(number^points); // Values storing the # of dice

    52 // and the addition

    53 /********************************************************** ************

    54 PARAMETRIC FORM SPECIFICATION

    55 **********************************************************************/

    56 // P(number) = uniform(number)

    57 plUniform p_number(number);

    58

    59 // Create the external functions to compute the mean and the

    60 // standard deviation

    61 plExternalFunction f_mu(number,f_mean);

    62 plExternalFunction f_sigma(number,f_std);

    6364 // P(points | number) = CndBellShape(points,f_mu(number),f_sigma(number))

    65 plCndBellShape p_addition(points,number,f_mu,f_sigma);

    66

    67 /********************************************************** ************

    68 DECOMPOSITION

    69 ********************************************************** ************/

    70 // P(points number) = P(points | number) P(number)

    71 plJointDistribution dice_jd(number^points,p_number*p_addition);

    72 dice_jd.draw_graph("addDice3.fig");

    73

    74 /********************************************************** ************

    75 PROGRAM QUESTION

    76 ********************************************************** ************/

    77 plCndDistribution cnd_question;

    78 plDistribution question;

    79 int v;80

    81 // Get P(number | points)

    82 dice_jd.ask(cnd_question,number,points);

    83

    84 unsigned int t=0;

    85

    86 coutt;

    88

    89 do {

    90 t--;

    91 coutv;

    93 result[points] = v;

    94

    95 // Get P(number | points = v)

  • 8/10/2019 BP With ProBT

    62/64

    62 APPENDIX A. ANSWERS TO EXERCISES

    96 cnd_question.instantiate(question,result);

    97 plDistribution compiled_question;

    98

    99 question.compile(compiled_question);

    100 cout

  • 8/10/2019 BP With ProBT

    63/64

    63

  • 8/10/2019 BP With ProBT

    64/64

    64 APPENDIX A. ANSWERS TO EXERCISES

    P(RI| )0 1

    P(RP| )0 1

    P(We| )0 1

    P(LI | )0 1

    P(E| )0 1

    P(JA|RI )RI JA

    0 1

    0

    1

    P(BA|LI )LI BA

    0 1

    0

    1

    P(AA|We )We AA

    0 1

    0

    1

    P(AP| AA )AA AP

    0 1

    0

    1

    P(MA|JA AA )JA AA MA

    0 1

    0 0

    0 1

    1 0

    1 1

    P(MP| MA )MA MP

    0 1

    0

    1

    P(VP| AA BA )AA BA VP

    0 1

    0 0

    0 1

    1 0

    1 1

    P(JP| JA RP )JA RP JP

    0 1

    0 0

    0 1

    1 0

    1 1

    P(BP| BA E )BA E BP

    0 1

    0 0

    0 1

    1 0

    1 1

    Figure A.5: Parametric Al forms of Peters party Bayesian program (SeeFigure A.1).