Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekbhr committed Nov 5, 2023
1 parent 3250490 commit 9b4d716
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 162 deletions.
9 changes: 5 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def get_version(path=VPATH):
"sphinx.ext.autosummary",
"sphinxarg.ext",
"sphinx_toolbox.collapse",
'nbsphinx',
"nbsphinx",
]
# 'numpydoc'

# Do not execute tutorial notebooks
nbsphinx_execute = 'never'
nbsphinx_execute = "never"

# This is needed to suppress autosummary reordering
numpydoc_show_class_members = False
Expand All @@ -78,12 +78,13 @@ def get_version(path=VPATH):

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"
on_rtd = os.environ.get("READTHEDOCS", None) == "True"

if not on_rtd: # only import and set the theme if we're building docs locally
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'

html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# Add any paths that contain custom static files (such as style sheets) here,
Expand Down
93 changes: 36 additions & 57 deletions sincei/ExponentialFamily.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ExponentialFamily:
family_name : int
Name of the family.
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "base"
self.family_params = family_params if family_params else {}
Expand Down Expand Up @@ -116,6 +117,7 @@ class Bernoulli(ExponentialFamily):
- "max_val" (int) corresponding to the max value (replaces infinity).
Empirically, values above 10 yield similar results.
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "bernoulli"
self.family_params = family_params if family_params else {"max_val": 30}
Expand Down Expand Up @@ -147,6 +149,7 @@ class Poisson(ExponentialFamily):
family_params of interest:
- "min_val" (int) corresponding to the min value (replaces 0).
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "poisson"
self.family_params = family_params if family_params else {"min_val": 1e-20}
Expand Down Expand Up @@ -186,22 +189,17 @@ class Beta(ExponentialFamily):
for computing the "nu" parameter.
- "method" (str): method use to compute the "nu" parameter per
feature. Two possibles: "MLE" and "MM". Default to "MLE".
- "eps" (float): minimum difference used for inverting the g
- "eps" (float): minimum difference used for inverting the g
function. Default to 1e-4
- "maxiter" (int): maximum number of iterations for the inversion
of the g function. Default to 100.
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "beta"
if family_params is None or "nu" not in family_params:
print("Beta distribution not initialized yet")
default_family_params = {
"min_val": 1e-5,
"n_jobs": 1,
"eps": 1e-4,
"maxiter": 100,
"method": "MLE"
}
default_family_params = {"min_val": 1e-5, "n_jobs": 1, "eps": 1e-4, "maxiter": 100, "method": "MLE"}
self.family_params = family_params if family_params else default_family_params
for k in kwargs:
self.family_params[k] = kwargs[k]
Expand All @@ -211,14 +209,11 @@ def __init__(self, family_params=None, **kwargs):
self.family_params[k] = default_family_params[k]

def sufficient_statistics(self, X: torch.Tensor):
X = X.clip(self.family_params["min_val"], 1-self.family_params["min_val"])
X = X.clip(self.family_params["min_val"], 1 - self.family_params["min_val"])
return torch.stack([torch.log(X), torch.log(1 - X)])

def natural_parametrization(self, theta: torch.Tensor):
nat_params = torch.stack([
theta * self.family_params["nu"],
(1 - theta) * self.family_params["nu"]
])
nat_params = torch.stack([theta * self.family_params["nu"], (1 - theta) * self.family_params["nu"]])
if nat_params.shape[1] == 1:
nat_params = nat_params.flatten()
return nat_params
Expand All @@ -227,22 +222,16 @@ def base_measure(self, X: torch.Tensor):
return torch.mul(X, 1 - X)

def log_partition(self, theta: torch.Tensor):
numerator = torch.sum(
torch.lgamma(self.natural_parametrization(theta)),
axis=0
)
numerator = torch.sum(torch.lgamma(self.natural_parametrization(theta)), axis=0)
# numerator = torch.sum(numerator, axis=0)
denominator = torch.lgamma(self.family_params["nu"])
return numerator - denominator

def exponential_term(self, X: torch.Tensor, theta: torch.Tensor):
return torch.sum(
torch.multiply(self.sufficient_statistics(X), self.natural_parametrization(theta)),
axis=0
)
return torch.sum(torch.multiply(self.sufficient_statistics(X), self.natural_parametrization(theta)), axis=0)

