Created
January 3, 2014 23:13
-
-
Save ahalbert/8248544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function BayesNet(vars) { | |
| this.variables = {}; | |
| this.numVars = Object.keys(this.variables).length; | |
| for (v in vars) { | |
| this.variables[v] = new BayesNode(vars[v]); | |
| this.variables[v].CPTorder = this.generateDomainRows(vars[v].parents); | |
| this.variables[v].fullCPT = {}; | |
| this.variables[v].blocks = false; | |
| } | |
| for (v in vars) { | |
| // for (var i = 0; i < this.variables[v].CPTorder.length; i++) { | |
| // this.variables[v].fullCPT[this.variables[v].CPTorder[i]] = this.variables[v].CPT[i]; | |
| // } | |
| } | |
| } | |
| function BayesNode(obj) { | |
| this.children = obj.children; | |
| this.parents = obj.pars; | |
| if (typeof obj.domain == 'undefined') | |
| this.domain = ['T','F']; | |
| else | |
| this.domain = obj.domain; | |
| this.observation = obj.observation; | |
| this.CPT = obj.CPT; | |
| this.sampleDistribution = []; | |
| for (var i = 0; i < this.CPT.length; i++) { | |
| var s = []; | |
| if(this.CPT[i].length == this.domain.length - 1) | |
| this.CPT[i].push(1 - sumArray(this.CPT[i])); | |
| s.push(this.CPT[i][0]); | |
| for (var j = 1; j < this.domain.length - 1; j++) { | |
| s.push(this.CPT[i][j]+s[j-1]); | |
| } | |
| s.push(1.0); | |
| this.sampleDistribution.push(s); | |
| } | |
| //TODO: Check if CPT is valid | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment