# function_space.SubsetGP

``` python
function_space.SubsetGP(
    model=static(),
    likelihood,
    mean,
    prior,
    caps=static(),
    jac,
    root,
    n_subset=static(),
    inputs,
    targets,
    n_seen=static(),
    structure='joint',
    versions=dict[str, int](),
)
```

A GP over the outputs, conditioned on the outputs at a subset of $m$ training inputs.

The kernel is

$$k(x, x') = J(x) \, \Sigma_0 \, J(x')^\top,$$

with $J$ the model’s Jacobian at the MAP $\theta_*$ and $\Sigma_0$ the prior’s covariance. The noise precision at the subset is the likelihood’s output Hessian there. With every training input this is the linearized Laplace posterior with the full dense GGN. The output Hessian is not rescaled by $N / m$, so a smaller subset gives a wider posterior than the full data would.

The mean is $f(x; \theta_*)$: the Laplace mean is the MAP for any prior location, and the GP view changes only the covariance.

`jac` and `root` are fixed at fit time and depend on the model and the MAP only. `at` and `log_marglik` read the prior and the likelihood’s `curvature_scale` on every call, so tuning them never refits. Both raise if one of the module’s fixed parameters changed in place since the fit (see `check_versions`).

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| model | ModelFn | The model as a function of the params. | `static()` |
| likelihood | \_L_co | The real likelihood. Its tensors (`sigma`) are leaves, so they can be tuned. | *required* |
| mean | Params | The MAP $\theta_*$, a parameter mapping. | *required* |
| prior | \_Pr | The prior. It gives $\Sigma_0$ and the location $\mu_0$. | *required* |
| caps | Capabilities | The model’s capabilities. | `static()` |
| jac | Tensor | The Jacobian at the subset, $[m E, P]$, for $E$ output entries per input and $P$ parameters. | *required* |
| root | Tensor | $A$ with $A A^\top$ the output Hessian of the unit likelihood, $[m, E, E]$. | *required* |
| n_subset | int | The number of subset rows $m$. | `static()` |
| inputs | Inputs | The subset’s rows. Shared leaves (0-dim tensors and non-tensors) are kept by reference, and `check_versions` does not cover them. | *required* |
| targets | Targets \| None | The subset’s targets, or `None` when the data had none. | *required* |
| n_seen | int | The number of rows the pass saw. | `static()` |
| structure | \_Structure | That of `SubsetOfData`. With `"independent"` the kernel is block diagonal over the outputs, so `at` gives diagonal $E \times E$ blocks. | `'joint'` |
| versions | Mapping\[str, int\] | Version counters of the module’s fixed parameters at fit time. The mean is the posterior’s own copy, so it has none. | `dict[str, int]()` |

## Attributes

| Name | Description |
|----|----|
| [requires](#laplacekit.function_space.SubsetGP.requires) | Needs vjp and vmap, for the Jacobian at new inputs. |

## Methods

| Name | Description |
|----|----|
| [at](#laplacekit.function_space.SubsetGP.at) | The Gaussian over the outputs at `inputs`, with one $E \times E$ block per input. |
| [check_versions](#laplacekit.function_space.SubsetGP.check_versions) | Raise `RuntimeError` if a fixed parameter of the module changed in place since the fit. |
| [log_marglik](#laplacekit.function_space.SubsetGP.log_marglik) | The GP evidence of the subset, after laplace-torch’s `FunctionalLaplace`. |

### at

``` python
function_space.SubsetGP.at(inputs, /)
```

The Gaussian over the outputs at `inputs`, with one $E \times E$ block per input.

Per input $x$, the covariance is

$$K_{xx} - B^\top B, \qquad B = L^{-1} W^\top K_{mx}, \qquad
L L^\top = I + W^\top K_{mm} W,$$

where $K_{ab}$ is the kernel between $a$ and $b$, $m$ stands for the subset, and $W$ is block diagonal with the roots $A$ of the output Hessians. This form needs no inverse of the output Hessian, so a singular one (softmax) is fine. The algebra runs in `stat_dtype` of the outputs. With a `HasMask` likelihood the masked entries of the subset drop out, as in `log_marglik`, also after a likelihood swap.

#### Parameters

| Name   | Type   | Description        | Default    |
|--------|--------|--------------------|------------|
| inputs | Inputs | A batch of inputs. | *required* |

#### Returns

| Name | Type           | Description                       |
|------|----------------|-----------------------------------|
|      | OutputGaussian | The output Gaussian of the batch. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | The model lacks vjp or vmap. |
|  | NotImplementedError | The model does not return a single tensor. |
|  | StructureError | The likelihood’s `curvature_scale` has more than one element, its mask is not bool or not shaped like the leading axes of the outputs, or `prior.cov_mv` refuses the parameters. |

### check_versions

``` python
function_space.SubsetGP.check_versions()
```

Raise `RuntimeError` if a fixed parameter of the module changed in place since the fit.

`jac` and `root` used its old value, so the GP no longer describes the model. `at` calls this first.

### log_marglik

``` python
function_space.SubsetGP.log_marglik()
```

The GP evidence of the subset, after laplace-torch’s `FunctionalLaplace`.

Let $K = J \Sigma_0 J^\top$ at the subset (through `prior.cov_mv`), $f$ the outputs at the MAP $\theta_*$, and $r = J (\theta_* - \mu_0)$ with $\mu_0$ the prior’s location.

- `lk.Gaussian`: the exact evidence of the linearized model, $\log \mathcal{N}(y; f - r, K + \sigma^2 I)$.

- Any other likelihood: R&W (3.44),

  $$\sum_i \log p(y_i \mid f_i) - \tfrac{1}{2} \Big(r^\top (K + 10^{-5} I)^{-1} r
  + \log \det (I + W^\top K W)\Big),$$

  with $W$ the roots of the output Hessians of the real likelihood. The $10^{-5}$ is laplace-torch’s `scatter(eps)`.

With `structure="independent"` $K$ is block diagonal over the outputs, so both are sums over the outputs. With a `HasMask` likelihood the masked output entries drop out of every term: the evidence is that of the observed entries only. Differentiable in the prior’s and the likelihood’s tensors.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Tensor | The log evidence, a 0-dim tensor in `stat_dtype` of the outputs. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | The model lacks vjp or vmap. |
|  | ValueError | The pass saw more rows than it kept (the evidence of a subset is not the data’s), or the data had no targets. |
|  | RuntimeError | A fixed parameter changed in place since the fit (`check_versions`). |
|  | StructureError | The model’s outputs are not one tensor, the `Gaussian`’s targets are not shaped like $f$, the likelihood’s `curvature_scale` has more than one element, its mask or `log_prob` is badly shaped, or `prior.cov_mv` refuses the parameters. |
