Analogy between Sampling and Optimization