# Custom likelihood

> **TIP:**
>
> The contract below is a protocol. Subclass it, as in `class MyClass(lk.core.Likelihood):`, and your editor marks a missing method in red. [Protocols in one page](../plugins/protocols.llms.md) shows how.

A likelihood that laplace**kit** doesn’t ship needs one function, `log_prob(f, y)`, which returns $\log p(y \mid f)$ per example. As an example, let’s use the Bradley–Terry model for pairwise preferences, as in reward models. There, a network scores two items, and the label says whether the first one wins. For real reward models, `lk.reward` ships this likelihood ready to use (see [Reward models](../quickstart/reward.llms.md)).

``` python
import math

import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
from torch import nn

import laplacekit as lk

torch.set_default_dtype(torch.float64)
torch.manual_seed(3)

x = 4 * torch.rand(400, 2, 1) - 2  # 400 pairs of items in [-2, 2]
u = torch.sin(2 * x).squeeze(-1)  # hidden utility of each item
y = torch.bernoulli(torch.sigmoid(3 * (u[:, 0] - u[:, 1])))
loader = list(zip(x.split(100), y.split(100), strict=True))

scorer = nn.Sequential(
    nn.Linear(1, 32), nn.ReLU(), nn.Linear(32, 32), nn.ReLU(), nn.Linear(32, 1)
)
model = nn.Sequential(scorer, nn.Flatten())  # [N, 2, 1] -> [N, 2] scores
opt = torch.optim.Adam(model.parameters(), lr=1e-2, weight_decay=1e-4)

for _ in range(500):
    f = model(x)
    loss = F.binary_cross_entropy_with_logits(f[:, 0] - f[:, 1], y)
    opt.zero_grad()
    loss.backward()
    opt.step()
```

`lk.likelihoods.from_log_prob` turns the function into a likelihood.

``` python
def bradley_terry(f, y):
    return -F.binary_cross_entropy_with_logits(
        f[:, 0] - f[:, 1], y, reduction="none"
    )


lik = lk.likelihoods.from_log_prob(bradley_terry)
post = lk.laplace(model, loader, likelihood=lik, subset=lk.subsets.all)
print(post)
```

    LaplacePosterior(
      subset         = all {"0.0.weight": (32, 1), "0.0.bias": (32,), "0.2.weight": (32, 32), "0.2.bias": (32,), "0.4.weight": (1, 32), "0.4.bias": (1,)}
      hessian_approx = PerBlockCurvature({'*': DiagGGN(fisher='empirical')})
      curvature      = BlockDiag[Diag] (3 blocks, 1153 parameters)
      prior          = Isotropic(prior_precision=4.2)
      likelihood     = from_log_prob(bradley_terry)
      hyper_tuner    = MarginalLikelihood()
      n_data         = 400
    )

A bare `log_prob` gives no distribution over $y$, so `lk.predict` refuses. `lk.predict_outputs` still gives the Gaussian over the two scores, and we finish the prediction by hand.

``` python
t = torch.linspace(-6, 6, 300)
grid = torch.stack([t, torch.zeros_like(t)], dim=1).unsqueeze(
    -1
)  # pairs (x, 0)

try:
    lk.predict(post, grid)
except lk.CapabilityError as e:
    print(e)

out = lk.predict_outputs(post, grid)
v = out.cov.per_input()  # [N, 2, 2]
var = v[:, 0, 0] + v[:, 1, 1] - 2 * v[:, 0, 1]
p_lap = torch.sigmoid(
    (out.mean[:, 0] - out.mean[:, 1]) / torch.sqrt(1 + math.pi / 8 * var)
)

with torch.no_grad():
    f_map = model(grid)

fig, ax = plt.subplots(figsize=(7, 4))
ax.axvspan(-2, 2, color="0.93", label="data range")
ax.plot(t, torch.sigmoid(f_map[:, 0] - f_map[:, 1]), label="MAP")
ax.plot(t, p_lap, label="Laplace")
ax.set_xlabel("item x")
ax.set_ylabel("P(x preferred over 0)")
ax.legend()
plt.show()
```

    Linearized gives a distribution over targets, but LogProb has none (HasDistribution); use lk.predict_outputs(posterior, inputs) for the Gaussian over f

![](likelihoods_files/figure-html/cell-4-output-2.png)

Outside the data range, the MAP model goes to $0$ or $1$, while the Laplace predictive stays less sure.

> **NOTE:**
>
> With scores $f = (f_1, f_2)$, the Bradley–Terry likelihood is $p(y = 1 \mid f) = \sigma(f_1 - f_2)$. Without the output Hessian or samples of $y$, the only curvature left is the empirical Fisher, the outer product of the per-example gradients at the observed labels. So `lk.laplace` picks `DiagGGN(fisher="empirical")`. The linearized scores are Gaussian with mean $m$ and covariance $V$, so the difference $f_1 - f_2$ has mean $m_1 - m_2$ and variance $V_{11} + V_{22} - 2 V_{12}$. The probit approximation then gives
>
> $$p(y = 1 \mid x, \mathcal{D}) \approx \sigma\left( \frac{m_1 - m_2}{\sqrt{1 + \frac{\pi}{8} (V_{11} + V_{22} - 2 V_{12})}} \right) .$$

The full tutorial: [Likelihoods](../plugins/likelihood.llms.md)
