Solution (source code)

= Solution

For observation $X=k$, the likelihood is proportional to $p^k(1-p)^{n-k}$. For $0<k<n$, its logarithmic <derivative> is $k/p-(n-k)/(1-p)$, vanishing uniquely at $k/n$; the logarithm is strictly concave. At $k=0$ or $k=n$, monotonicity gives the boundary maximum. Thus the <maximum-likelihood estimator> is
$$
\boxed{\widehat p_{\rm ML}=X/n}.
$$
A <uniform prior> multiplies this likelihood by a constant. The beta-integral normalization therefore gives the <posterior distribution>
$$
\boxed{p\mid X=k\sim\operatorname{Beta}(k+1,n-k+1)}.
$$
In particular the posterior mean $(k+1)/(n+2)$ is not automatically the optimal estimator for the weighted loss in the next part.