The TimeShift class#

Aliases#

halerium.core.operator.TimeShift
class TimeShift(operand, shift, initial_values=0.0)#

Shift the time axis index.

Shift the time-axis index by a given non-positive shift. In time-series analysis, this operator is also known as the lag operator or backshift operator (see, e.g., https://en.wikipedia.org/wiki/Lag_operator).

The operator acts on the time axis which is identified with the data axis and thus not otherwise accessible to the user for manipulation within graphs. For shifts affecting other axes, the user should employ the Slice operator.

Parameters:
  • operand (Operator) – The input of the operation.

  • shift (int) – The time index shift. Only non-positive shifts are accepted to prevent constructing of graphs where effects happen before their causes.

  • initial_values (int, float, np.ndarray, Operator) – The values for the result when the shifted time index goes outside the range of the operand. The default is 0. The shape of the initial_values must equal (-shift, *operand.shape) or be such that it can be broadcast to that shape.

Examples

The primary purpose of the TimeShift operator is for building statistical time-series models. For example, a simple normal random walk can be modelled by:

>>> from halerium.core import Graph, Variable, StaticVariable
>>> from halerium.core import get_generative_model, get_data_linker
>>> from halerium.core.operator import TimeShift
>>>
>>> with Graph('random_walk') as random_walk:
>>>     initial_positions = StaticVariable('initial_positions',
>>>                                        shape=(1,))
>>>     position = Variable('position', variance=1)
>>>     position.mean = TimeShift(position,
>>>                               shift=-1,
>>>                               initial_values=initial_positions)
>>>
>>> random_walk_model = get_generative_model(
>>>     graph=random_walk,
>>>     data=get_data_linker(
>>>         data={random_walk.initial_positions: [0.]},
>>>         graph=random_walk,
>>>         n_data=10))
>>>
>>> random_walk_model.get_example(random_walk.position)
accept(visitor)#
depends_on(operators, stop_operators=None, stop_criterion=None, include_links=True)#

Check if self depends on operators.

Check whether the operator depends on another operator. The chain of dependencies will be interrupted by the operators in stop_operators or the stop_criterion.

Parameters:
  • operators (Iterable, OperatorBase,) – The operators for which the possible influence on self is to be checked.

  • stop_operators (iterable, optional) – The operators which interrupt the dependency chain. The default is None.

  • stop_criterion (callable, optional) – A callable that accepts an operator as an argument and returns True for stop and False otherwise. The default is None which means don’t stop.

  • include_links (bool, optional) – Whether a linked variable if viewed as dependent on the linked source.

Returns:

result – Whether self depends on operators

Return type:

bool

Examples

>>> import halerium
>>> a = halerium.core.operator.Const(0.)
>>> b = a + 1
>>> c = b + 1
>>> c.depends_on(a)
True
>>> c.depends_on(a, stop_operators=[b])
False
evaluate(cache=None)#

Evaluate operator.

Evaluate the operator and return a numerical value for it. This involves evaluating all the operator’s operands, all operands of those operands, and so on. When evaluating a variable, a value from its distribution is drawn.

Values computed for operands are stored in a cache for reuse should such a value be required again during the evaluation (e.g. when an operator appears more than once in the operand tree). That cache can be provided with values for operators by the user to be used during evaluation.

Parameters:

cache (dict, optional) – The cache used to store values of intermediate results for later reuse in case the corresponding expression is encountered again during evaluation. The cache can be used to provide values for operators that might be encountered during evaluation.

Returns:

The result of evaluating the operator. A number or numpy ndarray.

Return type:

result

Examples

>>> import halerium.core as hal
>>>
>>> x = hal.Variable('x', shape=(), mean=0, variance=1)
>>> y = hal.exp(x) + 1
>>> z = hal.log(y - 1)
>>> z.evaluate({x: 3})
3.0
property expected_initial_values_shape#
get_containing_class(scopetor_cls)#

Get the closest parent that is of a specific type.

Parameters:

scopetor_cls (scopetor subclass) – The scopetor subclass of which the next containing instance is to be found.

Raises:

ValueError – If no containing scopetor of type scopetor_cls is found, an error is raised.

Returns:

scope – The containing scopetor of type scopetor_cls.

Return type:

scopetor_cls

Examples

>>> from halerium.core import Graph, Entity
>>> g = Graph("g")
>>> with g:
>>>     e = Entity("e")
>>>     with e:
>>>         Entity("m")
>>> g.e.m.get_containing_class(Graph) is g
True
>>> assert g.e.m.get_containing_class(Entity) is g.e
True
get_immediate_dependencies(follow_links)#

Get immediate dependencies.

Get the immediate dependencies of the operator. - If follow_links, for operators (variables) that are targets of a link,

return the source of that link.

  • For variables that are not targets of a link, return their distribution’s operator parameters.

  • For operators with operands (N-ary operators) return the operands.

Parameters:

follow_links (bool) – Whether to follow links. Otherwise ignore them.

Returns:

dependencies – The set of operators that self directly depends on.

Return type:

Set

get_operator_dependencies(stop_operators=None, stop_criterion=None, include_links=True)#

Get operator dependencies.

Get the operator chain that leads to the operator. The chain of dependencies will be interrupted by the operators in stop_operators or the stop_criterion.

Parameters:
  • stop_operators (iterable, optional) – A list, tuple or set of operators which interrupt the dependency chain. The default is None.

  • stop_criterion (callable, optional) – A callable that accepts an operator as an argument and returns True for stop and False otherwise. The default is None which means don’t stop.

  • include_links (bool, optional) – Whether a linked variable is viewed as dependent on the linked source.

Returns:

dependencies – A nested dictionary with the dependecies. Keys are operators, values are again dicts. The key-operators depend on the key-operators of the next dict and so on.

Return type:

dict

Examples

>>> import halerium
>>> a = halerium.core.operator.Const(0.)
>>> b = a + 1
>>> c = b + 1
>>> c.get_operator_dependencies()
{b: {a: {}}
>>> c.depends_on(a, stop_operators=[b])
{b: {}}
property global_name#

The global name of the scopee.

The global name is a chain of all parent names of this scopee separated by ‘/’ characters.

property initial_values#
is_child_of(scopetor, include_self=True)#

Whether is child of scopetor.

This method checks whether the scopee is a (grand-) child of a given scopetor.

Parameters:
  • scopetor (Scopetor) – The (possible) parent scopetor

  • include_self (bool) – If True, a.is_child_of(a) would return True. If False, a.is_child_of(a) would return False.

Returns:

Whether self is a (grand-)child of scopetor or not.

Return type:

bool

is_circular(stop_criterion=None)#
property name#

The name of the scopee.

property operand#
select_from_operator_dependencies(selection_criterion, pre_stop_operators=None, pre_stop_criterion=None, post_stop_operators=None, post_stop_criterion=None, include_links=True, selected=None)#

Select operators from operator dependency tree.

Scans the operator dependency tree for operators satisfying the selection criterion and collects these operators into a set.

Parameters:
  • selection_criterion (callable) – The selection criterion for inclusion. A callable that accepts an operator as an argument and returns True if the argument should be included.

  • pre_stop_operators (collections.abc.Collection[OperatorBase]) – A list, tuple or set of operators which interrupt the dependency chain before selection. The default is None.

  • pre_stop_criterion (callable, None) – The stop criterion before selection. A callable that accepts an operator as an argument and returns True when to stop following further dependencies. The default is None which means don’t stop.

  • post_stop_operators (collections.abc.Collection[OperatorBase]) – A list, tuple or set of operators which interrupt the dependency chain after selection. The default is None.

  • post_stop_criterion (callable, None) – The stop criterion after selection. A callable that accepts an operator as an argument and returns True when to stop following further dependencies. The default is None which means don’t stop.

  • include_links (bool) – Whether a linked variable is viewed as dependent on the linked source. The default is True.

  • selected (Set, None) – The set to which the selected operators are added.

Returns:

selected – The set to with the selected operators are added.

Return type:

Set

property shift#