In the book by Richard Sutton and Andrew Barto, "Reinforcement Learning - An Introduction", 2ed edition, at page 101 there is a proof, and I don't understand 1 passage of it.
We want to prove that any $\epsilon$-greedy policy with respect to an action-value function $q_\pi$ is an improvement over any $\epsilon$-soft policy $\pi$ is assured by the policy improvement theorem. Let $\pi'$ be the $\epsilon$-greedy policy. The conditions of the policy improvement theorem apply because for any $s \in \mathcal{S}$:
\begin{align} q_{\pi}(s, \pi'(s)) &= \sum\limits_a \pi'(a\mid s) q_{\pi}(s,a) \\ &= \dfrac{\epsilon}{|\mathcal{A}|} \sum\limits_a q_{\pi}(s,a) + (1-\epsilon) \max\limits_a q_{\pi}(s,a) \\ & \ge \dfrac{\epsilon}{|\mathcal{A}|} \sum\limits_a q_{\pi}(s,a) + (1-\epsilon) \sum\limits_a \dfrac{\pi(a\mid s)-\dfrac{\epsilon}{|\mathcal{A}(s)|}}{1-\epsilon} q_\pi(s,a) \end{align}
I did not understand this last passage. The book now has a note which tries to explain this passage:
(the sum is a weighted average with non-negative weights summing to 1, and as such it must be less than or equal to the largest number averaged)
But this still doesn't help me understand this last passage.
This question asks for the passage from line 1 to line 2, I am asking for the passage from line 2 to line 3.