Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Gamma distribution multivariate #358

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 56 additions & 14 deletions cuqi/distribution/_gamma.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,79 @@
import numpy as np
from scipy.special import loggamma, gammainc
import scipy.stats as sps
from cuqi.distribution import Distribution
from cuqi.utilities import force_ndarray

class Gamma(Distribution):
"""
amal-ghamdi marked this conversation as resolved.
Show resolved Hide resolved
Represents a multivariate Gamma distribution characterized by shape and rate parameters of independent random variables x_i. Each is distributed according to the PDF function

f(x; shape, rate) = rate^shape * x^(shape-1) * exp(-rate * x) / Gamma(shape)
nabriis marked this conversation as resolved.
Show resolved Hide resolved
amal-ghamdi marked this conversation as resolved.
Show resolved Hide resolved

where `shape` and `rate` are the parameters of the distribution, and Gamma is the Gamma function.

Parameters
----------
shape : float or array_like, optional
The shape parameter of the Gamma distribution. Must be positive.

rate : float or array_like, optional
The rate parameter of the Gamma distribution. Must be positive. The inverse of the scale parameter.
jakobsj marked this conversation as resolved.
Show resolved Hide resolved

Examples
--------
.. code-block:: python

import numpy as np
import cuqi
import matplotlib.pyplot as plt

# Create a Gamma distribution instance
shape = 1
rate = 1e-4
gamma_dist = cuqi.distribution.Gamma(shape=shape, rate=rate)
jakobsj marked this conversation as resolved.
Show resolved Hide resolved

# Generate samples
samples = gamma_dist.sample(10000)

# Plot histogram of samples
samples.hist_chain(0, bins=70)
"""
def __init__(self, shape=None, rate=None, is_symmetric=False, **kwargs):
# Init from abstract distribution class
super().__init__(is_symmetric=is_symmetric,**kwargs)

# Init specific to this distribution
self.shape = shape
self.rate = rate
self.rate = rate

@property
def shape(self):
return self._shape

@shape.setter
def shape(self, value):
self._shape = force_ndarray(value, flatten=True)

@property
def rate(self):
return self._rate

@rate.setter
def rate(self, value):
self._rate = force_ndarray(value, flatten=True)

@property
def scale(self):
return 1/self.rate

def pdf(self, x):
# sps.gamma.pdf(x, a=self.shape, loc=0, scale=self.scale)
# (self.rate**self.shape)/(gamma(self.shape)) * (x**(self.shape-1)*np.exp(-self.rate*x))
return np.exp(self.logpdf(x))

def logpdf(self, x):
# sps.gamma.logpdf(x, a=self.shape, loc=0, scale=self.scale)
return (self.shape*np.log(self.rate)-loggamma(self.shape)) + ((self.shape-1)*np.log(x) - self.rate*x)
return np.sum(sps.gamma.logpdf(x, a=self.shape, loc=0, scale=self.scale))
amal-ghamdi marked this conversation as resolved.
Show resolved Hide resolved

def cdf(self, x):
# sps.gamma.cdf(x, a=self.shape, loc=0, scale=self.scale)
return gammainc(self.shape, self.rate*x)
return np.prod(sps.gamma.cdf(x, a=self.shape, loc=0, scale=self.scale))

def _sample(self, N, rng=None):
if rng is not None:
return rng.gamma(shape=self.shape, scale=self.scale, size=(N))
return rng.gamma(shape=self.shape, scale=self.scale, size=(N, self.dim)).T
else:
return np.random.gamma(shape=self.shape, scale=self.scale, size=(N))
return np.random.gamma(shape=self.shape, scale=self.scale, size=(N, self.dim)).T

2 changes: 2 additions & 0 deletions cuqi/sampler/_conjugate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self, target: Posterior):
raise ValueError("Conjugate sampler only works with a Gaussian-type likelihood function")
if not isinstance(target.prior, Gamma):
raise ValueError("Conjugate sampler only works with Gamma prior")
if not target.prior.dim == 1:
raise ValueError("Conjugate sampler only works with univariate Gamma prior")

if isinstance(target.likelihood.distribution, (RegularizedGaussian, RegularizedGMRF)) and target.likelihood.distribution.preset not in ["nonnegativity"]:
raise ValueError("Conjugate sampler only works implicit regularized Gaussian likelihood with nonnegativity constraints")
Expand Down
Loading