# Gaussian

``` python
Gaussian(sigma=None, *, event_dims=0)
```

Independent Gaussian noise over the last `event_dims` dims of $f$.

Over the entries $e$ of $f$,

$$p(y \mid f) = \prod_e \mathcal{N}(y_e \mid f_e, \sigma_e^2), \qquad
H = \operatorname{diag}(1 / \sigma_e^2),$$

where $H$ is the output Hessian and $\sigma_e = \sigma$ unless $\sigma$ is per output.

`Gaussian()` learns $\sigma$: the hyper tuner of `lk.laplace` fits it together with the prior precision. A learned $\sigma$ is applied after the fit (`LateLikelihood`): the fit uses unit noise and keeps the weighted sum of squared residuals and the count of entries, so tuning $\sigma$ never refits.

`Gaussian(sigma=0.1)` fixes $\sigma$. A fixed $\sigma$ is applied at fit time: the curvature holds it, and no tuner changes it. A $\sigma$ with several elements is per output and also applied at fit time. The fit then keeps the sums of squared residuals and the counts per entry of the event. `lk.marglik.train` learns a sigma per output from `sigma_init=` (MacKay’s update); the hyper tuners leave one alone.

The predictive keeps the event shape of $f$. `log_prob` is per position: $f$’s shape without the last `event_dims`.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| sigma | float \| Tensor \| None | The noise scale $\sigma$, a float or a tensor of positive entries, kept fixed. A float stays in double precision until the fit casts it to its statistic’s dtype. `None` (the default) learns it, starting at 1 or at the tuner’s `sigma_init`. A tensor with several elements must broadcast to the event shape `f.shape[1:]` without adding dims (`[C]` for outputs `[N, C]`, `[C]` or `[T, C]` for `[N, T, C]`); otherwise the first call that sees $f$ raises `StructureError`. | `None` |
| event_dims | int | How many trailing dims of $f$ form one event. | `0` |

## Raises

| Name | Type       | Description                                |
|------|------------|--------------------------------------------|
|      | ValueError | `sigma` has an entry that is not positive. |

## Attributes

