Skip to main content

FXTensor

FXTensor is a Python library for tensor-based computations, particularly suited for modeling probabilistic systems and processes inspired by category theory. It leverages NumPy for efficient numerical computations. The library primarily supports labeled indices for enhanced readability while maintaining compatibility with unlabeled numeric indices.

Core Concepts

An FXTensor is defined by its profile and data, with optional string labels to make tensors more intuitive and meaningful.

  • Profile: A pair [domain, codomain] specifying the dimensions of input (domain) and output (codomain) indices. For labeled tensors, e.g., [[['a', 'b']], [['x', 'y', 'z']]] represents a 2x3 matrix with labeled rows and columns. For unlabeled tensors, [[2], [3]] specifies dimensions numerically.
  • Labels (Optional): String labels can be assigned to each dimension, enhancing interpretability. For example, input axis labeled ['a', 'b'] and output axis labeled ['x', 'y', 'z']. Unlabeled tensors have labels set to None.
  • Data: A NumPy array holding the tensor’s values. Its shape must match the total number of dimensions in the profile (len(domain) + len(codomain)).

Usage Examples

Basic Example: Labeled Tensor

import numpy as np
from fxtensor_salmon import FXTensor

# Create a 2x3 matrix with string labels
profile = [[['a', 'b']], [['x', 'y', 'z']]]
data = np.array([
    [0.1, 0.2, 0.7],  # a -> x, y, z
    [0.3, 0.3, 0.4]   # b -> x, y, z
])
tensor = FXTensor(profile, data=data)

# Access elements using labels
assert tensor.get_label_index(0, 'a') == 0  # Index of label 'a' on input axis
assert tensor.get_index_label(1, 2) == 'z'  # Label at index 2 on output axis

Unlabeled Tensor

# Create a 2x3 matrix with numeric indices
profile = [[2], [3]]
data = np.array([
    [0.1, 0.2, 0.7],
    [0.3, 0.3, 0.4]
])
tensor = FXTensor(profile, data=data)
assert tensor.labels == (None, None)  # No labels

Creating Tensor from Strands

# Create a tensor from labeled strands
profile = [[['a', 'b']], [['x', 'y', 'z']]]
strands = {
    "[[['a']], [['x']]]": 0.1,
    "[[['a']], [['y']]]": 0.2,
    "[[['a']], [['z']]]": 0.7,
    "[[['b']], [['x']]]": 0.3,
    "[[['b']], [['y']]]": 0.3,
    "[[['b']], [['z']]]": 0.4
}
tensor = FXTensor.from_strands(profile, strands)
assert tensor.labels == ([['a', 'b']], [['x', 'y', 'z']])

Labeled Tensor Composition

# P(Y|X) where X={a,b}, Y={x,y}
tensor1 = FXTensor(
    [[['a', 'b']], [['x', 'y']]],
    data=np.array([
        [0.2, 0.8],  # a -> x, y
        [0.6, 0.4]   # b -> x, y
    ])
)

# P(Z|Y) where Y={x,y}, Z={p,q}
tensor2 = FXTensor(
    [[['x', 'y']], [['p', 'q']]],
    data=np.array([
        [0.3, 0.7],  # x -> p, q
        [0.9, 0.1]   # y -> p, q
    ])
)

# Composition: P(Z|X) = P(Y|X) ; P(Z|Y)
result = tensor1.composition(tensor2)
assert result.labels == ([['a', 'b']], [['p', 'q']])

Labeled Tensor Product

# P(X) where X={a,b}
tensor1 = FXTensor(
    [[], [['a', 'b']]],
    data=np.array([0.3, 0.7])
)

# P(Y) where Y={x,y,z}
tensor2 = FXTensor(
    [[], [['x', 'y', 'z']]],
    data=np.array([0.2, 0.3, 0.5])
)

# Tensor product: P(X,Y) = P(X) ⊗ P(Y)
result = tensor1.tensor_product(tensor2)
assert result.labels == (None, [['a', 'b'], ['x', 'y', 'z']])

Simple Example: Weather Forecast (Labeled)

Model a weather system with states “Sunny” or “Rainy.”

  • State Tensor: Represents today’s weather probability with labels. If today is certainly sunny, the state is [1, 0].

    weather_states = ['Sunny', 'Rainy']
    sunny_today = FXTensor([[], [weather_states]], data=np.array([1, 0]))
    
  • Process Tensor: Represents a weather forecast as a labeled Markov kernel.

    forecast_matrix = np.array([
        [0.8, 0.2],  # Sunny -> Sunny: 0.8, Rainy: 0.2
        [0.4, 0.6]   # Rainy -> Sunny: 0.4, Rainy: 0.6
    ])
    forecast_tensor = FXTensor([[weather_states], [weather_states]], data=forecast_matrix)
    
  • Composition: Predict tomorrow’s weather by composing today’s state with the forecast.

    sunny_tomorrow = sunny_today.composition(forecast_tensor)
    sunny_idx = sunny_tomorrow.get_label_index(1, 'Sunny')
    p_sunny = sunny_tomorrow.data[sunny_idx]  # 0.8
    

