Compute the derivative of max integral function Here, I want to ask one question. 
If $$f(x)=\max(0,G(x))$$ 
Where
$$ G(x):=\max\limits_{0\leq u \leq x}\int_{0}^{u} g(y)dy$$  and $g(x)$ is any function from $\mathbb{R}$ to $\mathbb{R}$.
  Compute the derivative of $f(x)$
 A: Computing the derivative of $\max(0,G(x))$ is not difficult if you know the derivative of $G(x)$. Take a look here for example. The result is
$$\frac{d}{dx} \max(0,G(x)) = \begin{cases}
0 &\text{ if } G(x) < 0\\
G'(x)&\text{ otherwise }
\end{cases}$$
The difficult part is computing $G'(x)$

Let's assume $g(x)$ is continuous. Let $h(u) = \int_0^u g(y)dy$, then by the fundamental theorem of calculus, $h' = g$. Let $M$ be the (possibly infinite) set of points where $h$ has local maximum. Equivalently, these are zeros of $g$ where it changes from positive to negative. Now let
$$M(x) =\max\{h(m)\mid m\in M, m < x\}$$
Notice that $M(x)$ doesn't change in a small neighborhood of $x$, so $M'(x) = 0$. This is only true if $x \notin M$ though. $M(x)$ might not be differentiable in $x\in M$.
Then
$$G(x) = \begin{cases}
M(x) &\text{ if } h(x) < M(x) \\
h(x)&\text{ otherwise }
\end{cases}$$
$$G'(x) = \begin{cases}
0 &\text{ if } h(x) < M(x)\\
g(x)&\text{ otherwise }
\end{cases}$$

In summary:
$$G'(x) = \begin{cases}
0 &\text{ if } h(x) < M(x) \text{ or } G(x) < 0\\
g(x)&\text{ otherwise }
\end{cases}$$
