Let $I_t$ be your investment(in number of units) in a stock at time t and it depends on the value of the stock S in the following way:
$I_t = f(S_t)$
In this case, change in your wealth during an infinitesimal interval would be given by:
$dX_t = f(S_t)dS_t$
If $S_t$ is deterministic and non-stochastic:
$X_T − X_0 = \int_{S_0}^{S_T} f(S_t) dS_t$
$= F(S_T)-F(S_0)$
where
$ F(S) = \int_a^S f(x)dx .$ for whatever a is chosen
If however, $S_t$ is stochastic with
$dS_t = S_t × (μ_t dt + σ_t dW_t)$
then
$X_T − X_0 = F(S_T)-F(S_0) −0.5 \int_0^T f'(S_t) S_t^2 σ_t^2 dt$
My confusion is regarding the second part when S is stochastic. Is there a way to prove the stochastic case?
The expression comes from Ito's lemma. Let $F(S) = \int_a^S f(x)dx$ so that $F'(S) = f(S).$ Then if $dS = \mu S dt + \sigma S dS,$ and we let $Y_t=F(S_t),$ Ito's lemma gives $$ dY = F'(S)dS + \frac{1}{2}F''(S)\sigma^2 S^2dt \\= f(S)dS + \frac{1}{2}f'(S)\sigma^2S^2dt\\= dX + \frac{1}{2}f'(S)\sigma^2S^2 dt.$$
Integrating gives $$Y_T-Y_0 = X_T-X_0 +\frac{1}{2}\int_0^T f'(S_t)\sigma^2 S_t^2 dt.$$ Plugging in $Y_T-Y_0 = F(S_T)-F(S_0)$ and rearranging gives your expression.