Friday, 17 November 2017

Simulation of statistic riddle

So just proof the solution with a simulation.

1. Create 1.000.000 families with two children {m,f} 50:50 and born on {sa,su,mo,tu,we,th,fr} 1:7
2. Count chance, that family has two boys 1/4
3. Count chance, that family has two boys, if you know that one child is a boy 1/3
4. Count chance, that family has two boys, if you know that one child is a boy and born on tuesday 13/27

If you program it, it is obvious, that each additional information (one child is a boy, one child is a boy born on tuesday) changes the sample for the probability calculation.

SAP ABAP Guides, SAP ABAP Tutorials and Material, SAP ABAP Learning

Simple ABAP program

  CONSTANTS:
    c_female    TYPE i VALUE 0,
    c_male      TYPE i VALUE 1,
    c_saturday  TYPE i VALUE 0,
    c_sunday    TYPE i VALUE 1,
    c_monday    TYPE i VALUE 2,
    c_tuesday   TYPE i VALUE 3,
    c_wednesday TYPE i VALUE 4,
    c_thursday  TYPE i VALUE 5,
    c_friday    TYPE i VALUE 6.

  TYPES:
    BEGIN OF ys_family,
      child1_gender          TYPE i,
      child1_day_of_the_week TYPE i,
      child2_gender          TYPE i,
      child2_day_of_the_week TYPE i,
    END OF ys_family.

  DATA:
    lo_rnd    TYPE REF TO cl_random_number,
    ls_family TYPE ys_family,
    lt_family TYPE TABLE OF ys_family,
    li_won    TYPE i,
    li_lost   TYPE i,
    li_total  TYPE i.

  CREATE OBJECT lo_rnd.
  CALL METHOD lo_rnd->if_random_number~init.

  DO 1000000 TIMES.

    ls_family-child1_gender = lo_rnd->if_random_number~get_random_int( 1 ).
    ls_family-child1_day_of_the_week = lo_rnd->if_random_number~get_random_int( 6 ).
    ls_family-child2_gender = lo_rnd->if_random_number~get_random_int( 1 ).
    ls_family-child2_day_of_the_week = lo_rnd->if_random_number~get_random_int( 6 ).
    APPEND ls_family TO lt_family.

  ENDDO.

  WRITE: / 'Chance to have two male children'.
  CLEAR: li_won, li_lost.
  LOOP AT lt_family INTO ls_family.
    ADD 1 TO li_total.
    IF ls_family-child1_gender EQ c_male AND
       ls_family-child2_gender EQ c_male.
      ADD 1 TO li_won.
    ELSE.
      ADD 1 TO li_lost.
    ENDIF.
  ENDLOOP.
  WRITE: / 'total', li_total, 'Won:', li_won, 'Lost:', li_lost.
  li_won = li_won * 100 / li_total.
  li_lost = li_lost * 100 / li_total.
  li_total = li_total * 100 / li_total.
  WRITE: / 'total', li_total, '%', 'Won:', li_won, '%', 'Lost:', li_lost, '%'.

  ULINE.

  WRITE: / 'Chance to have two male children, if you know that first child is male'.
  CLEAR: li_won, li_lost.
  LOOP AT lt_family INTO ls_family.
    IF ls_family-child1_gender EQ c_male OR
       ls_family-child2_gender EQ c_male.
      ADD 1 TO li_total.
      IF ls_family-child1_gender EQ c_male AND
         ls_family-child2_gender EQ c_male.

        ADD 1 TO li_won.
      ELSE.
        ADD 1 TO li_lost.
      ENDIF.
    ENDIF.
  ENDLOOP.
  WRITE: / 'total', li_total, 'Won:', li_won, 'Lost:', li_lost.
  li_won = li_won * 100 / li_total.
  li_lost = li_lost * 100 / li_total.
  li_total = li_total * 100 / li_total.
  WRITE: / 'total', li_total, '%', 'Won:', li_won, '%', 'Lost:', li_lost, '%'.

  ULINE.

  WRITE: / 'Chance to have two male children, if you know that first child is male and born on tuesday'.
  CLEAR: li_won, li_lost.
  LOOP AT lt_family INTO ls_family.
    IF ls_family-child1_gender EQ c_male OR
       ls_family-child2_gender EQ c_male.
      IF ( ls_family-child1_gender EQ c_male AND
           ls_family-child1_day_of_the_week EQ c_tuesday ) OR
         ( ls_family-child2_gender EQ c_male AND
           ls_family-child2_day_of_the_week EQ c_tuesday ).
        ADD 1 TO li_total.
        IF ls_family-child1_gender EQ c_male AND
           ls_family-child2_gender EQ c_male.
          ADD 1 TO li_won.
        ELSE.
          ADD 1 TO li_lost.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
  WRITE: / 'total', li_total, 'Won:', li_won, 'Lost:', li_lost.
  li_won = li_won * 100 / li_total.
  li_lost = li_lost * 100 / li_total.
  li_total = li_total * 100 / li_total.
  WRITE: / 'total', li_total, '%', 'Won:', li_won, '%', 'Lost:', li_lost, '%'.

Output example

Chance to have two male children
 total 1.000.000 Won: 250.442 Lost: 749.558
 total 100 % Won: 25 % Lost: 75 %

Chance to have two male children, if you know that first child is male
 total 750.485 Won: 250.442 Lost: 499.943
 total 100 % Won: 33 % Lost: 67 %

Chance to have two male children, if you know that first child is male and born on Tuesday
 total 138.231 Won: 66.430 Lost: 71.701
 total 100 % Won: 48 % Lost: 52 %

No comments:

Post a Comment