Skip to content
Projects

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

NameDescription
FitThe maximum of the posterior.
ObservationA measured quantity.

Functions

NameDescription
fitReturn the maximum of the posterior by least squares in the log parameters.
forward_modelReturn the forward model theta -> quantities of a CR model.
posterior_gridReturn 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

NameTypeDescription
parametersdict[str, float]The best values by name.
log_stddict[str, float]The standard deviation of each parameter’s logarithm from the curvature (the Gauss-Newton covariance), by name.
chi2floatThe chi^2 at the best values.
successboolWhether the least-squares solver converged.

chi2attributeinstance attribute#

chi2: float

log_stdattributeinstance attribute#

log_std: dict[str, float]

parametersattributeinstance attribute#

parameters: dict[str, float]

successattributeinstance attribute#

success: bool

Observationclassdataclass#

class Observation(quantity: str, value: float, uncertainty: float = 0.1)

A measured quantity.

Attributes

NameTypeDescription
quantitystrThe name of the quantity.
valuefloatThe measured value.
uncertaintyfloatThe standard deviation of its logarithm (about the relative uncertainty).

quantityattributeinstance attribute#

quantity: str

uncertaintyattributeclass attributeinstance attribute#

uncertainty: float = 0.1

valueattributeinstance attribute#

value: float

fitfunction#

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

NameTypeDefaultDescription
forwardForwardrequiredThe forward model, taking the parameters in the order of bounds.
observationsSequence[Observation]requiredThe measurements.
boundsMapping[str, tuple[float, float]]requiredThe prior range of each parameter; a condition field’s in its unit (eV, m^-3) or as quantities.
startMapping[str, float] | NoneNoneStarting 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') -> Forward

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

Parameters

NameTypeDefaultDescription
modelModelrequiredThe model.
conditionsConditionsrequiredThe conditions the parameters are set in.
parametersSequence[str]requiredThe condition fields (as for opencr.scan).
outputsSequence[str]requiredThe quantities.
modestr'quasi-static'The steady-state mode.

posterior_gridfunction#

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

NameTypeDefaultDescription
forwardForwardrequiredThe forward model, taking the parameters in the order of bounds.
observationsSequence[Observation]requiredThe measurements.
boundsMapping[str, tuple[float, float]]requiredThe prior range of each parameter; a condition field’s in its unit (eV, m^-3) or as quantities.
pointsint25The 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.