How to optimize preconditioners for the conjugate gradient method: a stochastic approach