1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
ncases = 500;
fprintf (stdout, "\nTesting BayesianGraphicalModel discrete graph functionality\n\n");
ExecuteAFile (HYPHY_LIB_DIRECTORY+"TemplateBatchFiles"+DIRECTORY_SEPARATOR+"bayesgraph.ibf");
fprintf (stdout, "Loaded bayesgraph include file\n");
/* import Bayesian network structure and parameters
from XMLBIF (XML Bayesian Interchange Format)
as an associative list
*/
fprintf (stdout, "Import ALARM network from XMLBIF file...");
import_xmlbif ("alarm.xml", "Alarm");
if ( (Rows(Alarm))[0] == "Hypovolemia") {
fprintf (stdout, "[PASSED]\n");
} else {
fprintf (stdout, "[FAILED]\n");
}
// adjacency matrix of network
adjmat = list2adjmat(Alarm);
/* this object contains all the info we need to simulate data */
fprintf (stdout, "Simulate ", ncases, " cases from network object...");
sim = simulate_data (Alarm, ncases);
if (Rows(sim) == ncases) {
fprintf (stdout, "[PASSED]\n");
} else {
fprintf (stdout, "[FAILED]\n");
}
/* keys of associative list are variable (node) names */
names = Rows(Alarm);
/* a Bayesian Graphical Model object in HyPhy is constructed with
a single (associative list) argument
*/
nodes={};
for (i = 0; i < Abs(Alarm); i=i+1)
{
/* add_discrete_node ( node name,
max. # parents,
prior sample size,
# levels)
*/
nodes[Abs(nodes)] = add_discrete_node (names[i], 2, 0, (Alarm[names[i]])["Levels"]);
}
num_nodes = Abs(nodes);
/* construct BGM */
fprintf (stdout, "Create BGM object...\n");
BayesianGraphicalModel alarm_bgm = (nodes);
GetString (bgm_names_list, BayesianGraphicalModel, -1); // returns names of all BGMs
// THIS TEST IS BROKEN
/*
lLength = Rows(bgm_names_list) * Columns(bgm_names_list);
for (_i = 0; _i < lLength; _i += 1) {
if (bgm_names_list[_i] == "alarm_bgm") {
fprintf (stdout, "[PASSED]\n");
break;
}
}
if (_i == lLength) {
fprintf (stdout, "[FAILED]\n");
}
*/
/*
Assign data set to BGM.
attach_data ( BGM identifier,
data matrix,
Gibbs imputation #steps,
" " burnin,
" " #samples)
*/
fprintf (stdout, "Attaching data and caching node scores...");
attach_data ("alarm_bgm", sim, 0, 0, 0);
cache = get_node_score_cache("alarm_bgm");
if (Abs(cache) == 111) {
fprintf (stdout, "[PASSED]\n");
} else {
fprintf (stdout, "[FAILED]\n");
}
/* graph structural MCMC */
fprintf (stdout, "RUNNING GRAPH-MCMC\n");
result0 = graph_MCMC ("alarm_bgm", 100000, 100000, 100, 0);
temp = check_edgelist (result0, adjmat, 0.8);
fprintf (stdout, "\tTrue positives = ", temp[0], "\n");
fprintf (stdout, "\tFalse negatives = ", temp[1], "\n");
fprintf (stdout, "\tFalse positives = ", temp[2], "\n");
fprintf (stdout, "\tTrue negatives = ", temp[3], "\n");
sens = temp[0]/(temp[0]+temp[1]);
spec = temp[3]/(temp[2]+temp[3]);
fprintf (stdout, "\tSensitivity (TP/TP+FN) = ", sens, "\n");
fprintf (stdout, "\tSpecificity (TN/TN+FP) = ", spec, "\n");
fprintf (stdout, "Specificity > 75% and specificity > 90% for cutoff = 0.8 ... ");
if (sens > 0.75 && spec > 0.9) {
fprintf (stdout, "[PASSED]\n");
} else {
fprintf (stdout, "[FAILED]\n");
}
display_MCMC_chain (result0);
write_edgelist("TestBGM.graphMCMC.edges", result0, num_nodes, 1);
mcmc_graph_to_dotfile("TestBGM.graphMCMC.dot", 0.6, result0, nodes);
/* node order permutation MCMC */
fprintf (stdout, "RUNNING ORDER-MCMC\n");
result1 = order_MCMC ("alarm_bgm", 10000, 10000, 100);
temp = check_edgelist (result1, adjmat, 0.8);
fprintf (stdout, "\tTrue positives = ", temp[0], "\n");
fprintf (stdout, "\tFalse negatives = ", temp[1], "\n");
fprintf (stdout, "\tFalse positives = ", temp[2], "\n");
fprintf (stdout, "\tTrue negatives = ", temp[3], "\n");
sens = temp[0]/(temp[0]+temp[1]);
spec = temp[3]/(temp[2]+temp[3]);
fprintf (stdout, "\tSensitivity (TP/TP+FN) = ", sens, "\n");
fprintf (stdout, "\tSpecificity (TN/TN+FP) = ", spec, "\n");
fprintf (stdout, "Specificity > 75% and specificity > 90% for cutoff = 0.8 ... ");
if (sens > 0.75 && spec > 0.9) {
fprintf (stdout, "[PASSED]\n");
} else {
fprintf (stdout, "[FAILED]\n");
}
write_edgelist("TestBGM.orderMCMC.edges", result1, num_nodes, 1);
mcmc_graph_to_dotfile("TestBGM.orderMCMC.dot", 0.6, result1, nodes);
|