opencr.inference
Sensitivities, uncertainty propagation and inverse modelling on top of the solver.
sensitivitiesandpropagate(opencr.inference.sensitivity): how results depend on the rate data, and their uncertainty from the data’s;posterior_gridandfit(opencr.inference.inverse): plasma parameters from measured quantities such as line ratios.
Classes
| Name | Description |
|---|---|
Fit | The maximum of the posterior. |
Observation | A measured quantity. |
Propagation | The propagated uncertainty of some quantities. |
Functions
| Name | Description |
|---|---|
data_uncertainties | Return the relative uncertainty of each reaction's data from its provenance. |
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. |
propagate | Propagate the uncertainties of the rate data to the quantities outputs. |
sensitivities | Return d ln q / d ln k_r of the quantities outputs. |
Modules
inverseInverse modelling: plasma parameters from measured quantities such as line ratios.sensitivitySensitivities of CR results to the rate data, and the propagation of their uncertainties.
Fitclassdataclass#
opencr.inference.inverseView sourceclass Fit(parameters: dict[str, float], log_std: dict[str, float], chi2: float, success: bool)The maximum of the posterior.
Attributes
Observationclassdataclass#
opencr.inference.inverseView sourceclass Observation(quantity: str, value: float, uncertainty: float = 0.1)A measured quantity.
Attributes
Propagationclassdataclass#
opencr.inference.sensitivityView sourceclass Propagation(outputs: tuple[str, ...], nominal: np.ndarray, relative: np.ndarray, contributions: xr.DataArray | None, samples: np.ndarray | None, method: str)The propagated uncertainty of some quantities.
Attributes
| Name | Type | Description |
|---|---|---|
outputs | tuple[str, ...] | The quantity names. |
nominal | np.ndarray | The nominal values. |
relative | np.ndarray | The relative (log) standard deviations, sigma(ln q). |
contributions | xr.DataArray | None | For the linear method, (S_qr sigma_r)^2 over
(output, reaction); None for Monte Carlo. |
samples | np.ndarray | None | For Monte Carlo, the sampled values (sample, output). |
method | str | "linear" or "monte-carlo". |
contributionsattributeinstance attribute#
contributions: xr.DataArray | Nonemethodattributeinstance attribute#
method: strnominalattributeinstance attribute#
nominal: np.ndarrayoutputsattributeinstance attribute#
outputs: tuple[str, ...]relativeattributeinstance attribute#
relative: np.ndarraysamplesattributeinstance attribute#
samples: np.ndarray | Nonedominantmethod#
def dominant(output: str, number: int = 5) -> list[tuple[str, float]]Return the reactions contributing most to the variance of output (linear only).
intervalmethod#
def interval(coverage: float = 0.68) -> tuple[np.ndarray, np.ndarray]Return the (lower, upper) bounds of a log-normal interval of coverage.
to_datasetmethod#
def to_dataset() -> xr.DatasetReturn the nominal values and the relative uncertainties as a dataset.
data_uncertaintiesfunction#
opencr.inference.sensitivityView sourcedef data_uncertainties(model: Model, default: float | None = None) -> dict[str, float]Return the relative uncertainty of each reaction’s data from its provenance.
Reactions without a stated uncertainty get default (or are left out
when it is None); detailed-balance inverses are never listed.
fitfunction#
opencr.inference.inverseView sourcedef 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#
opencr.inference.inverseView sourcedef 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.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | Model | required | The model. |
conditions | Conditions | required | The conditions the parameters are set in. |
parameters | Sequence[str] | required | The condition fields (as for opencr.scan). |
outputs | Sequence[str] | required | The quantities. |
mode | str | 'quasi-static' | The steady-state mode. |
posterior_gridfunction#
opencr.inference.inverseView sourcedef 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.
propagatefunction#
opencr.inference.sensitivityView sourcedef propagate(model: Model, conditions: Conditions, outputs: Sequence[str], uncertainties: Mapping[str, float] | None = None, method: str = 'linear', samples: int = 200, seed: int = 0, mode: str = 'quasi-static', **options: Any) -> PropagationPropagate the uncertainties of the rate data to the quantities outputs.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | Model | required | The model. |
conditions | Conditions | required | The conditions of the steady state. |
outputs | Sequence[str] | required | Names of quantities. |
uncertainties | Mapping[str, float] | None | None | Relative (1 sigma, log-normal) uncertainty by reaction
name; by default data_uncertainties of the model. |
method | str | 'linear' | "linear" (first order, through sensitivities) or
"monte-carlo" (independent log-normal factors). |
samples | int | 200 | The number of Monte Carlo samples. |
seed | int | 0 | The seed of the Monte Carlo samples. |
mode | str | 'quasi-static' | The steady-state mode. |
**options | Any | {} | Passed on to sensitivities (step). |
Numerics
linear
monte carlo
interval
Assumptions:
- independent, log-normal data uncertainties (1 sigma), from each reaction’s provenance by default
- the uncertainty of the data only, not of a surrogate
Built from:
opencr.inference.sensitivity.sensitivities
sensitivitiesfunction#
opencr.inference.sensitivityView sourcedef sensitivities(model: Model, conditions: Conditions, outputs: Sequence[str], reactions: Sequence[str] | None = None, mode: str = 'quasi-static', step: float = 0.01) -> xr.DataArrayReturn d ln q / d ln k_r of the quantities outputs.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
model | Model | required | The model. |
conditions | Conditions | required | The conditions of the steady state. |
outputs | Sequence[str] | required | Names of quantities (opencr.analysis.quantities). |
reactions | Sequence[str] | None | None | The reactions to perturb; by default every reaction that is not a detailed-balance inverse. |
mode | str | 'quasi-static' | The steady-state mode. |
step | float | 0.01 | The step in ln k. |
Returns
xr.DataArray- A DataArray over (
output,reaction).
Numerics
sensitivity
Order of accuracy: 2.
Assumptions:
- central differences in , step =
step(0.01 by default); the order is in - a detailed-balance inverse moves with its forward reaction (
Model.scaled_rates), so detailed balance holds
Built from:
opencr.model.Model.scaled_rates