Solution (source code)

= Solution

The uniform prior and <likelihood> give the posterior density
$$
\pi(\theta\mid K)=\frac{(n+1)!}{K!(n-K)!}\theta^K(1-\theta)^{n-K},
$$
a <Beta distribution> with parameters $K+1,n-K+1$. Under <squared-error loss> for $\phi$, the posterior expected loss is $\mathbb E[\phi^2\mid K]-2a\mathbb E[\phi\mid K]+a^2$, minimized uniquely at the posterior mean of $\phi$, not at the product of the two posterior mean factors.

Using the supplied beta integral,
$$
\mathbb E[\theta(1-\theta)\mid K]
=\frac{(K+1)!(n-K+1)!/(n+3)!}{K!(n-K)!/(n+1)!}.
$$
Thus \b[the Bayes estimate is]
$$
\boxed{\widehat\phi_B=\frac{(K+1)(n-K+1)}{(n+2)(n+3)}.}
$$
This remains positive even on the endpoint samples, unlike the extended MLE; it integrates over the posterior uncertainty in $\theta$.