Source code for pybop.parameters.priors
import scipy.stats as stats
[docs]
class Gaussian:
"""
Represents a Gaussian (normal) distribution with a given mean and standard deviation.
This class provides methods to calculate the probability density function (pdf),
the logarithm of the pdf, and to generate random variates (rvs) from the distribution.
Parameters
----------
mean : float
The mean (mu) of the Gaussian distribution.
sigma : float
The standard deviation (sigma) of the Gaussian distribution.
"""
def __init__(self, mean, sigma):
self.name = "Gaussian"
self.mean = mean
self.sigma = sigma
[docs]
def pdf(self, x):
"""
Calculates the probability density function of the Gaussian distribution at x.
Parameters
----------
x : float
The point at which to evaluate the pdf.
Returns
-------
float
The probability density function value at x.
"""
return stats.norm.pdf(x, loc=self.mean, scale=self.sigma)
[docs]
def logpdf(self, x):
"""
Calculates the logarithm of the probability density function of the Gaussian distribution at x.
Parameters
----------
x : float
The point at which to evaluate the log pdf.
Returns
-------
float
The logarithm of the probability density function value at x.
"""
return stats.norm.logpdf(x, loc=self.mean, scale=self.sigma)
[docs]
def rvs(self, size):
"""
Generates random variates from the Gaussian distribution.
Parameters
----------
size : int
The number of random variates to generate.
Returns
-------
array_like
An array of random variates from the Gaussian distribution.
Raises
------
ValueError
If the size parameter is negative.
"""
if size < 0:
raise ValueError("size must be positive")
else:
return stats.norm.rvs(loc=self.mean, scale=self.sigma, size=size)
[docs]
def __repr__(self):
"""
Returns a string representation of the Gaussian object.
"""
return f"{self.name}, mean: {self.mean}, sigma: {self.sigma}"
[docs]
class Exponential:
"""
Represents an exponential distribution with a specified scale parameter.
This class provides methods to calculate the pdf, the log pdf, and to generate random
variates from the distribution.
Parameters
----------
scale : float
The scale parameter (lambda) of the exponential distribution.
"""
def __init__(self, scale):
self.name = "Exponential"
self.scale = scale
[docs]
def pdf(self, x):
"""
Calculates the probability density function of the exponential distribution at x.
Parameters
----------
x : float
The point at which to evaluate the pdf.
Returns
-------
float
The probability density function value at x.
"""
return stats.expon.pdf(x, scale=self.scale)
[docs]
def logpdf(self, x):
"""
Calculates the logarithm of the pdf of the exponential distribution at x.
Parameters
----------
x : float
The point at which to evaluate the log pdf.
Returns
-------
float
The log of the probability density function value at x.
"""
return stats.expon.logpdf(x, scale=self.scale)
[docs]
def rvs(self, size):
"""
Generates random variates from the exponential distribution.
Parameters
----------
size : int
The number of random variates to generate.
Returns
-------
array_like
An array of random variates from the exponential distribution.
Raises
------
ValueError
If the size parameter is negative.
"""
if size < 0:
raise ValueError("size must be positive")
else:
return stats.expon.rvs(scale=self.scale, size=size)
[docs]
def __repr__(self):
"""
Returns a string representation of the Uniform object.
"""
return f"{self.name}, scale: {self.scale}"