Skip to content

Instantly share code, notes, and snippets.

@waltherg
Last active November 19, 2017 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waltherg/cf6f4f4271e9f90afa0d080cf276a9bc to your computer and use it in GitHub Desktop.
Save waltherg/cf6f4f4271e9f90afa0d080cf276a9bc to your computer and use it in GitHub Desktop.
Bayesian Hypothesis Testing
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