Skip to main content

Statistical distributions for structural biology

Project description

rs-distributions

Documentation Build PyPI - Version PyPI - Python Version


Table of Contents

rs-distributions provides statistical tools which are helpful for structural biologists who wish to model their data using variational inference.

Installation

pip install rs-distributions

Distributions

rs_distributions.distributions provides learnable distributions that are important in structural biology. These distributions follow the conventions in torch.dist. Here's a small example of distribution matching between a learnable distribution, q, and a target distribion, p. The example works by minimizing the Kullback-Leibler divergence between q and p using gradients calculated by the implicit reparameterization method.

import torch
from rs_distributions import distributions as rsd

target_loc = 4.
target_scale = 2.

loc_initial_guess = 10.
scale_initial_guess  = 3.

loc = torch.tensor(loc_initial_guess, requires_grad=True)

scale_transform = torch.distributions.transform_to(
    rsd.FoldedNormal.arg_constraints['scale']
)
scale_initial_guess = scale_transform.inv(
    torch.tensor(scale_initial_guess)
)
unconstrained_scale = torch.tensor(
    torch.tensor(scale_initial_guess),
    requires_grad=True
)

p = rsd.FoldedNormal(
    target_loc,
    target_scale,
)

opt = torch.optim.Adam([loc, unconstrained_scale])

steps = 10_000
num_samples = 100
for i in range(steps):
    opt.zero_grad()
    scale = scale_transform(unconstrained_scale)
    q = rsd.FoldedNormal(loc, scale)
    z = q.sample((num_samples,))
    kl_div = q.log_prob(z) - p.log_prob(z)
    kl_div = kl_div.mean()
    kl_div.backward()
    opt.step()

This example uses the folded normal distribution which is important in X-ray crystallography.

Modules

Working with PyTorch distributions can be a little verbose. So in addition to the torch.distributions style implementation, we provide DistributionModule classes which enable learnable distributions with automatic bijections in less code. These DistributionModule classes are subclasses of torch.nn.Module. They automatically instantiate problem parameters as TransformedParameter modules following the constraints in the distribution definition. In the following example, a FoldedNormal DistributionModule is instantiated with an initial location and scale and trained to match a target distribution.

from rs_distributions import modules as rsm
import torch

loc_init = 10.
scale_init = 5.

q = rsm.FoldedNormal(loc_init, scale_init)
p = torch.distributions.HalfNormal(1.)

opt = torch.optim.Adam(q.parameters())

steps = 10_000
num_samples = 256
for i in range(steps):
    opt.zero_grad()
    z = q.rsample((num_samples,))
    kl = (q.log_prob(z) - p.log_prob(z)).mean()
    kl.backward()
    opt.step()

License

rs-distributions is distributed under the terms of the MIT license.

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

rs_distributions-0.0.4.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

rs_distributions-0.0.4-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file rs_distributions-0.0.4.tar.gz.

File metadata

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

File hashes

Hashes for rs_distributions-0.0.4.tar.gz
Algorithm Hash digest
SHA256 bb5b65f2618ca9b938dfa6ca9818dc83e27309896c9d03d1a5e06d460284fa40
MD5 91f04a7ca7da49908c56211a9f3b79aa
BLAKE2b-256 f407c4d7c53a24c49d85f6cd0aaf1d57c2901e5218236e891c401a2447e3e7f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rs_distributions-0.0.4.tar.gz:

Publisher: publish.yml on rs-station/distributions

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

File details

Details for the file rs_distributions-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for rs_distributions-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e3d3838976a7dd4bbe0d4a66561f7a8b766d9b1ff81833386f94b2c646a2eae2
MD5 ed2593a1254f3508d3b336ae91e26ebb
BLAKE2b-256 e844b8069c8382089c29a6f93eb7fa76cb544811a6ae62c8897e970bfe789b92

See more details on using hashes here.

Provenance

The following attestation bundles were made for rs_distributions-0.0.4-py3-none-any.whl:

Publisher: publish.yml on rs-station/distributions

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