diff options
| author | ziejd2 | 2018-04-25 16:43:19 -0500 |
|---|---|---|
| committer | ziejd2 | 2018-04-25 16:43:19 -0500 |
| commit | 74b673ba4a706085201a5610b938ff98f08f641d (patch) | |
| tree | cb39006ea1a39499e00dbbb0e0097087a4567031 /sourcecodes/parameter_learning | |
| parent | a781cb1ff2e7ae6de0f686bd02cd279261485b1e (diff) | |
| download | BNW-74b673ba4a706085201a5610b938ff98f08f641d.tar.gz | |
Bug fixes, code comments, and minor changes
Diffstat (limited to 'sourcecodes/parameter_learning')
35 files changed, 2897 insertions, 199 deletions
diff --git a/sourcecodes/parameter_learning/Predictmultiple.m b/sourcecodes/parameter_learning/Predictmultiple.m index f03568ef..a7236e35 100644 --- a/sourcecodes/parameter_learning/Predictmultiple.m +++ b/sourcecodes/parameter_learning/Predictmultiple.m @@ -1,4 +1,18 @@ function Predictmultiple(pre) +% Predictmultiple is used when predicting the impact of entering +% evidence on the network. The 'multiple' part refers to +% it working when evidence for multiple nodes is entered. +% +% The input is 'pre'-- the prefix for the network and data +% in BNW. It reads information from several files from BNW. +% +% The output is ???net_figure_new.txt. It also calls +% writeParameters_ev to write the parameter file. +% +% It is called by the run_octave_evd file in the 'sourcecodes' directory. + + + dfile=strcat(pre,'structure_input.txt'); sfile=dfile; dfile=strcat(pre,'continuous_input.txt'); @@ -28,14 +42,14 @@ mapfile = strcat(pre,'map.txt'); fmap = fopen(mapfile,'r'); for i=1:nnodes buffer = fgetl(mapfile); - temp = cell(1,4); - for j=1:4 + temp = cell(1,3); + for j=1:3 [next,buffer] = strtok(buffer); temp{j} = next; end labels_orig{i} = temp{1}; - means_orig{i} = str2num(temp{4}); - stdevs_orig{i} = str2num(temp{3}); + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); end fclose(fmap); diff --git a/sourcecodes/parameter_learning/Predictmultipleintervention.m b/sourcecodes/parameter_learning/Predictmultipleintervention.m new file mode 100644 index 00000000..7e6140a9 --- /dev/null +++ b/sourcecodes/parameter_learning/Predictmultipleintervention.m @@ -0,0 +1,107 @@ +function Predictmultipleintervention(pre) +% Predictmultipleintervention is used when predicting the impact of +% intervention on the network. The 'multiple' part refers to +% it working when intervention for multiple nodes is entered. +% +% The input is 'pre'-- the prefix for the network and data +% in BNW. It reads information from several files from BNW. +% +% The output is ???net_figure_new.txt. It also calls +% writeParameters_int to write the parameter file. +% +% It is called by the run_octave_inv file in the 'sourcecodes' directory. + +dfile=strcat(pre,'structure_input.txt'); +sfile=dfile; +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); + +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +fvarnamefile=strcat(pre,'varname.txt'); + +varfile = fopen(fvarnamefile,'r'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +[bnet]=parameterLearning(bnet,cases); + +fvarfile=strcat(pre,'var.txt'); +fvar = fopen(fvarfile,'r'); +select_var_new = fscanf(fvar,'%d'); + +nm = numel(select_var_new); + +varlabels = cell(1,nm); +varbuffer = fgetl(varfile); %get header line as a string +for j=1:nm + [varnext,varbuffer] = strtok(varbuffer); + varlabels{j} = varnext; + for i=1:nnodes + if strcmp(varlabels{j},labels{i}) + select_var_new(j)=i; + end + end + +end + + + + +fvardfile=strcat(pre,'vardata.txt'); + +fvard = fopen(fvardfile,'r'); + +select_var_data_new = fscanf(fvard,'%f'); + +means_orig = cell(1,nnodes); +stdevs_orig = cell(1,nnodes); +labels_orig = cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); + +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + +filename=strcat(pre,'net_figure_new.txt'); + +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); + +writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); + +end diff --git a/sourcecodes/parameter_learning/checkDiscreteNodes.m b/sourcecodes/parameter_learning/checkDiscreteNodes.m index c9d0692c..8cbce9d0 100644 --- a/sourcecodes/parameter_learning/checkDiscreteNodes.m +++ b/sourcecodes/parameter_learning/checkDiscreteNodes.m @@ -7,7 +7,9 @@ function [ ] = checkDiscreteNodes( bnet, cases) % bnet: BNT bnet % cases: cell array of data % +% checkDiscreteNodes is called by readInput.m % + node_sizes = bnet.node_sizes; dnodes = bnet.dnodes; ndisc = size(dnodes,2); diff --git a/sourcecodes/parameter_learning/checkStructure.m b/sourcecodes/parameter_learning/checkStructure.m index 5931c187..be3befda 100644 --- a/sourcecodes/parameter_learning/checkStructure.m +++ b/sourcecodes/parameter_learning/checkStructure.m @@ -1,7 +1,7 @@ function [ labels, cases, dag, node_sizes, ord_flag ] = checkStructure(labels, cases, dag, node_sizes) - %checkStructure Check to see if nodes are sorted correctly. They must be + %checkStructure Check to see if nodes are sorted correctly. Nodes must be % in topological order (i.e., parents before children) before parameter - % learning can take place. + % learning can take place. This function performs this sorting. % %Input and output have the same meaning. The output has just been %topologically ordered. @@ -9,6 +9,10 @@ function [ labels, cases, dag, node_sizes, ord_flag ] = checkStructure(labels, c % cases = cell array with the data. % dag = matrix with the strucutre of the network. % node_sizes = vector with the size of each node. +% +% checkStructure is called by readInput.m + + %make connections array %count how big you need the connections array to be diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m new file mode 100644 index 00000000..9d107628 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m @@ -0,0 +1,72 @@ +function Predictmultiple(pre) +dfile=strcat(pre,'structure_input.txt'); +sfile=dfile; +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); + +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +[bnet]=parameterLearning(bnet,cases); + +fvarfile=strcat(pre,'var.txt'); +fvar = fopen(fvarfile,'r'); +select_var_new = fscanf(fvar,'%d'); + +fvardfile=strcat(pre,'vardata.txt'); +fvard = fopen(fvardfile,'r'); +select_var_data_new = fscanf(fvard,'%f'); + +means_orig = cell(1,nnodes); +stdevs_orig = cell(1,nnodes); +labels_orig = cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); + +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + + +filename=strcat(pre,'net_figure_new.txt'); + +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); + +writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); + +end diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m b/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m new file mode 100644 index 00000000..e9f741f2 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m @@ -0,0 +1,95 @@ +function Predictmultipleintrvention(pre) +dfile=strcat(pre,'structure_input.txt'); +sfile=dfile; +dfile=strcat(pre,'continuous_input.txt'); +nnodefile=strcat(pre,'nnode.txt'); + +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + +fvarnamefile=strcat(pre,'varname.txt'); + +varfile = fopen(fvarnamefile,'r'); + +Std_flag=true; +[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); + +[bnet]=parameterLearning(bnet,cases); + +fvarfile=strcat(pre,'var.txt'); +fvar = fopen(fvarfile,'r'); +select_var_new = fscanf(fvar,'%d'); + +nm = numel(select_var_new); + +varlabels = cell(1,nm); +varbuffer = fgetl(varfile); %get header line as a string +for j=1:nm + [varnext,varbuffer] = strtok(varbuffer); + varlabels{j} = varnext; + for i=1:nnodes + if strcmp(varlabels{j},labels{i}) + select_var_new(j)=i; + end + end + +end + + + + +fvardfile=strcat(pre,'vardata.txt'); + +fvard = fopen(fvardfile,'r'); + +select_var_data_new = fscanf(fvard,'%f'); + +means_orig = cell(1,nnodes); +stdevs_orig = cell(1,nnodes); +labels_orig = cell(1,nnodes); +%Read in original means and standard deviations +mapfile = strcat(pre,'map.txt'); +fmap = fopen(mapfile,'r'); +for i=1:nnodes + buffer = fgetl(mapfile); + temp = cell(1,3); + for j=1:3 + [next,buffer] = strtok(buffer); + temp{j} = next; + end + labels_orig{i} = temp{1}; + means_orig{i} = str2num(temp{3}); + stdevs_orig{i} = str2num(temp{2}); +end +fclose(fmap); + +%Need to map the means and stdevs to the correct labels +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +%Read in labels in new order. +labelsnew = cell(1,nnodes); +mapdatafile = strcat(pre,'mapdata.txt'); +fmapdata = fopen(mapdatafile,'r'); +buffer = fgetl(fmapdata); +for i = 1:nnodes + [next,buffer ] = strtok(buffer); + labelsnew{i} = next; +end +fclose(fmapdata); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsnew{i},labels_orig{j}) + means{i} = means_orig{j}; + stdevs{i} = stdevs_orig{j}; + break + end + end +end + +filename=strcat(pre,'net_figure_new.txt'); + +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); + +writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); + +end diff --git a/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m b/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m new file mode 100644 index 00000000..c9d0692c --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m @@ -0,0 +1,37 @@ +function [ ] = checkDiscreteNodes( bnet, cases) + %checkDiscreteNodes Checks if states of discrete nodes are be integers from 1 to M + % where M is the number of states of the node. (M should be the same as + % node_sizes in the bnet). + % + %Input: + % bnet: BNT bnet + % cases: cell array of data + % +% +node_sizes = bnet.node_sizes; +dnodes = bnet.dnodes; +ndisc = size(dnodes,2); +ncases = size(cases,2); + +%check to see that all data for discrete nodes are integers +for i = 1:ndisc + inode = dnodes(i); + data = cases(inode,:); + isize = node_sizes(inode); + states = zeros(1,isize); + for j = 1:isize + states(j) = j; + end + for j = 1:ncases + k = int64(data{j}); + if ~any(k==states) + error(['Discrete nodes must be integers from 1 to the number of states']); + end + end +end + + +end + + + diff --git a/sourcecodes/parameter_learning/code_backup/checkStructure.m b/sourcecodes/parameter_learning/code_backup/checkStructure.m new file mode 100644 index 00000000..b4de9403 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/checkStructure.m @@ -0,0 +1,78 @@ +function [ labels, cases, dag, node_sizes, ord_flag ] = checkStructure(labels, cases, dag, node_sizes) + %checkStructure Check to see if nodes are sorted correctly. Nodes must be + % in topological order (i.e., parents before children) before parameter + % learning can take place. This function performs this sorting. + % + %Input and output have the same meaning. The output has just been + %topologically ordered. + % labels = cell array with the names of the nodes. + % cases = cell array with the data. + % dag = matrix with the strucutre of the network. + % node_sizes = vector with the size of each node. + +%make connections array +%count how big you need the connections array to be +nnodes = size(dag,1); +narcs = 0; +for i = 1:nnodes + for j = 1:nnodes + if dag(i,j) == 1 + narcs = narcs + 1; + end + end +end +%fill connections array with label names +connections = cell(narcs,2); +ncount = 0; +for i = 1:nnodes + for j = 1:nnodes + if dag(i,j) == 1 + ncount = ncount + 1; + connections{ncount,1} = labels{i}; + connections{ncount,2} = labels{j}; + end + end +end + +%get topologically sorted dag and labels +[new_dag, new_labels] = mk_adj_mat(connections, labels, 1); + +%check to see if order changed +ord_flag = 0; +for i = 1:nnodes + if ~strcmp(new_labels{i},labels{i}) + ord_flag = 1; + end +end + +if ord_flag + %get new ordering of nodes + order = cell(1,nnodes); + for i = 1:nnodes + for j = 1:nnodes + if strcmp(new_labels{j},labels{i}) + order{i} = j; + end + end + end + + %reorder cases and node_sizes + new_cases = cell(size(cases)); + for i = 1:nnodes + new_cases(order{i},:) = cases(i,:); + end + new_node_sizes = zeros(1,nnodes); + for i = 1:nnodes + new_node_sizes(order{i}) = node_sizes(i); + end + + + dag = new_dag; + cases = new_cases; + node_sizes = new_node_sizes; + labels = new_labels; +end + +end +%end checkStructure.m + diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m b/sourcecodes/parameter_learning/code_backup/drawFigure.m new file mode 100644 index 00000000..f84bffa3 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigure.m @@ -0,0 +1,390 @@ +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigure writes the parameters and data that are needed to draw the +%structure of a Bayesian network for BNW. +% This is the first function that + + + +if nargin < 8, + drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means); +else + drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata); +end; + +end + + + +function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line + +%Create an empty evidence cell array. + +%val=cases; +%for i = 1:nnodes +% val(i,1)=val(i,2); + +%end + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +evidence{selectvar}=selectdata; + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); + +%%%%Evidence node +fprintf(fileID,'%i\n',selectvar); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + xvals(j,1) = xvals(j,1)*stdevs{i}+means{i} + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1); + end + +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + + + + + +function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + +%Create an empty evidence cell array. +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); + +%Get canvas size + +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +%[x,y] = layout_dag(bnet.dag); + + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + %cases(i) + % MAX(cases(i)) + % MIN(cases(i)) + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigure.m~ b/sourcecodes/parameter_learning/code_backup/drawFigure.m~ new file mode 100644 index 00000000..404a65f7 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigure.m~ @@ -0,0 +1,388 @@ +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigure writes the parameters and data that are needed to draw the +%structure of a Bayesian network. + + +if nargin < 8, + drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means); +else + drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata); +end; + +end + + + +function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line + +%Create an empty evidence cell array. + +%val=cases; +%for i = 1:nnodes +% val(i,1)=val(i,2); + +%end + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +evidence{selectvar}=selectdata; + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); + +%%%%Evidence node +fprintf(fileID,'%i\n',selectvar); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + xvals(j,1) = xvals(j,1)*stdevs{i}+means{i} + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1); + end + +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + + + + + +function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means) +%Function to use if there is no entered evidence. +% +% +%Before each printed line, I will have a line that starts with %%% +% that describes what will be on that line +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + +%Create an empty evidence cell array. +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +fileID = fopen(filename,'w'); +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); + +%Get canvas size + +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +%[x,y] = layout_dag(bnet.dag); + + +x = x - min(x); +y = 1 - y; +y = y - min(y); + +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) + +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + %cases(i) + % MAX(cases(i)) + % MIN(cases(i)) + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; +end +%fprintf(fileID,'%s\t %\n',labels_temp{:}); + + +fclose(fileID); + +end + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/drawFigureM.m b/sourcecodes/parameter_learning/code_backup/drawFigureM.m new file mode 100644 index 00000000..91b8698f --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/drawFigureM.m @@ -0,0 +1,230 @@ +function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +%drawFigureM writes the parameters and data that are needed to draw the +%structure of a Bayesian network after added evidence or intervention + +fileID = fopen(filename,'w'); + + +A=cell2mat(cases'); +Amax=max(A); +Amin=min(A); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardized evidence for continuous nodes. + if bnet.node_sizes(di) == 1 + ev_dat(di) = (ev_dat(di) - means{di}) / stdevs{di}; + end + evidence{di}=ev_dat(di); + fprintf(fileID,'%i\t',di); +end + +fprintf(fileID,'\n'); + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open the file, and write the nodes to a file. +%%% The number of nodes +fprintf(fileID,'%i\n',nnodes); +%Get canvas size +labels_temp = cellstr(labels); +[x,y] = make_layout(bnet.dag); +x = x - min(x); +y = 1 - y; +y = y - min(y); +[x_dim,y_dim] = canvasSize(nnodes,x,y); + +%%% The dimensions of the canvas for the javascript code +fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim); +x = x*x_dim; +y = y*y_dim; +for i = 1:nnodes, +%%% The name and X- and Y-positions of each node + fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); +end + +%Get the number of parents and children for each node. +num_par = zeros(1,nnodes); +%For parents, sum down columns +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + num_par(i) = num_par(i) + 1; + end + end +end +num_child = zeros(1,nnodes); +for i = 1:nnodes, + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + num_child(i) = num_child(i) + 1; + end + end +end + +for i = 1:nnodes, + %%% The name and type of each node (1=continuous, the number of states + %%% if it is discrete + fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); + %%% The size of the node, I am going to keep them + %%% 250(width) by 150(height) for now + %Could modify this to change the width based on the length of the node + %name + fprintf(fileID,'%i\t%i\n',250,150); + %%% The number of parents of the node, and the parents + if num_par(i) == 0; + %%% If no parents: + fprintf(fileID,'%i\n',num_par(i)); + else + parents = zeros(1,num_par(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(j,i) == 1, + parents(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_par(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_par(i),parents(1,:)); + end + + + %%% The number of children of the node, and the children + if num_child(i) == 0; + %%% If no children: + fprintf(fileID,'%i\n',num_child(i)); + else + children = zeros(1,num_child(i)); + k = 1; + for j = 1:nnodes, + if bnet.dag(i,j) == 1, + children(1,k) = j; + k = k + 1; + end + end + format = '%i\t'; + for j = 1:num_child(i)-1, + format = strcat(format,'%i\t'); + end + format = strcat(format,'%i\n'); + %%%If there are parents: + fprintf(fileID,format,num_child(i),children(1,:)); + end + + predict = marginal_nodes(engine,i); + if isempty(evidence{i}) + if bnet.node_sizes(i) ~= 1, + for j = 1:bnet.node_sizes(i), + %%%For discrete nodes, the state and the percent of that state + fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); + end; + else + [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %%%For continuous nodes, print x and the pdf of a normal curve. + for j = 1:101, + %%Undo standardization + x_vals(j,1) = x_vals(j,1)*stdevs{i}+means{i}; + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + end; + end; + else + if bnet.node_sizes(i) == 1, + fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i)*stdevs{i}+means{i},1); + else + fprintf(fileID,'%6.4f\t%6.4f\n',ev_dat(i),1); + endif + end + +end + +fclose(fileID); +end + + + + + + + + + +function [x_dim, y_dim] = canvasSize(nnodes,x,y) +%canvasSize Function to calculate the size of the canvas to +% build the network structure + + +%I am going to assume that the node size will be +% height = 150, width = 250 +% so there will be a node spacing of +% 200 (in y-dim) and 300 (in x-dim). +y_space = 200; +x_space = 300; + +%Set default minimum x and y dimensions +x_dim = 1200; +y_dim = 1200; + +%get unique y values +y_unique = unique(y); +size_y = size(y_unique,2); +y_dim_temp = size_y*y_space; + +%get the maximum nodes in any layer +size_x = zeros(1,size_y); +for i = 1:size_y, + for j = 1:nnodes, + if y_unique(i) == y(j), + size_x(1,i) = size_x(1,i) + 1; + end; + end; +end; +size_x = max(size_x); +x_dim_temp = size_x*x_space; + +if x_dim_temp > x_dim, + x_dim = x_dim_temp; +end; + +if y_dim_temp > y_dim, + y_dim = y_dim_temp; +end; +end + +function [x_vals,y_vals] = calcGaussian(mu,Sigma,maxval,minval) +%Function to calculate 101 points of Gaussian function to use in plotting +% Gets the probability density of the mean value and 50 evenly spaced +% points up to 3Sigma below the mean and 50 evenly space points up to +% 3Sigma above the mean. +%maxval +%minval +x_vals = zeros(101,1); +y_vals = zeros(101,1); + +%x_vals(1,1) = mu - 3*Sigma; +x_vals(1,1) = minval - 1; +gap=((maxval+1)-(minval - 1))/100; +%x_vals(1,1) = 0;%mu - 3*Sigma; +for i = 1:100, + % x_vals(i+1,1) = x_vals(1,1) + i*6*Sigma/100; + x_vals(i+1,1) = x_vals(i,1) + gap; + %x_vals(i+1,1) = x_vals(i,1) + 1/100; +end + +for i = 1:101, + y_vals(i,1) = normpdf(x_vals(i,1),mu,Sigma); +end + +end diff --git a/sourcecodes/parameter_learning/code_backup/getParams.m b/sourcecodes/parameter_learning/code_backup/getParams.m new file mode 100644 index 00000000..31f84ffb --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/getParams.m @@ -0,0 +1,22 @@ +function [ bnet ] = getParams( bnet, cases ) +%getParams Code to initialize CPT and do parameter learning. +%This will be very basic for now. I can add more options later. + +dnodes = bnet.dnodes; +cnodes = bnet.cnodes; +nnodes = size(dnodes,2)+size(cnodes,2); + +%make dnodes tabular_CPT +for i = 1:size(dnodes,2) + bnet.CPD{dnodes(i)} = tabular_CPD(bnet,dnodes(i)); +end + +for i = 1:size(cnodes,2) + bnet.CPD{cnodes(i)} = gaussian_CPD(bnet,cnodes(i)); +end + +bnet = learn_params(bnet,cases); + + +end + diff --git a/sourcecodes/parameter_learning/code_backup/parameterLearning.m b/sourcecodes/parameter_learning/code_backup/parameterLearning.m new file mode 100644 index 00000000..872e94b1 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/parameterLearning.m @@ -0,0 +1,17 @@ +function [ bnet ] = parameterLearning( bnet,cases,engine_name ) +%parameterLearning Do parameter learning and inference + +%engine is an optional argument +if nargin < 3 + engine_name = 'jtree_inf_engine'; +end + + +%First do parameter learning with all the data +[bnet] = getParams(bnet,cases); + + + + +end + diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m b/sourcecodes/parameter_learning/code_backup/prepareInput.m new file mode 100644 index 00000000..838dcd2c --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/prepareInput.m @@ -0,0 +1,294 @@ +function [ ] = prepareInput( pre ) + % + % This function takes files that are uploaded to BNW and creates output + % files that can be used for structure and parameter learning. + % It replaces php code that was previously in bn_file_load_gom.php. + % There are several improvements in performance and ease of use: + % 1) Loading files is significantly (~5x) faster for large input files. + % 2) The allowed values for discrete variables are more flexible. + % (e.g., A genotype variable be 'B' and 'D' instead of having + % to replace to make them '1' and '2'.) + % 3) Continuous variables may be identified as continuous in some cases + % even if there is not a period. + % 4) The states of discrete variables should be correctly ordered in + % almost all cases. + % 5) An additional output file is written that will let users check if + % the input file has been uploaded and parsed correctly. + % 6) Future updates to this code should be easier than updating the php. + % + % + % Input: ???continuous_input_orig.txt + % This is the input file that is uploaded to BNW. + % It is directly written out by the BNW php code with no modification. + % The file format is a header line containing the variable names + % followed by the data, with each case in a row. + % + % Output: There are many output files. + % 1) The main output file is ???continuous_input.txt that can be + % used by the structure learning code and parameter learning codes. + % The first line is variable names, the second line is the node type + % (continuous nodes should have 1, discrete nodes have the number + % of states), and the rest is the data. + % 2) A new output file is ???input_desc.txt, a file that describes the + % data so users can check that it has been parsed correctly. + % 3) ???nlevels.txt: The states of discrete variables. + % 4) ???name.txt: The names of the variables as uploaded. + % 5) ???type.txt: The number of states for each variables + % (1 indicates a continuous variable.) + % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases + % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and + % ???parent.txt: Files with default values for structure learning. + % + +% open file for input, include error handling +dfile=strcat(pre,'continuous_input_orig.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Get the number of cases (the number of rows in the file excluding the header) +ncases = fskipl(fin,Inf) - 1; + +frewind(fin); + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data +data = cell(ncases,nnodes); +for i = 1:ncases + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{i,j} = next; + end +end + +% Determine whether or not the nodes are continuous or discrete. +% First, treat them as all discrete and get the states and number of stats(levels). +levels = cell(1,nnodes); +states = []; +for j = 1:nnodes + states{end+1} = unique(data(:,j)); + levels{j} = size(states{j},1); +end + +reason = cell(1,nnodes); +%Now do some checks to see if nodes are discrete or continuous +for j = 1:nnodes + % If there are 3 or less unique values, I will assume that the node is discrete. + if levels{j} < 4; + reason{j} = "It was determined to be discrete because there are a small number (<4) of possible values."; + continue + % If there are as many unique values as a third of the number of cases, + % I will assume that the node is continuous. + elseif levels{j} > ncases/3; + levels{j} = 1; + reason{j} = "It was determined to be continuous because there are a large number of possible values compared to the number of cases."; + continue + % If there are more than twenty unique values, + % I will assume that the node is continuous. + elseif levels{j} > 20; + levels{j} = 1; + reason{j} = "It was determined to be continuous because there are many (>20) possible values."; + continue + % Otherwise, I will scan through the individual values. + % If any of the values contain a '.', I will assume it is continuous. + else + reason{j} = "It was determined to be discrete by default."; + period_test = 0; + column = data(:,j); + k = 1; + while period_test == 0 + period_test = sum(cell2mat(strfind(column(k),"."))); + if period_test != 0; + reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period(.)."; + levels{j} = 1; + end + k++; + if k > ncases + break + end + end + end +end + +%I need to check if any discrete nodes are listed after continuous nodes. +%If so, I need to rearrange the columns. +max_disc = 0; +min_cont = nnodes + 1; +for i = 1:nnodes + if levels{i} > 1 + max_disc = i; + elseif min_cont == nnodes+1 + min_cont = i; + end +end +%If max_disc > min_cont, you need to rearrange the nodes +% to put the discrete nodes first. +if max_disc > min_cont + levels_old = levels; + labels_old = labels; + data_old = data; + states_old = states; + reason_old = reason; + new_order = {}; + for i=1:nnodes + if levels_old{i} > 1 + new_order{end+1} = i; + end + end + for i=1:nnodes + if levels_old{i} == 1 + new_order{end+1} = i; + end + end + labels = {}; + levels = {}; + states = {}; + reason = {}; + for i =1:nnodes + labels{i} = labels_old{new_order{i}}; + levels{i} = levels_old{new_order{i}}; + states{i} = states_old{new_order{i}}; + reason{i} = reason_old{new_order{i}}; + for j=1:ncases + data{j,i} = data_old{j,new_order{i}}; + end + end + +endif + + +%Write other files that are used by BNW for this key. +%The first group of files establish default settings for structure learning. +outfile = strcat(pre,'white.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'ban.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'k.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'1\n'); +fclose(fout); + +outfile = strcat(pre,'parent.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'4\n'); +fclose(fout); + +outfile = strcat(pre,'thr.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'0.5\n'); +fclose(fout); + + +%The next group of files have information about the uploaded file. +outfile = strcat(pre,'name.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fclose(fout); + +outfile = strcat(pre,'nnode.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',nnodes); +fclose(fout); + +outfile = strcat(pre,'nrows.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',ncases); +fclose(fout); + +outfile = strcat(pre,'type.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +fclose(fout); + +%This output file contains the states for discrete nodes. +% The unique matlab function already sorts the states. +outfile = strcat(pre,'nlevels.txt'); +fout = fopen(outfile,'w'); +for i = 1:nnodes + if levels{i} > 1 + fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); + fprintf(fout,'%s\n',states{i}{end}); + end +end +fclose(fout); + + +%Print a file with a short description of the input. +descfile = strcat(pre,'input_desc.txt'); +dout = fopen(descfile,'w'); +fprintf(dout,['As loaded, the input file had the following properties:\n\n']); +dout = fopen(descfile,'a'); +fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); +fprintf(dout,'The variable names are:\n'); +fprintf(dout,'%s\t',labels{1:end-1}); +fprintf(dout,'%s\n\n',labels{end}); +for i=1:nnodes + if levels{i} == 1 + fprintf(dout,'%s is a continuous variable.\n',labels{i}); + fprintf(dout,'%s\n',reason{i}); + column = str2double(data(:,i)); + colmean = mean(column); + colstd = std(column); + fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) + else + fprintf(dout,'%s is a discrete variable with %i states.\n',labels{i},levels{i}); + fprintf(dout,'%s\n',reason{i}); + fprintf(dout,'The states are: '); + fprintf(dout,'%s ',states{i}{1:end-1}); + fprintf(dout,'%s\n\n',states{i}{end}); + end +end +fclose(fout); + +outfile = strcat(pre,'continuous_input.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +%Need to replace states in discrete variables with integers for BNT +for i = 1:nnodes + if levels{i} > 1 + for j = 1:ncases + for k=1:size(states{i},1) + if data{j,i} == states{i}{k} + data{j,i} = sprintf('%i',num2cell(k){1});; + break + end + end + end + end +end +for i = 1:ncases + fprintf(fout,'%s\t',data{i,1:end-1}); + fprintf(fout,'%s\n',data{i,end}); +end +fclose(fout); + + + + + +end +% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/prepareInput.m~ b/sourcecodes/parameter_learning/code_backup/prepareInput.m~ new file mode 100644 index 00000000..9fc0f97f --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/prepareInput.m~ @@ -0,0 +1,294 @@ +function [ ] = prepareInput( pre ) + % + % This function takes files that are uploaded to BNW and creates output + % files that can be used for structure and parameter learning. + % It replaces php code that was previously in bn_file_load_gom.php. + % There are several improvements in performance and ease of use: + % 1) Loading files is significantly (~5x) faster for large input files. + % 2) The allowed values for discrete variables are more flexible. + % (e.g., A genotype variable be 'B' and 'D' instead of having + % to replace to make them '1' and '2'.) + % 3) Continuous variables may be identified as continuous in some cases + % even if there is not a period. + % 4) The states of discrete variables should be correctly ordered in + % almost all cases. + % 5) An additional output file is written that will let users check if + % the input file has been uploaded and parsed correctly. + % 6) Future updates to this code should be easier than updating the php. + % + % + % Input: ???continuous_input_orig.txt + % This is the input file that is uploaded to BNW. + % It is directly written out by the BNW php code with no modification. + % The file format is a header line containing the variable names + % followed by the data, with each case in a row. + % + % Output: There are many output files. + % 1) The main output file is ???continuous_input.txt that can be + % used by the structure learning code and parameter learning codes. + % The first line is variable names, the second line is the node type + % (continuous nodes should have 1, discrete nodes have the number + % of states), and the rest is the data. + % 2) A new output file is ???input_desc.txt, a file that describes the + % data so users can check that it has been parsed correctly. + % 3) ???nlevels.txt: The states of discrete variables. + % 4) ???name.txt: The names of the variables as uploaded. + % 5) ???type.txt: The number of states for each variables + % (1 indicates a continuous variable.) + % 6/7) ???nnode.txt and ???nrows.txt: number of nodes and cases + % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and + % ???parent.txt: Files with default values for structure learning. + % + +% open file for input, include error handling +dfile=strcat(pre,'continuous_input_orig.txt'); + +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Get the number of cases (the number of rows in the file excluding the header) +ncases = fskipl(fin,Inf) - 1; + +frewind(fin); + +% Read in first line to get the number of nodes and the node labels. +buffer = fgetl(fin); %get header line as a string +nnodes = numel(strfind(buffer,"\t")) + 1; +labels = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data +data = cell(ncases,nnodes); +for i = 1:ncases + buffer = fgetl(fin); + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{i,j} = next; + end +end + +% Determine whether or not the nodes are continuous or discrete. +% First, treat them as all discrete and get the states and number of stats(levels). +levels = cell(1,nnodes); +states = []; +for j = 1:nnodes + states{end+1} = unique(data(:,j)); + levels{j} = size(states{j},1); +end + +reason = cell(1,nnodes); +%Now do some checks to see if nodes are discrete or continuous +for j = 1:nnodes + % If there are 3 or less unique values, I will assume that the node is discrete. + if levels{j} < 4; + reason{j} = "This was determined to be discrete because there are few (<4) different values."; + continue + % If there are as many unique values as a third of the number of cases, + % I will assume that the node is continuous. + elseif levels{j} > ncases/3; + levels{j} = 1; + reason{j} = "This was determined to be continuous because there are a large number of different values compared to the number of cases."; + continue + % If there are more than twenty unique values, + % I will assume that the node is continuous. + elseif levels{j} > 20; + levels{j} = 1; + reason{j} = "This was determined to be continuous because there are many (>20) possible values."; + continue + % Otherwise, I will scan through the individual values. + % If any of the values contain a '.', I will assume it is continuous. + else + reason{j} = "This variable was determined to be discrete."; + period_test = 0; + column = data(:,j); + k = 1; + while period_test == 0 + period_test = sum(cell2mat(strfind(column(k),"."))); + if period_test != 0; + reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period (".")."; + levels{j} = 1; + end + k++; + if k > ncases + break + end + end + end +end + +%I need to check if any discrete nodes are listed after continuous nodes. +%If so, I need to rearrange the columns. +max_disc = 0; +min_cont = nnodes + 1; +for i = 1:nnodes + if levels{i} > 1 + max_disc = i; + elseif min_cont == nnodes+1 + min_cont = i; + end +end +%If max_disc > min_cont, you need to rearrange the nodes +% to put the discrete nodes first. +if max_disc > min_cont + levels_old = levels; + labels_old = labels; + data_old = data; + states_old = states; + reason_old = reason; + new_order = {}; + for i=1:nnodes + if levels_old{i} > 1 + new_order{end+1} = i; + end + end + for i=1:nnodes + if levels_old{i} == 1 + new_order{end+1} = i; + end + end + labels = {}; + levels = {}; + states = {}; + reason = {}; + for i =1:nnodes + labels{i} = labels_old{new_order{i}}; + levels{i} = levels_old{new_order{i}}; + states{i} = states_old{new_order{i}}; + reason{i} = reason_old{new_order{i}}; + for j=1:ncases + data{j,i} = data_old{j,new_order{i}}; + end + end + +endif + + +%Write other files that are used by BNW for this key. +%The first group of files establish default settings for structure learning. +outfile = strcat(pre,'white.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'ban.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'From\tTo\n'); +fclose(fout); + +outfile = strcat(pre,'k.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'1\n'); +fclose(fout); + +outfile = strcat(pre,'parent.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'4\n'); +fclose(fout); + +outfile = strcat(pre,'thr.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'0.5\n'); +fclose(fout); + + +%The next group of files have information about the uploaded file. +outfile = strcat(pre,'name.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fclose(fout); + +outfile = strcat(pre,'nnode.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',nnodes); +fclose(fout); + +outfile = strcat(pre,'nrows.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%i\n',ncases); +fclose(fout); + +outfile = strcat(pre,'type.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +fclose(fout); + +%This output file contains the states for discrete nodes. +% The unique matlab function already sorts the states. +outfile = strcat(pre,'nlevels.txt'); +fout = fopen(outfile,'w'); +for i = 1:nnodes + if levels{i} > 1 + fprintf(fout,'%s\t',labels{i},states{i}{1:end-1}); + fprintf(fout,'%s\n',states{i}{end}); + end +end +fclose(fout); + + +%Print a file with a short description of the input. +descfile = strcat(pre,'input_desc.txt'); +dout = fopen(descfile,'w'); +fprintf(dout,['As loaded, the input file had the following properties:\n\n']); +dout = fopen(descfile,'a'); +fprintf(dout,'There are %i variables and %i cases(rows)\n',size(labels,2),ncases); +fprintf(dout,'The variable names are:\n'); +fprintf(dout,'%s\t',labels{1:end-1}); +fprintf(dout,'%s\n\n',labels{end}); +for i=1:nnodes + if levels{i} == 1 + fprintf(dout,'%s is a continuous variable\n',labels{i}); + fprintf(dout,'%s\n',reason{i}); + column = str2double(data(:,i)); + colmean = mean(column); + colstd = std(column); + fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) + else + fprintf(dout,'%s is a discrete variable with %i states\n',labels{i},levels{i}); + fprintf(dout,'%s\n',reason{i}); + fprintf(dout,'The states are: '); + fprintf(dout,'%s ',states{i}{1:end-1}); + fprintf(dout,'%s\n\n',states{i}{end}); + end +end +fclose(fout); + +outfile = strcat(pre,'continuous_input.txt'); +fout = fopen(outfile,'w'); +fprintf(fout,'%s\t',labels{1:end-1}); +fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%i\t',levels{1:end-1}); +fprintf(fout,'%i\n',levels{end}); +%Need to replace states in discrete variables with integers for BNT +for i = 1:nnodes + if levels{i} > 1 + for j = 1:ncases + for k=1:size(states{i},1) + if data{j,i} == states{i}{k} + data{j,i} = sprintf('%i',num2cell(k){1});; + break + end + end + end + end +end +for i = 1:ncases + fprintf(fout,'%s\t',data{i,1:end-1}); + fprintf(fout,'%s\n',data{i,end}); +end +fclose(fout); + + + + + +end +% end of prepareInput.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInput.m b/sourcecodes/parameter_learning/code_backup/readInput.m new file mode 100644 index 00000000..891d7f36 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInput.m @@ -0,0 +1,63 @@ +function [ labels, cases, bnet, node_sizes, data,labelsold] = readInput( dfile, sfile, nnodes, std_flag ) + %readInput is to be used when reading in a network with a known structure + % + %Input: + % dfile = name of the file containing the data (required) + % sfile = name of the file containing the structure (required) + % nnodes = number of nodes in the network (required) + % std_flag = flag for whether or not to standardize the data. + % (optional-- Default is FALSE) + % + % See readInputData.m and readInputStructure.m for description of the + % format of the dfile and sfile, respectively. + % + %Output: + % labels = cell array with the names of the nodes. + % cases = cell array with the data. + % bnet = BNT bayesian network with the input structure. + +if nargin < 4 + std_flag = false(1); +end + + +% read in the file with the data +[labelsold,node_sizes,cases, data] = readInputData(dfile,nnodes); + + +% read in the file with the structure +[dag] = readInputStructure(sfile,labelsold); + + +% check the ordering of the nodes and reorder if necessary +[labels,cases,dag,node_sizes,ord_flag] = checkStructure(labelsold,cases,dag,node_sizes); + +dcount = 0; +for i = 1:nnodes + if node_sizes(i) ~= 1 + dcount = dcount + 1; + end +end +discrete = zeros(1,dcount); +dcount = 0; +for i = 1:nnodes + if node_sizes(i) ~= 1 + dcount = dcount + 1; + discrete(dcount) = i; + end +end + +bnet = mk_bnet(dag,node_sizes,'discrete',discrete,'names',labels); + +%bnet.dag + +checkDiscreteNodes(bnet,cases); + +% standardize continuous data to have a mean = 0 and std = 1 +if (std_flag) + [cases] = standardizeData(labels,node_sizes,cases); +end + + +end +% end of readInput.m diff --git a/sourcecodes/parameter_learning/code_backup/readInputData.m b/sourcecodes/parameter_learning/code_backup/readInputData.m new file mode 100644 index 00000000..706e2751 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInputData.m @@ -0,0 +1,75 @@ +function [ labels , node_sizes, cases, data] = readInputData( dfile , nnodes ) + % readColData reads data from a file containing data in columns + % that have text titles, and possibly other header text + % + % Input: + % dfile = name of the file containing the data.(required) + % nnodes = number of columns in the data file. (required) + % + % Function assumes the following format for the input file: + % 1) First line has labels for each of the nodes. There cannot + % be spaces in any node label. + % 2) The next line is the "node_sizes" of the nodes. If the + % nodes are discrete, this number will be equal to the number + % of states. If the nodes are continuous, they should be + % equal to 1. The function assumes that any nodes with + % node_size = 1 is continuous. + % 3) The rest of the file is numeric data. The data in the input + % data has the number of columns equal to the number of + % nodes in the network and the number of rows equal to + % the number of samples. + % + % + % Output: + % labels = cell array with node (column) labels. + % node_sizes = vector with the size of each node + % cases = cell array with the data. The cases array is transposed + % in comparison with the input data to agree with the format of + % cell data used in BNT. + +% open file for input, include error handling +fin = fopen(dfile,'r'); +if fin < 0 + error(['Could not open ',dfile,' for input']); +end + +% Read in first line to get the node labels. +labels = cell(1,nnodes); +buffer = fgetl(fin); %get header line as a string +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels{j} = next; +end + +% Read in the data. Use the vetorized fscanf function to load all +% numerical values into one vector. Then reshape this vector into a +% matrix. + +data = fscanf(fin,'%f'); % Load the numerical values into one long vector + + + + +nd = length(data); % total number of data points +nr = nd/nnodes; % number of rows; check (next statement) to make sure +if nr ~= round(nd/nnodes) + fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); + fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); + error('data is not rectangular') +end + +data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array + + +node_sizes = zeros(1,nnodes); +for j = 1:nnodes + node_sizes(j) = data(1,j); +end + +nr = nr - 1; +data(1,:) = []; +cases = cell(nnodes,nr); +cases(:,:) = num2cell(data'); + +end +% end of readInputData.m \ No newline at end of file diff --git a/sourcecodes/parameter_learning/code_backup/readInputStructure.m b/sourcecodes/parameter_learning/code_backup/readInputStructure.m new file mode 100644 index 00000000..6b3cbece --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/readInputStructure.m @@ -0,0 +1,72 @@ +function [ dag ] = readInputStructure( sfile, labels ) +%readInputStructure Read in file with structure information + % + %Input: + % sfile = name of the file containing the data (required) + % labels = cell array with node labels. (required) + % nnodes = number of columns in the data file. (required) + % + % Function assumes the following format for the structure input file: + % 1) The first line has node labels. These must be the same as + % in the input data file. They cannot contain spaces. + % 2) The remainder of the file contains the structure of the dag. + % The structure of a graph is a N-by-N matrix, where N is the + % number of nodes. There are 1's in the matrix representing + % parent-child relationships. For each 1, the row indicates + % the parent and the column indicates the child. For + % example, a 1 in the (2,3) position of the matrix indicates + % that there is an arc pointing from node 2 to node 3. + % + % + % Output: + % dag = matrix with the structure. +% +% Read in first line of the structure file +% open file for input, include error handling +fin = fopen(sfile,'r'); +if fin < 0 + error(['Could not open ',sfile,' for input']); +end + +nnodes = size(labels,2); +% Read in first line to get the node labels. +labels_test = cell(1,nnodes); +buffer = fgetl(fin); %get header line as a string +for j=1:nnodes + [next,buffer] = strtok(buffer); + labels_test{j} = next; +end + +for j=1:nnodes + if labels_test{j} ~= labels{j} + fprintf(['Label of node ',j,' is not consistent in input and structure files']) + end +end + +data = fscanf(fin,'%f'); + +nd = length(data); % total number of data points +nr = nd/nnodes; % number of rows; check (next statement) to make sure +if nr ~= round(nd/nnodes) + fprintf(1,'\ndata: nrow = %f\tncol = %d\n',nr,nnodes); + fprintf(1,'number of data points = %d does not equal nrow*ncol\n',nd); + error('Structure file does not have the correct dimensions (1)') +end +% check to make sure that structure is square +if nr ~= nnodes + error('Structure file does not have the correct dimensions (2)') +end + +data = reshape(data,nnodes,nr)'; % have to transpose the reshaped array + + +dag = zeros(nnodes,nnodes); +for i = 1:size(data,1) + for j = 1:size(data,2) + dag(i,j) = data(i,j); + end +end + + +end +% end of readInputStructure.m diff --git a/sourcecodes/parameter_learning/code_backup/runBN_initial.m b/sourcecodes/parameter_learning/code_backup/runBN_initial.m new file mode 100644 index 00000000..0deff1b5 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/runBN_initial.m @@ -0,0 +1,57 @@ +function runBN_initial(pre) +sfile=strcat(pre,'structure_input.txt'); +dfile=strcat(pre,'continuous_input.txt'); + +nnodefile=strcat(pre,'nnode.txt'); +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + + +mapfilename=strcat(pre,'mapdata.txt'); +mapvalfilename=strcat(pre,'map.txt'); + +mapfile = fopen(mapfilename,'w'); + +mapval = fopen(mapvalfilename,'w'); + + +Std_flag=true; +[labels,cases,bnet,node_sizes,data,labelsold]=readInput(dfile,sfile,nnodes,Std_flag); +s=std(data,0,1); +m=mean(data); + +for i=1:nnodes + fprintf(mapval,'%s\t%f\t%f\n',labelsold{i},s(i),m(i)); +end + +fprintf(mapfile,'%s',labels{1}); +for i=2:nnodes + fprintf(mapfile,'\t%s',labels{i}); +end +fprintf(mapfile,'\n'); +fclose(mapval); +fclose(mapfile); + +%Need to rearrange the means and stdevs to match the new labeling. +means = cell(1,nnodes); +stdevs = cell(1,nnodes); +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labels{i},labelsold{j}) + means{i} = m(j); + stdevs{i} = s(j); + break + end + end +end + + +[bnet]=parameterLearning(bnet,cases); + +filename=strcat(pre,'net_figure.txt'); + +drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means); + +writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m); + +end diff --git a/sourcecodes/parameter_learning/code_backup/standardizeData.m b/sourcecodes/parameter_learning/code_backup/standardizeData.m new file mode 100644 index 00000000..db5e04c7 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/standardizeData.m @@ -0,0 +1,25 @@ +function [ cases ] = standardizeData( labels, node_sizes, cases ) +%standardizeData standardizes continuous nodes so they have a mean = 0 +% and standard deviation = 1 + + +nnodes = size(labels,2); + +%fprintf(['Standardizing data for continuous nodes\n']) +for i = 1:nnodes + if node_sizes(i) == 1 + temp = cell2num(cases(i,:)); + [temp] = standardize(temp); + cases(i,:) = num2cell(temp); + end +end + +%write standardized data to file +%fprintf(['Standardized data is written to file standardized_data.txt\n']) +%fout = 'standardized_data.txt'; +%txt = sprintf([repmat('%s\t',1,size(labels,2))],labels{:}); +%dlmwrite(fout,txt,''); +%dlmwrite(fout,cell2num(cases'),'-append','delimiter','\t'); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters.m b/sourcecodes/parameter_learning/code_backup/writeParameters.m new file mode 100644 index 00000000..0790a8e2 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters.m @@ -0,0 +1,106 @@ +function [] = writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m) +%Writes a file that contains the parameters of the network with no evidence. + + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; + buffer = fgetl(flevels); + for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); +[engine,loglik] = enter_evidence(engine,evidence); + +%Open output file. +filename = strcat(pre,'parameters.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + predict = marginal_nodes(engine,nodeid); + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous node\n'; + fprintf(fileID,line); + %%% 'i' in the line below is correct: m and s are had original node labeling + adj_mu = predict.mu*s(i)+m(i); + adj_sigma = s(i)*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Discrete node with %i states\n'; + fprintf(fileID,line,bnet.node_sizes(nodeid)); + %line = 'Probability of each state\n'; + %fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m b/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m new file mode 100644 index 00000000..fc24e2e5 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m @@ -0,0 +1,151 @@ +function [] = writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) +%Writes a file that contains the parameters of the network after entering evidence. + +%Read in original node labels to get node IDs. +infile = strcat(pre,'continuous_input.txt'); +fin = fopen(infile,'r'); +labelsold = cell(1,nnodes); +buffer = fgetl(fin); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + labelsold{j} = next; +end +fclose(fin); + + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; +buffer = fgetl(flevels); +for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardize evidence for continuous nodes. + if bnet.node_sizes(di) == 1, + ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; + end + evidence{di} = ev_dat(di); +end + +[engine,loglik]=enter_evidence(engine,evidence); + +%Open output file. +filename = strcat(pre,'parameters_ev.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + predict = marginal_nodes(engine,nodeid); + if isempty(evidence{nodeid}) + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous parameters considering evidence:\n'; + fprintf(fileID,line); + %line = 'Mean and standard deviation of Gaussian distribution\n'; + %fprintf(fileID,line); + adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; + adj_sigma = stdevs{nodeid}*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Probability of states considering evidence:\n'; + fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + end + else + if bnet.node_sizes(nodeid) == 1; + line = 'Evidence was observed for this node. The observed value was:\n'; + fprintf(fileID,line); + adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; + fprintf(fileID,'%6.4f\n\n',adj_mu); + else + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + line = 'Evidence was observed for this node. The observed state was:\n'; + fprintf(fileID,line); + state_ev = uint16(ev_dat(nodeid)); +% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); + fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); + end + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/code_backup/writeParameters_int.m b/sourcecodes/parameter_learning/code_backup/writeParameters_int.m new file mode 100644 index 00000000..ed92d593 --- /dev/null +++ b/sourcecodes/parameter_learning/code_backup/writeParameters_int.m @@ -0,0 +1,186 @@ +function [] = writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) +%Writes a file that contains the parameters of the network after intervention. + + +%First read input file to get node labels to get node IDs. +infile = strcat(pre,'continuous_input.txt'); +fin = fopen(infile,'r'); +labelsold = cell(1,nnodes); +buffer = fgetl(fin); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + labelsold{j} = next; +end + +evidence = cell(1,nnodes); +engine = jtree_inf_engine(bnet); + +m = size(selectvar,1); + +%%Get the types of the nodes. +typefile = strcat(pre,'type.txt'); +ftype = fopen(typefile,'r'); +types = cell(1,nnodes); +buffer = fgetl(ftype); +buffer = fgetl(ftype); +for j = 1:nnodes + [next,buffer] = strtok(buffer); + types{j} = uint16(str2num(next)); +end + +max_states = 0; +disc_nodes = 0; +for j = 1:nnodes + if types{j} > max_states + max_states = types{j}; + end + if types{j} > 1 + disc_nodes = disc_nodes + 1; + end +end + +%Add 1 to max_states to account for node name +max_states = max_states + 1; + +%%Get mapping of discrete levels. +levelfile = strcat(pre,'nlevels.txt'); +flevels = fopen(levelfile,'r'); +levels = cell(disc_nodes,max_states); +ndisc_nodes = 0; +for i=1:disc_nodes + ndisc_nodes = ndisc_nodes + 1; +buffer = fgetl(flevels); +for j = 1:max_states + [next,buffer] = strtok(buffer); + if j == 1 + levels{i,j} = next; + else +% levels{i,j} = uint16(str2num(next)); + levels{i,j} = next; + end + if length(buffer) < 1 + break + end + end +end + + +ev_dat = zeros(1,nnodes); +for i = 1:m, + di=selectvar(i,1); + ev_dat(di)=selectdata(i,1); +%Need to standardize evidence for continuous nodes. + if bnet.node_sizes(di) == 1, + ev_dat(di) = (ev_dat(di) - means{di})/stdevs{di}; + end + evidence{di} = ev_dat(di); +end + +[engine,loglik]=enter_evidence(engine,evidence); + +%Get list of nodes that are children, grandchildren, etc. of intervened nodes +%int_nodes contains the list of these children nodes +int_nodes = zeros(1,nnodes); +%new_nodes is just a temporary array to know when to keep looking +new_nodes = zeros(1,nnodes); +for i = 1:nnodes + if !isempty(evidence{i}); + new_nodes(i) = 1; + int_nodes(i) = 1; + end +end +while sum(new_nodes) != 0 + new_nodes_old = new_nodes; + new_nodes = zeros(1,nnodes); + for i = 1:nnodes + if new_nodes_old(i) == 1 + for j = 1:nnodes + if int_nodes(j) == 0 + if bnet.dag(i,j) == 1, + new_nodes(j) = 1; + end + end + end + end + end + for i = 1:nnodes + if new_nodes(i) == 1; + int_nodes(i) = 1; + end + end +end + + +%Open output file. +filename = strcat(pre,'parameters_ev.txt'); +fileID = fopen(filename,'w'); + +for i = 1:nnodes + for j = 1:nnodes + if strcmp(labelsold{i},labels{j}); + nodeid = j; + break + end + end + %check to see if this is a node impacted by intervention + if int_nodes(nodeid) == 1 + %%%Print the name of the node + fprintf(fileID,'%s\n',labels{nodeid}); + predict = marginal_nodes(engine,nodeid); + if isempty(evidence{nodeid}) + %%%Print the type of node + if bnet.node_sizes(nodeid) == 1; + line = 'Continuous parameters considering intervention:\n'; + fprintf(fileID,line); + %line = 'Mean and standard deviation of Gaussian distribution\n'; + %fprintf(fileID,line); + adj_mu = predict.mu*stdevs{nodeid}+means{nodeid}; + adj_sigma = stdevs{nodeid}*predict.Sigma; + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + line = 'Probability of states considering intervention:\n'; + fprintf(fileID,line); + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + for j = 1:bnet.node_sizes(nodeid), + %%%For discrete nodes, the state and the percent of that state +% fprintf(fileID,'%i\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + fprintf(fileID,'%s\t%6.4f\n',levels{nodeid2,j+1},predict.T(j)); + end; + fprintf(fileID,'\n') + end + else + if bnet.node_sizes(nodeid) == 1; + line = 'Intervention on this node assigned the following value:\n'; + fprintf(fileID,line); + adj_mu = ev_dat(nodeid)*stdevs{nodeid}+means{nodeid}; + fprintf(fileID,'%6.4f\n\n',adj_mu); + else + nodeid2 = 0; + for k = 1:ndisc_nodes, + if strcmp(levels{k,1},labels{nodeid}), + nodeid2 = k; + break + end + end + line = 'Intervention on this node assigned the following state:\n'; + fprintf(fileID,line); + state_ev = uint16(ev_dat(nodeid)); +% fprintf(fileID,'%i\n\n',levels{nodeid2,state_ev+1}); + fprintf(fileID,'%s\n\n',levels{nodeid2,state_ev+1}); + end + end + end +end + + + +fclose(fileID); + +end + diff --git a/sourcecodes/parameter_learning/drawFigure.m b/sourcecodes/parameter_learning/drawFigure.m index 404a65f7..7da07a90 100644 --- a/sourcecodes/parameter_learning/drawFigure.m +++ b/sourcecodes/parameter_learning/drawFigure.m @@ -1,186 +1,17 @@ -function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means) %drawFigure writes the parameters and data that are needed to draw the -%structure of a Bayesian network. - - -if nargin < 8, - drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means); -else - drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata); -end; - -end - - - -function [] = drawFigureEv(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) -%Function to use if there is no entered evidence. -% +%structure of a Bayesian network for BNW. +% This is the function that is called to create the initial +% net_figure file for the network (before evidence or intervention). % -%Before each printed line, I will have a line that starts with %%% -% that describes what will be on that line - -%Create an empty evidence cell array. - -%val=cases; -%for i = 1:nnodes -% val(i,1)=val(i,2); - -%end - -A=cell2mat(cases'); -Amax=max(A); -Amin=min(A); - - -evidence = cell(1,nnodes); -engine = jtree_inf_engine(bnet); - -evidence{selectvar}=selectdata; - -[engine,loglik]=enter_evidence(engine,evidence); - -%Open the file, and write the nodes to a file. -fileID = fopen(filename,'w'); - -%%%%Evidence node -fprintf(fileID,'%i\n',selectvar); -%%% The number of nodes -fprintf(fileID,'%i\n',nnodes); -%Get canvas size -labels_temp = cellstr(labels); -[x,y] = make_layout(bnet.dag); - -x = x - min(x); -y = 1 - y; -y = y - min(y); - -[x_dim,y_dim] = canvasSize(nnodes,x,y); - -%%% The dimensions of the canvas for the javascript code -fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim) - -x = x*x_dim; -y = y*y_dim; -for i = 1:nnodes, -%%% The name and X- and Y-positions of each node - fprintf(fileID,'%s\t%i\t%i\n',labels{i},round(x(i)),round(y(i))); -end - -%Get the number of parents and children for each node. -num_par = zeros(1,nnodes); -%For parents, sum down columns -for i = 1:nnodes, - for j = 1:nnodes, - if bnet.dag(j,i) == 1, - num_par(i) = num_par(i) + 1; - end - end -end -num_child = zeros(1,nnodes); -for i = 1:nnodes, - for j = 1:nnodes, - if bnet.dag(i,j) == 1, - num_child(i) = num_child(i) + 1; - end - end -end - - -for i = 1:nnodes, - %%% The name and type of each node (1=continuous, the number of states - %%% if it is discrete - fprintf(fileID,'%s\t%i\n',labels{i},bnet.node_sizes(i)); - %%% The size of the node, I am going to keep them - %%% 250(width) by 150(height) for now - %Could modify this to change the width based on the length of the node - %name - fprintf(fileID,'%i\t%i\n',250,150); - %%% The number of parents of the node, and the parents - if num_par(i) == 0; - %%% If no parents: - fprintf(fileID,'%i\n',num_par(i)); - else - parents = zeros(1,num_par(i)); - k = 1; - for j = 1:nnodes, - if bnet.dag(j,i) == 1, - parents(1,k) = j; - k = k + 1; - end - end - format = '%i\t'; - for j = 1:num_par(i)-1, - format = strcat(format,'%i\t'); - end - format = strcat(format,'%i\n'); - %%%If there are parents: - fprintf(fileID,format,num_par(i),parents(1,:)); - end - - - %%% The number of children of the node, and the children - if num_child(i) == 0; - %%% If no children: - fprintf(fileID,'%i\n',num_child(i)); - else - children = zeros(1,num_child(i)); - k = 1; - for j = 1:nnodes, - if bnet.dag(i,j) == 1, - children(1,k) = j; - k = k + 1; - end - end - format = '%i\t'; - for j = 1:num_child(i)-1, - format = strcat(format,'%i\t'); - end - format = strcat(format,'%i\n'); - %%%If there are parents: - fprintf(fileID,format,num_child(i),children(1,:)); - end - - predict = marginal_nodes(engine,i); - if isempty(evidence{i}) - if bnet.node_sizes(i) ~= 1, - for j = 1:bnet.node_sizes(i), - %%%For discrete nodes, the state and the percent of that state - fprintf(fileID,'%i\t%6.4f\n',j,predict.T(j)); - end; - else - - [x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); - %%%For continuous nodes, print x and the pdf of a normal curve. - for j = 1:101, - %%Undo standardization - xvals(j,1) = xvals(j,1)*stdevs{i}+means{i} - fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); - end; - end; - else - fprintf(fileID,'%6.4f\t%6.4f\n',selectdata,1); - end - -end -%fprintf(fileID,'%s\t %\n',labels_temp{:}); - - -fclose(fileID); - -end - - - - - - -function [] = drawFigureNoEv(nnodes,bnet,labels,filename,cases,stdevs,means) -%Function to use if there is no entered evidence. -% % -%Before each printed line, I will have a line that starts with %%% -% that describes what will be on that line +% The output is the file specified by 'filename'. +% For BNW, this file is called: ???net_figure.txt +% where ??? is the prefix. +% +% drawFigure is called by runBN_intial.m +% + A=cell2mat(cases'); Amax=max(A); Amin=min(A); diff --git a/sourcecodes/parameter_learning/drawFigureM.m b/sourcecodes/parameter_learning/drawFigureM.m index 91b8698f..820f06dd 100644 --- a/sourcecodes/parameter_learning/drawFigureM.m +++ b/sourcecodes/parameter_learning/drawFigureM.m @@ -1,6 +1,15 @@ function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) %drawFigureM writes the parameters and data that are needed to draw the -%structure of a Bayesian network after added evidence or intervention +%structure of a Bayesian network after adding evidence or intervention +%It creates the net_figure_new file after evidence/intervetion. +% +% The output file is specified by 'filename'. +% For BNW, the file is named ???net_figure_new.txt +% where ??? is the prefix. +% +% drawFigureM is called by Predictmultiple.m and Predictmultipleintervention.m + + fileID = fopen(filename,'w'); diff --git a/sourcecodes/parameter_learning/parameterLearning.m b/sourcecodes/parameter_learning/parameterLearning.m index 872e94b1..3ef6c0b3 100644 --- a/sourcecodes/parameter_learning/parameterLearning.m +++ b/sourcecodes/parameter_learning/parameterLearning.m @@ -1,5 +1,14 @@ function [ bnet ] = parameterLearning( bnet,cases,engine_name ) %parameterLearning Do parameter learning and inference +% It returns the bnet with parameters learned from the data in cases. +% +% This is very basic now. It could be modified to use different engine +% types in the future. Now, I always use the 'jtree_inf_engine'. +% +% +% parameterLearning is called by runBN_initial.m, +% Predictmultiple.m, and Predictmultipleintervention.m + %engine is an optional argument if nargin < 3 @@ -15,3 +24,25 @@ end end +function [ bnet ] = getParams( bnet, cases ) +%getParams Code to initialize CPT and do parameter learning. +%This will be very basic for now. I can add more options later. +% + +dnodes = bnet.dnodes; +cnodes = bnet.cnodes; +nnodes = size(dnodes,2)+size(cnodes,2); + +%make dnodes tabular_CPT +for i = 1:size(dnodes,2) + bnet.CPD{dnodes(i)} = tabular_CPD(bnet,dnodes(i)); +end + +for i = 1:size(cnodes,2) + bnet.CPD{cnodes(i)} = gaussian_CPD(bnet,cnodes(i)); +end + +bnet = learn_params(bnet,cases); + + +end diff --git a/sourcecodes/parameter_learning/prepareInput.m b/sourcecodes/parameter_learning/prepareInput.m index 28a06c15..84d2ae17 100644 --- a/sourcecodes/parameter_learning/prepareInput.m +++ b/sourcecodes/parameter_learning/prepareInput.m @@ -39,6 +39,8 @@ function [ ] = prepareInput( pre ) % 8-12) ???ban.txt, ???white.txt, ???k.txt, ???thr.txt, and % ???parent.txt: Files with default values for structure learning. % + % It is called by the run_prep_input script in the 'sourcecodes' directory. + % open file for input, include error handling dfile=strcat(pre,'continuous_input_orig.txt'); @@ -81,28 +83,36 @@ for j = 1:nnodes levels{j} = size(states{j},1); end +reason = cell(1,nnodes); %Now do some checks to see if nodes are discrete or continuous for j = 1:nnodes % If there are 3 or less unique values, I will assume that the node is discrete. if levels{j} < 4; + reason{j} = "It was determined to be discrete because there are a small number (<4) of possible values."; continue % If there are as many unique values as a third of the number of cases, % I will assume that the node is continuous. elseif levels{j} > ncases/3; levels{j} = 1; + reason{j} = "It was determined to be continuous because there are a large number of possible values compared to the number of cases."; + continue % If there are more than twenty unique values, % I will assume that the node is continuous. elseif levels{j} > 20; levels{j} = 1; + reason{j} = "It was determined to be continuous because there are many (>20) possible values."; + continue % Otherwise, I will scan through the individual values. % If any of the values contain a '.', I will assume it is continuous. else + reason{j} = "It was determined to be discrete by default."; period_test = 0; column = data(:,j); k = 1; while period_test == 0 period_test = sum(cell2mat(strfind(column(k),"."))); if period_test != 0; + reason{j} = "This variable was determined to be continuous because there were several possible values and at least one value contained a period(.)."; levels{j} = 1; end k++; @@ -131,6 +141,7 @@ if max_disc > min_cont labels_old = labels; data_old = data; states_old = states; + reason_old = reason; new_order = {}; for i=1:nnodes if levels_old{i} > 1 @@ -145,10 +156,12 @@ if max_disc > min_cont labels = {}; levels = {}; states = {}; + reason = {}; for i =1:nnodes labels{i} = labels_old{new_order{i}}; levels{i} = levels_old{new_order{i}}; states{i} = states_old{new_order{i}}; + reason{i} = reason_old{new_order{i}}; for j=1:ncases data{j,i} = data_old{j,new_order{i}}; end @@ -228,19 +241,21 @@ descfile = strcat(pre,'input_desc.txt'); dout = fopen(descfile,'w'); fprintf(dout,['As loaded, the input file had the following properties:\n\n']); dout = fopen(descfile,'a'); -fprintf(dout,'There are %i variables and %i cases(rows)\n',size(labels,2),ncases); +fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); fprintf(dout,'The variable names are:\n'); fprintf(dout,'%s\t',labels{1:end-1}); fprintf(dout,'%s\n\n',labels{end}); for i=1:nnodes if levels{i} == 1 - fprintf(dout,'%s is a continuous variable\n',labels{i}); + fprintf(dout,'%s is a continuous variable.\n',labels{i}); + fprintf(dout,'%s\n',reason{i}); column = str2double(data(:,i)); colmean = mean(column); colstd = std(column); fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)) else - fprintf(dout,'%s is a discrete variable with %i states\n',labels{i},levels{i}); + fprintf(dout,'%s is a discrete variable with %i states.\n',labels{i},levels{i}); + fprintf(dout,'%s\n',reason{i}); fprintf(dout,'The states are: '); fprintf(dout,'%s ',states{i}{1:end-1}); fprintf(dout,'%s\n\n',states{i}{end}); diff --git a/sourcecodes/parameter_learning/readInput.m b/sourcecodes/parameter_learning/readInput.m index 891d7f36..87612c76 100644 --- a/sourcecodes/parameter_learning/readInput.m +++ b/sourcecodes/parameter_learning/readInput.m @@ -15,6 +15,9 @@ function [ labels, cases, bnet, node_sizes, data,labelsold] = readInput( dfile, % labels = cell array with the names of the nodes. % cases = cell array with the data. % bnet = BNT bayesian network with the input structure. + % + % readInput is called by runBN_initial.m + if nargin < 4 std_flag = false(1); diff --git a/sourcecodes/parameter_learning/readInputData.m b/sourcecodes/parameter_learning/readInputData.m index 706e2751..f06aeaa5 100644 --- a/sourcecodes/parameter_learning/readInputData.m +++ b/sourcecodes/parameter_learning/readInputData.m @@ -26,6 +26,10 @@ function [ labels , node_sizes, cases, data] = readInputData( dfile , nnodes ) % cases = cell array with the data. The cases array is transposed % in comparison with the input data to agree with the format of % cell data used in BNT. + % + % readInputData is called by readInput.m + + % open file for input, include error handling fin = fopen(dfile,'r'); diff --git a/sourcecodes/parameter_learning/readInputStructure.m b/sourcecodes/parameter_learning/readInputStructure.m index 6b3cbece..a91c34df 100644 --- a/sourcecodes/parameter_learning/readInputStructure.m +++ b/sourcecodes/parameter_learning/readInputStructure.m @@ -21,6 +21,9 @@ function [ dag ] = readInputStructure( sfile, labels ) % Output: % dag = matrix with the structure. % +% readInputStructure is called by runBN_initial.m + + % Read in first line of the structure file % open file for input, include error handling fin = fopen(sfile,'r'); diff --git a/sourcecodes/parameter_learning/runBN_initial.m b/sourcecodes/parameter_learning/runBN_initial.m index c2dec164..16ce06dc 100644 --- a/sourcecodes/parameter_learning/runBN_initial.m +++ b/sourcecodes/parameter_learning/runBN_initial.m @@ -1,4 +1,17 @@ function runBN_initial(pre) +% runBN_initial is used to create the net_figure file +% for a network without entered evidence or intervention. +% +% The input is 'pre'-- the prefix for the network and data +% in BNW. It uses this identifier to read several files from +% BNW. +% +% The output is ???net_figure.txt. It also calls writeParameters +% to write the parameter file. +% +% runBN_initial is called by run_octave in the 'sourcecodes' directory. +% + sfile=strcat(pre,'structure_input.txt'); dfile=strcat(pre,'continuous_input.txt'); @@ -21,7 +34,7 @@ s=std(data,0,1); m=mean(data); for i=1:nnodes - fprintf(mapval,'%s\t%d\t%f\t%f\n',labelsold{i},node_sizes(i),s(i),m(i)); + fprintf(mapval,'%s\t%f\t%f\n',labelsold{i},s(i),m(i)); end fprintf(mapfile,'%s',labels{1}); diff --git a/sourcecodes/parameter_learning/standardizeData.m b/sourcecodes/parameter_learning/standardizeData.m index db5e04c7..9f36fc72 100644 --- a/sourcecodes/parameter_learning/standardizeData.m +++ b/sourcecodes/parameter_learning/standardizeData.m @@ -1,7 +1,8 @@ function [ cases ] = standardizeData( labels, node_sizes, cases ) %standardizeData standardizes continuous nodes so they have a mean = 0 % and standard deviation = 1 - +% +% standardizeData is called by readInput.m nnodes = size(labels,2); @@ -14,12 +15,6 @@ for i = 1:nnodes end end -%write standardized data to file -%fprintf(['Standardized data is written to file standardized_data.txt\n']) -%fout = 'standardized_data.txt'; -%txt = sprintf([repmat('%s\t',1,size(labels,2))],labels{:}); -%dlmwrite(fout,txt,''); -%dlmwrite(fout,cell2num(cases'),'-append','delimiter','\t'); end diff --git a/sourcecodes/parameter_learning/writeParameters.m b/sourcecodes/parameter_learning/writeParameters.m index 0790a8e2..42b2a4ef 100644 --- a/sourcecodes/parameter_learning/writeParameters.m +++ b/sourcecodes/parameter_learning/writeParameters.m @@ -1,5 +1,10 @@ function [] = writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m) %Writes a file that contains the parameters of the network with no evidence. +% +% The file is called ???parameters.txt where ??? is the prefix in BNW +% for the network. +% +% writeParameters is called by runBN_intial.m %%Get the types of the nodes. diff --git a/sourcecodes/parameter_learning/writeParameters_ev.m b/sourcecodes/parameter_learning/writeParameters_ev.m index fc24e2e5..1f07c745 100644 --- a/sourcecodes/parameter_learning/writeParameters_ev.m +++ b/sourcecodes/parameter_learning/writeParameters_ev.m @@ -1,5 +1,11 @@ function [] = writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) %Writes a file that contains the parameters of the network after entering evidence. +% +% The file is called ???parameters_ev.txt where ??? is the prefix in BNW +% for the network. +% +% It is called by Predictmultiple.m + %Read in original node labels to get node IDs. infile = strcat(pre,'continuous_input.txt'); diff --git a/sourcecodes/parameter_learning/writeParameters_int.m b/sourcecodes/parameter_learning/writeParameters_int.m index ed92d593..69dcbb93 100644 --- a/sourcecodes/parameter_learning/writeParameters_int.m +++ b/sourcecodes/parameter_learning/writeParameters_int.m @@ -1,6 +1,10 @@ function [] = writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,selectvar,selectdata) %Writes a file that contains the parameters of the network after intervention. - +% +% The file is called ???parameters_ev.txt where ??? is the prefix in BNW +% for the network. +% +% writeParameters is called by Predictmultipleintervention.m %First read input file to get node labels to get node IDs. infile = strcat(pre,'continuous_input.txt'); |
