Skip to main content

utils

get_variable_name_and_lag

def get_variable_name_and_lag(node_name: NodeLike) -> Tuple[str, int]

Extract the variable name and time series lag from a node name.

A lag or future lag is indicated by the string 'lag(n=Y)' or 'future(n=Y)' where Y is the lag. The lag can be any integer. If these strings appear multiple times in a node name, then a ValueError is raised.

It is assumed the variable name and the lag or future lag are separated by a space. Anything prior to the space is considered the variable name.

Example: 'X lag(n=2)' -> 'X', -2 if lagged in the past, 'X future(n=2)' -> 'X', 2 if lagged in the future.

Arguments:

  • node_name: The name of the node.

Returns:

A tuple with the variable name and lag.

get_name_with_lag

def get_name_with_lag(variable_or_node_name: str, lag: int) -> str

Get the name of a lagged node.

If the lag is 0, then the variable name is returned. If the lag is not 0, then the old lag is removed and variable name is appended with the lag.

Example: 'X', -2 -> 'X lag(n=2)', # lagged in the past 'X', 2 -> 'X future(n=2)', # lagged in the future

Arguments:

  • variable_or_node_name: The name of the variable or node.
  • lag: The lag of the node.

Returns:

The name of the lagged node.

extract_names_and_lags

def extract_names_and_lags(
node_names: List[NodeLike]) -> Tuple[List[Dict[str, int]], int]

Extract the names and lags from a list of node names.

This is useful for converting a list of node names into a list of variable names and lags.

Example:

 ['X', 'Y', 'Z lag(n=2)'] -> [{'X': 0}, {'Y': 0}, {'Z': 2}], 2

Arguments:

  • node_names: List of node names.

Returns:

Tuple with the first element being a list of dictionaries with variable names and lags and the second element being the maximum lag.

pairwise

def pairwise(iterable: Iterable[T]) -> Iterable[Tuple[T, T]]

Equivalent to itertools.pairwise.

This is used because itertools.pairwise method is not evailable for Python 3.8 and 3.9.