Skip to content
Snippets Groups Projects
Example24_Bayesian_probability.mp 1.88 KiB
/* Example24.mp Bayesian attribute example

    Suppose there are 3 red and 2 green balls in a box. 
    We pick at random three balls, one at a time. 
    The result of the process can be denoted as RRR, RRG, or RGG, 
    representing sets of selected balls.

    The following MP schema demonstrates how to obtain all valid traces 
    and to calculate probabilities for the results.
    Please notice that the trace Type 1 probability is calculated as 1/7 ~ 0.142857,
    since Type 1 assumes that probability of selecting an alternative is 
    constant for the whole derivation process.

    run for scope 1
*/
SCHEMA RedGreen

ATTRIBUTES{ number selection_probability; };

ROOT Selection:
    /* there are precisely three ball selections */
    (+ <3> ( Select_Red | Select_Green ) +)

    /* possible selection configurations */
    ( RRR | RRG | RGG )
;

/* Constraints to shape the valid traces */
ENSURE  (#RRR == 1 <-> #Select_Red == 3) AND
        (#RRG == 1 <-> #Select_Red == 2) AND
        (#RGG == 1 <-> #Select_Red == 1);

/* Attribute calculations are done here */
COORDINATE  $res: ( RRR | RRG | RGG )
DO
    /* prepare for further calculations */
    selection_probability:= 1; 

    COORDINATE $s: ( Select_Red | Select_Green )
    DO 
    /* Probability of the result depends on the ball selection order */
        IF $s IS  Select_Red THEN 
            /* probability to select Red ball is:
                (number of Red balls available / total number of available balls) */
            selection_probability *:= 
				(3 - #Select_Red BEFORE $s)/(5 - #( Select_Red | Select_Green ) BEFORE $s);
        ELSE 
            /* probability to select Green ball */
            selection_probability *:= 
				(2 - #Select_Green BEFORE $s)/(5 - #( Select_Red | Select_Green ) BEFORE $s);
        FI;
    OD;

    /* Report the results */
    SAY( "Probability of result " $res " was " selection_probability);
OD;