Project: 060_Trust_or_Escalate_LLM_Judges_with_Provable_Guarant | Function: lambda_hat (iclr)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Computes the threshold lambda_hat that ensures the risk is at most alpha with probability 1-delta. This implements the core selective classification algorithm that provides provable guarantees by finding the optimal confidence threshold for model decisions.

**KEY TERMS:** {
  "lam_hat": "λ̂ (calibrated threshold)",
  "alpha": "α (risk tolerance)",
  "selective_risk_upper_bound": "R̂+(λ) (upper confidence bound)"
}

**CONTEXT CODE:**     # Labels in the example files are: "1": A is better, "2": B is better
    labels = torch.Tensor(labels) - 1

    phats, yhats = {}, {}
    for model_name, probs in predicted_probs.items():
        probs = torch.Tensor(probs)
        phats[model_name], yhats[model_name] = torch.max(probs, dim=-1)

    return phats, yhats, labels


class SelectiveClassificationUtil:
    def __init__(self, cal_phats: torch.Tensor, cal_yhats: torch.Tensor, cal_labels: torch.Tensor, delta: float):
        self.cal_phats = cal_phats
        self.cal_yhats = cal_yhats
        self.cal_labels = cal_labels

        # tolerance
        self.delta = delta

        # candidate lambdas: make sure there's data for top bin
        self.lambdas = torch.Tensor([lam for lam in torch.linspace(0, 1, 5000) if self.n_lambda(lam) >= 30])

    def selective_risk(self, lam: float):
        # compute empirical risk \hat{R}(\lambda) on calibration set
        num_correct = (self.cal_yhats[self.cal_phats >= lam] != self.cal_labels[self.cal_phats >= lam]).sum()
        num_total = (self.cal_phats >= lam).sum()

        return num_correct / num_total

    def selective_risk_upper_bound(self, lam: float):
        # compute upper bound for empirical risk \hat{R}+(\lambda)

        # upper-bound condition for supremum
        def _upper_bound_condition(r: float, lam: float):
            return binom.cdf(self.selective_risk(lam) * self.n_lambda(lam), self.n_lambda(lam), r) - self.delta

        # Check if brentq conditions are satisfied
        try:
            fa = _upper_bound_condition(0, lam)
            fb = _upper_bound_condition(0.9999, lam)
            if fa * fb >= 0:  # Same sign, cannot use brentq
                return 0.5  # Return reasonable fallback
            return brentq(_upper_bound_condition, 0, 0.9999, args=(lam,))
        except (ValueError, RuntimeError):
            return 0.5  # Return reasonable fallback for numerical issues

    def n_lambda(self, lam: float):
        return (self.cal_phats >= lam).sum()

    def lambda_hat(self, alpha: float):
        raise NotImplementedError("Function implementation has been masked for testing")

Complete the function implementation replacing "raise NotImplementedError".