Algebraic Optimization of Risk