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

Add mode method to more distribution classes #636

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions preliz/distributions/continuous_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def plot(**args):
interval,
levels,
"full",
legend,
figsize,
None,
xy_lim,
Expand All @@ -320,13 +321,23 @@ def plot(**args):
interval,
levels,
None,
legend,
figsize,
None,
xy_lim,
)

return interactive(plot, **plot_widgets)

def mode(self):
alpha_sum = np.sum(self.alpha)
K = len(self.alpha)
return np.where(
np.all(self.alpha > 1),
(self.alpha - 1) / (alpha_sum - K),
np.nan
)


class MvNormal(Continuous):
r"""
Expand Down Expand Up @@ -669,3 +680,10 @@ def plot(**args):
)

return interactive(plot, **plot_widgets)

def mode(self):
"""
Calculate the mode of the Multivariate Normal distribution.
For Multivariate Normal, the mode equals the mean.
"""
return self.mu
10 changes: 9 additions & 1 deletion preliz/distributions/discrete_weibull.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from preliz.internal.distribution_helper import all_not_none, eps, num_kurtosis, num_skewness
from preliz.internal.optimization import optimize_ml, optimize_moments
from preliz.internal.special import cdf_bounds, ppf_bounds_disc

from scipy.optimize import minimize_scalar

class DiscreteWeibull(Discrete):
R"""
Expand Down Expand Up @@ -107,6 +107,14 @@ def skewness(self):
def kurtosis(self):
return num_kurtosis(self)

def mode(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimization routines should be inside optimization module, as they can be potentially reused. The 100 is a magic number, maybe a better option is to use self.ppf(0.9999) or similar. I tried with a couple of examples and the mode is not always right,


def negative_pdf(x):
return -self.pdf(x)

result = minimize_scalar(negative_pdf, bounds=(0, 100), method='bounded')
return np.floor(result.x)

def rvs(self, size=None, random_state=None):
random_state = np.random.default_rng(random_state)
return self.ppf(random_state.uniform(size=size))
Expand Down
9 changes: 9 additions & 0 deletions preliz/distributions/exgaussian.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numba as nb
import numpy as np
from scipy.stats import skew
from scipy.special import erfcinv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an erfcinv function implemented in the special submodule


from preliz.distributions.distributions import Continuous
from preliz.internal.distribution_helper import all_not_none, eps
Expand Down Expand Up @@ -101,6 +102,14 @@ def entropy(self):
logpdf = self.logpdf(x_values)
return -np.trapz(np.exp(logpdf) * logpdf, x_values)

def mode(self):
tau = self.nu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why renamming the variable? I get nan for some combinations of parameters like pz.ExGaussian(0, 1, 10)

mu = self.mu
sigma = self.sigma

t = np.abs(tau) * np.sqrt(2/np.pi)
return mu - np.sign(tau) * np.sqrt(2*sigma) * erfcinv(t) + sigma**2/tau

def mean(self):
return self.mu + self.nu

Expand Down
3 changes: 3 additions & 0 deletions preliz/distributions/hypergeometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def kurtosis(self):
)
)

def mode(self):
return np.floor((self.n + 1) * (self.k + 1) / (self.N + 2))

def rvs(self, size=None, random_state=None):
random_state = np.random.default_rng(random_state)
return random_state.hypergeometric(self.k, self.N - self.k, self.n, size=size)
Expand Down
33 changes: 32 additions & 1 deletion preliz/distributions/logitnormal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numba as nb
import numpy as np

from scipy.optimize import root_scalar
from preliz.distributions.distributions import Continuous
from preliz.internal.distribution_helper import all_not_none, eps, from_precision, to_precision
from preliz.internal.special import (
Expand Down Expand Up @@ -149,6 +149,37 @@ def kurtosis(self):
pdf = self.pdf(x_values)
return np.trapz(((x_values - mean) / std) ** 4 * pdf, x_values) - 3

def mode(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the optimization module. Do you have a reference for this method? It seems to work fine for the values I manually checked. Maybe you can check how the mode is numerically computed here https://github.com/bgctw/logitnorm

def mode_equation(x):
# The equation is: logit(x) = σ²(2x-1) + μ
# We want to find the root of: logit(x) - σ²(2x-1) - μ = 0
return logit(x) - (self.sigma**2 * (2*x - 1)) - self.mu

#Left side
try:
sol1 = root_scalar(mode_equation, bracket=(eps, 0.5-eps)).root
except ValueError:
sol1 = None

#Right side
try:
sol2 = root_scalar(mode_equation, bracket=(0.5+eps, 1-eps)).root
except ValueError:
sol2 = None

if sol1 is None and sol2 is None:
# If no solutions found, return the median as an approximation
return self.median()
elif sol1 is None:
return sol2
elif sol2 is None:
return sol1
else:
# Return the solution with higher density
if self.pdf(sol1) >= self.pdf(sol2):
return sol1
return sol2

def rvs(self, size=None, random_state=None):
random_state = np.random.default_rng(random_state)
return expit(random_state.normal(self.mu, self.sigma, size))
Expand Down
15 changes: 15 additions & 0 deletions preliz/distributions/skewnormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ def kurtosis(self):
* ((delta * np.sqrt(2 / np.pi)) ** 4 / (1 - 2 * (delta**2) / np.pi) ** 2)
)

def mode(self):
Advaitgaur004 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives the wrong results for large absolute values of alpha. Not sure about the proper fix, but maybe it could be enough to ensure the mode is no smaller than mu (for positive values of alpha) and no larger than mu (for negative values of alpha)

alpha = self.alpha
delta = alpha / np.sqrt(1 + alpha**2)

# Calculate mo(alpha)
sqrt_2_pi = np.sqrt(2/np.pi)
term1 = sqrt_2_pi * delta
term2 = (1 - np.pi/4) * (sqrt_2_pi * delta)**3 / (1 - 2/np.pi * delta**2)
term3 = np.sign(alpha)/2 * np.exp(-2*np.pi/abs(alpha)) if alpha != 0 else 0

mo_alpha = term1 - term2 - term3

# Final mode calculation
return self.mu + self.sigma * mo_alpha

def rvs(self, size=None, random_state=None):
random_state = np.random.default_rng(random_state)
u_0 = random_state.normal(size=size)
Expand Down