The Algebra of Portfolio Dynamics