MedianScore#

class bayesflow.scores.MedianScore(**kwargs)[source]#

Bases: NormedDifferenceScore

\(S(\hat \theta, \theta) = | \hat \theta - \theta |\)

Scores a predicted median with the absolute error score.

NOT_TRANSFORMING_LIKE_VECTOR_WARNING = ()#

This variable contains names of prediction heads that should lead to a warning when the adapter is applied in inverse direction to them.

Prediction heads can output estimates in spaces other than the target distribution space. To such estimates the adapter cannot be straightforwardly applied in inverse direction, because the adapter is built to map vectors from the inference variable space. When subclassing ScoringRule, add the names of such heads to the following list to warn users about difficulties with a type of estimate whenever the adapter is applied to them in inverse direction.

classmethod from_config(config)#
get_config()#
get_head(key: str, output_shape: tuple[int, ...]) Sequential#

For a specified head key and output shape, request corresponding head network.

A head network has the following components that are called sequentially:

  1. subnet: A keras.Layer.

  2. dense: A trainable linear projection with as many units as are required by the next component.

  3. reshape: Changes shape of output of projection to match requirements of next component.

  4. link: Transforms unconstrained values into a constrained space for the final estimator. See links for examples.

This method initializes the components in reverse order to meet all requirements and returns them.

Parameters:
keystr

Name of head for which to request a link.

output_shape: Shape

The necessary shape of estimated values for the given key as returned by get_head_shapes_from_target_shape().

Returns:
headkeras.Sequential

Head network consisting of a learnable projection, a reshape and a link operation to parameterize estimates.

get_head_shapes_from_target_shape(target_shape: tuple[int, ...])#

Request a dictionary of names and output shapes of required heads from the score.

For a specified key, request a link from network output to estimation target.

If no link was specified for the key (e.g. upon initialization), return a linear activation.

Parameters:
keystr

Name of head for which to request a link.

Returns:
linkkeras.Layer

Activation function linking network output to estimation target.

get_subnet(key: str) Layer#

For a specified key, request a subnet to be used for projecting the shared condition embedding before further projection and reshaping to the heads output shape.

If no subnet was specified for the key (e.g. upon initialization), return just an instance of keras.layers.Identity.

Parameters:
keystr

Name of head for which to request a subnet.

Returns:
linkkeras.Layer

Subnet projecting the shared condition embedding.

score(estimates: dict[str, Tensor], targets: Tensor, weights: Tensor = None) Tensor#

Computes the scoring function based on the absolute difference between estimates and targets.

\(S(\hat \theta, \theta; k) = | \hat \theta - \theta |^k\)

This function extracts the Tensor named "value" from the estimates dictionary and computes the element-wise absolute difference between the estimates and the true targets. The difference is then exponentiated by k. The final score is computed using the aggregate() method, which optionally applies weighting.

Parameters:
estimatesdict[str, Tensor]

A dictionary containing tensors of estimated values. The “value” key must be present.

targetsTensor

A tensor of true target values.

weightsTensor, optional

A tensor of weights corresponding to each estimate-target pair. If provided, it is used to compute a weighted aggregate score.

Returns:
Tensor

The aggregated score based on the element-wise absolute difference raised to the power of self.k, optionally weighted.

k#

Exponent to absolute difference