Skip to main content

torchstats

This package implements various probability distributions in PyTorch, among those univariate distributions from scipy, categorical distributions, mixture models, and Bayesian networks. It implements computing probabilities and densities, and rudimentary functionality for fitting distributions to data.

Examples

Categorical distributions in a one-hot event space.

import torch
from torchstats import CategoricalOneHot

d = CategoricalOneHot([0.1, 0.5, 0.2, 0.2])
events = d.sample(5)
# tensor([[0., 1., 0., 0.],
#         [0., 0., 0., 1.],
#         [0., 1., 0., 0.],
#         [0., 0., 0., 1.],
#         [0., 1., 0., 0.]], dtype=torch.float64)
d.probability((events, events))
# tensor([0.5000, 0.2000, 0.5000, 0.2000, 0.5000], dtype=torch.float64)

# Probabilities and densities are always computed for hyperrectangles in event space.
# These hyperrectangles are represented as tuples of lower bound and upper bounds.
# This allows us to compute, for example, the probability of all categories except the first.
d.probability((torch.tensor([0.0, 0.0, 0.0, 0.0]), torch.tensor([0.0, 1.0, 1.0, 1.0])))
# tensor([0.9000], dtype=torch.float64

A mixture model of truncated Gaussian distributions.

import torch
from torchstats import MixtureModel

data = torch.rand(100)
d = MixtureModel.fit_truncnorm_mixture(data, bounds=(0.0, 1.0), n_components=3, n_init=3)
d.weights
# tensor([0.4785, 0.1701, 0.3514], dtype=torch.float64)
d.density(data)
# ...

A Bayesian network

import torch
from torchstats import BayesianNetwork, Categorical

factory = BayesianNetwork.Factory()
n1 = factory.new_node("var1")
n1.discrete_event_space(0, 1, 2)
n1.set_conditional_probability({}, Categorical([0.1, 0.2, 0.7]))

n2 = factory.new_node("var2")
n2.discrete_event_space(0, 1, 2)
n2.set_conditional_probability({}, Categorical([0.8, 0.1, 0.1]))

n3 = factory.new_node("var3")
n3.set_parents(n1, n2)
n3.discrete_event_space(0, 1)
n3.set_conditional_probability({n1: 0, n2: 0}, Categorical([1.0, 0.0])) 
n3.set_conditional_probability({n1: 0, n2: (1, 2)}, Categorical([0.1, 0.9]))
n3.set_conditional_probability({n1: (1, 2), n2: (0, 2)}, Categorical([0.0, 1.0]))

n1.hidden = n2.hidden = True  # make n1 and n2 hidden/latent variables

d = factory.create()
type(d)
# <class 'torchstats.bayesian_network.BayesianNetwork'>
d.output_space.input_shape
# torch.Size([1])
d.probability((torch.tensor(0), torch.tensor(0)))
# tensor([0.0820], dtype=torch.float64)
d.probability((torch.tensor(1), torch.tensor(1)))
# tensor([0.0820], dtype=torch.float64)
d.probability((torch.tensor(1), torch.tensor(1)))
# tensor([0.9180], dtype=torch.float64)

Installation

Install torchstats from PyPI using

pip install torchstats

To extend torchstats, clone this repository and run

pip install -e .[all]

to install development and testing dependencies. To run the tests, run either pytest or nox.

Metadata

Release files for torchstats 0.1.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 torchstats 0.1.0
File Size Uploaded
torchstats-0.1.0.tar.gz 31.3 kB Details

Built distribution (wheel)

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

Total release size: 69.0 kB

Release files / torchstats-0.1.0.tar.gz

Download URL torchstats-0.1.0.tar.gz
Size 31.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6443ab4e7d79f6b022ccd27c70f9d6d4ae78aa65b5394cf03837f1166c5dd96a
BLAKE2b-256 checksum
How to use checksums
2bc84b16731cc0370434952f9eea708178d85c200acd5a720ebf21acabf0df92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.7

Release files / torchstats-0.1.0-py3-none-any.whl

Download URL torchstats-0.1.0-py3-none-any.whl
Size 37.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e24e0e9a8641bed2feca6d7caa7a41bf12ca2cbf33c65c1bd5af961798b039d
BLAKE2b-256 checksum
How to use checksums
636526a0904be28386872c7e6db7bbb86afbbd243b9f91f65643433378749905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

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