Let $g(x)=(x-a)\mathbf 1\_{x\ge a}$ for some $a>0$ and let $X$ be a non-negative random variable with cdf $F$ and $E[X]<+\infty$. I want to calculate $$\frac{d}{da}E[g(X)]$$ To do that I thought as a first step to use this formula [here](https://en.wikipedia.org/wiki/Expected_value#General_definition):



> 
> ...Using representations as Riemann–Stieltjes integral and integration by parts the formula can be restated as
>  $$\operatorname {E}[g(X)]=\int \_{-\infty }^{\infty }g(x)\,\mathrm{d}P(X\le x)=g(a)+\int \_{a}^{\infty }g'(x)P(X>x)\,\mathrm {d} x$$
>  if $P\left(g(X)\geq g(a)\right)=1$.
> 
> 
> 


So I calculated $$g'(x)=\begin{cases}0, & 0\le x< a \\ \text{undefined}, & x=a \\ 1, & a<x \end{cases}$$ and since $P(g(X)>g(a))=P(g(X)>0)=1$, I wrote: $$E[g(X)]=g(a)+\int\_{a}^{+\infty}(1-F(x))\,\mathrm{d}x=\int\_{a}^{+\infty}(1-F(x))\,\mathrm{d}x \tag{1}$$
My **first question:** Is this correct, or is $g'(x)|\_{x=a}$ undefined a problem?


So, taking the derivative of $(1)$ gives me that $$\frac{d}{da}\int\_{a}^{+\infty}1-F(x)\, \mathrm dx=F(a)-1 \tag{2}$$
My **second question:** Is this derivative correct? 


Thanks in advance!



Law of unconsious statistician: application in characteristic function
Formula (1) is correct for any random variable (r.v.) $X$ and any real $a$, even without assuming that $X$ is nonnegative and/or continuous. Indeed, by the Fubini--Tonelli theorem, 
$$\int\_a^\infty (1-F(x))\,dx 
=\int\_a^\infty P(X>x)\,dx =\int\_a^\infty dx \int\_{(x,\infty)}P(X\in du)
$$ 
$$=\int\_{\mathbb R}dx\,I\{x\ge a\}\int\_{\mathbb R}I\{u>x\} P(X\in du) $$ 
$$=\int\_{\mathbb R}P(X\in du)\int\_{\mathbb R}dx\,I\{u>x\ge a\} $$
$$=\int\_{\mathbb R}P(X\in du)(u-a)\_+ = E(X-a)\_+; 
$$
here $I\{\cdot\}$ denotes the indicator function. 


Assume now that $E X\_+<\infty$ (your conditions $X\ge0$ and $EX<\infty$ are more than enough for that). Then $E(X-a)\_+<\infty$ for all real $a$. Moreover, by (1), the right and left derivatives of $E(X-t)\_+$ in $t$ at $t=a$ equal $-\lim\_{x\downarrow a}P(X>x)=-P(X>a)=F(a)-1$ and $-\lim\_{x\uparrow a}P(X>x)=-P(X\ge a)=F(a-)-1$, respectively. If $P(X=a)=0$ then the derivative of $E(X-t)\_+$ in $t$ at $t=a$ exists and equals $-P(X>a)=F(a)-1$. 


**Addendum:** A more direct way to compute these one-sided derivatives (again for any r.v. $X$ with $E X\_+<\infty$) is as follows. 
Let $f\_{a,h}(x):=\frac1h\,[(x-a)\_+-(x-a-h)\_+]$. Then $0\le f\_{a,h}(x)\le1$ for real $x$ and real $h>0$, and $\lim\_{h\downarrow0}f\_{a,h}(x)=I\{x>a\}$. 
So, the right derivative of $E(X-t)\_+$ in $t$ at $t=a$ equals 
$$-\lim\_{h\downarrow0}\frac{E(X-a)\_+-E(X-a-h)\_+}h
=-\lim\_{h\downarrow0}Ef\_{a,h}(X)=-EI\{X>a\}=-P(X>a),$$ 
by the dominated convergence theorem. 
Quite similarly, for the left derivative.


