# priors.Diagonal

``` python
priors.Diagonal(prior_precision=1.0, mean=None)
```

Diagonal Gaussian prior $\mathcal{N}(\mu, \operatorname{diag}(\lambda)^{-1})$.

On a per-block operator (`BlockDiag` or `Joint`), $\lambda$ is split in the operator’s block order, which must be the block order of `Layout.by_module` (as for every operator a fit returns). A hand-built one in another order gets the wrong entries.

`Diagonal()` starts every precision at 1, and `lk.laplace` expands it to one precision per parameter (`expand`) before the tune.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| prior_precision | float \| Tensor | Prior precisions $\lambda > 0$: a float or 0-dim tensor (the same for every parameter), or a vector `[P]` flat over all parameters, in mapping order. The marginal-likelihood tuner of `lk.laplace` tunes it by default. | `1.0` |
| mean | Params \| None | Prior mean $\mu$ as a parameter mapping. `None` means zero. | `None` |

## Methods

| Name | Description |
|----|----|
| [add_precision_to](#laplacekit.priors.Diagonal.add_precision_to) | `op` plus $\operatorname{diag}(\lambda)$. |
| [cov_mv](#laplacekit.priors.Diagonal.cov_mv) | Covariance times `v`: $v / \lambda$, elementwise. |
| [expand](#laplacekit.priors.Diagonal.expand) | The prior with one precision per parameter entry. |
| [location](#laplacekit.priors.Diagonal.location) | The mean, shaped like `like`. |
| [log_prob](#laplacekit.priors.Diagonal.log_prob) | Log density at `params`. |
| [tunable](#laplacekit.priors.Diagonal.tunable) | The precisions. |

### add_precision_to

``` python
priors.Diagonal.add_precision_to(op, /)
```

`op` plus $\operatorname{diag}(\lambda)$.

A per-block operator gets $\lambda$ split by block through `op.add_per_block`; any other operator gets `op.add_diagonal(prior_precision)`.

#### Parameters

| Name | Type | Description           | Default    |
|------|------|-----------------------|------------|
| op   | Op   | A precision operator. | *required* |

#### Returns

| Name | Type | Description                                            |
|------|------|--------------------------------------------------------|
|      | Op   | An operator of the same type, $\text{op} + \Lambda_0$. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `prior_precision` has another size than the per-block operator, or the operator can’t hold a diagonal (a `KronEigenbasis`, say). |

### cov_mv

``` python
priors.Diagonal.cov_mv(v, /)
```

Covariance times `v`: $v / \lambda$, elementwise.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| v | Params | Parameter mapping with the posterior’s keys and shapes. | *required* |

#### Returns

| Name | Type   | Description                      |
|------|--------|----------------------------------|
|      | Params | $\Sigma v$, a parameter mapping. |

### expand

``` python
priors.Diagonal.expand(params, /)
```

The prior with one precision per parameter entry.

#### Parameters

| Name   | Type   | Description                       | Default    |
|--------|--------|-----------------------------------|------------|
| params | Params | The parameters the prior is over. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Self | `self` when the precision is already a vector, else a copy whose precision is the scalar repeated over all entries of `params`. |

### location

``` python
priors.Diagonal.location(like, /)
```

The mean, shaped like `like`.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| like | Params | Parameter mapping whose names, shapes, dtypes and devices the mean must have. | *required* |

#### Returns

| Name | Type   | Description                    |
|------|--------|--------------------------------|
|      | Params | The mean, a parameter mapping. |

### log_prob

``` python
priors.Diagonal.log_prob(params, /)
```

Log density at `params`.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| params | Params | Parameter mapping with the posterior’s keys and shapes. | *required* |

#### Returns

| Name | Type   | Description                       |
|------|--------|-----------------------------------|
|      | Tensor | A 0-dim tensor, $\log q(\theta)$. |

### tunable

``` python
priors.Diagonal.tunable()
```

The precisions.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Tensor | The prior’s precision, which is a leaf of the prior and positive, since the tune works on its log. |