def _derivative_log_likelihood(self, X: torch.Tensor, theta: torch.Tensor):
X = X.clip(self.family_params['eps'], 1-self.family_params['eps'])
X = X.clip(self.family_params["eps"], 1 - self.family_params["eps"])
return (
torch.log(X / (1 - X))
+ torch.digamma((1 - theta) * self.family_params["nu"])
Expand All @@ -253,19 +242,13 @@ def initialize_family_parameters(self, X: torch.Tensor = None):
p = X.shape[1]

def compute_beta_param(x):
y = x[x > self.family_params['eps']]
y = y[y < 1-self.family_params['eps']]
return scipy.stats.beta.fit(
y,
floc=0,
fscale=1,
method=self.family_params["method"]
)

y = x[x > self.family_params["eps"]]
y = y[y < 1 - self.family_params["eps"]]
return scipy.stats.beta.fit(y, floc=0, fscale=1, method=self.family_params["method"])

self.family_params["nu"] = torch.Tensor(
Parallel(n_jobs=self.family_params["n_jobs"], batch_size=100, backend="threading")(
delayed(compute_beta_param)(X[:, idx])
for idx in tqdm(range(p))
delayed(compute_beta_param)(X[:, idx]) for idx in tqdm(range(p))
)
)
self.family_params["nu"] = torch.sum(self.family_params["nu"][:, :2], axis=1)
Expand All @@ -276,8 +259,8 @@ def compute_beta_param(x):
def invert_g(self, X: torch.Tensor = None):
"""Dichotomy to find where derivative maxes out"""

X = X.clip(self.family_params['eps'], 1-self.family_params['eps'])
X = X.clip(self.family_params["eps"], 1 - self.family_params["eps"])

# Initialize dichotomy parameters.
min_val = torch.zeros(X.shape)
max_val = torch.ones(X.shape)
Expand Down Expand Up @@ -305,7 +288,7 @@ class SigmoidBeta(Beta):
This distribution is similar to the previous Beta (which it
inherits from) but the natural parameter is re-parametrized using
a Sigmoid. This is shown expeerimentally to stabilize the
a Sigmoid. This is shown expeerimentally to stabilize the
optimisation by removing the ]0,1[ constraint.
family_params of interest:
Expand All @@ -314,23 +297,22 @@ class SigmoidBeta(Beta):
for computing the "nu" parameter.
- "method" (str): method use to compute the "nu" parameter per
feature. Two possibles: "MLE" and "MM". Default to "MLE".
- "eps" (float): minimum difference used for inverting the g
- "eps" (float): minimum difference used for inverting the g
function. Default to 1e-4
- "maxiter" (int): maximum number of iterations for the inversion
of the g function. Default to 100.
"""

def natural_parametrization(self, theta: torch.Tensor):
nat_params = torch.stack([
torch.sigmoid(theta) * self.family_params["nu"],
(1 - torch.sigmoid(theta)) * self.family_params["nu"]
])
nat_params = torch.stack(
[torch.sigmoid(theta) * self.family_params["nu"], (1 - torch.sigmoid(theta)) * self.family_params["nu"]]
)
if nat_params.shape[1] == 1:
nat_params = nat_params.flatten()
return nat_params


def _derivative_log_likelihood(self, X: torch.Tensor, logit_theta: torch.Tensor):
X = X.clip(self.family_params['eps'], 1-self.family_params['eps'])
X = X.clip(self.family_params["eps"], 1 - self.family_params["eps"])
return (
torch.log(X / (1 - X))
+ torch.digamma((1 - logit_theta) * self.family_params["nu"])
Expand All @@ -340,8 +322,8 @@ def _derivative_log_likelihood(self, X: torch.Tensor, logit_theta: torch.Tensor)
def invert_g(self, X: torch.Tensor = None):
"""Dichotomy to find where derivative maxes out"""

X = X.clip(self.family_params['eps'], 1-self.family_params['eps'])
X = X.clip(self.family_params["eps"], 1 - self.family_params["eps"])

# Initialize dichotomy parameters.
min_val = torch.zeros(X.shape)
max_val = torch.ones(X.shape)
Expand All @@ -363,6 +345,7 @@ def invert_g(self, X: torch.Tensor = None):

return torch.logit(logit_theta)


class Gamma(ExponentialFamily):
r"""Gamma distribution using a standard formulation.
Expand All @@ -375,22 +358,17 @@ class Gamma(ExponentialFamily):
for computing the "nu" parameter.
- "method" (str): method use to compute the "nu" parameter per
feature. Two possibles: "MLE" and "MM". Default to "MLE".
- "eps" (float): minimum difference used for inverting the g
- "eps" (float): minimum difference used for inverting the g
function. Default to 1e-4
- "maxiter" (int): maximum number of iterations for the inversion
of the g function. Default to 100.
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "gamma"
if family_params is None or "nu" not in family_params:
print("Gamma distribution not initialized yet")
default_family_params = {
"min_val": 1e-5,
"max_val": 10e6,
"n_jobs": 1,
"eps": 1e-4,
"maxiter": 100
}
default_family_params = {"min_val": 1e-5, "max_val": 10e6, "n_jobs": 1, "eps": 1e-4, "maxiter": 100}
self.family_params = family_params if family_params else default_family_params
for k in kwargs:
self.family_params[k] = kwargs[k]
Expand Down Expand Up @@ -463,20 +441,21 @@ class LogNormal(ExponentialFamily):
for computing the "nu" parameter.
- "method" (str): method use to compute the "nu" parameter per
feature. Two possibles: "MLE" and "MM". Default to "MLE".
- "eps" (float): minimum difference used for inverting the g
- "eps" (float): minimum difference used for inverting the g
function. Default to 1e-4
- "maxiter" (int): maximum number of iterations for the inversion
of the g function. Default to 100.
"""

def __init__(self, family_params=None, **kwargs):
self.family_name = "log_normal"
if family_params is None or "nu" not in family_params:
print("Log Normal distribution not initialized yet")
default_family_params = {
"min_val": 1e-8,
"min_val": 1e-8,
"max_val": 10e6,
"n_jobs": 1,
"eps": 1e-4,
"n_jobs": 1,
"eps": 1e-4,
"maxiter": 100,
}
self.family_params = family_params if family_params else default_family_params
Expand Down
Loading

0 comments on commit 9b4d716

Please sign in to comment.