%% 3.24 Mysterious Transfer 
% Solution by simulation. Two bags 1st: 4 white, 3 black balls;
% 2nd: 3 white, 5 black. One ball randomly transfered 1st->2nd, then
% one ball selected from 2nd bag. (a) probability 2nd ball is black,
%(b) if the 2nd black, probability black was transfered.
%Solution: (a) 38/63= 0.603174603174603; (b) 9/19= 0.473684210526316.
clear all
close all


  disp('Mysterious Transfer')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  msize = 10;
%

sh0=0; sh1=0; sa=0; sah1=0; sah0=0;
M=10000000;
for i = 1:M
    h1 = rand < 3/(4+3);   h0=1-h1;
        %h1=1 if black; h1=0 if white;
        %h0=0 if black; h0=1 if white
    a = rand < (5 + h1)/(3+5+1); 
        %after transfer 5+h1 black balls 
        %a = 1 if black selected; a=0 if white
    %========SUMS============
    sh1=sh1+h1;  sh0=sh0+h0; %# of 1st black/white                                        
    sa=sa+a;     %# of 2nd black
    sah1 = sah1 + a*h1; %2nd black 1st black
    sah0 = sah0 + a*h0; %2nd black 1st white
end
%-----------
pa=sa/M                      %0.602951200000000
ph1=sh1/M                 %0.428622100000000
ph0=sh0/M                 %0.571377900000000
pah1 = sah1/sh1         %0.666662778237520
pah0 = sah0/sh0         %0.555157628602716
ph1a = pah1*ph1/pa  %0.473912980022264
ph0a = pah0*ph0/pa  %0.526087019977736
%