| Name | Description |
|----|----|
| [per_output](#laplacekit.Gaussian.per_output) | Whether $\sigma$ has several elements, one per output entry. |

## Methods

| Name | Description |
|----|----|
| [check_outputs](#laplacekit.Gaussian.check_outputs) | Raise `StructureError` when a per-output $\sigma$ doesn’t fit $f$’s event shape. |
| [curvature_scale](#laplacekit.Gaussian.curvature_scale) | $1 / \sigma^2$ for a learned $\sigma$; 1 for a fixed one, which the fit applied. |
| [distribution](#laplacekit.Gaussian.distribution) | `Normal(f, sigma)`, with the last `event_dims` dims as the event. |
| [fit_stat](#laplacekit.Gaussian.fit_stat) | The statistic $[r, m]$ for $N$ examples with $E$ entries each. |
| [log_lik](#laplacekit.Gaussian.log_lik) | The log-likelihood from the statistic $[r, m]$ of `fit_stat`. |
| [log_prob](#laplacekit.Gaussian.log_prob) | Gaussian log density per position, summed over the event. |
| [output_hessian](#laplacekit.Gaussian.output_hessian) | $\operatorname{diag}(1 / \sigma_e^2)$. |
| [tunable](#laplacekit.Gaussian.tunable) | The hyperparameters a hyper tuner may change. |
| [unit](#laplacekit.Gaussian.unit) | The likelihood the fit uses: unit noise for a learned sigma, else itself. |

### check_outputs

``` python
Gaussian.check_outputs(f, /)
```

Raise `StructureError` when a per-output $\sigma$ doesn’t fit $f$’s event shape.

As a `HasOutputCheck`, the probe calls it on its own forward, before any data is fitted.

#### Parameters

| Name | Type    | Description                           | Default    |
|------|---------|---------------------------------------|------------|
| f    | Outputs | Outputs of the probe’s plain forward. | *required* |

### curvature_scale

``` python
Gaussian.curvature_scale()
```

$1 / \sigma^2$ for a learned $\sigma$; 1 for a fixed one, which the fit applied.

#### Returns

| Name | Type   | Description                                         |
|------|--------|-----------------------------------------------------|
|      | Tensor | A tensor computed from the current hyperparameters. |

### distribution

``` python
Gaussian.distribution(f, /)
```

`Normal(f, sigma)`, with the last `event_dims` dims as the event.

#### Parameters

| Name | Type    | Description                | Default    |
|------|---------|----------------------------|------------|
| f    | Outputs | Model outputs for a batch. | *required* |

#### Returns

| Name | Type         | Description                                    |
|------|--------------|------------------------------------------------|
|      | Distribution | A torch distribution over the batch’s targets. |

#### Raises

| Name | Type           | Description                                          |
|------|----------------|------------------------------------------------------|
|      | StructureError | A per-output $\sigma$ doesn’t fit $f$’s event shape. |

### fit_stat

``` python
Gaussian.fit_stat(f, y, weights, /)
```

The statistic $[r, m]$ for $N$ examples with $E$ entries each.

$$r = \sum_{n=1}^N w_n \lVert y_n - f_n \rVert^2, \qquad m = \sum_{n=1}^N w_n E,$$

with $w_n = 1$ when `weights` is None. With a per-output $\sigma$, the same two sums per entry of the event, shaped `[2, *f.shape[1:]]`.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| f | Outputs | Model outputs for a batch. | *required* |
| y | Targets | Targets for the same batch. | *required* |
| weights | Tensor \| None | Per-example weights, or `None` for all ones. | *required* |

#### Returns

| Name | Type   | Description            |
|------|--------|------------------------|
|      | Tensor | The batch’s statistic. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `y` is not shaped like `f`, or a per-output $\sigma$ doesn’t fit $f$’s event shape. |

### log_lik

``` python
Gaussian.log_lik(stat, /)
```

The log-likelihood from the statistic $[r, m]$ of `fit_stat`.

$$\log p = -\frac{1}{2} \Big(m \log(2 \pi \sigma^2) + \frac{r}{\sigma^2}\Big).$$

For a per-output $\sigma$, the same per entry of the event, summed.

#### Parameters

| Name | Type   | Description                         | Default    |
|------|--------|-------------------------------------|------------|
| stat | Tensor | `fit_stat` summed over all batches. | *required* |

#### Returns

| Name | Type   | Description                                               |
|------|--------|-----------------------------------------------------------|
|      | Tensor | The summed log-likelihood at the current hyperparameters. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `stat` doesn’t fit the kind of $\sigma$: shape `[2]` for a one-element $\sigma$, `[2, *event]` (broadcast from $\sigma$’s shape) for a per-output one. A per-output $\sigma$ is fixed at fit time, so a fit with one kind of $\sigma$ can’t take the other. |

### log_prob

``` python
Gaussian.log_prob(f, y, /)
```

Gaussian log density per position, summed over the event.

#### Parameters

| Name | Type    | Description                 | Default    |
|------|---------|-----------------------------|------------|
| f    | Outputs | Model outputs for a batch.  | *required* |
| y    | Targets | Targets for the same batch. | *required* |

#### Returns

| Name | Type   | Description                                                   |
|------|--------|---------------------------------------------------------------|
|      | Tensor | A tensor shaped `[N]`, or `[N, *positions]` (per token, say). |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `y` is not shaped like `f`, or a per-output $\sigma$ doesn’t fit $f$’s event shape. |

### output_hessian

``` python
Gaussian.output_hessian(f, /)
```

$\operatorname{diag}(1 / \sigma_e^2)$.

#### Parameters

| Name | Type    | Description                | Default    |
|------|---------|----------------------------|------------|
| f    | Outputs | Model outputs for a batch. | *required* |

#### Returns

| Name | Type          | Description                     |
|------|---------------|---------------------------------|
|      | OutputHessian | The per-example output Hessian. |

#### Raises

| Name | Type           | Description                                          |
|------|----------------|------------------------------------------------------|
|      | StructureError | A per-output $\sigma$ doesn’t fit $f$’s event shape. |

### tunable

``` python
Gaussian.tunable()
```

The hyperparameters a hyper tuner may change.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | dict\[str, Tensor\] | `{"sigma": sigma}` when $\sigma$ is learned and has one element, else an empty dict: a per-output $\sigma$ is applied at fit time. |

### unit

``` python
Gaussian.unit()
```

The likelihood the fit uses: unit noise for a learned sigma, else itself.

The copy keeps the class and every other field, so a subclass keeps its hooks (a mask, say).

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Self | A copy with a fixed unit sigma when sigma is learned, else `self` (by identity): a fixed sigma is applied at fit time. |
