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

Conversation

Advaitgaur004
Copy link

@Advaitgaur004 Advaitgaur004 commented Jan 23, 2025

Description

This PR targeted #604 and work is in progress.

Implemented Modes

  • StudentT
  • Discreteweibull
  • exgaussian
  • hypergeometric
  • logitnormal

@Advaitgaur004
Copy link
Author

  • Binomial Distribution:
    Becomes bimodal when n is large and p is 0.5
  • Beta Distribution:
    Becomes bimodal when both shape parameters (α and β) are less than 1
  • Mixture Distribution:
    Explicitly created by combining two Gaussian distributions
  • Beta-Binomial Distribution:
    Bimodality occurs with small α and β parameters

@rohanbabbar04 These need to be changed if we are opting to send a tuple for multimodal purposes.

@Advaitgaur004
Copy link
Author

@aloctavodia Issues have been updated for discrete_weibull; I will use minimize_scale from scipy.optimize since direct calculation for mode in discrete_weibull isn't feasible. For the rest, the mode is calculated as provided in Wikipedia or some reliable sources.

@@ -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,

@@ -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

@@ -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)

@@ -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

preliz/distributions/skew_studentt.py Outdated Show resolved Hide resolved
@@ -150,6 +150,21 @@ def kurtosis(self):
* ((delta * np.sqrt(2 / np.pi)) ** 4 / (1 - 2 * (delta**2) / np.pi) ** 2)
)

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 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants