Zing Forum

Reading

PyHGF: A Neurocomputational Library Based on Predictive Coding and Its Applications in Computational Psychiatry

PyHGF is an open-source Python library that implements the Hierarchical Gaussian Filter (HGF) model, providing a powerful Bayesian inference tool for computational psychiatry research to help understand how the brain processes uncertainty.

预测编码分层高斯滤波器计算精神病学贝叶斯推断神经科学Python库不确定性处理精神健康
Published 2026-04-27 19:17Recent activity 2026-04-27 19:26Estimated read 5 min
PyHGF: A Neurocomputational Library Based on Predictive Coding and Its Applications in Computational Psychiatry
1

Section 01

PyHGF: A Practical Tool for Predictive Coding Theory and Its Core Value in Computational Psychiatry

PyHGF is an open-source Python library that implements the Hierarchical Gaussian Filter (HGF) model, which is based on predictive coding theory. It provides Bayesian inference tools for computational psychiatry research to help understand the mechanisms by which the brain processes uncertainty. This article will introduce it from aspects such as background, methods, applications, and limitations.

2

Section 02

Background: Predictive Coding Theory and the Origin of the HGF Model

Predictive coding theory originates from Helmholtz's idea of "unconscious inference" and was formalized into a mathematical model in the early 21st century. Its core is that the brain generates predictions through prior knowledge, updates internal models using prediction errors, and adopts a hierarchical structure (lower layers process specific sensory features, higher layers encode contextual intentions). The HGF was proposed by Mathys et al. in 2014, modeling the environment as a multi-layer Gaussian random walk system. It approximates the posterior distribution through variational inference, and the key parameter "precision weight" determines the extent to which prediction errors affect state updates.

3

Section 03

Methods: Design and Implementation Details of the PyHGF Library

As a pure Python library, PyHGF provides a concise API and flexible model definitions (custom hierarchical structures or predefined models such as binary/continuous outcome models). Model fitting supports maximum likelihood estimation or MCMC sampling, and built-in analysis tools can extract hidden state trajectories, prediction error sequences, etc. Technically, it adopts a modular design, integrates with ecosystems like NumPy, SciPy, and ArviZ, and supports parameter recovery tests to verify model reliability.

4

Section 04

Applications: Clinical Insights and Experimental Examples in Computational Psychiatry

PyHGF is widely used in computational psychiatry: individuals with autism may have abnormal precision weight regulation; patients with anxiety disorders show threat learning biases; schizophrenia may involve hierarchical reasoning dysregulation. Experimental examples include reinforcement learning tasks (slower learning rates in patients with depression), perceptual decision-making tasks (separating perceptual sensitivity from decision bias), eye-tracking (predicting gaze patterns), etc.

5

Section 05

Limitations and Future Development Directions

The limitations of PyHGF include that the Gaussian assumption of the HGF model may simplify environmental dynamics, and variational approximation may fail to capture the multimodal structure of the posterior distribution. Future directions include supporting more flexible hierarchical structures (nonlinear coupling), integrating deep learning components to handle high-dimensional inputs, and developing an online learning version for real-time applications (such as brain-computer interfaces).

6

Section 06

Conclusion: The Significance and Value of PyHGF

PyHGF transforms predictive coding theory into a practical tool, providing a quantitative framework for understanding the brain's uncertainty processing, learning mechanisms, and mental disorders. It is not only a software package but also an entry point into the theoretical world of the predictive brain, facilitating mechanism research and intervention development in the mental health field.