POSTERIOR_XLOGY

This function computes x \log(y) with numerical protection at x=0, where the term is defined to be zero instead of producing undefined arithmetic.

The stabilized elementwise transformation is:

f(x, y) = x\log(y), \quad \text{with } f(0, y)=0

This is useful in posterior information summaries such as entropy and cross-entropy terms where zero-probability weights should contribute exactly zero.

Excel Usage

=POSTERIOR_XLOGY(x, y)
  • x (list[list], required): 2D array of multipliers (unitless).
  • y (list[list], required): 2D array of logarithm arguments (unitless).

Returns (list[list]): Elementwise xlogy values as a 2D array.

Example 1: Stable xlogy terms for binary posterior probabilities

Inputs:

x y
0.5 0.5 0.5 0.5

Excel formula:

=POSTERIOR_XLOGY({0.5,0.5}, {0.5,0.5})

Expected output:

Result
-0.346574 -0.346574
Example 2: Zero multipliers produce zero contributions

Inputs:

x y
0 1 0.2 0.8

Excel formula:

=POSTERIOR_XLOGY({0,1}, {0.2,0.8})

Expected output:

Result
0 -0.223144
Example 3: Matrix input shape is preserved in output

Inputs:

x y
0.1 0.2 0.9 0.8
0.3 0.4 0.7 0.6

Excel formula:

=POSTERIOR_XLOGY({0.1,0.2;0.3,0.4}, {0.9,0.8;0.7,0.6})

Expected output:

Result
-0.0105361 -0.0446287
-0.107002 -0.20433
Example 4: Scalar inputs are normalized to single-cell 2D arrays

Inputs:

x y
0.25 0.5

Excel formula:

=POSTERIOR_XLOGY(0.25, 0.5)

Expected output:

-0.173287

Python Code

import numpy as np
from scipy.special import xlogy as scipy_xlogy

def posterior_xlogy(x, y):
    """
    Compute numerically stable x times log y terms for posterior information calculations.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.xlogy.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        x (list[list]): 2D array of multipliers (unitless).
        y (list[list]): 2D array of logarithm arguments (unitless).

    Returns:
        list[list]: Elementwise xlogy values as a 2D array.
    """
    try:
        def to2d(v):
            return [[v]] if not isinstance(v, list) else v

        def to_excel_2d(value):
            arr = np.asarray(value)
            if arr.ndim == 0:
                return [[float(arr)]]
            if arr.ndim == 1:
                return [[float(val) for val in arr.tolist()]]
            if arr.ndim == 2:
                return [[float(val) for val in row] for row in arr.tolist()]
            flat = arr.ravel()
            return [[float(val) for val in flat.tolist()]]

        x = to2d(x)
        y = to2d(y)

        if not isinstance(x, list) or not all(isinstance(row, list) for row in x):
            return "Error: x must be a 2D list"
        if not isinstance(y, list) or not all(isinstance(row, list) for row in y):
            return "Error: y must be a 2D list"

        try:
            x_arr = np.array(x, dtype=float)
            y_arr = np.array(y, dtype=float)
        except Exception:
            return "Error: x and y must contain numeric values"

        result = scipy_xlogy(x_arr, y_arr)
        return to_excel_2d(result)
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of multipliers (unitless).
2D array of logarithm arguments (unitless).