Last active
November 19, 2017 19:28
-
-
Save waltherg/cf6f4f4271e9f90afa0d080cf276a9bc to your computer and use it in GitHub Desktop.
Bayesian Hypothesis Testing
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 Pmf(domain){ | |
this.domain = {} | |
for(var i = 0; i < domain.length; i++){ | |
value = domain[i][0]; | |
probability = domain[i][1]; | |
this.domain[value] = probability; | |
} | |
} | |
Pmf.prototype.normalize = function(){ | |
total = 0; | |
keys = Object.keys(this.domain); | |
for(var i = 0; i < keys.length; i++){ | |
mass = this.domain[keys[i]]; | |
total += mass; | |
} | |
for(var i = 0; i < keys.length; i++){ | |
this.domain[keys[i]] /= total; | |
} | |
}; | |
Pmf.prototype.print = function(id){ | |
inner_html = document.querySelector(id).innerHTML; | |
keys = Object.keys(this.domain); | |
for(var i = 0; i < keys.length; i++){ | |
inner_html += keys[i] + ": " + Math.round(this.domain[keys[i]] * 100) / 100; | |
inner_html += "<br/>"; | |
} | |
document.querySelector(id).innerHTML = inner_html; | |
}; | |
function Hypotheses(hypotheses, id){ | |
let this_hypotheses = new Pmf(hypotheses); | |
this.hypotheses = this_hypotheses; | |
this.hypotheses.normalize(); | |
this.id = id; | |
} | |
Hypotheses.prototype.print = function(){ | |
document.querySelector(this.id).innerHTML += "Hypthesis: Probability<br/>"; | |
this.hypotheses.print(this.id); | |
}; | |
Hypotheses.prototype.update = function(datum){ | |
keys = Object.keys(this.hypotheses.domain); | |
for(var i = 0; i < keys.length; i++){ | |
likelihood = this.get_likelihood(keys[i], datum); | |
this.hypotheses.domain[keys[i]] *= likelihood; | |
} | |
this.hypotheses.normalize(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment