Skip to main content

A Python library for geometric computing on BREGman MANifolds with applications.

Project description

pyBregMan Logo

pyBregMan:

A Python library for geometric computing on BREGman MANifolds with applications. The focus of the library is to provide a specialized framework for Bregman manifolds: dually flat manifolds which consists of two convex generators related via Legendre–Fenchel duality.

Documentation is available on Read the Docs.

Installation

pyBregMan can be installed via pip3 or git.

For the former (using pip3), one can call:

pip3 install pyBregMan

For git installation, one can directly clone from GitHub.

git clone https://github.com/alexandersoen/pyBregMan.git
cd pyBregMan
pip3 install .

Getting Started

To use pyBregMan, a user can define Points on a specified Bregman manifold, where additional geometric objects (Geodesics, notions of dissimilarities, etc) can be defined on. Such a procedure consists of:

  • defining a BregmanManifold object;
  • specifying data via Point objects;
  • defining additional objects which act on a manifold object and data.

In addition, we also provide an inbuilt visualization framework which calls matplotlib.

In the following code example, we define the manifold of bivariate Gaussian distribution. We then define and compute different types of centroids. Finally, we visualize the data and centroids.

Pre-defined manifolds can be found in the bregman.application submodule. The following demonstrates how to define a manifold object for bivariate Gaussians. We additionally define two points.

import numpy as np

from bregman.application.distribution.exponential_family.gaussian import GaussianManifold
from bregman.base import LAMBDA_COORDS, DualCoords, Point

# Define Bivariate Normal Manifold
manifold = GaussianManifold(input_dimension=2)

# Define data
to_vector = lambda mu, sigma: np.concatenate([mu, sigma.flatten()])
mu_1, sigma_1 = np.array([0.0, 1.0]), np.array([[1.0, 0.5], [0.5, 2.0]])
mu_2, sigma_2 = np.array([1.0, 2.0]), np.array([[2.0, 1.0], [1.0, 1.0]])

point_1 = Point(LAMBDA_COORDS, to_vector(mu_1, sigma_1))
point_2 = Point(LAMBDA_COORDS, to_vector(mu_2, sigma_2))

We note that the points are not specific to the Gaussian manifold. However, given compatible dimensions, one can utilize manifold specific method. For instance, we can define the KL divergence between the two points.

# KL divergence can be calculated
kl = manifold.kl_divergence(point_1, point_2)
rkl = manifold.kl_divergence(point_2, point_1)

print("KL(point_1 || point_2):", kl)
print("KL(point_2 || point_1):", rkl)

# >>> KL(point_1 || point_2): 0.9940936082534253
# >>> KL(point_2 || point_1): 1.2201921060322891

More complicated geometric objects can be imported from other submodules. In this example, we will import various objects to allow us to compute a centroid of the two Gaussian distributions we have defined.

from bregman.barycenter.bregman import BregmanBarycenter, SkewBurbeaRaoBarycenter
from bregman.application.distribution.exponential_family.gaussian.geodesic import FisherRaoKobayashiGeodesic

# We can define and calculate centroids
theta_barycenter = BregmanBarycenter(manifold, DualCoords.THETA)
eta_barycenter = BregmanBarycenter(manifold, DualCoords.ETA)
br_barycenter = SkewBurbeaRaoBarycenter(manifold)
dbr_barycenter = SkewBurbeaRaoBarycenter(manifold, DualCoords.ETA)

theta_centroid = theta_barycenter([point_1, point_2])
eta_centroid = eta_barycenter([point_1, point_2])
br_centroid = br_barycenter([point_1, point_2])
dbr_centroid = dbr_barycenter([point_1, point_2])

# Mid point of Fisher-Rao Geodesic is its corresponding centroid of two points
fr_geodesic = FisherRaoKobayashiGeodesic(manifold, point_1, point_2)
fr_centroid = fr_geodesic(t=0.5)

print("Right-Sided Centroid:", manifold.convert_to_display(theta_centroid))
print("Left-Sided Centroid:", manifold.convert_to_display(eta_centroid))
print("Bhattacharyya Centroid:", manifold.convert_to_display(br_centroid))
print("Fisher-Rao Centroid:", manifold.convert_to_display(fr_centroid))

# >>> Right-Sided Centroid: $\mu$ = [0.33333333 1.55555556]; $\Sigma$ = [[1.33333333 0.66666667]
# >>>  [0.66666667 1.11111111]]
# >>> Left-Sided Centroid: $\mu$ = [0.5 1.5]; $\Sigma$ = [[1.75 1.  ]
# >>>  [1.   1.75]]
# >>> Bhattacharyya Centroid: $\mu$ = [0.41772374 1.53238683]; $\Sigma$ = [[1.53973074 0.82458984]
# >>>  [0.82458984 1.40126629]]
# >>> Fisher-Rao Centroid: $\mu$ = [0.5326167  1.62759115]; $\Sigma$ = [[1.72908532 0.98542147]
# >>>  [0.98542147 1.54545598]]

Finally, one can simply visualize the objects by using the inbuilt matplotlib visualizer.

from bregman.visualizer.matplotlib.callback import VisualizeGaussian2DCovariancePoints
from bregman.visualizer.matplotlib.matplotlib import MatplotlibVisualizer

# These objects can be visualized through matplotlib
visualizer = MatplotlibVisualizer(manifold, (0, 1))
visualizer.plot_object(point_1, c="black")
visualizer.plot_object(point_2, c="black")
visualizer.plot_object(theta_centroid, c="red", label="Right-Sided Centroid")
visualizer.plot_object(eta_centroid, c="blue", label="Left-Sided Centroid")
visualizer.plot_object(br_centroid, c="purple", label="Bhattacharyya Centroid")
visualizer.plot_object(fr_centroid, c="pink", label="Fisher-Rao Centroid")
visualizer.add_callback(VisualizeGaussian2DCovariancePoints())

visualizer.visualize(LAMBDA_COORDS)  # Display coordinate type

Centroid Example

Code can be found in examples/centroids.py.

Additional Links

Tutorial: Data Representations on the Bregman Manifold

A tutorial published at the ICML'24 GRAM workshop is available which explores different methods for summarizing data on a Bregman manifold.

Technical Report

A preprint of the technical report for pyBregMan is available on arXiv.

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

pybregman-0.1.0.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

pyBregMan-0.1.0-py3-none-any.whl (56.3 kB view details)

Uploaded Python 3

File details

Details for the file pybregman-0.1.0.tar.gz.

File metadata

  • Download URL: pybregman-0.1.0.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for pybregman-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d9e850354f60be4069828949633d1e3fe11ef940fdec827d3da2599929a907c
MD5 958b42adb2427836164f5abaaafb81aa
BLAKE2b-256 b68f2a93630429c1024383753389540eb471f4de64bb7ceccbbb510b3184feb0

See more details on using hashes here.

File details

Details for the file pyBregMan-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyBregMan-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 56.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for pyBregMan-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87b8f642c6aedd9caaf45e7b2c0bf5c65330fe184b5de56b6de1b627c37b6b9b
MD5 8dc111b8913ab45e7ed29e399a21c193
BLAKE2b-256 625a46871ac82c4e400e59e83d50a2215a14d0e7004cd75275fea5cb84cb6f5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page