Var

Contents

Var#

class liesel.model.Var(value, dist=None, name='', inference=None, bijector=None, convert='default', distribution=None)[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().

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")

Note

Note that, for accessing keyword arguments, you do not use the Var.name attribute of the looked-for input variable or node, but the argument name. Consider this case from above:

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"]

Here, we retrieve the variable a with the name "a". But for the indexing, we use the argument name "loc" from the call to lsl.Dist.

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")
Parameters:
  • value (Any) – The value of the variable.

  • dist (Dist | None, default: None) – The probability distribution of the variable.

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

  • inference (TypeAliasType, default: None) – Additional information that can be used to set up inference algorithms.

  • bijector (None | TypeAliasType | Literal['auto'], default: None) – Bijector for variable transformation. If "auto", uses the default event space bijector defined by the variable’s distribution. If None, no transformation takes place. If not None, the variable will call biject() with this bijector upon initialization. Any supplied inference information will be passed to the bijected variable.

  • convert (Callable[[Any], Any] | Literal['default'], default: 'default') – A function used to process the value of this variable. The default uses the function stored in convert_value, which is jax.numpy.asarray.

  • distribution (Dist | None, default: None) – Deprecated argument name for the probability distribution of the variable, kept for backwards-compatibility. Please use the new name dist.

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.

Methods

all_input_nodes([to])

Returns all input nodes as a unique tuple.

all_input_vars([to])

Returns all input variables as a unique tuple.

all_output_nodes([of])

Returns all output nodes as a unique tuple.

all_output_vars([of])

Returns all output variables as a unique tuple.

biject([bijector, inference, name])

Transforms the variable using a bijector.

convert_value(x)

The function used to process the value of this variable, if convert="default" is supplied during init.

diagnose([verbose])

Provides a dataframe with diagnostic information about this variable's submodel.

ensure_name()

Ensures that the variable has a name.

get_inference(key)

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

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

new_obs(value[, dist, name, distribution, ...])

Initializes a strong variable that holds observed data.

new_param(value[, dist, name, inference, ...])

Initializes a strong variable that acts as a model parameter.

new_value(value[, name, inference, convert])

Initializes a strong variable without a distribution.

plot([show, save_path, width, height, prog, ...])

Plots the variables of the Liesel sub-model that terminates in this variable.

plot_nodes([show, save_path, width, height, ...])

Plots the nodes of the Liesel sub-model that terminates in this variable.

plot_vars([show, save_path, width, height, ...])

Plots the variables of the Liesel sub-model that terminates in this variable.

predict(samples[, newdata])

Returns an array of predictions for this variable.

sample(shape, seed[, posterior_samples, ...])

Draws samples from the parental model for this variable.

transform([bijector, inference, name])

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.

inference

auto_transform

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

bijected_var

Transformed variable.

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.