Skip to content

crashmetrics.uncertainty

uncertainty

Probabilistic ratings: how likely is a score or a star rating?

A crash test or simulation gives one result, but the same design scatters from test to test (dummy positioning, material, airbag timing) and a surrogate model has its own error. monte_carlo varies the inputs of any scoring function and reports the spread of its outputs, so a result can be stated as "84 % chance of 5 stars" or "HIC below 700 with 97 % probability" instead of one number.

Inputs scatter by:

  • normal: absolute standard deviation (normal(25.0) for HIC);
  • relative: coefficient of variation (relative(0.08): 8 %);
  • uniform: plus or minus a range around the nominal value;
  • scale: a multiplier around 1 (e.g. for a whole time history).

Functions:

  • monte_carlo: Sample the inputs and run the function; returns a MonteCarloResult with probabilities, distributions and percentiles.
  • star_probabilities: Probability of each star rating.

Scatter dataclass

Scatter(kind: str, size: float, nonnegative: bool = True)

Scatter of one input around its nominal value.

kind instance-attribute

kind: str

normal, relative, uniform or scale.

nonnegative class-attribute instance-attribute

nonnegative: bool = True

Clip samples at zero (criteria values are magnitudes).

MonteCarloResult dataclass

MonteCarloResult(
    inputs: dict[str, NDArray[float64]],
    outputs: dict[str, NDArray[float64]],
    nominal: dict[str, float],
)

Inputs and outputs of a Monte Carlo study.

inputs instance-attribute

inputs: dict[str, NDArray[float64]]

Sampled inputs (nominal values repeated for inputs without scatter).

outputs instance-attribute

outputs: dict[str, NDArray[float64]]

One array per output ("value" if the function returns a number).

nominal instance-attribute

nominal: dict[str, float]

Outputs of the nominal (unscattered) inputs.

rows

rows() -> list[dict[str, float]]

One dict per sample with inputs and outputs, e.g. for crashmetrics.sensitivity.rank_sensitivity or a CSV file.

probability

probability(
    output: str | None = None,
    *,
    at_least: float | None = None,
    below: float | None = None,
    equals: float | None = None,
) -> float

Share of samples meeting a condition, e.g. probability("stars", at_least=5) or probability("hic15", below=700).

distribution

distribution(
    output: str | None = None,
) -> dict[float, float]

Probability of each distinct value (for discrete outputs such as stars), sorted by value.

percentiles

percentiles(
    output: str | None = None,
    q: Sequence[float] = (5.0, 50.0, 95.0),
) -> dict[float, float]

Percentiles of an output.

summary

summary(outputs: Sequence[str] | None = None) -> str

Nominal value, mean and 5 / 50 / 95 % percentiles per output.

normal

normal(sd: float, nonnegative: bool = True) -> Scatter

Normal scatter with an absolute standard deviation.

relative

relative(cv: float, nonnegative: bool = True) -> Scatter

Normal scatter with a relative standard deviation (0.05 = 5 %).

uniform

uniform(
    half_range: float, nonnegative: bool = True
) -> Scatter

Uniform scatter: nominal plus or minus half_range.

scale

scale(cv: float) -> Scatter

A multiplier around 1 with relative standard deviation cv; use with nominal 1.0 to scale signals or groups of values together.

monte_carlo

monte_carlo(
    func: Callable[
        [dict[str, float]], float | Mapping[str, float]
    ],
    nominal: Mapping[str, float],
    scatter: Mapping[str, Scatter],
    n: int = 1000,
    seed: int | Generator | None = None,
) -> MonteCarloResult

Run func on n scattered copies of the inputs.

Parameters:

Name Type Description Default
func Callable[[dict[str, float]], float | Mapping[str, float]]

Takes a dict of input values and returns a number or a dict of numbers (e.g. {"points": 26.1, "stars": 4}).

required
nominal Mapping[str, float]

Nominal value of every input.

required
scatter Mapping[str, Scatter]

Scatter of the inputs that vary (others stay nominal).

required
n int

Number of samples.

1000
seed int | Generator | None

Random seed or generator, for repeatable results.

None

star_probabilities

star_probabilities(
    result: MonteCarloResult,
    output: str = "stars",
    max_stars: int = 5,
) -> dict[int, float]

Probability of each star rating 0..max_stars.