Model#
- class liesel.model.Model(nodes_and_vars, grow=True, copy=False, to_float32=True)[source]#
Bases:
objectA model with a static graph.
Tip
If you have an existing model and want to make changes to it, you can use the
Model.pop_nodes_and_vars()method to release the nodes and variables of the model. You can then make changes to them, for example i.e. changing the distribution of a variable or the inputs of a calculation. Afterwards, you initialize a new model with your changed variables. If you simply want to change the value of a variable, it is not necessary to callpop_nodes_and_vars(), you can simply override theVar.valueattribute. Remeber to callModel.update()afterwards.- Parameters:
nodes_and_vars (
Iterable[Node|Var]) – The nodes and variables to include in the model.grow (
bool) – Whether aGraphBuildershould be used to grow the model (finding the recursive inputs of the nodes and variables), and to add the model nodes. (default:True)copy (
bool) – Whether the nodes and variables should be copied upon initialization. (default:False)to_float32 (
bool) – Whether to convert the dtype of the values of the added nodes from float64 to float32. Only takes effect ifgrow=True. (default:True)
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.
GraphBuilderA 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
Returns an unfrozen deep copy of the model nodes and variables.
extract_position(position_keys[, model_state])Extracts a position from a model state.
groups()Collects the groups from all nodes and variables.
Returns a subgraph that consists of the input nodes and their parent nodes.
parental_submodel(*of)Returns a new model that consists only of the given variables and nodes and their parent variables and nodes.
plot_nodes([show, save_path, width, height, ...])Plots the nodes of this model.
plot_vars([show, save_path, width, height, prog])Plots the variables of this model.
Pops the nodes and variables out of this model.
predict(samples[, predict, newdata])Returns a dictionary of predictions.
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_state(position[, model_state, inplace])Updates and returns a model state given a position.
Returns a subgraph that consists of the input variables and their parent variables.
Attributes
Whether to update the model automatically if the value of a node is modified.
The log-likelihood of the model.
The log-prior of the model.
The (unnormalized) log-probability / log-posterior of the model.
The directed graph of the model nodes.
A mapping of the model nodes with their names as keys.
A mapping of the observed model variables with their names as keys.
A mapping of the model parameters with their names as keys.
The state of the model as a dict of node names and states.
The directed graph of the model variables.
A mapping of the model variables with their names as keys.