
%% ALARM

% Your house has a security alarm system. The house is
% located in a seismically active area and the alarm system can be
% occasionally set off by an earthquake. You have two neighbors, Mary
% and John, who do not know each other. If they hear the alarm, they
% call you, but this is not guaranteed. They also call you from time
% to time just to chat.

%P(bulglary|both John and Mary call)=?


% random numbers to default start
s = RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(s);
%
B=1000000;
bs=[]; es=[]; as=[]; js=[]; ms=[]; %save history
for  i=1:B
b=rand<=0.001;   %bulglary
e=rand<=0.002;   %earthquake                                                                      
if(b)  if(e)  a=rand<=0.95; else a=rand<0.94; end;
          else  if(e) a=rand<=0.29; else a=rand<0.001; end;
end
if(a) j=rand<=0.9; else j=rand<=0.05; end; %john calls
if(a) m=rand<=0.7; else m=rand<=0.01; end; %mary calls
%set hard evidence
mh=1; 
jh=1;
%%hard evidence filter
         if(m == mh & j==jh) 
          bs=[bs b]; es=[es e]; as =[as a]; js=[js j]; ms=[ms m];
          end;
end
  allbs=sum(bs)         %608
  tmj= length(bs)          %1026
  pbs=allbs/tmj           %0.5926
 
