Take a look at this previous question; in the answer by Julian Helfferich we arrive at the following formula for an infinitesimal change of entropy: $$dS = \frac{1}{T}\sum_i d(E_ip_i) - d(E_i)p_i$$ all this is completely fine for me, no problem here. But then comes the crucial point: we can state that $\sum _i d(E_i p_i)$ is the expectation value of the change of the energy of the system and also we can say that $\sum _i d(E_i) p_i$ is the expectation value of the work done on the system. Using those statements, and also the first principle of thermodynamics, we can immediately show that: $$dS=\frac{dQ}{T}$$ that indeed is what we are trying to show.
I can see roughly that this two statements are true, but I would like a formal mathematical proof of those; for example: regarding the first statement we can surely say that:
$$\langle E \rangle= \sum _i E_i p_i$$
and so we can say that the variation of the expectation value is:
$$\sum _i d(E_ip_i)$$
seems easy right? But here comes the problem: is the variation of the expectation value equal to the expectation value of the variation? Is this the right way to show that $\sum _i d(E_ip_i)=dE$?
But most importantly: How can we show that $\sum _i d(E_i)p_i=dW$? How can we prove this? I have also tried to read the related wikipedia page, but the explanation there does not convince me: the mathematical derivation is not reported there.