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 (seenew_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 (seenew_calc()
).Tip
You should initialize variables through one of the four constructors:
new_param()
,new_obs()
,new_calc()
, andnew_value()
.- Parameters:
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 transformedvariable 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 byplot_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 byplot_vars()
.- .Dist :
A node representing a
tensorflow_probability
Distribution
.
Accessing inputs
Calc
andDist
objects support access to their inputs via square-bracket syntax. Thus, with aVar
object, you can use square bracket indexing on its attributesVar.value_node
andVar.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
andVar.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
Returns all input nodes as a unique tuple.
Returns all input variables as a unique tuple.
Returns all output nodes as a unique tuple.
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
Additional meta-information about the variable as a dict.
Whether the variable should automatically be transformed to the unconstrained space
R**n
upon model initialization.The distribution node of the variable.
The groups that this variable is a part of.
Whether the variable has a probability distribution.
The log-probability of the variable.
The model the variable is part of.
The name of the variable.
The nodes of the variable as a list.
Whether the variable is observed.
Whether the variable is a parameter.
The role of the variable.
Whether the variable is strong.
The value of the variable.
The value node of the variable.
The proxy node for the value of the variable.
Whether the variable is weak.