Model

Contents

Model#

class liesel.model.Model(*nodes_and_vars, grow=True, copy=False, to_float32=True, validate_log_prob_decomposition=True)[source]#

Bases: object

A probabilistic graphical model.

Parameters:
  • nodes_and_vars (Node | Var | Iterable[Node | Var]) – The nodes and variables to include in the model.

  • grow (bool, default: True) – Whether a GraphBuilder should be used to grow the model (finding the recursive inputs of the nodes and variables), and to add the model nodes.

  • copy (bool, default: False) – Whether the nodes and variables should be copied upon initialization.

  • to_float32 (bool, default: True) – Whether to convert the dtype of the values of the added nodes from float64 to float32. Only takes effect if grow=True.

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.

GraphBuilder

A graph builder, which can be used to set up and manipulate a model if you need more control.

Examples

Here, we set up a basic model based on three variables:

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

We now build a model:

>>> model = lsl.Model([c])
>>> model
Model(9 nodes, 3 vars)

Methods

add(*args[, copy, add_to_seeds])

Adds a variable number of variables or nodes to this model.

copy([clear_state])

Returns a new model filled with deep copies of all model nodes and variables.

copy_nodes_and_vars()

Returns an unfrozen deep copy of the model nodes and variables.

copy_vars()

Returns an unfrozen deep copy of the model variables.

diagnose([verbose])

Provides a dataframe with diagnostic information about the model.

drop_singletons()

Drops any singleton nodes and variables, i.e. nodes or variables that have neither outputs nor inputs.

extract_position(position_keys[, model_state])

Extracts a position from a model state.

groups()

Collects the groups from all nodes and variables.

join(model[, by, copy, suffix])

Joins a second model into this one.

join_by_all(model[, copy])

Joins a second model into this one by all overlapping variable names.

modify_names(fn)

Modifies the names of all variables and nodes in the model according to the supplied function.

node_parental_subgraph(*of)

Returns a subgraph that consists of the input nodes and their parent nodes.

parental_submodel(*of[, clear_state])

Returns a new model that consists only of the given variables and nodes and their parent variables and nodes.

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

Plots the variables of this model.

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

Plots the nodes of this model.

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

Plots the variables of this model.

pop_nodes_and_vars()

Pops the nodes and variables out of this model.

pop_vars()

Pops the variables out of this model.

predict(samples[, predict, newdata])

Returns a dictionary of predictions.

prefix_names(prefix)

Adds a prefix to the names of all variables and nodes in the model.

rebuild_graph(*vars_nodes_and_names)

Rebuilds the model graph by re-discovering the outputs of all supplied nodes and variables.

replace(old, new)

Replaces the old with the new node or variable.

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

Draws samples from the model.

set_seed(seed)

Splits and sets the seed / PRNG key.

simulate(seed[, skip])

Updates the model state simulating from the probability distributions in the model using a provided random seed, optionally skipping specified nodes.

update(*names)

Updates the target nodes and their recursive inputs if they are outdated.

update_graph()

Updates the model graph by re-discovering the outputs of all nodes and variables in the graph.

update_state(position[, model_state, inplace])

Updates and returns a model state given a position.

var_parental_subgraph(*of)

Returns a subgraph that consists of the input variables and their parent variables.

Attributes

auto_update

Whether to update the model automatically if the value of a node is modified.

graph_outdated

Whether the model graph is outdated.

locked

Whether the model graph is locked.

log_lik

The log-likelihood of the model.

log_prior

The log-prior of the model.

log_prob

The (unnormalized) log-probability / log-posterior of the model.

model_nodes

Dictionary of nodes with the "_model" prefix in their name.

node_graph

The directed graph of the model nodes.

nodes

A mapping of the model nodes with their names as keys.

observed

A mapping of the observed model variables with their names as keys.

parameters

A mapping of the model parameters with their names as keys.

seed_nodes_and_vars

The seed nodes and variables passed to the model during initialization.

state

The state of the model as a dict of node names and states.

var_graph

The directed graph of the model variables.

vars

A mapping of the model variables with their names as keys.