Skip to main content

Python Semantic Information Dynamics: Transfer Entropy and Partial Information Decomposition with Large Language Models

Project description

PSIDyn Logo

PSIDyn

Tests Python 3.9+ License: AGPL-3.0

Python Semantic Information Dynamics

A Python package for computing information-theoretic measures over natural language using LLMs as semantic probability estimators.

Overview

PSIDyn provides tools for measuring how information flows between text sources using LLMs to estimate semantic probabilities. It implements:

  • Transfer Entropy: Measure directed information flow between text sequences
  • Partial Information Decomposition (PID): Decompose the information that two sources provide about a target into redundt, uniqu, and synergistic contributions

Installation

pip install psidyn

For GPU quantization support (recommended for large models):

pip install psidyn[quantization]

Quick Start

from psidyn import PID, Droplet

# Initialize with a language model
model = PID(model_name="meta-llama/Llama-3.2-3B")

# Create text samples as Droplets
posts = [
    Droplet(user_id="premise1", timestamp=0, content="The sky is blue"),
    Droplet(user_id="premise2", timestamp=1, content="Blue things are calming"),
    Droplet(user_id="claim", timestamp=2, content="The sky is calming"),
]

# Compute PID
result = model.compute_pointwise_pid(
    posts,
    source_user_1="premise1",
    source_user_2="premise2",
    target_user="claim",
    target_post_idx=2,
    lag_window=10,
)

print(f"Redundancy: {result['redundancy_bits_per_token']:.3f}")
print(f"Unique (premise 1): {result['unique_x1_bits_per_token']:.3f}")
print(f"Unique (premise 2): {result['unique_x2_bits_per_token']:.3f}")
print(f"Synergy: {result['synergy_bits_per_token']:.3f}")

Core Concepts

Droplet

A Droplet represents a unit of text with metadata:

@dataclass
class Droplet:
    user_id: str      # Identifier for the source/author
    timestamp: int    # Temporal ordering
    content: str      # The text content
    post_id: str      # Optional unique identifier

PID Atoms

The Partial Information Decomposition breaks down joint information I(Y; X1, X2) into:

  • Redundancy: Information that both sources provide about the target
  • Unique X1: Information only source 1 provides
  • Unique X2: Information only source 2 provides
  • Synergy: Information that emerges only when both sources are combined

Marginalization Methods

PSIDyn supports two methods for computing conditional probabilities:

  • "omit" (default): Physically remove source text from the sequence
  • "mask": Use attention masking to block information flow

The "omit" method provides true marginal probabilities and is recommended for PID.

Redundancy Functionals

Two redundancy measures are available:

  • "mmi" (default): Minimum Mutual Information - R = min(I(Y;X1), I(Y;X2))
  • "ccs": Common Change in Surprisal - based on co-information sign matching

API Reference

PID

class PID(Trident):
    def compute_pointwise_pid(
        self,
        posts: List[Droplet],
        source_user_1: str,
        source_user_2: str,
        target_user: str,
        target_post_idx: int,
        lag_window: int,
        redundancy: Literal["mmi", "ccs"] = "mmi",
        method: Literal["mask", "omit"] = "omit",
    ) -> Dict[str, float]:
        """Compute pointwise PID for a single target."""

TransferEntropy

class TransferEntropy(Trident):
    def compute_transfer_entropy(
        self,
        posts: List[Droplet],
        source_user: str,
        target_user: str,
        lag_window: int,
    ) -> Dict[str, Any]:
        """Compute transfer entropy from source to target."""

Requirements

  • Python >= 3.9
  • PyTorch >= 2.0.0
  • Transformers >= 4.30.0
  • NumPy >= 1.21.0
  • Pandas >= 1.3.0

For quantization:

  • bitsandbytes >= 0.41.0
  • accelerate >= 0.20.0

Citation

If you use PSIDyn in your research, please cite:

@software{psidyn,
  author = {Goodall, Leonardo; Luppi, Andrea; Mediano, Pedro},
  title = {PSIDyn: Python Semantic Information Dynamics},
  year = {2024},
  url = {https://github.com/LeoGoodall/psidyn}
}

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).

This means:

  • You can use, modify, and distribute this software
  • Any modifications must also be open source under AGPL-3.0
  • If you run a modified version as a network service, you must provide the source code to users

See LICENSE for the full text.

For commercial licensing inquiries, contact the author.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psidyn-0.1.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psidyn-0.1.0-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file psidyn-0.1.0.tar.gz.

File metadata

  • Download URL: psidyn-0.1.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for psidyn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a38bdcefcd2003c5b0438dad9034df675fccfb0a6c066dff9f077209e3ba93c9
MD5 f33eebf0c6882dc121a8903ad95fe960
BLAKE2b-256 f4094f4ffd36102c35254f5f0407b1b84657c1e653c79e4bd128fb4a233af31e

See more details on using hashes here.

Provenance

The following attestation bundles were made for psidyn-0.1.0.tar.gz:

Publisher: publish.yml on LeoGoodall/psidyn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file psidyn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: psidyn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for psidyn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 073e90295c4d8aaa0e007a93e262626935319cac5d45e3110c15ce1fdba5bd6e
MD5 8e1368b8c7a55af464cff0d222bdfa52
BLAKE2b-256 0a35ddbcf54e7e15ad4aa89c77f8f973243e1a9c260c73379234ed772b7bf2d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for psidyn-0.1.0-py3-none-any.whl:

Publisher: publish.yml on LeoGoodall/psidyn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page