Skip to content

Commit

Permalink
notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
LegrandNico committed Nov 6, 2023
1 parent 61aacb4 commit aae4d47
Show file tree
Hide file tree
Showing 19 changed files with 1,533 additions and 1,496 deletions.
233 changes: 122 additions & 111 deletions docs/source/notebooks/0-Creating_networks.ipynb

Large diffs are not rendered by default.

33 changes: 19 additions & 14 deletions docs/source/notebooks/0-Creating_networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ kernelspec:
(probabilistic_networks)=
# Creating and manipulating networks of probabilistic nodes

```{code-cell} ipython3
---
Open an interactive version of this notebook here: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ilabcode/pyhgf/blob/master/docs/source/notebooks/0-Creating_networks.ipynb).

editable: true
slideshow:
slide_type: ''
---
%%capture
import sys
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from pyhgf.model import HGF
from pyhgf.typing import Indexes
if 'google.colab' in sys.modules:
! pip install pyhgf
```

[pyhgf](https://ilabcode.github.io/pyhgf/index.html#) is designed with inspiration from graph neural network libraries that can support message-passing schemes and perform belief propagation through networks of probabilistic nodes. Here, this principle is applied to predictive processing and focuses on networks that are structured as **rooted trees** and perform variational message passing to update beliefs about the state of the environment, inferred from the observations at the root of the tree. While this library is optimized to implement the standard two-level and three-level HGF {cite:p}`2011:mathys,2014:mathys`, as well as the generalized HGF {cite:p}`weber:2023`, it can also be applied to much larger use cases, with the idea is to generalize belief propagation as it has been described so far to larger and more complex networks that will capture a greater variety of environmental structure. Therefore, the library is also designed to facilitate the creation and manipulation of such probabilistic networks. Importantly, here we consider that a probabilistic network should be defined by the following four variables:
1. the network parameters
Expand Down Expand Up @@ -69,18 +84,7 @@ One of the advantages of reasoning this way is that it dissociates variables tha

### Creating probabilistic nodes

```{code-cell} ipython3
---
editable: true
slideshow:
slide_type: ''
---
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

from pyhgf.model import HGF
from pyhgf.typing import Indexes

parameters = {"mean": 0.0, "precision": 1.0}

Expand All @@ -90,7 +94,8 @@ edges = (
Indexes(None, (2,), (0,), None),
Indexes(None, None, None, (1,)),
)
```

+++

The code above illustrates creating a probabilistic network of 3 nodes with simple parameter sets $(mean = 0.0, precision = 1.0)$. Node 2 is the value parent of node 1. Node 3 is the value parent of node 2 and has no parents.

Expand Down
70 changes: 35 additions & 35 deletions docs/source/notebooks/0-Theory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"id": "6430ec8b-1b42-450b-9ed5-a9866384042f",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:20.407191Z",
"iopub.status.busy": "2023-11-03T09:52:20.406750Z",
"iopub.status.idle": "2023-11-03T09:52:21.152481Z",
"shell.execute_reply": "2023-11-03T09:52:21.151588Z"
"iopub.execute_input": "2023-11-06T08:57:30.382361Z",
"iopub.status.busy": "2023-11-06T08:57:30.382024Z",
"iopub.status.idle": "2023-11-06T08:57:31.321607Z",
"shell.execute_reply": "2023-11-06T08:57:31.320709Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -52,10 +52,10 @@
"id": "054c77b0-855c-46bf-b861-96d430d90d56",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.155138Z",
"iopub.status.busy": "2023-11-03T09:52:21.154863Z",
"iopub.status.idle": "2023-11-03T09:52:21.305431Z",
"shell.execute_reply": "2023-11-03T09:52:21.304572Z"
"iopub.execute_input": "2023-11-06T08:57:31.326085Z",
"iopub.status.busy": "2023-11-06T08:57:31.325740Z",
"iopub.status.idle": "2023-11-06T08:57:31.517781Z",
"shell.execute_reply": "2023-11-06T08:57:31.516362Z"
},
"tags": [
"hide-input"
Expand Down Expand Up @@ -117,10 +117,10 @@
"id": "a4cac17f-6ce5-41a3-8960-a781dc95e6b7",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.308107Z",
"iopub.status.busy": "2023-11-03T09:52:21.307906Z",
"iopub.status.idle": "2023-11-03T09:52:21.419801Z",
"shell.execute_reply": "2023-11-03T09:52:21.419039Z"
"iopub.execute_input": "2023-11-06T08:57:31.522291Z",
"iopub.status.busy": "2023-11-06T08:57:31.522048Z",
"iopub.status.idle": "2023-11-06T08:57:31.682838Z",
"shell.execute_reply": "2023-11-06T08:57:31.681516Z"
}
},
"outputs": [
Expand Down Expand Up @@ -182,10 +182,10 @@
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.422301Z",
"iopub.status.busy": "2023-11-03T09:52:21.422092Z",
"iopub.status.idle": "2023-11-03T09:52:21.543625Z",
"shell.execute_reply": "2023-11-03T09:52:21.542734Z"
"iopub.execute_input": "2023-11-06T08:57:31.686615Z",
"iopub.status.busy": "2023-11-06T08:57:31.686367Z",
"iopub.status.idle": "2023-11-06T08:57:31.842152Z",
"shell.execute_reply": "2023-11-06T08:57:31.841013Z"
},
"slideshow": {
"slide_type": ""
Expand Down Expand Up @@ -290,10 +290,10 @@
"id": "1da6f085-191c-4b9d-b425-10a0f29d9fec",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.546122Z",
"iopub.status.busy": "2023-11-03T09:52:21.545929Z",
"iopub.status.idle": "2023-11-03T09:52:21.772815Z",
"shell.execute_reply": "2023-11-03T09:52:21.771970Z"
"iopub.execute_input": "2023-11-06T08:57:31.846357Z",
"iopub.status.busy": "2023-11-06T08:57:31.846059Z",
"iopub.status.idle": "2023-11-06T08:57:32.205951Z",
"shell.execute_reply": "2023-11-06T08:57:32.204587Z"
},
"tags": [
"hide-input"
Expand Down Expand Up @@ -368,10 +368,10 @@
"id": "a44fa090-e2b0-4029-969c-595e0083d09a",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.775270Z",
"iopub.status.busy": "2023-11-03T09:52:21.775065Z",
"iopub.status.idle": "2023-11-03T09:52:21.964786Z",
"shell.execute_reply": "2023-11-03T09:52:21.964011Z"
"iopub.execute_input": "2023-11-06T08:57:32.209577Z",
"iopub.status.busy": "2023-11-06T08:57:32.209266Z",
"iopub.status.idle": "2023-11-06T08:57:32.457816Z",
"shell.execute_reply": "2023-11-06T08:57:32.456267Z"
},
"tags": [
"hide-input"
Expand Down Expand Up @@ -439,10 +439,10 @@
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2023-11-03T09:52:21.967356Z",
"iopub.status.busy": "2023-11-03T09:52:21.967128Z",
"iopub.status.idle": "2023-11-03T09:52:22.245198Z",
"shell.execute_reply": "2023-11-03T09:52:22.244364Z"
"iopub.execute_input": "2023-11-06T08:57:32.461492Z",
"iopub.status.busy": "2023-11-06T08:57:32.461241Z",
"iopub.status.idle": "2023-11-06T08:57:32.820595Z",
"shell.execute_reply": "2023-11-06T08:57:32.819349Z"
},
"slideshow": {
"slide_type": ""
Expand Down Expand Up @@ -548,7 +548,7 @@
"source": [
"### The propagation of prediction and prediction errors\n",
"\n",
"Having described the model as a specific configuration of predictive nodes offer many advantages, especially in term of modularity for the user. However, the model itself is not limited to the description of the generative process that we covered in the previous examples. The most interesting, and also the more complex, part of the modelling consists of the capability for the network to update the hierarchical structure in a Bayesian optimal way as new observations are presented. These steps are defined by a set of simple, one-step update equations that represent changes in beliefs about the hidden states (i.e. the sufficient statistics of the nodes) specified in the generative model. These equations were first described in {cite:t}`2011:mathys`, and the update equations for volatility and value coupling in the generalized Hierarchical Gaussian filter (on which most of the update functions in [pyhgf](https://github.com/ilabcode/pyhgf) are based) have been described in {cite:p}`weber:2023`. The exact computations in each step especially depend on the nature of the coupling (via {term}`VAPE`s vs. {term}`VOPE`s) between the parent and children nodes. It is beyond the scope of this tutorial to dive into the derivation of these steps and we refer the interested reader to the mentioned papers. Here, we provide a general overview of the dynamic of the update sequence that supports belief updating. The computations triggered by any observation at each time point can be ordered in time as shown in the [belief update algorithm](#belief-update)."
"Having described the model as a specific configuration of predictive nodes offer many advantages, especially in term of modularity for the user. However, the model itself is not limited to the description of the generative process that we covered in the previous examples. The most interesting, and also the more complex, part of the modelling consists of the capability for the network to update the hierarchical structure in a Bayesian optimal way as new observations are presented. These steps are defined by a set of simple, one-step update equations that represent changes in beliefs about the hidden states (i.e. the sufficient statistics of the nodes) specified in the generative model. These equations were first described in {cite:t}`2011:mathys`, and the update equations for volatility and value coupling in the generalized Hierarchical Gaussian filter (on which most of the update functions in [pyhgf](https://github.com/ilabcode/pyhgf) are based) have been described in {cite:p}`weber:2023`. The exact computations in each step especially depend on the nature of the coupling (via {term}`VAPE`s vs. {term}`VOPE`s) between the parent and children nodes. It is beyond the scope of this tutorial to dive into the derivation of these steps and we refer the interested reader to the mentioned papers. Here, we provide a general overview of the dynamic of the update sequence that supports belief updating. The computations triggered by any observation at each time point can be ordered in time as shown in the belief update algorithm."
]
},
{
Expand Down Expand Up @@ -641,18 +641,18 @@
"id": "b29ec7fb-2a4b-4d64-ac66-426f3bc9b6d5",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-03T09:52:22.248056Z",
"iopub.status.busy": "2023-11-03T09:52:22.247851Z",
"iopub.status.idle": "2023-11-03T09:52:22.517271Z",
"shell.execute_reply": "2023-11-03T09:52:22.516357Z"
"iopub.execute_input": "2023-11-06T08:57:32.824614Z",
"iopub.status.busy": "2023-11-06T08:57:32.824348Z",
"iopub.status.idle": "2023-11-06T08:57:33.199792Z",
"shell.execute_reply": "2023-11-06T08:57:33.198191Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: Fri Nov 03 2023\n",
"Last updated: Mon Nov 06 2023\n",
"\n",
"Python implementation: CPython\n",
"Python version : 3.9.16\n",
Expand All @@ -662,9 +662,9 @@
"jax : 0.4.14\n",
"jaxlib: 0.4.14\n",
"\n",
"numpy : 1.22.0\n",
"seaborn : 0.12.2\n",
"matplotlib: 3.7.1\n",
"numpy : 1.22.0\n",
"\n",
"Watermark: 2.4.3\n",
"\n"
Expand Down
Loading

0 comments on commit aae4d47

Please sign in to comment.