00001 #ifndef BELIEFNETH
00002 #define BELIEFNETH
00003 
00008 #include "ExampleSpec.h"
00009 #include "Example.h"
00010 #include "../util/lists.h"
00011 
00012 
00047 typedef struct _BeliefNetNode_ {
00048    int numAllocatedRows;
00049    int numParentCombinations;
00050 
00051    
00052    
00053    float **eventCounts;
00054 
00055    
00056    float *rowCounts;
00057 
00058    
00059    float numSamples;
00060 
00061    ExampleSpecPtr spec;
00062    int attributeID;
00063 
00064    struct _BeliefNet_ *bn;
00065 
00066    VoidListPtr parentIDs;
00067    VoidListPtr childrenIDs;
00068    void *userData;
00069 
00070    
00071 
00072 
00073    void *tmpInternalData;
00074 } BeliefNetNodeStruct, *BeliefNetNode;
00075 
00076 
00077 
00078 
00079 void BNNodeFree(BeliefNetNode bnn);
00080 
00081 
00082 void BNNodeSetCPTFrom(BeliefNetNode target, BeliefNetNode source);
00083 
00084 
00085 BeliefNetNode BNNodeClone(BeliefNetNode bnn);
00086 
00091 void BNNodeAddParent(BeliefNetNode bnn, BeliefNetNode parent);
00092 
00093 
00098 int BNNodeLookupParentIndex(BeliefNetNode bnn, BeliefNetNode parent);
00099 
00104 int BNNodeLookupParentIndexByID(BeliefNetNode bnn, int id);
00105 
00110 void BNNodeRemoveParent(BeliefNetNode bnn, int parentIndex);
00111 
00113 BeliefNetNode BNNodeGetParent(BeliefNetNode bnn, int parentIndex);
00114 
00116 int BNNodeGetParentID(BeliefNetNode bnn, int parentIndex);
00117 
00119 int BNNodeGetNumParents(BeliefNetNode bnn);
00120 
00122 int BNNodeGetNumChildren(BeliefNetNode bnn);
00123 
00125 int BNNodeHasParent(BeliefNetNode bnn, BeliefNetNode parent);
00126 
00128 int BNNodeHasParentID(BeliefNetNode bnn, int parentID);
00129 
00130 void BNNodeAddValue(BeliefNetNode bnn, char *valueName);
00131 int BNNodeLookupValue(BeliefNetNode bnn, char *valueName);
00132 
00134 int BNNodeGetNumValues(BeliefNetNode bnn);
00135 
00137 int BNNodeGetNumParameters(BeliefNetNode bnn);
00138 
00140 char *BNNodeGetName(BeliefNetNode bnn);
00141 
00146 int BNNodeStructureEqual(BeliefNetNode bnn, BeliefNetNode otherNode);
00147 
00148 
00155 void BNNodeInitCPT(BeliefNetNode bnn);
00156 
00157 
00162 void BNNodeZeroCPT(BeliefNetNode bnn);
00163 
00172 void BNNodeFreeCPT(BeliefNetNode bnn);
00173 
00174 
00175 int BNNodeGetID(BeliefNetNode bnn);
00176 
00177 void BNNodeSetUserData(BeliefNetNode bnn, void *data);
00178 
00179 #define BNNodeGetUserData(bnn) (((BeliefNetNode)bnn)->userData)
00180 
00181 
00189 void  BNNodeAddSample(BeliefNetNode bnn, ExamplePtr e);
00190 
00192 void  BNNodeAddSamples(BeliefNetNode bnn, VoidListPtr samples);
00193 
00200 void  BNNodeAddFractionalSample(BeliefNetNode bnn, ExamplePtr e, float weight);
00201 
00203 void  BNNodeAddFractionalSamples(BeliefNetNode bnn, VoidListPtr samples, float weight);
00204 
00211 float BNNodeGetCPTRowCount(BeliefNetNode bnn, ExamplePtr e);
00212 
00213 
00224 float BNNodeGetP(BeliefNetNode bnn, int value);
00225 void  BNNodeSetCPIndexed(BeliefNetNode bnn, int row, int value,
00226                                                       float probability);
00227 float BNNodeGetCPIndexed(BeliefNetNode bnn, int row, int value);
00228 
00235 float BNNodeGetCP(BeliefNetNode bnn, ExamplePtr e);
00236 
00249 void  BNNodeSetCP(BeliefNetNode bnn, ExamplePtr e, float probability);
00250 
00251 
00252 
00253 
00254 
00255 void BNNodeSetPriorStrength(BeliefNetNode bnn, double strength);
00256 void BNNodeSmoothProbabilities(BeliefNetNode bnn, double strength);
00257 
00258 void BNNodeSetRandomValueGivenParents(BeliefNetNode bnn, ExamplePtr e);
00259 
00260 
00262 float BNNodeGetNumSamples(BeliefNetNode bnn);
00263 
00265 int BNNodeGetNumCPTRows(BeliefNetNode bnn);
00266 
00278 typedef struct _BeliefNet_ {
00279    char *name;
00280    ExampleSpecPtr spec;
00281    VoidListPtr nodes;
00282 
00283    
00284    VoidListPtr topoSort;
00285    int hasCycle;
00286 
00287    void *userData;
00288 } BeliefNetStruct, *BeliefNet;
00289 
00291 BeliefNet BNNew(void);
00292 
00294 void BNFree(BeliefNet bn);
00295 
00297 BeliefNet BNClone(BeliefNet bn);
00298 
00300 BeliefNet BNCloneNoCPTs(BeliefNet bn);
00301 
00310 BeliefNet BNNewFromSpec(ExampleSpecPtr es);
00311 
00312 int BNStructureEqual(BeliefNet bn, BeliefNet otherNet);
00313 
00321 int BNGetSimStructureDifference(BeliefNet bn, BeliefNet otherNet);
00322 
00328 void BNSetName(BeliefNet bn, char *name);
00329 
00330 
00331 
00337 ExampleSpec *BNGetExampleSpec(BeliefNet bn);
00338 
00339 
00340 
00341 
00342 
00343 
00344 BeliefNetNode BNNewNode(BeliefNet bn, char *nodeName);
00345 
00346 
00347 
00348 
00349 BeliefNetNode BNLookupNode(BeliefNet bn, char *name);
00350 
00354 BeliefNetNode BNGetNodeByID(BeliefNet bn, int id);
00355 
00357 int BNGetNumNodes(BeliefNet bn);
00358 
00370 BeliefNetNode BNGetNodeByElimOrder(BeliefNet bn, int index);
00371 
00379 int BNHasCycle(BeliefNet bn);
00380 
00392 void BNFlushStructureCache(BeliefNet bn);
00393 
00394 
00395 void BNZeroCPTs(BeliefNet bn);
00396 
00397 
00398 
00399 
00405 void BNAddSample(BeliefNet bn, ExamplePtr e);
00406 
00408 void BNAddSamples(BeliefNet bn, VoidListPtr samples);
00409 
00415 void BNAddFractionalSample(BeliefNet bn, ExamplePtr e, float weight);
00416 
00418 void BNAddFractionalSamples(BeliefNet bn, VoidListPtr samples, float weight);
00419 
00420 
00421 float BNGetLogLikelihood(BeliefNet bn, ExamplePtr e);
00422 
00429 long BNGetNumIndependentParameters(BeliefNet bn);
00430 
00432 long BNGetNumParameters(BeliefNet bn);
00433 
00435 long BNGetMaxNodeParameters(BeliefNet bn);
00436 
00437 
00443 ExamplePtr BNGenerateSample(BeliefNet bn);
00444 
00454 void BNSetPriorStrength(BeliefNet bn, double strength);
00455 
00461 void BNSmoothProbabilities(BeliefNet bn, double strength);
00462 
00467 void BNSetUserData(BeliefNet bn, void *data);
00468 
00469 
00474 #define BNGetUserData(bn) (((BeliefNet)bn)->userData)
00475 
00481 BeliefNet BNReadBIF(char *fileName);
00482 
00483 
00489 BeliefNet BNReadBIFFILEP(FILE *file);
00490 
00498 void BNWriteBIF(BeliefNet bn, FILE *out);
00499 
00504 void BNPrintStats(BeliefNet bn);
00505 
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 
00514 void BNNodeCopyCPT(BeliefNetNode bnn, float ***eventCounts, float **rowCounts);
00515 
00516 void BNNodeRestoreCPT(BeliefNetNode bnn, float **eventCounts, float *rowCounts);
00517 
00518 void BNNodeAllocateCPT(BeliefNetNode bnn, float ***eventCounts, float **rowCounts);
00519 
00520 void BNNodeFreeCopiedCPT(BeliefNetNode bnn, float **eventCounts, float *rowCounts);
00521 
00522 void BNNodeRemoveParentByID(BeliefNetNode bnn, int parentID);
00523 void BNNodeRemoveEdge(BeliefNetNode parent, BeliefNetNode child);
00524 
00525 
00526 
00527 void  BNNodeShrink(BeliefNetNode bnn, VoidListPtr data, int doDirichlet);
00528 void BNNodeTruncateToNParents(BeliefNetNode bnn, VoidListPtr data, 
00529                                int maxParents);
00530 
00531 void BNRemoveNode(BeliefNet bn, BeliefNetNode node);
00532 
00533 void BNNodeCPTGetP(BeliefNetNode bnn, ExamplePtr e, double *p);
00534 double BNNodeCPTGetCP(BeliefNetNode bnn, ExamplePtr e);
00535 void BNNodeGetJointProb(BeliefNetNode parent, BeliefNetNode child, double p[2][2]);
00536 
00554 BeliefNet BNInitLikelihoodSampling(BeliefNet bn, ExamplePtr e);
00555 
00562 void BNAddLikelihoodSamples(BeliefNet bn, BeliefNet newNet, ExamplePtr e, int numSamples);
00563 
00565 BeliefNet BNLikelihoodSampleNTimes(BeliefNet bn, ExamplePtr e, int numSamples);
00569 #endif