correlation_loss

Contents

correlation_loss#

class braintools.fit.losses.correlation_loss(prediction, target, *, per_channel=False)#

Compute one minus the Pearson correlation between two time series.

\[L = 1 - r(\hat y, y), \qquad r = \frac{\sum_i (\hat y_i - \bar{\hat y})(y_i - \bar y)} {\sqrt{\sum_i (\hat y_i - \bar{\hat y})^2 \sum_i (y_i - \bar y)^2}}.\]

The loss lies in [0, 2] and is invariant to the scale and offset of either input.

Parameters:
  • prediction (Any) – Model output, time on axis 0.

  • target (Any) – Data of the same shape. Units are not compared (the loss is scale-invariant); only the magnitudes are used.

  • per_channel (bool) – If False, correlate the flattened arrays. If True, correlate each column along the time axis 0 and average the correlations over the trailing axes.

Returns:

Scalar loss.

Return type:

Array

Raises:

ValueError – If the shapes differ.

Notes

A constant input (zero variance) has an undefined correlation, which is taken to be 0 (loss 1), as functional_connectivity() does; its gradient is then 0 rather than nan. Works under jax.jit and jax.vmap and is differentiable.

Examples

>>> import jax.numpy as jnp
>>> from braintools.fit import losses
>>> t = jnp.linspace(0., 6., 50)
>>> round(float(losses.correlation_loss(3. * jnp.sin(t) + 1., jnp.sin(t))), 6)
0.0
>>> round(float(losses.correlation_loss(-jnp.sin(t), jnp.sin(t))), 6)
2.0