Optimal Ridge Regularization for Out-of-Distribution Prediction