Advanced Example: Multidimensional System (Labeled)

Model the joint probability of Season (Spring, Summer, Other) and Weather (Sunny, Rainy) given Location (Urban, Rural).

  • Profile: [[['Urban', 'Rural']], [['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy']]]

  • Data: A 3D array of shape (2, 3, 2).

    location_labels = ['Urban', 'Rural']
    season_labels = ['Spring', 'Summer', 'Other']
    weather_labels = ['Sunny', 'Rainy']
    process_data = np.random.rand(2, 3, 2)
    process_data /= process_data.sum(axis=(1, 2), keepdims=True)
    process_tensor = FXTensor([[location_labels], [season_labels, weather_labels]], data=process_data)
    

Key Method Applications

marginalization(start_B)

# Get P(Season | Location) by marginalizing Weather
season_tensor = process_tensor.marginalization(start_B=2)
assert season_tensor.labels == ([['Urban', 'Rural']], [['Spring', 'Summer', 'Other']])

discard_prefix(start_B)

# Get P(Weather | Location) by marginalizing Season
weather_tensor = process_tensor.discard_prefix(start_B=2)
assert weather_tensor.labels == ([['Urban', 'Rural']], [['Sunny', 'Rainy']])

conditionalization(start_B)

# Compute P(Weather | Location, Season)
cond_tensor = process_tensor.conditionalization(start_B=2)
assert cond_tensor.labels == ([['Urban', 'Rural'], ['Spring', 'Summer', 'Other']], [['Sunny', 'Rainy']])

tensor_product(other)

# Add Traffic (Low, High) system
traffic_labels = ['Low', 'High']
traffic_state = FXTensor([[], [traffic_labels]], data=np.array([0.7, 0.3]))
joint_tensor = process_tensor.tensor_product(traffic_state)
assert joint_tensor.labels == (None, [['Urban', 'Rural'], ['Spring', 'Summer', 'Other'], ['Sunny', 'Rainy'], ['Low', 'High']])

Theoretical Background: Relation to Markov Categories

The fxtensor-salmon library is designed based on the Markov Category, a framework for categorical probability theory.

Markov Category Basics

  • Objects: State spaces, represented in FXTensor as domain or codomain (e.g., [['Urban', 'Rural']] or [[2]]).
  • Morphisms: Markov kernels (probabilistic transitions), represented by FXTensor instances with profile and data.

Categorical Operations and Methods

  1. Composition (composition): Combines morphisms f: A -> B and g: B -> C. Corresponds to connecting wires in string diagrams.
  2. Tensor Product (tensor_product): Combines independent systems. Represented as side-by-side diagrams.
  3. Discard (marginalization, discard_prefix): Sums over output axes to eliminate them.
  4. Copy (delta_tensor): Deterministic copying operation.

Probabilistic Properties

  • is_markov(): Verifies if the tensor satisfies the normalization condition (sum of outputs equals 1 or 0).
  • Labeled tensors enable intuitive interpretation via get_label_index and get_index_label.

Testing

Tests are implemented in tests/test_fxtensor.py using pytest.

pytest

References

Metadata

Release files for fxtensor_salmon 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fxtensor_salmon 0.2.0
File Size Uploaded
fxtensor_salmon-0.2.0.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fxtensor_salmon 0.2.0
File Interpreter ABI Platform
fxtensor_salmon-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / fxtensor_salmon-0.2.0.tar.gz

Download URL fxtensor_salmon-0.2.0.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7de7797d4bcb7c1be8682f99febc746d9c18a28e4471b082663e94188a8d0956
BLAKE2b-256 checksum
How to use checksums
ea80115c0c738018a0c4e570c06b23782cf81a42b4fcdc9bb380bde59bc66bbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / fxtensor_salmon-0.2.0-py3-none-any.whl

Download URL fxtensor_salmon-0.2.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2917781cdb5fde4aa0d9cc615c352f939bd9874276e8aed426f894b3fc7eab5a
BLAKE2b-256 checksum
How to use checksums
0b4959451c683f3378613f810e3f24d7fa73f84fb5c94ea816dcc6e616467395
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release history Release notifications | RSS feed

1.2.0

2 release files

1.1.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page