Principled algorithms for domain adaptation and generalization