Skip to content

Commit

Permalink
ehgf update steps
Browse files Browse the repository at this point in the history
  • Loading branch information
LegrandNico committed Sep 29, 2023
1 parent 8b2c9c7 commit 0698c66
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 56 deletions.
40 changes: 20 additions & 20 deletions docs/source/notebooks/1.1-Binary_HGF.ipynb

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions docs/source/notebooks/1.2-Categorical_HGF.ipynb

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions docs/source/notebooks/1.3-Continuous_HGF.ipynb

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/pyhgf/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class HGF(object):
network only has one input node.
model_type :
The model implemented (can be `"continuous"`, `"binary"` or `"custom"`).
update_type :
The type of update to perform for volatility coupling. Can be `"eHGF"`
(defaults) or `"standard"`. The eHGF update step was proposed as an alternative
to the original definition in that it starts by updating the mean and then the
precision of the parent node, which generally reduces the errors associated with
impossible parameter space and improves sampling.
n_levels :
The number of hierarchies in the model, including the input vector. Cannot be
less than 2.
Expand All @@ -58,6 +64,7 @@ def __init__(
self,
n_levels: Optional[int] = 2,
model_type: str = "continuous",
update_type: str = "eHGF",
initial_mu: Dict = {
"1": 0.0,
"2": 0.0,
Expand Down Expand Up @@ -138,6 +145,7 @@ def __init__(
"""
self.model_type = model_type
self.update_type = update_type
self.verbose = verbose
self.n_levels = n_levels
self.edges: Edges
Expand Down
7 changes: 6 additions & 1 deletion src/pyhgf/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
continuous_input_prediction_error,
continuous_node_prediction,
continuous_node_prediction_error,
ehgf_continuous_node_prediction_error,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -333,7 +334,11 @@ def get_update_sequence(
# --------------------------

# case 1 - default to a continuous node
update_fn = continuous_node_prediction_error
# choose between the eHGF and standard update step
if hgf.update_type == "eHGF":
update_fn = ehgf_continuous_node_prediction_error
elif hgf.update_type == "standard":
update_fn = continuous_node_prediction_error
prediction_fn = continuous_node_prediction

# case 2 - this is an input node
Expand Down
117 changes: 117 additions & 0 deletions src/pyhgf/updates/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,123 @@ def continuous_node_prediction_error(
return attributes


@partial(jit, static_argnames=("edges", "node_idx"))
def ehgf_continuous_node_prediction_error(
attributes: Dict, time_step: float, node_idx: int, edges: Edges, **args
) -> Dict:
"""eHGF prediction-error step for value and volatility parents of a continuous node.
This update step uses a different order for the mean and precision as compared to
the standard HGF, respectively:
1. Update volatility parent(s).
2. Update value parent(s).
If a value/volatility parent has multiple children, all the children will update
the parent together, therefor this function should only be called once per group
of child nodes. The method :py:meth:`pyhgf.model.HGF.get_update_sequence`
ensures that this function is only called once all the children have been
updated.
Then returns the structure of the new parameters.
Parameters
----------
attributes :
The attributes of the probabilistic nodes.
time_step :
The interval between the previous time point and the current time point.
node_idx :
Pointer to the node that needs to be updated. After continuous updates, the
parameters of value and volatility parents (if any) will be different.
edges :
The edges of the probabilistic nodes as a tuple of
:py:class:`pyhgf.typing.Indexes`. The tuple has the same length as node number.
For each node, the index list value and volatility parents and children.
Returns
-------
attributes :
The updated attributes of the probabilistic nodes.
See Also
--------
update_continuous_input_parents, update_binary_input_parents
References
----------
.. [1] Weber, L. A., Waade, P. T., Legrand, N., Møller, A. H., Stephan, K. E., &
Mathys, C. (2023). The generalized Hierarchical Gaussian Filter (Version 1).
arXiv. https://doi.org/10.48550/ARXIV.2305.10937
"""
# list value and volatility parents
value_parents_idxs = edges[node_idx].value_parents
volatility_parents_idxs = edges[node_idx].volatility_parents

# return here if no parents node are provided
if (value_parents_idxs is None) and (volatility_parents_idxs is None):
return attributes

########################
# Update value parents #
########################
if value_parents_idxs is not None:
for value_parent_idx in value_parents_idxs:
# if this child is the last one relative to this parent's family, all the
# children will update the parent at once, otherwise just pass and wait
if edges[value_parent_idx].value_children[-1] == node_idx:
# in the eHGF update step, we use the expected precision here
# as we haven't computed it yet due to the reverse update order
pi_value_parent = attributes[value_parent_idx]["pihat"]

# Estimate the mean of the posterior distribution
mu_value_parent = prediction_error_mean_value_parent(
attributes, edges, value_parent_idx, pi_value_parent
)
# Update this parent's parameters
attributes[value_parent_idx]["mu"] = mu_value_parent

# Estimate the precision of the posterior distribution
pi_value_parent = prediction_error_precision_value_parent(
attributes, edges, value_parent_idx
)

# Update this parent's parameters
attributes[value_parent_idx]["pi"] = pi_value_parent

#############################
# Update volatility parents #
#############################
if volatility_parents_idxs is not None:
for volatility_parent_idx in volatility_parents_idxs:
# if this child is the last one relative to this parent's family, all the
# children will update the parent at once, otherwise just pass and wait
if edges[volatility_parent_idx].volatility_children[-1] == node_idx:
# in the eHGF update step, we use the expected precision here
# as we haven't computed it yet due to the reverse update order
pi_volatility_parent = attributes[volatility_parent_idx]["pihat"]

# Estimate the new mean of the volatility parent
mu_volatility_parent = prediction_error_mean_volatility_parent(
attributes,
edges,
time_step,
volatility_parent_idx,
pi_volatility_parent,
)
attributes[volatility_parent_idx]["mu"] = mu_volatility_parent

# Estimate the new precision of the volatility parent
pi_volatility_parent = prediction_error_precision_volatility_parent(
attributes, edges, time_step, volatility_parent_idx
)

# Update this parent's parameters
attributes[volatility_parent_idx]["pi"] = pi_volatility_parent

return attributes


@partial(jit, static_argnames=("edges", "node_idx"))
def continuous_node_prediction(
attributes: Dict, time_step: float, node_idx: int, edges: Edges, **args
Expand Down

0 comments on commit 0698c66

Please sign in to comment.