opencr.inference.inverse
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.
Classes
| Name | Description |
|---|---|
Fit | The maximum of the posterior. |
Observation | A measured quantity. |
Functions
| Name | Description |
|---|---|
fit | Return the maximum of the posterior by least squares in the log parameters. |
forward_model | Return the forward model theta -> quantities of a CR model. |
posterior_grid | Return the posterior on a log-spaced grid of the parameters. |
Fitclassdataclass#
class Fit(parameters: dict[str, float], log_std: dict[str, float], chi2: float, success: bool)The maximum of the posterior.
Attributes
Observationclassdataclass#
class Observation(quantity: str, value: float, uncertainty: float = 0.1)A measured quantity.
Attributes
fitfunction#
def fit(forward: Forward, observations: Sequence[Observation], bounds: Mapping[str, tuple[float, float]], start: Mapping[str, float] | None = None) -> FitReturn the maximum of the posterior by least squares in the log parameters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
forward | Forward | required | The forward model, taking the parameters in the order of bounds. |
observations | Sequence[Observation] | required | The measurements. |
bounds | Mapping[str, tuple[float, float]] | required | The prior range of each parameter; a condition field’s in its unit (eV, m^-3) or as quantities. |
start | Mapping[str, float] | None | None | Starting values (default: the geometric centre of each range),
in the units of bounds. |
forward_modelfunction#
def forward_model(model: Model, conditions: Conditions, parameters: Sequence[str], outputs: Sequence[str], mode: str = 'quasi-static') -> ForwardReturn the forward model theta -> quantities of a CR model.
posterior_gridfunction#
def posterior_grid(forward: Forward, observations: Sequence[Observation], bounds: Mapping[str, tuple[float, float]], points: int = 25) -> xr.DatasetReturn the posterior on a log-spaced grid of the parameters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
forward | Forward | required | The forward model, taking the parameters in the order of bounds. |
observations | Sequence[Observation] | required | The measurements. |
bounds | Mapping[str, tuple[float, float]] | required | The prior range of each parameter; a condition field’s in its unit (eV, m^-3) or as quantities. |
points | int | 25 | The grid points per parameter. |
Returns
xr.Dataset- A dataset with
posterior(normalised over the grid, per log cell), xr.Datasetchi2, and the attributesbest(the grid maximum) and thexr.Dataset- posterior
meanandstdof each parameter’s logarithm.