I would like to take some slight shortcuts with regard to Romke's excellent solution while making use of the comfortable features of Mathematica's statistical framework.
Prior
$\theta|I \sim Beta(6,14)$
We can interpret this (conjugate) prior as having seen (or believing in) 18 Bernoulli-trials and having observed 5 successes and 13 failures before making the actual experiment (part of our background information $I$) and code this as:
priorDist = With[ { a = 6, b = 14 }, BetaDistribution[a, b] ];
prior = Function[ θ, PDF[ priorDist ] @ θ ];
Plot[ prior[θ], {θ, 0, 1}, Filling -> Axis, Axes-> {True,False} ]
Likelihood
$y|\theta, I \sim B(n, \theta)$
Given a series of $n = 1830$ Bernoulli-trials with $y = 420$ successes we will have a the Binomal PDF as a likelihood function:
likelihood = With[
{
n = 1830,
y = 420
},
Function[θ, Likelihood[ BinomialDistribution[n, θ], {y} ] ]
];
Plot[likelihood[θ], {θ, 0, 1}, Filling -> Axis, Axes -> {True, False}, PlotRange -> All]
As Romke pointed out, this function is a PDF depending on $\theta$ but not a probability distribution. So it does not integrate to 1:
NIntegrate[ likelihood[θ], {θ, 0, 1} ]
0.00054615
Posterior
$p(\theta|y,I) \propto p(y|\theta,I) \cdot p(\theta|I)$
Bayes-Rule tells us, that the PDF of the posterior distribution equals the product of the likelihood function and the prior PDF up to a normalizing constant (often called the evidence), which Romke has used in his explicit answer:
$p(y|I) = \int_0^1 p(y|\theta,I) p(\theta|I)$
Being a bit more lazy we can make use of the fact that ProbabilityDistribution
has an option Method -> "Normalize"
which will take care to adjusting the PDF to meet the requirements of a probability distribution:
posteriorDistribution = ProbabilityDistribution[
prior[θ] likelihood[θ],
{θ, 0, 1},
Method -> "Normalize"
];
Plot[ PDF[ posteriorDistribution ][θ], {θ,0,1},
Filling->Axis, Axes->{True,False}, PlotRange->All]
After so many additional data, the posterior (of course) is very close to the likelihood. We can immediately get some summarizing statistics:
Through[{Mean, StandardDeviation}@posteriorDistribution] // N
{0.23027, 0.00978554}
RandomVariate
will also work properly, so that we can sample from the posterior:
RandomVariate[ posteriorDistribution, 10 ]
{0.222989, 0.227517, 0.218535, 0.228404, 0.226061, 0.235919, 0.234529, 0.230528, 0.22929, 0.247735}
Since the prior and the posterior distributions here have the same functional form (conjugate prior), we may have immediately used the fact that:
$\theta|y,I \sim Beta(a + y, b + n - y)$
Equal[
PDF[ BetaDistribution[6 + 420, 14 + 1830 - 420], θ ],
PDF[posteriorDistribution, θ]
] // Reduce
True
You could also look at likelihood[θ] prior[θ]//Simplify
to see that the posterior does have the form:
$constant \times \theta^\alpha (1 - \theta)^\beta$
Update
Note that in general the normalizing constant (e.g. the evidence) can be obtained as an expectation:
$p(y|I) = \int p(y,\theta|I)d\theta = \int p(y|\theta,I) p(\theta|I)d\theta = \mathbb{E}[ p(y|\theta,I) ] = \mathbb{E}[\mathcal{L}(\theta)]$
posteriorPDF = With[
{ evidence = Expectation[ likelihood[θ], θ \[Distributed] priorDist ] },
Function[
θ,
prior[θ] likelihood[θ] / evidence
]
];
posteriorPDF[θ] == PDF[BetaDistribution[6 + 420, 14 + 1830 - 420], θ] // Reduce
True