% simulate.m
% 6.867 Fall 2003
% Problem Set #2
% Problem 2 (active parameter estimation)

function y = simulate(x,theta0,theta1)
% SYNOPSIS performs Monte-Carlo simulation of P(y|x) where x,y binary
% variables and
%   P(0|0) = 1-theta0   P(0|1) = 1-theta1
%   P(1|0) = theta0     P(1|1) = theta1
% generates iid samples of y conditioned on x on element by element basis
% INPUT x is 0 or 1 (or a vector of 0's and 1's)
%       theta0 & theta1 are probabilities.
% OUTPUT y is 0 or 1 (or a vector of 0's and 1's) generated
% based upon x and P(y|x) model specified by (theta0,theta1).

y = zeros(size(x)); % allocate storage for y's
u = rand(size(x));  % generate iid uniform random numbers between 0 and 1
N = length(x);

I0 = find(x==0); % find all cases where x=0
y(I0) = (u(I0)<theta0);

I1 = find(x==1); % find all cases where x=1
y(I1) = (u(I1)<theta1);
