Project: s41467-024-48578-6 | Function: intra_order_correlation_matrix_by_order (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Computation of intra-order correlation matrix for temporal hypergraphs implementing the mathematical framework for characterizing temporal dependencies within hyperedges of the same order, using centered adjacency matrices and temporal lag analysis to quantify mesoscopic persistent structures

**KEY TERMS:** {
  "correlation_matrix": "C^(d)(τ) matrix encoding temporal correlations",
  "annealed_adjacency_mtx": "μ^(d) time-averaged adjacency matrix",
  "centered_adjacency_matrix_t": "[A^(d)(t) - μ^(d)] centered adjacency at time t",
  "centered_adjacency_matrix_t_lagged": "[A^(d)(t+τ) - μ^(d)] centered adjacency at lag τ",
  "factorial(order)": "(d-1)! normalization factor",
  "T": "total number of time steps",
  "tau": "τ temporal lag parameter"
}

**CONTEXT CODE:** from typing import Dict

import numpy as np
from scipy import sparse
from scipy.special import factorial

from hypergraphx.linalg import (
    annealed_adjacency_matrix,
)


def intra_order_correlation_matrix_by_order(
    adjacency_matrices_all_orders: Dict[int, Dict[int, sparse.csc_array]],
    annealed_adjacency_matrices_all_orders=None,
    order=1,
    tau=0,
) -> sparse.csc_array:
    """Compute the intra-order correlation matrix for hyperedges of order d and time lag tau.

    Parameters
    ----------
    adjacency_matrices_all_orders: a dictionary {order : {time : adjacency matrix}}.
    annealed_adjacency_matrices_all_orders: a dictionary {order : annealed adjacency matrix}.
    order: the order.
    tau: the temporal lag.

    Returns
    -------
    The intra-order correlation matrix of order d at time lag tau, as a sparse matrix.
    """
    raise NotImplementedError("Function has been masked for testing purposes")


def intra_order_correlation_function_by_order(
    adjacency_matrices_all_orders: Dict[int, Dict[int, sparse.csc_array]],
    annealed_adjacency_matrices_all_orders=None,
    order=1,
    tau=0,
) -> float:
    """Compute the intra-order correlation function for hyperedges of order d and time lag tau.

    Parameters
    ----------
    adjacency_matrices_all_orders: a dictionary {order : {time : adjacency matrix}}.
    annealed_adjacency_matrices_all_orders: a dictionary {order : annealed adjacency matrix}.
    order: the order.
    tau: the temporal lag.

    Returns
    -------
    The intra-order correlation function of order d at time lag tau.
    """
    correlation_matrix = intra_order_correlation_matrix_by_order(
        adjacency_matrices_all_orders,
        annealed_adjacency_matrices_all_orders,
        order,
        tau,
    )
    correlation_function = correlation_matrix.trace()

    return correlation_function


def intra_order_correlation_matrices_all_orders(
    adjacency_matrices_all_orders: Dict[int, Dict[int, sparse.csc_array]],
    annealed_adjacency_matrices_all_orders=None,
    max_order=None,
    tau=0,
):  # -> Tuple[sparse.csc_array]: ### to check
    """Compute the intra-order correlation matrices for hyperedges of all orders and time lag tau.

    Parameters
    ----------
    adjacency_matrices_all_orders: a dictionary {order : {time : adjacency matrix}}.
    annealed_adjacency_matrices_all_orders: a dictionary {order : annealed adjacency matrix}.
    max_order: the maximum order.
    tau: the temporal lag.

    Returns
    -------
    The intra-order correlation matrices for all orders at time lag tau, as a dictionary {order : sparse matrix}.
    """

    correlation_matrices = dict()
    if max_order == None:
        max_order = max(adjacency_matrices_all_orders.keys())
    for order in range(1, max_order + 1):
        correlation_matrix = intra_order_correlation_matrix_by_order(
            adjacency_matrices_all_orders,
            annealed_adjacency_matrices_all_orders,
            order,
            tau,
        )
        correlation_matrices[order] = correlation_matrix

    return correlation_matrices


def intra_order_correlation_functions_all_orders(
    adjacency_matrices_all_orders: Dict[int, Dict[int, sparse.csc_array]],
    annealed_adjacency_matrices_all_orders=None,
    max_order=None,
    tau=0,
):  # -> Tuple[float]: ### to check
    """Compute the intra-order correlation function for hyperedges of every order and time lag tau.

    Parameters
    ----------
    adjacency_matrices_all_orders: a dictionary {order : {time : adjacency matrix}}.
    annealed_adjacency_matrices_all_orders: a dictionary {order : annealed adjacency matrix}.
    max_order: the maximum order.


Complete the function implementation replacing "raise NotImplementedError".