Hebbian Learning from First Principles