How does practicing restraint and performing actions of merit purify the mind? I just don't understand how morally good actions, can reliably change the state of mind positively over time, in the case where the mind state is negative. For example, let's say there was a hypothetical girl who wanted to purify her mind, but was always agitated, resentful and frustrated. How would practicing the five precepts, make her calm and peaceful?
Well, it seems obvious to me now, but it wasn't always, that our immoral impulsive behavior, both of possessive type and of repulsive type, comes from emotional hangups, so giving in to those hangups feeds them, or creates circumstances that feed them indirectly. So, if this immoral behavior comes from the mind, then by managing behavior we learn to control what? 

Anyone who tried the precepts knows they are impossible to keep without paying attention to one's mind and emotions. Precepts force us to control our external behavior, which requires controlling our mind, that's it. No magic.