How to calculate the Bayesian posterior analytically and by simulation? I am working with this model:  
Prior: $P(\lambda)$~ N(0, 1), only the positive part
likelihood:  $P(x) = 1 - e^{-\lambda x}$  or $P(\vec{x}|\lambda)=\prod(1-e^{-\lambda x})$
Posterior: $P(\lambda|\vec{x})$
What is $P(\lambda|\vec{x})$ ?
I have something looks like this:

My understanding is that $P(x)$ is always a density function.
In here, my model is NOT a density, it is a exponential cdf.  So, I am a bit confused.  Is my model wrong, is this possible?  Thanks!
 A: A model distribution and a prior distribution are defined by either their pdf or their cdf. So your dataset is an exponential sample with parameter $\lambda$, which means that
$$
F_\lambda(x)=1-e^{-\lambda x}\ \text{ and }\ f_\lambda(x)=\lambda e^{-x\lambda}
$$
are the cdf and pdf for this model. Therefore the likelihood function is defined as
$$
L(\lambda|x_1,\ldots,x_n) = \prod_{i=1}^n f_\lambda(x_i) = \lambda^n \exp\left\{-\lambda\sum_{i=1}^n x_i\right\}\,.
$$
If your prior on $\lambda$ is a truncated normal $\text{N}(0,1)$, then its pdf is given by
$$
\pi(\lambda) = \sqrt{\dfrac{2}{\pi}}\ \exp\{-\lambda^2/2\}\mathbb{I}_{\lambda>0}\,.
$$
(The cdf is irrelevant for the computation of the posterior.)
Now the posterior distribution is defined via Bayes' formula by
$$
\pi(\lambda|x_1,\ldots,x_n) \propto \pi(\lambda) L\lambda|x_1,\ldots,x_n)\,.
$$
Hence,
\begin{align*}
\pi(\lambda|x_1,\ldots,x_n) &\propto \lambda^n \exp\{-\lambda^2/2\} \exp\left\{-\lambda\sum_{i=1}^n x_i\right\}\mathbb{I}_{\lambda>0}\\
&\propto \lambda^n \exp\left\{-\frac{1}{2}\left(\lambda+\sum_{i=1}^n x_i\right)^2\right\}\mathbb{I}_{\lambda>0}
\end{align*}
which is the density of a non-standard distribution.
A: Suppose that $X_1,\dots,X_n$, given $\Lambda=\lambda$, are conditionally independent and identically distributed, such that $X_1\sim\mathrm{Exp}(\lambda)$, and you believe a priori that $\Lambda\sim\mathrm{Ga}(\alpha_0,\beta_0)$, in which $\alpha_0,\beta_0>0$ are specified real numbers.
After you get a sample $x=(x_1,\dots,x_n)$, the likelihood is the sampling density seem as a function of $\lambda$.
$$
  L_x(\lambda) = f_{X_1,\dots,X_n\mid\Lambda}(x_1,\dots,x_n\mid\lambda) = \prod_{i=1}^n f_{X_i\mid\Lambda}(x_i\mid\lambda) = \lambda^n e^{-\lambda\sum_{i=1}^n x_i} \,I_{(0,\infty)}(\lambda)\, .
$$ 
Bayes' Theorem gives
$$
  f_{\Lambda\mid X_1,\dots,X_n}(\lambda\mid x_1,\dots,x_n) \propto L_x(\lambda)\,f_\Lambda(\lambda) \propto \lambda^{\alpha_0+n-1} e^{-(\beta_0+\sum_{i=1}^n x_i)\lambda}\,I_{(0,\infty)}(\lambda) \, ,
$$
yielding that a posteriori
$$
  \Lambda\mid X_1=x_1,\dots,X_n=x_n\sim\mathrm{Ga}\!\left(\alpha_0+n,\beta_0+\sum_{i=1}^n x_i\right) \, .
$$
The Bayes estimate with quadratic loss is
$$
  \mathrm{E}\left[\Lambda\mid X_1=x_1,\dots,X_n=x_n\right] = \frac{\alpha_0+n}{\beta_0+\sum_{i=1}^n x_i} \, .
$$
A credible interval can be obtained by Monte Carlo.
a0 <- 6
b0 <- 20

n <- 45
x <- rexp(n, rate = 5)

N <- 10^6

level <- 0.95

post <- rgamma(N, shape = a0 + n, rate = b0 + sum(x))

cat("Bayes estimate:", (a0+n)/(b0+sum(x)), "\n")
cat(sprintf("%.2f", level*100), "% Credible Interval: [ ", 
    quantile(post,(1-level)/2), " ; ", 
    quantile(post,(1+level)/2), " ]\n", sep = "")

