Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active August 29, 2015 14:01
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 kenwebb/575357d9a4c8dc0bf4e3 to your computer and use it in GitHub Desktop.
Save kenwebb/575357d9a4c8dc0bf4e3 to your computer and use it in GitHub Desktop.
Bayesian inference 3 - using x and θ
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Fri May 09 2014 12:10:02 GMT-0400 (EDT)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Bayesian inference 3 - using x and θ
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: 575357d9a4c8dc0bf4e3
Keywords:
My Notes
--------
This is associated with XholonWorkbook ceb4083319ebfa4a7bd0 and c48e278ac02dce038398
Dr James V Stone of the Psychology Department at Sheffield University in the UK, has written a book called "Bayes' Rule - A Tutorial Introduction to Bayesian Analysis". The first chapter[1] includes an extensive introductory example called "Poxy Diseases", and sections on "a more succinct notation".
References
----------
(1) http://jim-stone.staff.shef.ac.uk/BookBayes2012/bookbayesch01.pdf
(2) http://en.wikipedia.org/wiki/Bayesian_inference
]]></Notes>
<_-.XholonClass>
<BayesianSystem/>
<!-- types of objects (entities) in the statistician Thomas Bayes' domain [2] -->
<BayesianEntity>
<HypothesisBE> <!-- H "H stands for any hypothesis whose probability may be affected by data (called evidence below). Often there are competing hypotheses, from which one chooses the most probable." -->
<Smallpox/> <!-- θ_s hypothesis that the patient's disease is smallpox -->
<Chickenpox/> <!-- θ_c hypothesis that the patient's disease is chickenpox -->
</HypothesisBE>
<EvidenceBE> <!-- E data "the evidence E corresponds to new data that were not used in computing the prior probability" -->
<Spots/> <!-- x evidence that the patient's symptoms are spots -->
</EvidenceBE>
<AntecedentBE>
<PriorProbabilityBE/> <!-- P(H) "the prior probability, is the probability of H before E is observed. This indicates one's previous estimate of the probability that a hypothesis is true, before gaining the current evidence." -->
<LikelihoodBE/> <!-- P(E|H) "the probability of observing E given H. As a function of E with H fixed, this is the likelihood. The likelihood function should not be confused with P(H | E) as a function of H rather than of E. It indicates the compatibility of the evidence with the given hypothesis." -->
</AntecedentBE>
<PosteriorProbabilityBE/> <!-- P(H|E) "the posterior probability, is the probability of H given E, i.e., after E is observed. This tells us what we want to know: the probability of a hypothesis given the observed evidence." -->
<MarginalLikelihoodBE/> <!-- P(E) "P(E) is sometimes termed the marginal likelihood or 'model evidence'. This factor is the same for all possible hypotheses being considered. (This can be seen by the fact that the hypothesis H does not appear anywhere in the symbol, unlike for all the other factors.) This means that this factor does not enter into determining the relative probabilities of different hypotheses." -->
<PosteriorRatioBE/> <!-- ratio between two (or more?) posterior probabilities -->
<!-- collections, containers -->
<HypothesesBE/>
<EvidencesBE/> <!-- symptoms -->
<ModelsBE/> <!-- diseases, a set of candidate models -->
<ModelBE/> <!-- disease -->
</BayesianEntity>
</_-.XholonClass>
<xholonClassDetails>
<LikelihoodBE>
<port name="evidence" connector="../../../EvidencesBE/Spots"/>
<port name="hypothesis" connector=".[@roleName='p(x|θ_c)']/../../../HypothesesBE/Chickenpox"/>
<port name="hypothesis" connector=".[@roleName='p(x|θ_s)']/../../../HypothesesBE/Smallpox"/>
</LikelihoodBE>
<PriorProbabilityBE>
<port name="hypothesis" connector=".[@roleName='p(θ_c)']/../../../HypothesesBE/Chickenpox"/>
<port name="hypothesis" connector=".[@roleName='p(θ_s)']/../../../HypothesesBE/Smallpox"/>
</PriorProbabilityBE>
<MarginalLikelihoodBE>
<port name="evidence" connector="../../../EvidencesBE/Spots"/>
</MarginalLikelihoodBE>
<PosteriorProbabilityBE>
<port name="hypothesis" connector=".[@roleName='p(θ_c|x)']/../../../HypothesesBE/Chickenpox"/>
<port name="hypothesis" connector=".[@roleName='p(θ_s|x)']/../../../HypothesesBE/Smallpox"/>
<port name="evidence" connector="../../../EvidencesBE/Spots"/>
</PosteriorProbabilityBE>
</xholonClassDetails>
<BayesianSystem>
<!-- see Figure 1.4 [1] -->
<HypothesesBE>
<Chickenpox/> <!-- θ_c -->
<Smallpox/> <!-- θ_s -->
</HypothesesBE>
<EvidencesBE>
<Spots/> <!-- x -->
</EvidencesBE>
<ModelsBE>
<!-- the goal is to select the most probable of these candidate models, this is called 'model selection' -->
<ModelBE>
<LikelihoodBE roleName="p(x|θ_c)" val="0.8"/>
<PriorProbabilityBE roleName="p(θ_c)" val="0.1"/>
<MarginalLikelihoodBE roleName="p(x)" val="0.081"/>
<PosteriorProbabilityBE roleName="p(θ_c|x)"/> <!-- calculated result should be 0.988 -->
</ModelBE>
<ModelBE>
<LikelihoodBE roleName="p(x|θ_s)" val="0.9"/> <!-- maximum likelihood estimate (MLE) -->
<PriorProbabilityBE roleName="p(θ_s)" val="0.001"/>
<MarginalLikelihoodBE roleName="p(x)" val="0.081"/>
<PosteriorProbabilityBE roleName="p(θ_s|x)"/> <!-- calculated result should be 0.011 -->
</ModelBE>
</ModelsBE>
<PosteriorRatioBE/>
</BayesianSystem>
<PosteriorProbabilityBEbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var posterior;
var beh = {
postConfigure: function() {
posterior = this.cnode.parent();
var likelihood = posterior.parent().first();
var prior = likelihood.next();
var marginalLikelihood = prior.next();
posterior.val((likelihood.val() * prior.val() / marginalLikelihood.val()));
posterior.println(posterior.xhc().name() + " " + posterior.role() + " = "
+ posterior.val().toFixed(3));
}
}
]]></PosteriorProbabilityBEbehavior>
<PosteriorRatioBEbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var pratio;
var beh = {
postConfigure: function() {
pratio = this.cnode.parent();
var pprob1 = pratio.xpath("../ModelsBE/ModelBE[1]/PosteriorProbabilityBE");
var pprob2 = pratio.xpath("../ModelsBE/ModelBE[2]/PosteriorProbabilityBE");
pratio.val(pprob1.val() / pprob2.val());
pratio.println(pratio.xhc().name() + " " + pprob1.role() + " / "
+ pprob2.role() + " = " + pratio.val().toFixed(1));
pratio.println("In summary:\n It's about " + Math.ceil(pratio.val())
+ " times more probable that the patient has " + pprob1.hypothesis.xhc().name()
+ " rather than " + pprob2.hypothesis.xhc().name() + ".");
}
}
]]></PosteriorRatioBEbehavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Diseases</title>
<rect id="BayesianSystem/ModelsBE" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>Smallpox</title>
<rect id="BayesianSystem/HypothesesBE/Smallpox" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment