Explain the difference between precision and recall in the context of a binary classification problem. Provide a detailed explanation of each metric, including their formulas and the scenarios where optimizing for one over the other might be necessary.