# core.CurvatureFit

``` python
core.CurvatureFit(
    model=static(),
    likelihood=static(),
    params,
    curvature=static(),
    caps=None,
    *,
    _seed=_Seed.draw(),
)
```

A model, likelihood, params and single-pass method bound for fitting.

The fit steps are the plain fold, plus `start()` for uniform code. Build it with `fit_curvature`, which checks the likelihood.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| model | ModelFn | The model as a function of the params. | `static()` |
| likelihood | Likelihood | The likelihood. The curvature sees its unit version (see `LateLikelihood`). | `static()` |
| params | Params | The params the curvature is taken at. | *required* |
| curvature | Curvature\[S, Op\] | The single-pass curvature. | `static()` |
| caps | Capabilities \| None | The probed capabilities. `None` skips the model checks. | `None` |

## Attributes

| Name | Description |
|----|----|
| [ctx](#laplacekit.core.CurvatureFit.ctx) | The context the curvature sees, with the unit likelihood. |

## Methods

| Name | Description |
|----|----|
| [finalize](#laplacekit.core.CurvatureFit.finalize) | The curvature from the statistic over all the data. |
| [init](#laplacekit.core.CurvatureFit.init) | The empty statistic. |
| [posterior](#laplacekit.core.CurvatureFit.posterior) | Build the posterior without repeating the model, likelihood, and layout. |
| [start](#laplacekit.core.CurvatureFit.start) | The first (and only) pass, for code that also runs multi-pass methods. |
| [update](#laplacekit.core.CurvatureFit.update) | Fold one batch into the statistic. |

### finalize

``` python
core.CurvatureFit.finalize(stat, /)
```

The curvature from the statistic over all the data.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| stat | FitStat\[S\] | The statistic over all the data, after `all_reduce`. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Op | The curvature’s `finalize`: the per-example average $\bar{C}$. The posterior multiplies it by `n_data`. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | ValueError | The curvature’s `finalize` refuses `stat`, e.g. a zero total weight. |

### init

``` python
core.CurvatureFit.init()
```

The empty statistic.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | FitStat\[S\] | The identity of `+`: the curvature’s `init`, no data-fit term and a zero count. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | With `caps`, the capabilities lack something the curvature needs. |
|  | StructureError | The curvature refuses the params or layout. |
|  | ValueError | The curvature refuses its settings for these params (e.g. `LowRankGGN` with `rank + oversample` above the parameter count). |

### posterior

``` python
core.CurvatureFit.posterior(curvature, /, prior, n_data)
```

Build the posterior without repeating the model, likelihood, and layout.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| curvature | C | The finalized curvature, the per-example average. | *required* |
| prior | Pr | The prior. Its location must match the params. | *required* |
| n_data | Tensor | The effective data size $N$ the curvature is multiplied by. | *required* |

#### Returns

| Name | Type                      | Description                                |
|------|---------------------------|--------------------------------------------|
|      | LaplacePosterior\[C, Pr\] | The Laplace posterior at the fit’s params. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | The prior’s location doesn’t match the params, or the curvature can’t absorb the prior precision (e.g. KFAC on KFAC). |

### start

``` python
core.CurvatureFit.start()
```

The first (and only) pass, for code that also runs multi-pass methods.

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Running\[Op\] | A `Running` step whose `finish` gives the curvature as `Finished.result`. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | With `caps`, the capabilities lack something the curvature needs. |

### update

``` python
core.CurvatureFit.update(stat, batch, /)
```

Fold one batch into the statistic.

A `HasTargetCheck` likelihood checks the batch’s targets first.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| stat | FitStat\[S\] | The statistic so far. | *required* |
| batch | Batch | The next batch. Its generator comes from the fit’s seed and the examples seen before it. | *required* |

#### Returns

| Name | Type         | Description                  |
|------|--------------|------------------------------|
|      | FitStat\[S\] | `stat` with the batch added. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | CapabilityError | With `caps`, the batch fails the curvature’s requirements (`Requirements.check_batch`). With or without `caps`, the curvature needs targets the batch lacks. |
|  | ValueError | The likelihood’s `check_targets` finds a bad target value. |
|  | StructureError | The likelihood’s `check_targets` finds badly shaped targets, or a `LateLikelihood` has an active mask, which `fit_stat` can’t apply. |
