Var

Contents

Var#

class liesel.model.nodes.Var(value, distribution=None, name='')[source]#

Bases: object

A variable in a statistical model, typically with a probability distribution.

A variable in Liesel is typically a random variable, e.g. an observed or latent variable with a probability distribution, or a model parameter with a prior distribution. Note that observed variables and model parameters should typically be declared with the obs() and param() helper functions. Other quantities can also be declared as variables, e.g. fixed data like hyperparameters or design matrices, or quantities that are computed from other nodes, e.g. structured additive predictors in semi-parametric regression models.

If a Data or Calc node does not have an associated probability distribution, it is possible but not necessary to declare it as a variable. There is no hard and fast rule when a node without a probability distribution should be declared as a variable and when not. The advantages of a variable in this case are: (1) easier access via the Model.vars attribute, and (2) more explicit visualization with the plot_vars() function. This might be particularly desirable for the hyperparameters of a prior distribution.

Parameters:
  • value (Any) – The value of the variable.

  • distribution (Optional[Dist]) – The probability distribution of the variable. (default: None)

  • name (str) – The name of the variable. If you do not specify a name, a unique name will be automatically generated upon initialization of a Model. (default: '')

See also

obs

Helper function to declare a variable as an observed quantity.

param

Helper function to declare a variable as a model parameter.

Calc

A node representing a general calculation/operation in JAX or Python.

Data

A node representing some static data.

Dist

A node representing a tensorflow_probability Distribution.

Examples

A simple variable without a distribution and without a name:

>>> x = lsl.Var(1.0)
>>> x
Var(name="")

Adding this variable to a model leads to an automatically generated name:

>>> model = lsl.GraphBuilder().add(x).build_model()
>>> x
Var(name="v0")

A simple variable with a name:

>>> x = lsl.Var(1.0, name="x")
>>> x
Var(name="x")

Methods

all_input_nodes()

Returns all input nodes as a unique tuple.

all_input_vars()

Returns all input variables as a unique tuple.

all_output_nodes()

Returns all output nodes as a unique tuple.

all_output_vars()

Returns all output variables as a unique tuple.

update()

Updates the variable.

Attributes

info

Additional meta-information about the variable as a dict.

auto_transform

Whether the variable should automatically be transformed to the unconstrained space R**n upon model initialization.

dist_node

The distribution node of the variable.

groups

The groups that this variable is a part of.

has_dist

Whether the variable has a probability distribution.

log_prob

The log-probability of the variable.

model

The model the variable is part of.

name

The name of the variable.

nodes

The nodes of the variable as a list.

observed

Whether the variable is observed.

parameter

Whether the variable is a parameter.

role

The role of the variable.

strong

Whether the variable is strong.

value

The value of the variable.

value_node

The value node of the variable.

var_value_node

The proxy node for the value of the variable.

weak

Whether the variable is weak.