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