Skip to main content

A dedicated inverse-trigamma function implementation over positive reals

Project description

itrigamma

A self-contained implementation of the inverse-trigamma function, $\psi'^{-1}$, for use in variational inference, empirical bayes, and other settings involving variance estimation wrt log-gamma random variables.

Written in C with fast bindings for

  • Python (python/citrigamma.pyx)
  • R (r/RCall_itrigamma.c)

Install (Python)

pip install itrigamma

Test (Python)

Copy+paste into shell:

python <<'TEST_PY'
import time, numpy as np, math
from itrigamma import itrigamma

x = np.random.default_rng(0).random(10_000_000)

t = time.perf_counter()
y = itrigamma(x)
dt = time.perf_counter() - t

print(f'Approx. {(x.size/dt) / 1_000_000:.1f} million evals per sec.')

KNOWN = [
    (1.0e-8, 100000000.499999999167),
    (0.05, 20.49583523915460591),
    (0.10, 10.491681821078422372),
    (0.20, 5.4834517798530476662),
    (0.50, 2.4599529483523074137),
    (1.00, 1.4262551202150789904),
    (2.00, 0.87666407746426017692),
    (5.00, 0.49616873470410694489),
    (10.0, 0.33508104437803564513),
    (20.0, 0.23077632915242632912),
    (50.0, 0.14337981235286147770),
]

for i, (x0, y0) in enumerate(KNOWN):
    assert math.isclose(itrigamma(x0), y0, abs_tol=1e-12)
    print(f'  --> check {i}:\t{(x0,y0)}\tGOOD')

TEST_PY

Output (Exact results vary wrt machine precision.)

TEST_PY
Approx. 8.2 million evals per sec.
  --> check 0:	(1e-08, 100000000.5)	GOOD
  --> check 1:	(0.05, 20.495835239154605)	GOOD
  --> check 2:	(0.1, 10.491681821078423)	GOOD
  --> check 3:	(0.2, 5.4834517798530475)	GOOD
  --> check 4:	(0.5, 2.4599529483523073)	GOOD
  --> check 5:	(1.0, 1.426255120215079)	GOOD
  --> check 6:	(2.0, 0.8766640774642602)	GOOD
  --> check 7:	(5.0, 0.49616873470410694)	GOOD
  --> check 8:	(10.0, 0.33508104437803565)	GOOD
  --> check 9:	(20.0, 0.23077632915242632)	GOOD
  --> check 10:	(50.0, 0.14337981235286149)	GOOD

Install (R)

First clone this repository

git clone https://github.com/nolan-h-hamilton/itrigamma.git
cd itrigamma

Then build with base R's r/utils/SHLIB

PKG_CPPFLAGS="-I$(pwd)/src" R CMD SHLIB r/RCall_itrigamma.c src/itrigamma.c -o r_itrigamma.so

Within R, you can then load the shared library and use base R's .Call for a simple wrapper, e.g.,

# load
dyn.load("r_itrigamma.so")
# easiest: include the following as a wrapper around .Call
itrigamma = function(x) .Call("r_itrigamma", as.double(x))

# e.g.,
x_ = seq(1.0e-8, 100, length.out = 50)
y = itrigamma(x_)
plot(x_,y, type='l')

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

itrigamma-0.0.3a0.tar.gz (86.8 kB view details)

Uploaded Source

Built Distribution

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

itrigamma-0.0.3a0-cp312-cp312-macosx_11_0_arm64.whl (34.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file itrigamma-0.0.3a0.tar.gz.

File metadata

  • Download URL: itrigamma-0.0.3a0.tar.gz
  • Upload date:
  • Size: 86.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for itrigamma-0.0.3a0.tar.gz
Algorithm Hash digest
SHA256 a905a1a2b6812e144b70b8291059e79b86ffeeced70738f195a3d84f41b4d34f
MD5 69bf3adb57702493bb688249e2d8d89a
BLAKE2b-256 8234bf4b22666dc83773faf4c7bf2804cca4104fb81007e1f512d33c82bf69b9

See more details on using hashes here.

File details

Details for the file itrigamma-0.0.3a0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for itrigamma-0.0.3a0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b3455095a4d051d1cf85125ddff4ab91d9e6c059fab35ee36ce9c5ac5a4d19c
MD5 d45613a14578e74e881637b8ef11de9c
BLAKE2b-256 1ffb8d99a477e87d7021e4f45d5af85b85c19ae2cfa4e0b6d8526e95327f51a3

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