# Bridge

``` python
Bridge(normalized=False, fallback='raise')
```

The Laplace bridge: a Dirichlet over the class probabilities, matched to the logits.

After Hobbhahn et al. (2022). The predictive is the Dirichlet’s mean. Per input (and per position), from logits $f \sim \mathcal{N}(\bar f, V)$ over $K$ classes, with $\mathbf{1}$ the vector of ones:

1.  The shift shared by all logits is removed first:

    $$\bar f' = \bar f - r \, \mathbf{1}^\top \bar f, \qquad
    v'_k = V_{kk} - \frac{(V \mathbf{1})_k^2}{\mathbf{1}^\top V \mathbf{1}}, \qquad
    r = \frac{V \mathbf{1}}{\mathbf{1}^\top V \mathbf{1}}.$$

    When $\mathbf{1}^\top V \mathbf{1}$ is zero to rounding, $\bar f' = \bar f$ and $v'_k = V_{kk}$. “Zero to rounding” is $|\mathbf{1}^\top V \mathbf{1}| \le \epsilon \big(\sum_k \sqrt{V_{kk}}\big)^2$, with $\epsilon$ the machine epsilon of the covariance’s dtype. That is a bound on $\sum_{ij} |V_{ij}|$ from the diagonal, up to $K$ times looser than laplace-torch’s $\epsilon \sum_{ij} |V_{ij}|$, that every covariance type can compute.

2.  The Dirichlet parameters, computed in float64 and in log space, are

    $$\alpha_k \propto \frac{1}{v'_k} \Big(1 - \frac{2}{K}
    + \frac{e^{\bar f'_k}}{K^2} \sum_j e^{-\bar f'_j}\Big),$$

    and the predictive is $p_k = \alpha_k / \sum_j \alpha_j$.

At $K = 2$ the two corrected variances are equal, so the plain bridge is the softmax of the corrected mean whatever the covariance. For two classes use `Probit` (or `normalized=True`).

A row is invalid when a corrected variance is $\le 0$ (a zero output covariance, for one) or an $\alpha_k$ is not finite. `fallback` says what happens to it.

Needs only $V \mathbf{1}$, $\mathbf{1}^\top V \mathbf{1}$ and $\operatorname{diag} V$ per position, so a `SampleCov` or a `FactorCov` never builds a $K \times K$ block. Outputs may have extra axes, as for `Probit`.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| normalized | bool | Divide $\bar f'$ by $\sqrt{c}$ before step 2, with $c = \big(\frac{1}{K} \sum_k v'_k\big) / \sqrt{K / 2}$. | `False` |
| fallback | Literal\['raise', 'probit'\] | `"raise"` raises on invalid rows. `"probit"` gives them $\operatorname{softmax}\big(\tilde f / \sqrt{1 + \pi \max(v', 0) / 8}\big)$ on the shift-corrected moments instead, with $\tilde f$ the $\bar f'$ of step 1 (not divided by $\sqrt{c}$) centred over the classes, as in laplace-torch. | `'raise'` |

## Methods

| Name                                    | Description               |
|-----------------------------------------|---------------------------|
| [supports](#laplacekit.Bridge.supports) | True for a `Categorical`. |

### supports

``` python
Bridge.supports(lik, f_mu, /)
```

True for a `Categorical`.

#### Parameters

| Name | Type       | Description      | Default    |
|------|------------|------------------|------------|
| lik  | Likelihood | The likelihood.  | *required* |
| f_mu | Tensor     | The output mean. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | bool | True when `__call__` can turn outputs like `f_mu` into a distribution. |
