# opencr.inference.inverse

*module*

Inverse modelling: plasma parameters from measured quantities such as line ratios.

Given measured quantities ``y_i`` (`opencr.analysis.quantities`, e.g.
Balmer line ratios) with relative uncertainties ``s_i``, and parameters
``theta`` of the conditions (``electron_temperature``, ``electron_density``,
``density:<state>``) with log-uniform priors on ``[low, high]``, the
posterior is

    p(theta | y) ~ exp(-chi^2 / 2),   chi^2 = sum_i (ln m_i(theta) - ln y_i)^2 / s_i^2,

with log-normal errors (``s_i`` is the standard deviation of ``ln y_i``,
about the relative uncertainty). `posterior_grid` evaluates it on a grid
of the parameters (exact for one to three parameters) and `fit` finds
the maximum by least squares in the log parameters. The forward model is the
CR model, or any callable with the same signature, e.g. a trained surrogate
(`opencr.ml.Surrogate.forward`), for speed.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L1-L1)

## opencr.inference.inverse.Fit

*class* · *dataclass*

```python
class Fit
```

The maximum of the posterior.

**Attributes**

- `parameters` (`dict[str, float]`) — The best values by name.
- `log_std` (`dict[str, float]`) — The standard deviation of each parameter's logarithm from the curvature (the Gauss-Newton covariance), by name.
- `chi2` (`float`) — The chi^2 at the best values.
- `success` (`bool`) — Whether the least-squares solver converged.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L161-L176)

### opencr.inference.inverse.Fit.chi2

*attribute* · *instance attribute*

```python
chi2: float
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L175-L175)

### opencr.inference.inverse.Fit.log_std

*attribute* · *instance attribute*

```python
log_std: dict[str, float]
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L174-L174)

### opencr.inference.inverse.Fit.parameters

*attribute* · *instance attribute*

```python
parameters: dict[str, float]
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L173-L173)

### opencr.inference.inverse.Fit.success

*attribute* · *instance attribute*

```python
success: bool
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L176-L176)

## opencr.inference.inverse.Observation

*class* · *dataclass*

```python
class Observation
```

A measured quantity.

**Attributes**

- `quantity` (`str`) — The name of the quantity.
- `value` (`float`) — The measured value.
- `uncertainty` (`float`) — The standard deviation of its logarithm (about the relative uncertainty).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L40-L60)

### opencr.inference.inverse.Observation.quantity

*attribute* · *instance attribute*

```python
quantity: str
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L51-L51)

### opencr.inference.inverse.Observation.uncertainty

*attribute* · *class attribute* · *instance attribute*

```python
uncertainty: float = 0.1
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L53-L53)

### opencr.inference.inverse.Observation.value

*attribute* · *instance attribute*

```python
value: float
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L52-L52)

## opencr.inference.inverse.fit

*function*

```python
def fit(forward: Forward, observations: Sequence[Observation], bounds: Mapping[str, tuple[float, float]], start: Mapping[str, float] | None = None) -> Fit
```

Return the maximum of the posterior by least squares in the log parameters.

**Parameters**

- `forward` (`Forward`) — The forward model, taking the parameters in the order of ``bounds``.
- `observations` (`Sequence[Observation]`) — The measurements.
- `bounds` (`Mapping[str, tuple[float, float]]`) — The prior range of each parameter; a condition field's in its unit (eV, m^-3) or as quantities.
- `start` (`Mapping[str, float] | None`) (default: `None`) — Starting values (default: the geometric centre of each range), in the units of ``bounds``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L179-L222)

## opencr.inference.inverse.forward_model

*function*

```python
def forward_model(model: Model, conditions: Conditions, parameters: Sequence[str], outputs: Sequence[str], mode: str = 'quasi-static') -> Forward
```

Return the forward model ``theta -> quantities`` of a CR model.

**Parameters**

- `model` (`Model`) — The model.
- `conditions` (`Conditions`) — The conditions the parameters are set in.
- `parameters` (`Sequence[str]`) — The condition fields (as for `opencr.scan`).
- `outputs` (`Sequence[str]`) — The quantities.
- `mode` (`str`) (default: `'quasi-static'`) — The steady-state mode.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L63-L88)

## opencr.inference.inverse.posterior_grid

*function*

```python
def posterior_grid(forward: Forward, observations: Sequence[Observation], bounds: Mapping[str, tuple[float, float]], points: int = 25) -> xr.Dataset
```

Return the posterior on a log-spaced grid of the parameters.

**Parameters**

- `forward` (`Forward`) — The forward model, taking the parameters in the order of ``bounds``.
- `observations` (`Sequence[Observation]`) — The measurements.
- `bounds` (`Mapping[str, tuple[float, float]]`) — The prior range of each parameter; a condition field's in its unit (eV, m^-3) or as quantities.
- `points` (`int`) (default: `25`) — The grid points per parameter.

**Returns**

- (`xr.Dataset`) — A dataset with ``posterior`` (normalised over the grid, per log cell),
- (`xr.Dataset`) — ``chi2``, and the attributes ``best`` (the grid maximum) and the
- (`xr.Dataset`) — posterior ``mean`` and ``std`` of each parameter's logarithm.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/inference/inverse.py#L111-L158)
