Var#
- class liesel.model.Var(value, dist=None, name='', inference=None, bijector=None, convert='default', distribution=None)[source]#
Bases:
objectA 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().Accessing inputs
CalcandDistobjects support access to their inputs via square-bracket syntax. Thus, with aVarobject, you can use square bracket indexing on its attributesVar.value_nodeandVar.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.nameattribute 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
awith the name"a". But for the indexing, we use the argument name"loc"from the call tolsl.Dist.Swapping out inputs
You can also use square-bracket indexing on
Var.value_nodeandVar.dist_nodeto 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 aModel.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. IfNone, no transformation takes place. If notNone, the variable will callbiject()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 inconvert_value, which isjax.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 namedist.
See also
Var.new_obsInitializes a strong variable that holds observed data.
Var.new_paramInitializes a strong variable that acts as a model parameter.
Var.new_calcInitializes a weak variable that is a function of other variables.
Var.new_valueInitializes 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.
CalcA 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().ValueA 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().DistA node representing a
tensorflow_probabilityDistribution.
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.
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.
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
Additional meta-information about the variable as a dict.
Whether the variable should automatically be transformed to the unconstrained space
R**nupon model initialization.Transformed variable.
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.