-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: main
Are you sure you want to change the base?
Changes from all commits
9d0527d
d1bc1b2
1273eb5
f52447b
88a9fc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an |
||
|
||
from preliz.distributions.distributions import Continuous | ||
from preliz.internal.distribution_helper import all_not_none, eps | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
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 ( | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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,