Autograd Gamma
autograd-gamma provides autograd-compatible approximations to the gamma and beta family of functions, enabling automatic differentiation for these special functions. The current version is 0.5.0, with a release cadence that is infrequent but active, releasing updates as new functions or fixes are integrated.
Warnings
- gotcha For automatic differentiation, it is crucial to use `autograd.numpy` instead of standard `numpy` when defining functions that will be differentiated. `autograd-gamma` functions are designed to work with `autograd.numpy`'s array types.
- gotcha Functions for the incomplete beta function (e.g., `betainc`, `betaincln`) were added in version 0.4.0. Attempting to import or use these functions in `autograd-gamma` versions prior to 0.4.0 will result in an `ImportError` or `AttributeError`.
- gotcha The functions provided by `autograd-gamma` are *approximations* to the gamma and beta functions, specifically optimized for compatibility with `autograd`. They are not intended for arbitrary precision computations where exact values are required, which might be found in libraries like `scipy.special`.
Install
-
pip install autograd-gamma
Imports
- gammainc
from autograd_gamma import gammainc
- gammaincln
from autograd_gamma import gammaincln
- betainc
from autograd_gamma import betainc
Quickstart
import autograd.numpy as np
from autograd import grad
from autograd_gamma import gammainc
# Define a function using autograd-gamma's gammainc
def calculate_value(x):
# For autograd, inputs to gammainc should be autograd.numpy arrays
# The 'a' parameter (first argument) is the shape parameter, 'x' is the integral upper limit
return gammainc(2.0, x) # Example: incomplete gamma function for a=2.0
# Compute the gradient of the function with respect to x
gradient_function = grad(calculate_value)
# Test with a value (must be an autograd.numpy array for gradient)
x_val = np.array(0.5)
value = calculate_value(x_val)
gradient = gradient_function(x_val)
print(f"Function value gammainc(2.0, {x_val}) = {value}")
print(f"Gradient d/dx gammainc(2.0, x) at x={x_val} = {gradient}")