Skip to content

log_signature

from tensorsig.classical.signatures import log_signature

Compute log-signatures for batched paths.

The log-signature is a compressed representation of the signature. Two bases are supported:

  • mode="words" (default): Signatory-style Lyndon words basis (triangular/gather projection)
  • mode="hall": classic Hall basis

Parameters:

Name Type Description Default
path Tensor

Tensor of shape (batch, length, dim) representing batched paths. For a single path, pass path.unsqueeze(0) to add a batch dimension.

required
depth int

Maximum depth to truncate log-signature computation. The output dimension is logsigdim(dim, depth) for mode="hall" and logsigdim_words(dim, depth) for mode="words".

required
stream bool

If True, computed log-signatures are returned for each step. Default is False.

False
method str

Computation method: "default" (signature then log) or "bch_sparse" (sparse Hall-BCH). The default remains signature then log because the ambient Hall bracket table and higher-order BCH expansion can be more expensive than tensor-algebra computation. Default is "default".

'default'
mode str

Basis for the log-signature coordinates: "words" (default) or "hall". "words" is only available with method="default".

'words'
sparse bool

If True, use sparse signature computation for paths with repeated points. Only applies when method="default". Default is False.

False
eps float

Threshold for change detection when using sparse mode. Default is 0.0.

0.0
lengths Tensor

Tensor of shape (batch,) with valid lengths for padded batches when using sparse mode. Default is None.

None

Returns:

Type Description
Tensor

If stream=False: Tensor of shape (batch, D) where D = logsigdim(dim, depth) for mode="hall" and D = logsigdim_words(dim, depth) for mode="words".

If stream=True: Tensor of shape (batch, length-1, D) with the same D definition as above.

Raises:

Type Description
ValueError

If path is not three-dimensional, if method is not "default" or "bch_sparse", or if an unsupported mode/method combination is requested.

Examples:

