Conditional expectation on exponential distribution How can I compute the conditional expectation C-X in the following formula
E[C-X|X$<$C]; 
where C is a constant and X a random variable following exponential distribution? 
 A: Suppose that $X \sim \mathcal{E}(\lambda)$, then we have
\begin{align*}
\mathbb{E}[C-X \mid X < C] &= \frac{\mathbb{E}[ (C-X)I_{X<C}]}{P(X<C)} \\
&= \frac{CP(X<C) - \mathbb{E}[XI_{X<C}]}{P(X<C)} \\
&= C - \frac{\mathbb{E}[XI_{X<C}]}{1- e^{-\lambda C}}
\end{align*}
Where $I_{\cdot}$ is the indicator function.
Now we need to solve the expectation which is basic integration
\begin{align*}
\mathbb{E}[XI_{X<C}] &= \int_0^C \lambda x e^{-\lambda x} dx \\
&= \frac{1}{\lambda} - e^{-\lambda C}(C + \frac{1}{\lambda} )
\end{align*}
Thus we finally have 
$$
\mathbb{E}[C-X \mid X < C] = C + \frac{ e^{-\lambda C}(C + \frac{1}{\lambda} ) - \frac{1}{\lambda}}{1 - e^{-\lambda C}}
$$
Here is a little R code to check if it works with $\lambda = 0.01$ and $C=45$
Ck<-45
X<-rexp(1e7,0.01)
Y<-X[X<Ck]   ## Select only the values of X < Ck
mean(Ck-Y)   ## empirical expectation 
Ck + (Ck*exp(-0.01*Ck) + (exp(-0.01*Ck)-1)/0.01)/(1-exp(-0.01*Ck)) 

More generally when you have an conditional expectation of the type 
$$
\mathbb{E}[X \mid A]
$$
you can use the relation 
$$
\mathbb{E}[X \mid A] = \frac{\mathbb{E}[X I_A]}{P(A)}
$$
