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