Var

Contents

Var#

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

Bases: object

A variable in a statistical model.

A variable in Liesel is often a random variable, e.g. an observed or latent variable with a probability distribution (see new_obs()), or a model parameter with a prior distribution (see new_param()).

Other quantities can also be declared as variables, e.g. fixed data like hyperparameters or design matrices (see new_value()), or quantities that are computed from other nodes, e.g. structured additive predictors in semi-parametric regression models (see new_calc()).

Tip

You should initialize variables through one of the four constructors: new_param(), new_obs(), new_calc(), and new_value().

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

.Var.new_obs : Initializes a strong variable that holds observed data. .Var.new_param : Initializes a strong variable that acts as a model parameter. .Var.new_calc :

Initializes a weak variable that is a function of other variables.

.Var.new_value : Initializes a strong variable without a distribution. Var.transform() : Transforms a variable by adding a new transformed

variable as an input. This is useful for variables that are constrained to a certain domain, e.g. positive values.

.Calc :

A node representing a general calculation/operation in JAX or Python. Use this instead of new_calc() if you want to hide your calculation in the model graph produced by plot_vars().

.Value :

A node representing a static value. Use this instead of new_value() if you want to hide your value in the model graph produced by plot_vars().

.Dist :

A node representing a tensorflow_probability Distribution.

Accessing inputs

Calc and Dist objects support access to their inputs via square-bracket syntax. Thus, with a Var object, you can use square bracket indexing on its attributes Var.value_node and Var.dist_node. You can access both keyword and positional arguments this way.

>>> import tensorflow_probability.substrates.jax.distributions as tfd

Access keyword inputs to a calculator Var.value_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_calc(lambda x: x + 1.0, x=a)
>>> b.value_node["x"]
Var(name="a")

Access positional inputs to a calculator Var.value_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_calc(lambda x: x + 1.0, a)
>>> b.value_node[0]
Var(name="a")

Access keyword inputs to a distribution Var.dist_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_obs(1.0, lsl.Dist(tfd.Normal, loc=a, scale=1.0))
>>> b.dist_node["loc"]
Var(name="a")

Access positional inputs to a distribution Var.dist_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_obs(1.0, lsl.Dist(tfd.Normal, a, scale=1.0))
>>> b.dist_node[0]
Var(name="a")

Swapping out inputs

You can also use square-bracket indexing on Var.value_node and Var.dist_node to swap out existing inputs. This allows you to easily make changes to your model.

Swap out inputs to a calculator via Var.value_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_calc(lambda x: x + 1.0, x=a)
>>> c = lsl.Var.new_value(3.0, name="c")
>>> b.value_node["x"] = c
>>> b.value_node["x"]
Var(name="c")

Swap out inputs to a distribution via Var.dist_node:

>>> a = lsl.Var.new_value(2.0, name="a")
>>> b = lsl.Var.new_obs(1.0, lsl.Dist(tfd.Normal, loc=a, scale=1.0))
>>> c = lsl.Var.new_value(3.0, name="c")
>>> b.dist_node["loc"] = c
>>> b.dist_node["loc"]
Var(name="c")

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.

new_calc(function, *inputs[, name, ...])

Initializes a weak variable that is a function of other variables.

new_obs(value[, distribution, name])

Initializes a strong variable that holds observed data.

new_param(value[, distribution, name])

Initializes a strong variable that acts as a model parameter.

new_value(value[, name])

Initializes a strong variable without a distribution.

transform([bijector])

Transforms the variable, making it a function of a new variable.

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.