%% add BNT (may require changes) current_dir=pwd; %saves current working directory cd 'C:\MATLAB\work\bnt-master'; %this should point to the BNT directory addpath(genpathKPM(pwd)); %add BNT paths cd(current_dir); %return to working directory %% create a synthetic dataset from Fig. 1 of Kelner and Lerner (2012) clear all clc num_of_nodes=5; %total number of nodes X1=1; %node names. Note: DAG must possess topological order. X2=2; C=3; X3=4; X4=5; dag=zeros(num_of_nodes,num_of_nodes); %five nodes 'X1','X2','C','X3','X4' dag(X1,X2)=1; %an edge from X1 to X2 dag(X2,C)=1; %an edge from X2 to C dag(X3,X4)=1; %an edge from X3 to X4 dag(C,X4)=1; %an edge from C to X4 discrete_nodes = 1:num_of_nodes; %all nodes are discrete node_sizes = 2*ones(1,num_of_nodes); %all nodes have two possible values %create BNT bnet = mk_bnet(dag, node_sizes, 'names',{'X1','X2','C','X3','X4'},'discrete', discrete_nodes); bnet.CPD{X1} = tabular_CPD(bnet, X1, [0.3 0.7]); bnet.CPD{X3} = tabular_CPD(bnet, X3, [0.7 0.3]); bnet.CPD{X2} = tabular_CPD(bnet, X2, [0.7 0.3 0.3 0.7]); bnet.CPD{C} = tabular_CPD(bnet, C, [0.85 0.25 0.15 0.75]); bnet.CPD{X4} = tabular_CPD(bnet, X4, [0.1 0.8 0.3 0.2 0.9 0.2 0.7 0.8]); num_of_instances=1000; %set number of instances my_train_samples=cell(num_of_nodes,num_of_instances); my_test_samples=cell(num_of_nodes,num_of_instances); %generate train and test datasets for c1=1:num_of_instances my_train_samples(:,c1)=sample_bnet(bnet); end for c1=1:num_of_instances my_test_samples(:,c1)=sample_bnet(bnet); end %% learning an RMCV classifier %initial DAG init_dag=dag; init_dag(C,X4)=0; % %common alternatives: % %empty DAG % init_dag=zeros(num_of_nodes,num_of_nodes); % %NBC % init_dag=zeros(num_of_nodes,num_of_nodes); % init_dag(C,[X1 X2 X3 X4])=1; %learn BN using RMCV [final_dag,history_dags,history_scores]=rmcv_gs(init_dag,my_train_samples,my_test_samples,node_sizes,C,4);