Main Page | Modules | Data Structures | File List | Globals | Related Pages

BeliefNet.h

Go to the documentation of this file.
00001 #ifndef BELIEFNETH
00002 #define BELIEFNETH
00003 
00008 #include "ExampleSpec.h"
00009 #include "Example.h"
00010 #include "../util/lists.h"
00011 /*#include <stdio.h>*/
00012 
00047 typedef struct _BeliefNetNode_ {
00048    int numAllocatedRows;
00049    int numParentCombinations;
00050 
00051    // indexed first by parents in order in counting order
00052    //  second index on value for this variable
00053    float **eventCounts;
00054 
00055    // the number of times this parent combination occured.
00056    float *rowCounts;
00057 
00058    // This is the sum of all the rowCounts;
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    /* used internally by graph algorithms or inference algorithms,
00071        do not use it for things you want preserved across calls,
00072        use userData for that */
00073    void *tmpInternalData;
00074 } BeliefNetNodeStruct, *BeliefNetNode;
00075 
00076 
00077 //BeliefNetNode BNNodeNew(void);
00078 
00079 void BNNodeFree(BeliefNetNode bnn);
00080 
00081 // NOTE! Both nodes better have CPTs allocated and they better be the same
00082 void BNNodeSetCPTFrom(BeliefNetNode target, BeliefNetNode source);
00083 
00084 // Note: this does NOT clone user data, merely copies the pointer
00085 BeliefNetNode BNNodeClone(BeliefNetNode bnn);
00086 
00091 void BNNodeAddParent(BeliefNetNode bnn, BeliefNetNode parent);
00092 //void BNNodeAddChild(BeliefNetNode bnn, BeliefNetNode child);
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 // returns the attribute id of the node (once the node's identity is set)
00175 int BNNodeGetID(BeliefNetNode bnn);
00176 
00177 void BNNodeSetUserData(BeliefNetNode bnn, void *data);
00178 //inline void *BNNodeGetUserData(BeliefNetNode bnn) {return bnn->userData;}
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 // Reset all row counts to 'strength'.  This effectively sets the confidence
00252 //  in the current probabilities to be equal to what you would have after
00253 //  seeing 'strength' samples which add up to the current probabilities.
00254 //  (which may not be possible, but that's ok!)
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    /* these next two contain cached values, will be 0 and -1 till filled */
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 // Needed for reading and writing examples that will work with the net
00331 
00337 ExampleSpec *BNGetExampleSpec(BeliefNet bn);
00338 
00339 //void BNSetUserData(BeliefNet bn, int nodeID, void *data);
00340 //void *BNGetUserData(BeliefNet bn, int nodeID);
00341 
00342 //void BNAddNode(BeliefNet bn, BeliefNetNode bnn, char *nodeName);
00343 
00344 BeliefNetNode BNNewNode(BeliefNet bn, char *nodeName);
00345 
00346 /* Looks up the node by name.
00347 
00348 This is an O(N) operation. If it isn't found returns NULL. */
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 /* \brief Calls BNNodeZeroCPT on every node in the network. */
00395 void BNZeroCPTs(BeliefNet bn);
00396 
00397 //void BNPrint(BeliefNet bn, FILE *out);
00398 //void BNPrintStats(BeliefNet bn, FILE *out);
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 /* \brief Returns the loglikelihood of the example given the network and parameters. */
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 // void *BNGetUserData(BeliefNet bn) {return bn->userData;}
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   Mattr added below here and in .c.
00511   Geoff, if you approve, I guess move up where they belong
00512 **************************************************/
00513 // Allocates and fills in eventCounts and rowCounts
00514 void BNNodeCopyCPT(BeliefNetNode bnn, float ***eventCounts, float **rowCounts);
00515 // Restores from eventCounts and rowCounts back into the node
00516 void BNNodeRestoreCPT(BeliefNetNode bnn, float **eventCounts, float *rowCounts);
00517 // Just allocates
00518 void BNNodeAllocateCPT(BeliefNetNode bnn, float ***eventCounts, float **rowCounts);
00519 // Frees the result of one of the above two functions
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 // If doDirichlet, then the event and rowCounts will be dirichlet parameters
00526 //  otherwise, they will be shrunken event and rowCounts
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 /* BELIEFNETH */

Generated for VFML by doxygen hosted by SourceForge.net Logo