>>> import torch
>>> from tensorsig.algebra.hall import logsigdim
>>> from tensorsig.classical.signatures import log_signature
>>> from tensorsig.algebra.lyndon import logsigdim_words
>>>
>>> # Single path (add batch dimension)
>>> path = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]).unsqueeze(0)
>>> log_sig = log_signature(path, depth=2)
>>> log_sig.shape
torch.Size([1, 3])
>>> logsigdim_words(2, 2)
3
>>>
>>> # Batched paths
>>> batch_paths = torch.tensor([
...     [[0.0, 0.0], [1.0, 1.0]],
...     [[0.0, 0.0], [2.0, 2.0]],
... ])
>>> log_sig = log_signature(batch_paths, depth=2)
>>> log_sig.shape
torch.Size([2, 3])
>>>
>>> # Streaming log-signatures
>>> log_sig_stream = log_signature(path, depth=2, stream=True)
>>> log_sig_stream.shape
torch.Size([1, 2, 3])
>>>
>>> # Using the direct BCH method in Hall coordinates
>>> log_sig_bch = log_signature(path, depth=2, method="bch_sparse", mode="hall")
>>> log_sig_bch.shape
torch.Size([1, 3])
Source code in src/tensorsig/classical/log_signature.py
def log_signature(
    path: Tensor,
    depth: int,
    stream: bool = False,
    method: str = "default",
    mode: str = "words",
    sparse: bool = False,
    eps: float = 0.0,
    lengths: Tensor | None = None,
) -> Tensor:
    """Compute log-signatures for batched paths.

    The log-signature is a compressed representation of the signature. Two bases are
    supported:

    - ``mode=\"words\"`` (default): Signatory-style Lyndon words basis (triangular/gather projection)
    - ``mode=\"hall\"``: classic Hall basis

    Parameters
    ----------
    path : Tensor
        Tensor of shape ``(batch, length, dim)`` representing batched paths.
        For a single path, pass ``path.unsqueeze(0)`` to add a batch dimension.
    depth : int
        Maximum depth to truncate log-signature computation. The output dimension
        is ``logsigdim(dim, depth)`` for ``mode="hall"`` and
        ``logsigdim_words(dim, depth)`` for ``mode="words"``.
    stream : bool, optional
        If True, computed log-signatures are returned for each step. Default is False.
    method : str, optional
        Computation method: "default" (signature then log) or "bch_sparse"
        (sparse Hall-BCH). The default remains signature then log because the
        ambient Hall bracket table and higher-order BCH expansion can be more
        expensive than tensor-algebra computation. Default is "default".
    mode : str, optional
        Basis for the log-signature coordinates: "words" (default) or "hall".
        "words" is only available with ``method=\"default\"``.
    sparse : bool, optional
        If True, use sparse signature computation for paths with repeated points.
        Only applies when ``method=\"default\"``. Default is False.
    eps : float, optional
        Threshold for change detection when using sparse mode. Default is 0.0.
    lengths : Tensor, optional
        Tensor of shape ``(batch,)`` with valid lengths for padded batches when
        using sparse mode. Default is None.

    Returns
    -------
    Tensor
        If ``stream=False``: Tensor of shape ``(batch, D)`` where
        ``D = logsigdim(dim, depth)`` for ``mode=\"hall\"`` and
        ``D = logsigdim_words(dim, depth)`` for ``mode=\"words\"``.

        If ``stream=True``: Tensor of shape ``(batch, length-1, D)`` with
        the same ``D`` definition as above.

    Raises
    ------
    ValueError
        If ``path`` is not three-dimensional, if ``method`` is not
        "default" or "bch_sparse", or if an unsupported ``mode``/``method``
        combination is requested.

    Examples
    --------
    >>> import torch
    >>> from tensorsig.algebra.hall import logsigdim
    >>> from tensorsig.classical.signatures import log_signature
    >>> from tensorsig.algebra.lyndon import logsigdim_words
    >>>
    >>> # Single path (add batch dimension)
    >>> path = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]).unsqueeze(0)
    >>> log_sig = log_signature(path, depth=2)
    >>> log_sig.shape
    torch.Size([1, 3])
    >>> logsigdim_words(2, 2)
    3
    >>>
    >>> # Batched paths
    >>> batch_paths = torch.tensor([
    ...     [[0.0, 0.0], [1.0, 1.0]],
    ...     [[0.0, 0.0], [2.0, 2.0]],
    ... ])
    >>> log_sig = log_signature(batch_paths, depth=2)
    >>> log_sig.shape
    torch.Size([2, 3])
    >>>
    >>> # Streaming log-signatures
    >>> log_sig_stream = log_signature(path, depth=2, stream=True)
    >>> log_sig_stream.shape
    torch.Size([1, 2, 3])
    >>>
    >>> # Using the direct BCH method in Hall coordinates
    >>> log_sig_bch = log_signature(path, depth=2, method="bch_sparse", mode="hall")
    >>> log_sig_bch.shape
    torch.Size([1, 3])
    """
    if path.ndim != 3:
        msg = (
            f"Path must be of shape (batch, path_length, path_dim); got {path.shape}. "
            "Wrap a single path with path.unsqueeze(0)."
        )
        raise ValueError(msg)

    mode = (mode or "words").lower()
    if mode not in {"hall", "words"}:
        raise ValueError(f"Unsupported mode '{mode}'. Use 'hall' or 'words'.")

    method = (method or "default").lower()
    if method == "bch_sparse":
        if mode != "hall":
            raise ValueError("mode='words' is only supported with method='default'.")
        if not sparse_bch_supports_depth(depth):
            log_sig = _batch_log_signature(
                path,
                depth=depth,
                stream=stream,
                mode=mode,
                sparse=sparse,
                eps=eps,
                lengths=lengths,
            )
        else:
            log_sig = _batch_log_signature_bch(
                path,
                depth=depth,
                stream=stream,
            )
    elif method in {"default", None}:
        log_sig = _batch_log_signature(
            path,
            depth=depth,
            stream=stream,
            mode=mode,
            sparse=sparse,
            eps=eps,
            lengths=lengths,
        )
    else:
        raise ValueError(
            f"Unsupported method '{method}'. Use 'default' or 'bch_sparse'."
        )

    return log_sig