We present an explicit formula for mean-variance optimization in the context of APT models (also called multi-factor models),and related generalizations with trading costs. Our explicit formula has two desirable features: 1. the solutions are well-defined and numerically stable in the presence...