From 25b843f6bbacb1937bdb960777b73acbece64115 Mon Sep 17 00:00:00 2001 From: ziejd2 Date: Wed, 24 Feb 2021 14:36:59 -0600 Subject: GENENET8 update --- sourcecodes/parameter_learning/Predictmultiple.m | 2 +- .../Predictmultipleintervention.m | 2 +- .../code_backup/Predictmultiple.m | 72 ---- .../code_backup/Predictmultipleintrvention.m | 95 ----- .../code_backup/checkDiscreteNodes.m | 37 -- .../code_backup/checkStructure.m | 78 ----- .../parameter_learning/code_backup/drawFigure.m | 390 --------------------- .../parameter_learning/code_backup/drawFigure.m~ | 388 -------------------- .../parameter_learning/code_backup/drawFigureM.m | 230 ------------ .../parameter_learning/code_backup/getParams.m | 22 -- .../parameter_learning/code_backup/looCrossValid.m | 241 ------------- .../code_backup/parameterLearning.m | 17 - .../parameter_learning/code_backup/prepareInput.m | 294 ---------------- .../parameter_learning/code_backup/prepareInput.m~ | 294 ---------------- .../parameter_learning/code_backup/readInput.m | 63 ---- .../parameter_learning/code_backup/readInputData.m | 75 ---- .../code_backup/readInputStructure.m | 72 ---- .../parameter_learning/code_backup/runBN_initial.m | 57 --- .../code_backup/standardizeData.m | 25 -- .../code_backup/writeParameters.m | 106 ------ .../code_backup/writeParameters_ev.m | 151 -------- .../code_backup/writeParameters_int.m | 186 ---------- sourcecodes/parameter_learning/createJSON.m | 13 +- sourcecodes/parameter_learning/createSVG.m | 28 +- sourcecodes/parameter_learning/drawFigure.m | 73 +++- sourcecodes/parameter_learning/drawFigureM.m | 25 +- sourcecodes/parameter_learning/kfoldCrossValid.m | 21 +- sourcecodes/parameter_learning/looCrossValid.m | 18 +- sourcecodes/parameter_learning/modifyEdges.m | 179 +++++----- sourcecodes/parameter_learning/normpdf.m | 50 +++ sourcecodes/parameter_learning/normrnd.m | 130 +++++++ sourcecodes/parameter_learning/prepareInput.m | 84 ++++- sourcecodes/parameter_learning/removeNodes.m | 39 ++- sourcecodes/parameter_learning/runBN_initial.m | 2 +- .../parameter_learning/testSetPredictions.m | 32 +- sourcecodes/parameter_learning/writeParameters.m | 34 +- .../parameter_learning/writeParameters_ev.m | 28 +- .../parameter_learning/writeParameters_int.m | 29 +- sourcecodes/parameter_learning/writeSettings.m | 200 +++++++++++ 39 files changed, 784 insertions(+), 3098 deletions(-) delete mode 100644 sourcecodes/parameter_learning/code_backup/Predictmultiple.m delete mode 100644 sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m delete mode 100644 sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m delete mode 100644 sourcecodes/parameter_learning/code_backup/checkStructure.m delete mode 100644 sourcecodes/parameter_learning/code_backup/drawFigure.m delete mode 100644 sourcecodes/parameter_learning/code_backup/drawFigure.m~ delete mode 100644 sourcecodes/parameter_learning/code_backup/drawFigureM.m delete mode 100644 sourcecodes/parameter_learning/code_backup/getParams.m delete mode 100644 sourcecodes/parameter_learning/code_backup/looCrossValid.m delete mode 100644 sourcecodes/parameter_learning/code_backup/parameterLearning.m delete mode 100644 sourcecodes/parameter_learning/code_backup/prepareInput.m delete mode 100644 sourcecodes/parameter_learning/code_backup/prepareInput.m~ delete mode 100644 sourcecodes/parameter_learning/code_backup/readInput.m delete mode 100644 sourcecodes/parameter_learning/code_backup/readInputData.m delete mode 100644 sourcecodes/parameter_learning/code_backup/readInputStructure.m delete mode 100644 sourcecodes/parameter_learning/code_backup/runBN_initial.m delete mode 100644 sourcecodes/parameter_learning/code_backup/standardizeData.m delete mode 100644 sourcecodes/parameter_learning/code_backup/writeParameters.m delete mode 100644 sourcecodes/parameter_learning/code_backup/writeParameters_ev.m delete mode 100644 sourcecodes/parameter_learning/code_backup/writeParameters_int.m create mode 100644 sourcecodes/parameter_learning/normpdf.m create mode 100644 sourcecodes/parameter_learning/normrnd.m create mode 100644 sourcecodes/parameter_learning/writeSettings.m (limited to 'sourcecodes/parameter_learning') diff --git a/sourcecodes/parameter_learning/Predictmultiple.m b/sourcecodes/parameter_learning/Predictmultiple.m index a7236e35..2abd1df9 100644 --- a/sourcecodes/parameter_learning/Predictmultiple.m +++ b/sourcecodes/parameter_learning/Predictmultiple.m @@ -79,7 +79,7 @@ end filename=strcat(pre,'net_figure_new.txt'); -drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new,pre); writeParameters_ev(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); diff --git a/sourcecodes/parameter_learning/Predictmultipleintervention.m b/sourcecodes/parameter_learning/Predictmultipleintervention.m index 7e6140a9..b4ad58b8 100644 --- a/sourcecodes/parameter_learning/Predictmultipleintervention.m +++ b/sourcecodes/parameter_learning/Predictmultipleintervention.m @@ -100,7 +100,7 @@ end filename=strcat(pre,'net_figure_new.txt'); -drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new); +drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,select_var_new,select_var_data_new,pre); writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,select_var_new,select_var_data_new); diff --git a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m b/sourcecodes/parameter_learning/code_backup/Predictmultiple.m deleted file mode 100644 index 9d107628..00000000 --- a/sourcecodes/parameter_learning/code_backup/Predictmultiple.m +++ /dev/null @@ -1,72 +0,0 @@ -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 deleted file mode 100644 index e9f741f2..00000000 --- a/sourcecodes/parameter_learning/code_backup/Predictmultipleintrvention.m +++ /dev/null @@ -1,95 +0,0 @@ -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 deleted file mode 100644 index c9d0692c..00000000 --- a/sourcecodes/parameter_learning/code_backup/checkDiscreteNodes.m +++ /dev/null @@ -1,37 +0,0 @@ -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 deleted file mode 100644 index b4de9403..00000000 --- a/sourcecodes/parameter_learning/code_backup/checkStructure.m +++ /dev/null @@ -1,78 +0,0 @@ -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 deleted file mode 100644 index f84bffa3..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigure.m +++ /dev/null @@ -1,390 +0,0 @@ -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~ deleted file mode 100644 index 404a65f7..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigure.m~ +++ /dev/null @@ -1,388 +0,0 @@ -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 deleted file mode 100644 index 91b8698f..00000000 --- a/sourcecodes/parameter_learning/code_backup/drawFigureM.m +++ /dev/null @@ -1,230 +0,0 @@ -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 deleted file mode 100644 index 31f84ffb..00000000 --- a/sourcecodes/parameter_learning/code_backup/getParams.m +++ /dev/null @@ -1,22 +0,0 @@ -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/looCrossValid.m b/sourcecodes/parameter_learning/code_backup/looCrossValid.m deleted file mode 100644 index 21d6560d..00000000 --- a/sourcecodes/parameter_learning/code_backup/looCrossValid.m +++ /dev/null @@ -1,241 +0,0 @@ -function looCrossValid(pre,predict_label) -% This function will peform leave-one-out cross-validation. -% This requires the specification of the name of the variable -% that you want to predict. -% For now, I will assume that the variable is a discrete variable. - - - -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'); - -Std_flag=true; -[labels,cases,bnet]=readInput(dfile,sfile,nnodes,Std_flag); - -for i=1:nnodes - if strcmp(labels(i),predict_label) - predict_node = i; - end -end - -predict_cases = bnet.node_sizes(predict_node); - -if predict_cases == 1 - looCV_continuous(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases); -else - looCV_discrete(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases); -endif - -end - -function looCV_continuous(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases) - -ncases = size(cases,2); - -%%Read in original means and standard deviations to report output as -%% untransformed values. -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 - - -loopredictions=zeros(size(cases,2),2); - -%t=cputime; -%First get loo predictions -for i =1:ncases -% i - current_data = cases(:,i); - cases_new = cases; - cases_new(:,i) = []; - evidence = current_data; - evidence{predict_node} = {}; - [bnet]=parameterLearning(bnet,cases_new); - engine = jtree_inf_engine(bnet); - [engine,loglik] = enter_evidence(engine,evidence); - predict = marginal_nodes(engine,predict_node); - adj_mu = predict.mu*stdevs{predict_node}+means{predict_node}; - adj_sigma = stdevs{predict_node}*predict.Sigma; - loopredictions(i,1) = adj_mu; - loopredictions(i,2) = adj_sigma; -end -%e=cputime-t; - - -%Open output file. -filename = strcat(pre,'looCV.txt'); -fileID = fopen(filename,'w'); - -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); - - -%%Print the predictions -fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); -fprintf(fileID,'Mean\tStDev\n'); -for i = 1:ncases - fprintf(fileID,'%i\t',i); - fprintf(fileID,'%6.4f\t%6.4f\n',loopredictions(i,:)); -end - -end - - -function looCV_discrete(pre,predict_label,nnodes,labels,cases,bnet,predict_node,predict_cases) - -ncases = size(cases,2); - -%This next section just gets the original names of the levels. -% so they can be written to the output file. -%%Get the maximum_number of states so array will be big enough -%%Add 1 because the input includes the node name -max_states = max(bnet.node_sizes) + 1; -disc_nodes = size(bnet.dnodes,2); - -%%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 - -pred_levels = cell(1,predict_cases); -for i = 1:disc_nodes - if strcmp(levels{i,1},predict_label); - for j = 1:predict_cases - pred_levels{j} = levels{i,j+1}; - end - break - end -end - -loopredictions=zeros(size(cases,2),predict_cases); - -%t=cputime; -%First get loo predictions -for i =1:ncases -% i - current_data = cases(:,i); - cases_new = cases; - cases_new(:,i) = []; - evidence = current_data; - evidence{predict_node} = {}; - - [bnet]=parameterLearning(bnet,cases_new); - engine = jtree_inf_engine(bnet); - [engine,loglik] = enter_evidence(engine,evidence); - predict = marginal_nodes(engine,predict_node); - for j = 1:predict_cases - loopredictions(i,j) = predict.T(j); - end -end -%e=cputime-t; - -%Now compare with actual outcomes -actual_states = zeros(1,predict_cases); -for i=1:ncases - for j = 1:predict_cases - if cell2mat(cases(predict_node,i)) == j - actual_states(j) = actual_states(j) + 1; - end - end -end - -actual_states; -pred_states = zeros(1,ncases); - -for i=1:ncases - max_state = 1; - for j = 2:predict_cases - if loopredictions(i,j) > loopredictions(i,max_state) - max_state = j; - end - end - pred_states(i) = max_state; -end - -correct = 0; -for i=1:ncases - if pred_states(i) == cell2mat(cases(predict_node,i)) - correct = correct + 1; - end -end - -accuracy = correct/ncases; - -%Open output file. -filename = strcat(pre,'looCV.txt'); -fileID = fopen(filename,'w'); - -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); - - -%%Print the accuracy -fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); - -%%Print the predictions -fprintf(fileID,'Predicted likelihood of each state for each case:\n'); -fprintf(fileID,'%s\t','Case'); -fprintf(fileID,'%s\t',pred_levels{1:end-1}); -fprintf(fileID,'%s\n',pred_levels{end}); -for i = 1:ncases - fprintf(fileID,'%i\t',i); - fprintf(fileID,'%6.4f\t',loopredictions(i,1:end-1)); - fprintf(fileID,'%6.4f\n',loopredictions(i,end)); -end - -end diff --git a/sourcecodes/parameter_learning/code_backup/parameterLearning.m b/sourcecodes/parameter_learning/code_backup/parameterLearning.m deleted file mode 100644 index 872e94b1..00000000 --- a/sourcecodes/parameter_learning/code_backup/parameterLearning.m +++ /dev/null @@ -1,17 +0,0 @@ -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 deleted file mode 100644 index 838dcd2c..00000000 --- a/sourcecodes/parameter_learning/code_backup/prepareInput.m +++ /dev/null @@ -1,294 +0,0 @@ -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~ deleted file mode 100644 index 9fc0f97f..00000000 --- a/sourcecodes/parameter_learning/code_backup/prepareInput.m~ +++ /dev/null @@ -1,294 +0,0 @@ -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 deleted file mode 100644 index 891d7f36..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInput.m +++ /dev/null @@ -1,63 +0,0 @@ -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 deleted file mode 100644 index 706e2751..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInputData.m +++ /dev/null @@ -1,75 +0,0 @@ -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 deleted file mode 100644 index 6b3cbece..00000000 --- a/sourcecodes/parameter_learning/code_backup/readInputStructure.m +++ /dev/null @@ -1,72 +0,0 @@ -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 deleted file mode 100644 index 0deff1b5..00000000 --- a/sourcecodes/parameter_learning/code_backup/runBN_initial.m +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index db5e04c7..00000000 --- a/sourcecodes/parameter_learning/code_backup/standardizeData.m +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index 0790a8e2..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters.m +++ /dev/null @@ -1,106 +0,0 @@ -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 deleted file mode 100644 index fc24e2e5..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters_ev.m +++ /dev/null @@ -1,151 +0,0 @@ -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 deleted file mode 100644 index ed92d593..00000000 --- a/sourcecodes/parameter_learning/code_backup/writeParameters_int.m +++ /dev/null @@ -1,186 +0,0 @@ -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/createJSON.m b/sourcecodes/parameter_learning/createJSON.m index 65758356..e6fd7ea2 100644 --- a/sourcecodes/parameter_learning/createJSON.m +++ b/sourcecodes/parameter_learning/createJSON.m @@ -12,6 +12,10 @@ function [ ] = createJSON( pre ) % +nnodefile=strcat(pre,'nnode.txt'); +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + % open file for input, include error handling dfile=strcat(pre,'structure_input.txt'); @@ -22,7 +26,6 @@ end % Read in first line to get the number of nodes and the node labels. buffer = strtrim(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); @@ -40,8 +43,14 @@ for i = 1:nnodes end -% open file to read in model averaging scores + +%Check to see if a file with model averaging scores exists dfile2=strcat(pre,'structure_input_temp.txt'); +%if it does not exist, then just open the other structure file again +if exist(dfile2, 'file') != 2 + dfile2 = dfile; + frewind(dfile2); +end fin2 = fopen(dfile2,'r'); if fin2 < 0 diff --git a/sourcecodes/parameter_learning/createSVG.m b/sourcecodes/parameter_learning/createSVG.m index f7cc9b64..2d190b68 100644 --- a/sourcecodes/parameter_learning/createSVG.m +++ b/sourcecodes/parameter_learning/createSVG.m @@ -16,6 +16,10 @@ function [ ] = createSVG( pre ) % +nnodefile=strcat(pre,'nnode.txt'); +fnnode = fopen(nnodefile,'r'); +nnodes = fscanf(fnnode,'%d'); + % open file for input, include error handling dfile=strcat(pre,'structure_input.txt'); @@ -26,19 +30,20 @@ end % 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 + j, buffer [next,buffer] = strtok(buffer); labels{j} = next; end - % Read in the edges edges = cell(nnodes,nnodes); for i = 1:nnodes buffer = fgetl(fin); for j = 1:nnodes + i, j, buffer [next,buffer] = strtok(buffer); edges{i,j} = next; end @@ -48,6 +53,13 @@ end % open file to read in model averaging scores dfile2=strcat(pre,'structure_input_temp.txt'); +%If it does not exist, then just read in the regular structure file +%% as the model averaging score file +if exist(dfile2, 'file') != 2 + dfile2 = dfile; + frewind(dfile2); +end + if (exist(dfile2) == 0) scores = cell(nnodes,nnodes); for i = 1:nnodes @@ -107,11 +119,11 @@ end outfile = strcat(pre,'graphviz_svg.txt'); fout = fopen(outfile,'w'); -fprintf(fout,"digraph G {\n"); +fprintf(fout,"digraph \"\" {\n"); %fprintf(fout,"size=\"10,10\"; ratio = fill;\n"); fprintf(fout,"size=\"10,10\"; remincross = true;\n"); -fprintf(fout,"node [shape=rectangle, width=1.0, fontsize=22];\n"); -fprintf(fout,"edge [fontsize=16];\n"); +fprintf(fout,"node [shape=box, fontsize=18, margin=\"0.05,0\", height=0.3, rounded=true];\n"); +fprintf(fout,"edge [fontsize=12];\n"); for i = 1:nedges fprintf(fout,"\"%s\" -> \"%s\" [ label=\"%3.2f\", penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},scores1(i),2*scores1(i)); end @@ -121,11 +133,11 @@ fclose(fout); outfile2 = strcat(pre,'graphviz_svg_no_edge.txt'); fout2 = fopen(outfile2,'w'); -fprintf(fout2,"digraph G {\n"); +fprintf(fout2,"digraph \"\" {\n"); %fprintf(fout,"size=\"10,10\"; ratio = fill;\n"); fprintf(fout2,"size=\"10,10\"; remincross = true;\n"); -fprintf(fout2,"node [shape=rectangle, width=1.0, fontsize=22];\n"); -fprintf(fout2,"edge [fontsize=16];\n"); +fprintf(fout2,"node [shape=box, fontsize=18, margin=\"0.05,0\", height=0.3,rounded=true];\n"); +fprintf(fout2,"edge [fontsize=12];\n"); for i = 1:nedges fprintf(fout,"\"%s\" -> \"%s\" [ penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},2*scores1(i)); end diff --git a/sourcecodes/parameter_learning/drawFigure.m b/sourcecodes/parameter_learning/drawFigure.m index c48f5b2d..8255f6b9 100644 --- a/sourcecodes/parameter_learning/drawFigure.m +++ b/sourcecodes/parameter_learning/drawFigure.m @@ -1,4 +1,4 @@ -function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means) +function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,pre) %drawFigure writes the parameters and data that are needed to draw the %structure of a Bayesian network for BNW. % This is the function that is called to create the initial @@ -11,6 +11,10 @@ function [] = drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means) % % drawFigure is called by runBN_intial.m % +% This was modified to show the original distributions of continuous nodes +% as mixtures of Gaussian distributions instead of marginalizing the nodes. +% JZ, 7-15-2019 + A=cell2mat(cases'); Amax=max(A); @@ -26,6 +30,11 @@ fileID = fopen(filename,'w'); %%% The number of nodes fprintf(fileID,'%i\n',nnodes); +%Open file to write violin plot data + +violin_file = strcat(pre,'violin_orig.txt'); +vfile = fopen(violin_file,'w'); + %Get canvas size labels_temp = cellstr(labels); @@ -68,7 +77,6 @@ for i = 1:nnodes, end end - for i = 1:nnodes, %%% The name and type of each node (1=continuous, the number of states %%% if it is discrete @@ -131,22 +139,67 @@ for i = 1:nnodes, 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, + %% The name of the node for the violin plot data + if bnet.node_sizes(i) == 1; + fprintf(vfile,'Data for new node\n'); + fprintf(vfile,'%s\n',labels{i}); + end + + %The next line is marginal node method for continuous variables. + %%[x_vals,y_vals] = calcGaussian(predict.mu,predict.Sigma,Amax(i),Amin(i)); + %Instead get mixture of Gaussian distributions. + s=struct(bnet.CPD{i}); + no_gaussians = size(s.Wsum)(1); + Wsum = sum(s.Wsum); + weights = zeros(no_gaussians,1); + for j=1:no_gaussians, + weights(j) = s.Wsum(j)/Wsum; + end; + + + %Get random samples from normals to make violin plots + for j=1:no_gaussians + normrnd_count = int16(1000*weights(j)); + if normrnd_count > 0 + violin_data = normrnd(s.mean(j),sqrt(s.cov(j)),normrnd_count,1); + %violin_data = normrnd(s.mean(j),sqrt(s.cov(j)),1000,1); + for k = 1:size(violin_data) + fprintf(vfile,'%6.4f\n',violin_data(k)); + end + end + end + + + all_y = zeros(101,1); + for j=1:no_gaussians + [x_vals,y_vals] = calcGaussian(s.mean(j),sqrt(s.cov(j)),Amax(i),Amin(i)); + y_vals = y_vals*weights(j); + all_y = all_y + y_vals; + end; + + %%%For continuous nodes, print x and the pdf of curve. + for j = 1:101, + %Write data to make violin plots + %no_lines = int16(100*y_vals(j,1)); + %if no_lines > 0; + % for k = 1:no_lines + % fprintf(vfile,'%6.4f\n',x_vals(j,1)); + % end + %end %%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)); + %%fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),y_vals(j,1)); + fprintf(fileID,'%6.4f\t%6.4f\n',x_vals(j,1),all_y(j,1)); end; - end; + +end + end %fprintf(fileID,'%s\t %\n',labels_temp{:}); fclose(fileID); +fclose(vfile); end diff --git a/sourcecodes/parameter_learning/drawFigureM.m b/sourcecodes/parameter_learning/drawFigureM.m index 820f06dd..4a666ada 100644 --- a/sourcecodes/parameter_learning/drawFigureM.m +++ b/sourcecodes/parameter_learning/drawFigureM.m @@ -1,4 +1,4 @@ -function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata) +function [] = drawFigureM(nnodes,bnet,labels,filename,cases,stdevs,means,selectvar,selectdata,pre) %drawFigureM writes the parameters and data that are needed to draw the %structure of a Bayesian network after adding evidence or intervention %It creates the net_figure_new file after evidence/intervetion. @@ -51,6 +51,11 @@ y = 1 - y; y = y - min(y); [x_dim,y_dim] = canvasSize(nnodes,x,y); +%Open file to write violin plot data +violin_file = strcat(pre,'violin_evidence.txt'); +vfile = fopen(violin_file,'w'); + + %%% The dimensions of the canvas for the javascript code fprintf(fileID,'%i\t%i\t\n',x_dim,y_dim); x = x*x_dim; @@ -141,7 +146,22 @@ for i = 1:nnodes, 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)); + %% The name of the node for the violin plot data + if bnet.node_sizes(i) == 1; + fprintf(vfile,'Data for new node\n'); + fprintf(vfile,'%s\n',labels{i}); + end + + %Get random samples from normals to make violin plots + violin_data = normrnd(predict.mu,sqrt(predict.Sigma),1000,1); + %violin_data = normrnd(s.mean(j),sqrt(s.cov(j)),1000,1); + for k = 1:size(violin_data) + fprintf(vfile,'%6.4f\n',violin_data(k)); + end + + + + [x_vals,y_vals] = calcGaussian(predict.mu,sqrt(predict.Sigma),Amax(i),Amin(i)); %%%For continuous nodes, print x and the pdf of a normal curve. for j = 1:101, %%Undo standardization @@ -160,6 +180,7 @@ for i = 1:nnodes, end fclose(fileID); +fclose(vfile); end diff --git a/sourcecodes/parameter_learning/kfoldCrossValid.m b/sourcecodes/parameter_learning/kfoldCrossValid.m index 02f34866..40b72c61 100644 --- a/sourcecodes/parameter_learning/kfoldCrossValid.m +++ b/sourcecodes/parameter_learning/kfoldCrossValid.m @@ -117,7 +117,7 @@ end filename = strcat(pre,'kfoldCV.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %Calculate RMSEP (root mean square error of prediction) and q^2 %First, calculate TSS (total sum of squares) and @@ -136,19 +136,22 @@ end rmsep = sqrt(press/ncases); q_squared = 1 - press/tss; -%% Print rmseq and q^2 -fprintf(fileID,'RMS error of predictions: %6.4f\n',rmsep); -fprintf(fileID,'Q^2 of predictions: %6.4f\n\n',q_squared); +%% Print rmsep and q^2 +%fprintf(fileID,'RMS error of predictions: %6.4f\n',rmsep); +%fprintf(fileID,'Q^2 of predictions: %6.4f\n\n',q_squared); %%Print the predictions -fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); +%%fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); fprintf(fileID,'CaseRow\tFoldNumber\tActualValue\tPredictionMean\tPredictionStDev\n'); for i = 1:ncases fprintf(fileID,'%i\t%i\t%6.4f\t',i,kfold_index(i),case_adj(i)); fprintf(fileID,'%6.4f\t%6.4f\n',kfoldPredictions(i,:)); end +%%Print rmse and q^2 +fprintf(fileID,'k-fold CV for %s; RMSE= %6.4f; Q^2= %6.4f\n',predict_label,rmsep,q_squared); + fflush(fileID); fclose(fileID); @@ -267,14 +270,14 @@ accuracy = correct/ncases; filename = strcat(pre,'kfoldCV.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %%Print the accuracy -fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); +%fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); %%Print the predictions -fprintf(fileID,'Predicted likelihood of each state for each case:\n'); +%fprintf(fileID,'Predicted likelihood of each state for each case:\n'); fprintf(fileID,'%s\t%s\t%s\t','CaseRow','FoldNumber','ActualState'); fprintf(fileID,'%s\t',pred_levels{1:end-1}); fprintf(fileID,'%s\n',pred_levels{end}); @@ -286,6 +289,8 @@ for i = 1:ncases fprintf(fileID,'%6.4f\n',kfoldPredictions(i,end)); end +fprintf(fileID,'k-fold CV for %s; Fraction of accurate predictions= %6.4f\n',predict_label,accuracy); + fflush(fileID); fclose(fileID); diff --git a/sourcecodes/parameter_learning/looCrossValid.m b/sourcecodes/parameter_learning/looCrossValid.m index 840e486e..8cfffe79 100644 --- a/sourcecodes/parameter_learning/looCrossValid.m +++ b/sourcecodes/parameter_learning/looCrossValid.m @@ -2,7 +2,7 @@ function looCrossValid(pre,predict_label) % This function will peform leave-one-out cross-validation. % This requires the specification of the name of the variable % that you want to predict. - +% Modifying to output a table. Sept 2020 sfile=strcat(pre,'structure_input.txt'); @@ -93,7 +93,7 @@ end filename = strcat(pre,'looCV.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %Calculate RMSEP (root mean square error of prediction) and q^2 %First, calculate TSS (total sum of squares) and @@ -113,18 +113,17 @@ end rmsep = sqrt(press/ncases); q_squared = 1 - press/tss; -%% Print rmseq and q^2 -fprintf(fileID,'RMS error of predictions: %6.4f\n',rmsep); -fprintf(fileID,'Q^2 of predictions: %6.4f\n\n',q_squared); %%Print the predictions -fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); +%%fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); fprintf(fileID,'CaseRow\tActualValue\tPredictionMean\tPredictionStDev\n'); for i = 1:ncases fprintf(fileID,'%i\t%6.4f\t',i,case_adj(i)); fprintf(fileID,'%6.4f\t%6.4f\n',loopredictions(i,:)); end +%% Print rmse and q^2 +fprintf(fileID,'LOOCV predictions of %s; RMSE= %6.4f; Q^2= %6.4f\n',predict_label,rmsep,q_squared); fflush(fileID); fclose(fileID); @@ -232,14 +231,13 @@ accuracy = correct/ncases; filename = strcat(pre,'looCV.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %%Print the accuracy -fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); +%fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); %%Print the predictions -fprintf(fileID,'Predicted likelihood of each state for each case:\n'); fprintf(fileID,'%s\t%s\t','CaseRow','ActualState'); fprintf(fileID,'%s\t',pred_levels{1:end-1}); fprintf(fileID,'%s\n',pred_levels{end}); @@ -251,6 +249,8 @@ for i = 1:ncases fprintf(fileID,'%6.4f\n',loopredictions(i,end)); end +fprintf(fileID,'LOOCV predictions of %s; Fraction of accurate predictions= %6.4f\n',predict_label,accuracy); + fflush(fileID); fclose(fileID); diff --git a/sourcecodes/parameter_learning/modifyEdges.m b/sourcecodes/parameter_learning/modifyEdges.m index 2b336710..6542d9fb 100644 --- a/sourcecodes/parameter_learning/modifyEdges.m +++ b/sourcecodes/parameter_learning/modifyEdges.m @@ -2,132 +2,147 @@ function [ ] = modifyEdges( pre_old, pre_new ) % This function will allow users to add or delete edges from the network. % % Input: - % 1) pre_oldstructure_input.txt - % Original structure file. - % 2) pre_olddel_edge.txt - % A list of the edges that should be deleted. - % 3) pre_oldadd_edge.txt - % A list of the edges that should be added. + % pre_oldstructure_input.txt-- old structure file that is just used to make sure the node order is consistent + % pre_oldmodify_edge.txt-- file with network structure after modification % % Output: % pre_newstructure_input.txt-- structure file with edges added/deleted. + % pre_newstructure_input_temp.txt-- structure file with model averaging scores. + % September 16, 2020: I am only writing the file with model averaging scores + % if no edges were added to the network. % % open file for input, include error handling dfile=strcat(pre_old,'structure_input.txt'); - fin = fopen(dfile,'r'); if fin < 0 error(['Could not open ',dfile,' for input']); end -% Read in first line to get the number of nodes and the node labels. + +% open file for input, include error handling +mfile=strcat(pre_old,'modify_edge.txt'); +fin2 = fopen(mfile,'r'); +if fin2 < 0 + error(['Could not open ',mfile,' for input']); +end +% Read in first line to get the number of nodes +nnodes = str2num(fgetl(fin2)); + +% Read in first line of old structure file to get the node labels. buffer = fgetl(fin); %get header line as a string -nnodes = numel(strfind(buffer,"\t")); labels = cell(1,nnodes); for j=1:nnodes [next,buffer] = strtok(buffer); labels{j} = next; end -% Read in the edges -edges = cell(nnodes,nnodes); -for i = 1:nnodes - buffer = fgetl(fin); - for j = 1:nnodes - [next,buffer] = strtok(buffer); - edges{i,j} = next; - end +%Read in network structure information +buffer = fgetl(fin2); +buffer = buffer(2:end-1); +buffer = strrep(buffer,"\"",""); +labels2 = cell(1,nnodes); +for j=1:nnodes + [next,buffer] = strtok(buffer,","); + labels2{j} = next; end +nedges = str2num(fgetl(fin2)); +buffer = fgetl(fin2); +buffer = buffer(2:end-1); +buffer = strrep(buffer,"\"",""); +sources = cell(1,nedges); +for j=1:nedges + [next,buffer] = strtok(buffer,","); + sources{j} = str2num(next); +end -% open file with edges to be deleted -dedgefile=strcat(pre_old,'del_edge.txt'); -fin2 = fopen(dedgefile,'r'); -ndel=fskipl(fin2,Inf) - 1; -frewind(fin2); -delfrom = cell(1,ndel); -delto = cell(1,ndel); +buffer = fgetl(fin2); +buffer = buffer(2:end-1); +buffer = strrep(buffer,"\"",""); +targets = cell(1,nedges); +for j=1:nedges + [next,buffer] = strtok(buffer,","); + targets{j} = str2num(next); +end -buffer = fgetl(fin2); %get header line -for j=1:ndel - buffer = fgetl(fin2); %get line with actual variables - [next,buffer] = strtok(buffer); - delfrom{j} = next; - [next,buffer] = strtok(buffer); - delto{j} = next; +buffer = fgetl(fin2); +buffer = buffer(2:end-1); +weights = cell(1,nedges); +for j=1:nedges + [next,buffer] = strtok(buffer,","); + weights{j} = next; end -%get index of edges to delete -idel_from = []; -idel_to = []; -for j = 1:ndel - for k = 1:nnodes - if strcmp(labels{k},delfrom{j}) - idel_from = [idel_from;k]; - endif - if strcmp(labels{k},delto{j}) - idel_to = [idel_to;k]; + +%label_map is the index in "labels" that corresponds to each label in "labels2" +label_map = cell(1,nnodes); +for i = 1:nnodes + current = labels2{i}; + for j = 1:nnodes + if strcmp(current,labels{j}) + label_map{i} = j; endif end end -for i = 1:ndel - edges(idel_from(i),idel_to(i)) = "0"; -end -fclose(fin2); +edges_out = cell(nnodes,nnodes); +scores_out = cell(nnodes,nnodes); -% open file with edges to be added -aedgefile=strcat(pre_old,'add_edge.txt'); -fin3 = fopen(aedgefile,'r'); -nadd=fskipl(fin3,Inf) - 1; -frewind(fin3); +for i = 1:nedges + source_i = label_map{sources{i}}; + target_i = label_map{targets{i}}; + scores_out(source_i,target_i) = weights{i}; + edges_out(source_i,target_i) = "1"; +end -addfrom = cell(1,nadd); -addto = cell(1,nadd); -buffer = fgetl(fin3); %get header line -for j=1:nadd - buffer = fgetl(fin3); %get line with actual variables - [next,buffer] = strtok(buffer); - addfrom{j} = next; - [next,buffer] = strtok(buffer); - addto{j} = next; -end +tf = cellfun('isempty',edges_out); +edges_out(tf) = {"0"}; +scores_out(tf) = {"0"}; -%get index of edges to added -iadd_from = []; -iadd_to = []; -for j = 1:nadd - for k = 1:nnodes - if strcmp(labels{k},addfrom{j}) - iadd_from = [iadd_from;k]; - endif - if strcmp(labels{k},addto{j}) - iadd_to = [iadd_to;k]; - endif +test_score = 1; +for i=1:nnodes + for j=1:nnodes + if strcmp(scores_out{i,j},"1.1") + test_score = 0; + end end end -for i = 1:nadd - edges(iadd_from(i),iadd_to(i)) = "1"; -end - - - - -outfile = strcat(pre_new,'structure_input.txt'); +if test_score == 1 +outfile = strcat(pre_new,'structure_input_temp.txt'); fout = fopen(outfile,'w'); fprintf(fout,'%s\t',labels{1:end-1}); -fprintf(fout,'%s\n',labels{end}); +fprintf(fout,'%s\t\n',labels{end}); for i = 1:nnodes - fprintf(fout,'%s\t',edges{i,1:end-1}); - fprintf(fout,'%s\n',edges{i,end}); + fprintf(fout,'%s\t',scores_out{i,1:end-1}); + fprintf(fout,'%s\t\n',scores_out{i,end}); end fclose(fout); +end + + + +outfile2 = strcat(pre_new,'structure_input.txt'); +fout2 = fopen(outfile2,'w'); +fprintf(fout2,'%s\t',labels{1:end-1}); +fprintf(fout2,'%s\t\n',labels{end}); +for i = 1:nnodes + fprintf(fout2,'%s\t',edges_out{i,1:end-1}); + fprintf(fout2,'%s\t\n',edges_out{i,end}); +end +fclose(fout2); + + + + + + + end diff --git a/sourcecodes/parameter_learning/normpdf.m b/sourcecodes/parameter_learning/normpdf.m new file mode 100644 index 00000000..2b154f02 --- /dev/null +++ b/sourcecodes/parameter_learning/normpdf.m @@ -0,0 +1,50 @@ +function p = normpdf(x,m,s); +% Normal probability density function +% +% pdf = normpdf(x,m,s); +% +% Computes the PDF of a the normal distribution +% with mean m and standard deviation s +% default: m=0; s=1; +% x,m,s must be matrices of same size, or any one can be a scalar. +% +% see also: NORMCDF, NORMINV + +% Reference(s): + +% Version 1.28 Date: 23.Sep.2002 +% Copyright (c) 2000-2002 by Alois Schloegl + +% This program is free software; you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation; either version 2 of the License, or +% (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program; if not, write to the Free Software +% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +if nargin==1, + m=0;s=1; +elseif nargin==2, + s=1; +end; + +% allocate output memory and check size of argument +z = (x-m)./s; % if this line causes an error, input arguments do not fit. + +%p = ((2*pi)^(-1/2))*exp(-z.^2/2)./s; +SQ2PI = 2.5066282746310005024157652848110; +p = exp(-z.^2/2)./(s*SQ2PI); + +p((x==m) & (s==0)) = inf; + +p(isinf(z)~=0) = 0; + +p(isnan(x) | isnan(m) | isnan(s) | (s<0)) = nan; + diff --git a/sourcecodes/parameter_learning/normrnd.m b/sourcecodes/parameter_learning/normrnd.m new file mode 100644 index 00000000..0267ddf6 --- /dev/null +++ b/sourcecodes/parameter_learning/normrnd.m @@ -0,0 +1,130 @@ +## Copyright (C) 2012 Rik Wehbring +## Copyright (C) 1995-2012 Kurt Hornik +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} normrnd (@var{mu}, @var{sigma}) +## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r}) +## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r}, @var{c}, @dots{}) +## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, [@var{sz}]) +## Return a matrix of random samples from the normal distribution with +## parameters mean @var{mu} and standard deviation @var{sigma}. +## +## When called with a single size argument, return a square matrix with +## the dimension specified. When called with more than one scalar argument the +## first two arguments are taken as the number of rows and columns and any +## further arguments specify additional matrix dimensions. The size may also +## be specified with a vector of dimensions @var{sz}. +## +## If no size arguments are given then the result matrix is the common size of +## @var{mu} and @var{sigma}. +## @end deftypefn + +## Author: KH +## Description: Random deviates from the normal distribution + +function rnd = normrnd (mu, sigma, varargin) + + if (nargin < 2) + print_usage (); + endif + + if (!isscalar (mu) || !isscalar (sigma)) + [retval, mu, sigma] = common_size (mu, sigma); + if (retval > 0) + error ("normrnd: mu and sigma must be of common size or scalars"); + endif + endif + + if (iscomplex (mu) || iscomplex (sigma)) + error ("normrnd: MU and SIGMA must not be complex"); + endif + + if (nargin == 2) + sz = size (mu); + elseif (nargin == 3) + if (isscalar (varargin{1}) && varargin{1} >= 0) + sz = [varargin{1}, varargin{1}]; + elseif (isrow (varargin{1}) && all (varargin{1} >= 0)) + sz = varargin{1}; + else + error ("normrnd: dimension vector must be row vector of non-negative integers"); + endif + elseif (nargin > 3) + if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin))) + error ("normrnd: dimensions must be non-negative integers"); + endif + sz = [varargin{:}]; + endif + + if (!isscalar (mu) && !isequal (size (mu), sz)) + error ("normrnd: mu and sigma must be scalar or of size SZ"); + endif + + if (isa (mu, "single") || isa (sigma, "single")) + cls = "single"; + else + cls = "double"; + endif + + if (isscalar (mu) && isscalar (sigma)) + if (!isnan (mu) && !isinf (mu) && (sigma > 0) && (sigma < Inf)) + rnd = mu + sigma * randn (sz); + else + rnd = NaN (sz, cls); + endif + else + rnd = mu + sigma .* randn (sz); + k = isnan (mu) | isinf (mu) | !(sigma > 0) | !(sigma < Inf); + rnd(k) = NaN; + endif + +endfunction + + +%!assert(size (normrnd (1,2)), [1, 1]); +%!assert(size (normrnd (ones(2,1), 2)), [2, 1]); +%!assert(size (normrnd (ones(2,2), 2)), [2, 2]); +%!assert(size (normrnd (1, 2*ones(2,1))), [2, 1]); +%!assert(size (normrnd (1, 2*ones(2,2))), [2, 2]); +%!assert(size (normrnd (1, 2, 3)), [3, 3]); +%!assert(size (normrnd (1, 2, [4 1])), [4, 1]); +%!assert(size (normrnd (1, 2, 4, 1)), [4, 1]); + +%% Test class of input preserved +%!assert(class (normrnd (1, 2)), "double"); +%!assert(class (normrnd (single(1), 2)), "single"); +%!assert(class (normrnd (single([1 1]), 2)), "single"); +%!assert(class (normrnd (1, single(2))), "single"); +%!assert(class (normrnd (1, single([2 2]))), "single"); + +%% Test input validation +%!error normrnd () +%!error normrnd (1) +%!error normrnd (ones(3),ones(2)) +%!error normrnd (ones(2),ones(3)) +%!error normrnd (i, 2) +%!error normrnd (2, i) +%!error normrnd (1,2, -1) +%!error normrnd (1,2, ones(2)) +%!error normrnd (1, 2, [2 -1 2]) +%!error normrnd (1,2, 1, ones(2)) +%!error normrnd (1,2, 1, -1) +%!error normrnd (ones(2,2), 2, 3) +%!error normrnd (ones(2,2), 2, [3, 2]) +%!error normrnd (ones(2,2), 2, 2, 3) diff --git a/sourcecodes/parameter_learning/prepareInput.m b/sourcecodes/parameter_learning/prepareInput.m index 3147d54e..39368582 100644 --- a/sourcecodes/parameter_learning/prepareInput.m +++ b/sourcecodes/parameter_learning/prepareInput.m @@ -1,6 +1,5 @@ function [ ] = prepareInput( pre ) - % Jan. 2019: Modifying to allow for missing data. - % + % % 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. @@ -32,12 +31,13 @@ function [ ] = prepareInput( pre ) % 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 + % 3) ???input_table.txt Similar to above file, but in tabular format. + % 4) ???nlevels.txt: The states of discrete variables. + % 5) ???name.txt: The names of the variables as uploaded. + % 6) ???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 + % 7/8) ???nnode.txt and ???nrows.txt: number of nodes and cases + % 9-13) ???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. @@ -54,6 +54,7 @@ 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. @@ -65,6 +66,8 @@ for j=1:nnodes labels{j} = next; end + + % Read in the data data = cell(ncases,nnodes); for i = 1:ncases @@ -75,11 +78,25 @@ for i = 1:ncases end end +%Read one more line of the file to make sure that +% it does not contain a final line of data +buffer = fgetl(fin); +if length(buffer) > 1 + ncases = ncases + 1; + for j = 1:nnodes + [next,buffer] = strtok(buffer); + data{ncases,j} = next; + end +end + + + % Remove all rows that have missing data from data file remove_count = sum(any(strcmp(data,"NA"),2)); data(any(strcmp(data,"NA"),2),:)=[]; ncases = ncases - remove_count; + % 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); @@ -89,36 +106,54 @@ for j = 1:nnodes levels{j} = size(states{j},1); end + + +% Remove any variables that have a single variable +remove_labels = cell(1,nnodes); +remove = []; +for j = 1:nnodes + if levels{j} == 1 + remove_labels{j} = labels{j}; + remove(end+1) = j; + end +end + +nnodes = nnodes - size(remove,2); +data(:,remove)=[]; +labels(remove) = []; +levels(remove) = []; +states(:,remove) = []; + 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."; + reason{j} = "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."; + reason{j} = "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."; + reason{j} = "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."; + reason{j} = "Default classification"; 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(.)."; + reason{j} = "At least one value contained a period(.)."; levels{j} = 1; end k++; @@ -129,6 +164,8 @@ for j = 1:nnodes 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; @@ -175,7 +212,6 @@ if max_disc > min_cont 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'); @@ -241,14 +277,24 @@ for i = 1:nnodes end fclose(fout); - %Print a file with a short description of the input. descfile = strcat(pre,'input_desc.txt'); dout = fopen(descfile,'w'); +tablefile = strcat(pre,'input_table.txt'); +dout_table = fopen(tablefile,'w'); fprintf(dout,['As loaded, the input file had the following properties:\n\n']); -dout = fopen(descfile,'a'); +if size(remove,1) > 0 + fprintf(dout,['The following variables were removed because they contained only one value:\n']); + for j = 1:size(remove_labels,2) + if remove_labels{j}!= 1 + fprintf(dout,'%s\n',remove_labels{j}); + end + end + fprintf(dout,['\n']); +end fprintf(dout,'There are %i variables and %i cases(rows).\n',size(labels,2),ncases); fprintf(dout,'%i cases(rows) have been removed because they contained NA (missing data).\n',remove_count); +fprintf(dout_table,'Variable\tType\tStates/Mean (St Dev)\tReason for classification\n'); fprintf(dout,'The variable names are:\n'); fprintf(dout,'%s\t',labels{1:end-1}); fprintf(dout,'%s\n\n',labels{end}); @@ -259,16 +305,20 @@ for i=1:nnodes 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)) + fprintf(dout,'It has a mean of %6.3f and a standard deviation of %6.3f\n\n',mean(column),std(column)); + fprintf(dout_table,'%s\tContinuous\t%6.3f (%6.3f)\t%s\n',labels{i},mean(column),std(column),reason{i}); 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}); + fprintf(dout_table,'%s\tDiscrete\t%i\t%s\n',labels{i},levels{i},reason{i}); end end -fclose(fout); +fprintf(dout_table,'There are %i variables and %i cases; %i variables and %i rows were removed.\n',size(labels,2),ncases,size(remove,1),remove_count); +fclose(dout); +fclose(dout_table); outfile = strcat(pre,'continuous_input.txt'); fout = fopen(outfile,'w'); diff --git a/sourcecodes/parameter_learning/removeNodes.m b/sourcecodes/parameter_learning/removeNodes.m index 8740d999..45f09cb7 100644 --- a/sourcecodes/parameter_learning/removeNodes.m +++ b/sourcecodes/parameter_learning/removeNodes.m @@ -59,27 +59,34 @@ if fin2 < 0 dellabels = {}; ndel = 0; else - buffer = fgetl(fin2); %get header line as a string - ndel = numel(strfind(buffer," ")) + 1; - dellabels = cell(1,ndel); - for j=1:ndel - [next,buffer] = strtok(buffer); - dellabels{j} = next; + buffer = fgetl(fin2); %get first line as a string + if buffer < 0 + dellabels = {}; + ndel = 0; + else + ndel = numel(strfind(buffer," ")) + 1; + dellabels = cell(1,ndel); + for j=1:ndel + [next,buffer] = strtok(buffer); + dellabels{j} = next; + end end end %get index of variables to delete -delindex = []; -for j=1:nnodes - for k = 1:ndel - if strcmp(labels{j},dellabels{k}) - delindex = [delindex;j]; - endif - end -end +if ndel > 0 + delindex = []; + for j=1:nnodes + for k = 1:ndel + if strcmp(labels{j},dellabels{k}) + delindex = [delindex;j]; + endif + end + end -labels(:,[delindex])=[]; -data(:,[delindex])=[]; + labels(:,[delindex])=[]; + data(:,[delindex])=[]; +end outfile = strcat(pre_new,'continuous_input_orig.txt'); fout = fopen(outfile,'w'); diff --git a/sourcecodes/parameter_learning/runBN_initial.m b/sourcecodes/parameter_learning/runBN_initial.m index 16ce06dc..d87e468b 100644 --- a/sourcecodes/parameter_learning/runBN_initial.m +++ b/sourcecodes/parameter_learning/runBN_initial.m @@ -63,7 +63,7 @@ end filename=strcat(pre,'net_figure.txt'); -drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means); +drawFigure(nnodes,bnet,labels,filename,cases,stdevs,means,pre); writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m); diff --git a/sourcecodes/parameter_learning/testSetPredictions.m b/sourcecodes/parameter_learning/testSetPredictions.m index 23a50868..978326b0 100644 --- a/sourcecodes/parameter_learning/testSetPredictions.m +++ b/sourcecodes/parameter_learning/testSetPredictions.m @@ -4,7 +4,7 @@ function [ ] = testSetPredictions( pre ) % in the uploaded data file. % % - % Input: ???ts_input.txt + % Input: ???ts_upload.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 that you want to predict. @@ -157,7 +157,6 @@ for i=1:nnodes end else for j=1:ntestcases - labels{i} if !strcmp(data_test{j,i},"NA") for jj = 1:size(levels,1) if strcmp(labels{i},levels{jj,1}) @@ -179,7 +178,7 @@ end predict_cases = bnet.node_sizes(predict_node); if predict_cases == 1 - ts_continuous(pre,bnet,nnodes,predict_label,predict_node,data_test,means,stdevs) + ts_continuous(pre,bnet,nnodes,predict_label,predict_node,data_test,means,stdevs); else pred_levels = cell(1,predict_cases); for i = 1:disc_nodes @@ -190,7 +189,7 @@ else break end end - ts_discrete(pre,bnet,nnodes,predict_label,predict_node,data_test,pred_levels,predict_cases) + ts_discrete(pre,bnet,nnodes,predict_label,predict_node,data_test,pred_levels,predict_cases); end %delete(dfile) @@ -224,7 +223,7 @@ end filename = strcat(pre,'ts_output.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %Calculate RMSEP (root mean square error of prediction) and q^2 @@ -235,14 +234,11 @@ actual_values = []; predictions_removeNA = []; for i=1:ntestcases if !strcmp(data_test(i,predict_node),'NA') - actual_values = [actual_values, cell2num(data_test(i,predict_node))] - predictions_removeNA = [predictions_removeNA,predictions(i,1)] + actual_values = [actual_values, cell2num(data_test(i,predict_node))]; + predictions_removeNA = [predictions_removeNA,predictions(i,1)]; end end -size(actual_values) -size(predictions_removeNA) - average = mean(actual_values); average = average*stdevs{predict_node}+means{predict_node}; tss = 0; @@ -256,12 +252,12 @@ rmsep = sqrt(press/length(actual_values)); q_squared = 1 - press/tss; %% Print rmseq and q^2 -fprintf(fileID,'RMS error of predictions: %6.4f\n',rmsep); -fprintf(fileID,'Q^2 of predictions: %6.4f\n\n',q_squared); +%fprintf(fileID,'RMS error of predictions: %6.4f\n',rmsep); +%fprintf(fileID,'Q^2 of predictions: %6.4f\n\n',q_squared); %%Print the predictions -fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); +%fprintf(fileID,'Predicted mean and standard deviation for each case:\n'); fprintf(fileID,'CaseRow\tActualValue\tPredictionMean\tPredictionStDev\n'); for i = 1:ntestcases if strcmp(data_test{i,predict_node},"NA") @@ -273,6 +269,8 @@ for i = 1:ntestcases fprintf(fileID,'%6.4f\t%6.4f\n',predictions(i,:)); end +fprintf(fileID,'Test set predictions for %s; RMSE= %6.4f; Q^2= %6.4f\n',predict_label,rmsep,q_squared); + fflush(fileID); fclose(fileID); @@ -333,13 +331,13 @@ accuracy = correct/length(pred_states); filename = strcat(pre,'ts_output.txt'); fileID = fopen(filename,'w'); -fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); +%fprintf(fileID,'Variable that was predicted: %s\n\n',predict_label); %%Print the accuracy -fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); +%fprintf(fileID,'Fraction of accurate predictions: %6.4f\n\n',accuracy); %%Print the predictions -fprintf(fileID,'Predicted likelihood of each state for each case:\n'); +%fprintf(fileID,'Predicted likelihood of each state for each case:\n'); fprintf(fileID,'%s\t%s\t','CaseRow','ActualState'); fprintf(fileID,'%s\t',pred_levels{1:end-1}); fprintf(fileID,'%s\n',pred_levels{end}); @@ -355,6 +353,8 @@ for i = 1:ntestcases fprintf(fileID,'%6.4f\n',predictions(i,end)); end +fprintf(fileID,'Test set predictions of %s; Fraction of accurate predictions= %6.4f\n',predict_label,accuracy); + fflush(fileID); fclose(fileID); diff --git a/sourcecodes/parameter_learning/writeParameters.m b/sourcecodes/parameter_learning/writeParameters.m index 6efafe8a..cb119c76 100644 --- a/sourcecodes/parameter_learning/writeParameters.m +++ b/sourcecodes/parameter_learning/writeParameters.m @@ -5,7 +5,7 @@ function [] = writeParameters(pre,nnodes,bnet,labels,cases,labelsold,s,m) % for the network. % % writeParameters is called by runBN_intial.m - +% Simplifying output to read in and make table on webpage. %%Get the types of the nodes. typefile = strcat(pre,'type.txt'); @@ -76,15 +76,32 @@ for i = 1:nnodes 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); + CPD=struct(bnet.CPD{nodeid}); + no_gaussians = size(CPD.Wsum)(1); + if no_gaussians == 1 + %%%line = 'Continuous node\n'; + %%%fprintf(fileID,line); + adj_mu = CPD.mean*s(i)+m(i); + adj_sigma = s(i)*sqrt(CPD.cov); + fprintf(fileID,'Mean\tSt Dev\n'); + fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); + else + %%%%line = 'Continuous node modeled as a mixture of %i Gaussian distributions\n'; + %%%%fprintf(fileID,line,no_gaussians); + fprintf(fileID,'Mean\tSt Dev\tWeight\n'); + Wsum = sum(CPD.Wsum); + for j=1:no_gaussians + adj_mu = CPD.mean(j)*s(i)+m(i); + adj_sigma = s(i)*sqrt(CPD.cov(j)); + weight = CPD.Wsum(j)/Wsum; + fprintf(fileID,'%6.4f\t%6.4f\t%6.4f\n',adj_mu,adj_sigma,weight); + end + fprintf(fileID,'\n'); + end else - line = 'Discrete node with %i states\n'; - fprintf(fileID,line,bnet.node_sizes(nodeid)); + %%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; @@ -94,6 +111,7 @@ for i = 1:nnodes break end end + fprintf(fileID,'State\tProbability\n'); 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)); diff --git a/sourcecodes/parameter_learning/writeParameters_ev.m b/sourcecodes/parameter_learning/writeParameters_ev.m index 1f07c745..13524f26 100644 --- a/sourcecodes/parameter_learning/writeParameters_ev.m +++ b/sourcecodes/parameter_learning/writeParameters_ev.m @@ -97,21 +97,22 @@ for i = 1:nnodes end end %%%Print the name of the node - fprintf(fileID,'%s\n',labels{nodeid}); + fprintf(fileID,'%s considering evidence\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 = '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; + adj_sigma = stdevs{nodeid}*sqrt(predict.Sigma); + fprintf(fileID,'Mean\tSt Dev\n'); fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); else - line = 'Probability of states considering evidence:\n'; - fprintf(fileID,line); + %line = 'Probability of states considering evidence:\n'; + %fprintf(fileID,line); nodeid2 = 0; for k = 1:ndisc_nodes, if strcmp(levels{k,1},labels{nodeid}), @@ -119,6 +120,7 @@ for i = 1:nnodes break end end + fprintf(fileID,'State\tProbability\n'); 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)); @@ -128,10 +130,11 @@ for i = 1:nnodes end else if bnet.node_sizes(nodeid) == 1; - line = 'Evidence was observed for this node. The observed value was:\n'; - fprintf(fileID,line); + %%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); + fprintf(fileID,'Observed as\tValue\n'); + fprintf(fileID,'Evidence\t%6.4f\n\n',adj_mu); else nodeid2 = 0; for k = 1:ndisc_nodes, @@ -140,11 +143,12 @@ for i = 1:nnodes break end end - line = 'Evidence was observed for this node. The observed state was:\n'; - fprintf(fileID,line); + %%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}); + fprintf(fileID,'Observed as\tValue\n'); + fprintf(fileID,'Evidence\t %s\n\n',levels{nodeid2,state_ev+1}); end end end diff --git a/sourcecodes/parameter_learning/writeParameters_int.m b/sourcecodes/parameter_learning/writeParameters_int.m index 69dcbb93..98e4edf1 100644 --- a/sourcecodes/parameter_learning/writeParameters_int.m +++ b/sourcecodes/parameter_learning/writeParameters_int.m @@ -5,6 +5,7 @@ function [] = writeParameters_int(pre,bnet,nnodes,labels,cases,stdevs,means,sele % for the network. % % writeParameters is called by Predictmultipleintervention.m +% Modifying to simplify output to make a table on webpage. %First read input file to get node labels to get node IDs. infile = strcat(pre,'continuous_input.txt'); @@ -129,21 +130,22 @@ for i = 1:nnodes %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}); + fprintf(fileID,'%s considering intervention\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 = '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; + adj_sigma = stdevs{nodeid}*sqrt(predict.Sigma); + fprintf(fileID,"Mean\tSt Dev\n"); fprintf(fileID,'%6.4f\t%6.4f\n\n',adj_mu,adj_sigma); else - line = 'Probability of states considering intervention:\n'; - fprintf(fileID,line); + %line = 'Probability of states considering intervention:\n'; + %fprintf(fileID,line); nodeid2 = 0; for k = 1:ndisc_nodes, if strcmp(levels{k,1},labels{nodeid}), @@ -151,6 +153,7 @@ for i = 1:nnodes break end end + fprintf(fileID,"State\tProbability\n") 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)); @@ -160,10 +163,11 @@ for i = 1:nnodes end else if bnet.node_sizes(nodeid) == 1; - line = 'Intervention on this node assigned the following value:\n'; - fprintf(fileID,line); + %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); + fprintf(fileID,"Fixed by\tValue\n"); + fprintf(fileID,'Intervention\t%6.4f\n\n',adj_mu); else nodeid2 = 0; for k = 1:ndisc_nodes, @@ -172,11 +176,12 @@ for i = 1:nnodes break end end - line = 'Intervention on this node assigned the following state:\n'; - fprintf(fileID,line); + %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}); + fprintf(fileID,"Fixed by\tValue\n"); + fprintf(fileID,'Intervention\t%s\n\n',levels{nodeid2,state_ev+1}); end end end diff --git a/sourcecodes/parameter_learning/writeSettings.m b/sourcecodes/parameter_learning/writeSettings.m new file mode 100644 index 00000000..0c5ad865 --- /dev/null +++ b/sourcecodes/parameter_learning/writeSettings.m @@ -0,0 +1,200 @@ +function writeSettings(pre) + %function to write tables of settings used in structure learning + % so they can be reviewed by users + + +parfile = strcat(pre,'parent.txt'); +paropen = fopen(parfile,'r'); +par_val = fscanf(paropen,'%i'); + +kfile = strcat(pre,'k.txt'); +kopen = fopen(kfile,'r'); +k_val = fscanf(kopen,'%i'); + +thrfile = strcat(pre,'thr.txt'); +thropen = fopen(thrfile,'r'); +thr_val = fscanf(thropen,'%f'); + +filename = strcat(pre,'slsettings.txt'); +fileID = fopen(filename,'w'); +fprintf(fileID,'Structure learning settings\n'); +fprintf(fileID,'Parameter\tValue\n'); +fprintf(fileID,'Maximum number of parents\t%i\n',par_val); +fprintf(fileID,'Number of structures for model averaging\t%i\n',k_val); +fprintf(fileID,'Model averaging selection threshold\t%6.3f\n',thr_val); +fprintf(fileID,'\n'); + +tierfile = strcat(pre,'tier.txt'); +if exist(tierfile,'file') == 2 + tieropen = fopen(tierfile,'r'); + tiers = fscanf(tieropen,'%s'); + tiers = strsplit(",",tiers); + tiers(size(tiers,2)) = []; + fprintf(fileID,'Tier assignments\n'); + fprintf(fileID,'Variable\tTier\n'); + notiers = str2num(tiers{1}); + i = 1; + for j = 1:notiers + i = i + 1; + tiername = tiers{i}; + i = i + 1; + novars = str2num(tiers{i}); + if novars == 0 + fprintf(fileID,'No variables\t%s\n',tiername); + else + for k = 1:novars + i = i + 1; + fprintf(fileID,'%s\t%s\n',tiers{i},tiername); + end + end + end +fprintf(fileID,"\n"); +end + + +descfile = strcat(pre,'tierdesc.txt'); +%format of ???tierdesc.txt is: +%% Assume that there are n tiers: +%% For Tier i: +%% within tier interactions allowed: +%% (i-1)*n+1 true/false for yes, +%% (i-1)*n+2 true/false for no; (not needed, opposite of above) +%% Tier j (for 1 to n, with i != j) +%% Can j be parent of Tier i +%% Can j be child of Tier i +if exist(descfile,'file') == 2 + %%I am going to assume that if ???tierdesc.txt exists, then + %%% ???tiers.txt exists and notiers has been defined. + within = cell(notiers,1); + parents = cell(notiers,notiers); + children = cell(notiers,notiers); + within(:) = 0; + parents(:) = 0; + children(:) = 0; + descopen = fopen(descfile,'r'); + desc = fscanf(descopen,'%s'); + desc = strsplit(",",desc); + desc(size(desc,2)) = []; + for i = 1:notiers + %%Pull out the section of desc array for this tiers + desc_current = desc((i-1)*notiers*2+1:i*notiers*2); + for j = 1:notiers + if j < i + if strcmp(desc_current{j*2+1},'true') + parents{i,j} = j; + end + if strcmp(desc_current{j*2+2},'true') + children{i,j} = j; + end + end + if i == j + if strcmp(desc_current{1},'true') + within{i,1} = 1; + end + end + if j > i + if strcmp(desc_current{(j-1)*2+1},'true') + parents{i,j} = j; + end + if strcmp(desc_current{(j-1)*2+2},'true') + children{i,j} = j; + end + end + end + end + fprintf(fileID,"Within tier interactions\n"); + fprintf(fileID,"Tier\tAre within tier interaction allowed?\n"); + for i = 1:notiers + i1 = sprintf("%i",i); + if within{i,1} == 1 + fprintf(fileID,"Tier%s\tYes\n",i1); + else + fprintf(fileID,"Tier%s\tNo\n",i1); + end + end + fprintf(fileID,'\n'); + fprintf(fileID,'Allowed parents\n'); + fprintf(fileID,'Tier\tOther tiers containing potential parents\n'); + for i=1:notiers + lineout = ''; + for j=1:notiers + if parents{i,j} != 0 + j1 = sprintf("%i",parents{i,j}); + lineout = strcat(lineout,j1,',',{' '}); + end + end + col1 = sprintf("%i",i); + col1 = strcat("Tier",col1); + if size(lineout,1) > 0 + fprintf(fileID,'%s\t%s\n',col1,lineout{1,1}(1:end-2)); + else + fprintf(fileID,'%s\tNone\n',col1); + end + end + fprintf(fileID,'\n'); + fprintf(fileID,'Allowed children\n'); + fprintf(fileID,'Tier\tOther tiers containing potential children\n'); + for i=1:notiers + lineout = ''; + for j=1:notiers + if children{i,j} != 0 + j1 = sprintf("%i",children{i,j}); + lineout = strcat(lineout,j1,',',{' '}); + end + end + col1 = sprintf("%i",i); + col1 = strcat("Tier",col1); + if size(lineout,1) > 0 + fprintf(fileID,'%s\t%s\n',col1,lineout{1,1}(1:end-2)); + else + fprintf(fileID,'%s\tNone\n',col1); + end + end +end + +banfile = strcat(pre,'ban.txt'); +if exist(banfile,'file') == 2 + banopen = fopen(banfile,'r'); + ban = fscanf(banopen,'%s'); + ban = strsplit(",",ban); + ban(end) = []; + nban = size(ban,2)/2; + if nban > 0 + fprintf(fileID,'\n'); + fprintf(fileID,'Banned edges\n'); + fprintf(fileID,'From\tTo\n'); + for j=1:nban + j1 = (j-1)*2 + 1; + j2 = (j-1)*2 + 2; + fprintf(fileID,"%s\t%s\n",ban{1,j1},ban{1,j2}) + end + end +end + + + +whitefile = strcat(pre,'white.txt'); +if exist(whitefile,'file') == 2 + whiteopen = fopen(whitefile,'r'); + white = fscanf(whiteopen,'%s'); + white = strsplit(",",white); + white(end) = []; + nwhite = size(white,2)/2; + if nwhite > 0 + fprintf(fileID,'\n'); + fprintf(fileID,'Required edges\n'); + fprintf(fileID,'From\tTo\n'); + for j=1:nwhite + j1 = (j-1)*2 + 1; + j2 = (j-1)*2 + 2; + fprintf(fileID,"%s\t%s\n",white{1,j1},white{1,j2}) + end + end +end + + + + +fclose(fileID); + +end \ No newline at end of file -- cgit 1.4.1