Skip to main content

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 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

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

fxtensor_salmon-0.2.1.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

fxtensor_salmon-0.2.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file fxtensor_salmon-0.2.1.tar.gz.

File metadata

  • Download URL: fxtensor_salmon-0.2.1.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for fxtensor_salmon-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ac5dd13c46d91cf8131ab1fec847bec95cc4cf6212600c66cb3ccf2140bf86cb
MD5 94f6bbecfd371039641507773679498e
BLAKE2b-256 cf6a33a13a7a70f21b9a0c5e5d5fc13e084e5902eab6679ac97e0beea62a56df

See more details on using hashes here.

File details

Details for the file fxtensor_salmon-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fxtensor_salmon-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44e2c24088047cfb00da5272ddf145bb83f5ae351ab92e8b3a50d425e31485bc
MD5 3b1052637c45599feec0dc8689eebb8a
BLAKE2b-256 6899ca0eea209c3e5933c7293649e90116f6888c12615a025469c40440b41d82

See more details on using hashes here.

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