Markov Tensor Module
Project description
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 havelabelsset toNone. - 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
FXTensorasdomainorcodomain(e.g.,[['Urban', 'Rural']]or[[2]]). - Morphisms: Markov kernels (probabilistic transitions), represented by
FXTensorinstances with profile and data.
Categorical Operations and Methods
- Composition (
composition): Combines morphismsf: A -> Bandg: B -> C. Corresponds to connecting wires in string diagrams. - Tensor Product (
tensor_product): Combines independent systems. Represented as side-by-side diagrams. - Discard (
marginalization,discard_prefix): Sums over output axes to eliminate them. - 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_indexandget_index_label.
Testing
Tests are implemented in tests/test_fxtensor.py using pytest.
pytest
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fxtensor_salmon-0.2.0.tar.gz.
File metadata
- Download URL: fxtensor_salmon-0.2.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7de7797d4bcb7c1be8682f99febc746d9c18a28e4471b082663e94188a8d0956
|
|
| MD5 |
4d1ade5c33f42bdf4bc6506e04f7a968
|
|
| BLAKE2b-256 |
ea80115c0c738018a0c4e570c06b23782cf81a42b4fcdc9bb380bde59bc66bbb
|
File details
Details for the file fxtensor_salmon-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fxtensor_salmon-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2917781cdb5fde4aa0d9cc615c352f939bd9874276e8aed426f894b3fc7eab5a
|
|
| MD5 |
bec4b3e65a732f1fedb36bacd97d2443
|
|
| BLAKE2b-256 |
0b4959451c683f3378613f810e3f24d7fa73f84fb5c94ea816dcc6e616467395
|