% data = genSample_net1(N) generates N samples form Network 1 in 
% Figure 4
% data is NxM matrix where each row corresponds to sample from the network
% the columns are ordered as: A,B,C,D,E

function data = genSample_net1(N)

%number of variables
M = 5; 
A=1; B=2; C=3; D=4; E=5;

%simple encoding of the bayesian network for problem 1
condProb = cell(M,1);  
parents  = cell(M,1);

condProb{A}=.2;
condProb{B}= .2;
condProb{C} =[.1, .6, .6, .8];
condProb{D}=  [.3, .7 ];
condProb{E}=  [.3, .7];

parents{A}=[];
parents{B}= [];
parents{C}= [A B];
parents{D}= C;
parents{E}= C;

%the seed number should be function of alpha  and  N
rand('state',10000*N);

data= size(N,M);
for i=1:N
    for j=1:M
        if(~isempty(parents{j}))
            parentsValue = data(i,parents{j});
        else
            parentsValue=[];
        end
        prob = condProb{j}(toInt(parentsValue)+1);
        if(rand<prob)
            data(i,j)= 1;
        else
            data(i,j)=0;
        end
    end
end


%%%% helper function
function val = toInt(vec)

val=0;
fac=1;
for  i=1:length(vec)
    val = val + fac * vec(i);
    fac = fac * 2;
end
