Skip to content

Commit

Permalink
Fixed typos, fixed one incomplete doc string (see formula.py l19; is …
Browse files Browse the repository at this point in the history
…this correct?), made docstring punctation and emphasis (more) consistent.
  • Loading branch information
jt-lab committed Nov 29, 2023
1 parent 2d4b260 commit e1a27ed
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 160 deletions.
6 changes: 3 additions & 3 deletions bambi/backend/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def probit(x):
"""Probit function that ensures result is in (0, 1)"""
"""Probit function that ensures result is in (0, 1)."""
eps = np.finfo(float).eps
result = 0.5 + 0.5 * pt.erf(x / pt.sqrt(2))
result = pt.switch(pt.eq(result, 0), eps, result)
Expand All @@ -13,7 +13,7 @@ def probit(x):


def cloglog(x):
"""Cloglog function that ensures result is in (0, 1)"""
"""Cloglog function that ensures result is in (0, 1)."""
eps = np.finfo(float).eps
result = 1 - pt.exp(-pt.exp(x))
result = pt.switch(pt.eq(result, 0), eps, result)
Expand All @@ -23,7 +23,7 @@ def cloglog(x):


def logit(x):
"""Logit function that ensures result is in (0, 1)"""
"""Logit function that ensures result is in (0, 1)."""
eps = np.finfo(float).eps
result = pt.sigmoid(x)
result = pt.switch(pt.eq(result, 0), eps, result)
Expand Down
12 changes: 6 additions & 6 deletions bambi/backend/model_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def build_intercept(self, bmb_model):
self.output += InterceptTerm(self.component.intercept_term).build(bmb_model)

def build_offsets(self):
"""Add intercept term to the PyMC model.
"""Add intercept term to the PyMC model
We have linear predictors of the form 'X @ b + Z @ u'. This is technically part of
'X @ b' but it is added separately for convenience reasons.
Expand All @@ -75,10 +75,10 @@ def build_offsets(self):
self.output += offset.data.squeeze()

def build_common_terms(self, pymc_backend, bmb_model):
"""Add common (fixed) terms to the PyMC model.
"""Add common (fixed) terms to the PyMC model
We have linear predictors of the form 'X @ b + Z @ u'.
This creates the 'b' parameter vector in PyMC, computes `X @ b`, and adds it to ``self.mu``.
This creates the 'b' parameter vector in PyMC, computes 'X @ b', and adds it to ``self.mu``.
Parameters
----------
Expand Down Expand Up @@ -115,7 +115,7 @@ def build_common_terms(self, pymc_backend, bmb_model):
self.output += pt.dot(data, coefs)

def build_hsgp_terms(self, pymc_backend, bmb_model):
"""Add HSGP (Hilbert-Space Gaussian Process approximation) terms to the PyMC model.
"""Add HSGP (Hilbert-Space Gaussian Process approximation) terms to the PyMC model
The linear predictor 'X @ b + Z @ u' can be augmented with non-parametric HSGP terms
'f(x)'. This creates the 'f(x)' and adds it ``self.output``.
Expand All @@ -128,10 +128,10 @@ def build_hsgp_terms(self, pymc_backend, bmb_model):
self.output += hsgp_term.build(bmb_model)

def build_group_specific_terms(self, pymc_backend, bmb_model):
"""Add group-specific (random or varying) terms to the PyMC model.
"""Add group-specific (random or varying) terms to the PyMC model
We have linear predictors of the form 'X @ b + Z @ u'.
This creates the 'u' parameter vector in PyMC, computes `Z @ u`, and adds it to
This creates the 'u' parameter vector in PyMC, computes 'Z @ u', and adds it to
``self.output``.
"""
for term in self.component.group_specific_terms.values():
Expand Down
10 changes: 5 additions & 5 deletions bambi/backend/pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self):
self.components = {}

def build(self, spec):
"""Compile the PyMC model from an abstract model specification.
"""Compile the PyMC model from an abstract model specification
Parameters
----------
Expand Down Expand Up @@ -132,7 +132,7 @@ def build_response(self, spec):
response_component.build_response(self, spec)

def build_potentials(self, spec):
"""Add potentials to the PyMC model.
"""Add potentials to the PyMC model
Potentials are arbitrary quantities that are added to the model log likelihood.
See 'Factor Potentials' in
Expand Down Expand Up @@ -313,7 +313,7 @@ def _run_vi(self, **kwargs):
return self.vi_approx

def _run_laplace(self, draws, omit_offsets, include_mean):
"""Fit a model using a Laplace approximation.
"""Fit a model using a Laplace approximation
Mainly for pedagogical use, provides reasonable results for approximately
Gaussian posteriors. The approximation can be very poor for some models
Expand Down Expand Up @@ -369,12 +369,12 @@ def distributional_components(self):


def _posterior_samples_to_idata(samples, model):
"""Create InferenceData from samples.
"""Create InferenceData from samples
Parameters
----------
samples: array
Posterior samples
Posterior samples.
model: PyMC model
Returns
Expand Down
12 changes: 6 additions & 6 deletions bambi/backend/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def name(self):


class InterceptTerm:
"""Representation of an intercept term in a PyMC model.
"""Representation of an intercept term in a PyMC model
Parameters
----------
Expand Down Expand Up @@ -194,7 +194,7 @@ def name(self):


class ResponseTerm:
"""Representation of a response term in a PyMC model.
"""Representation of a response term in a PyMC model
Parameters
----------
Expand All @@ -209,19 +209,19 @@ def __init__(self, term, family):
self.family = family

def build(self, pymc_backend, bmb_model):
"""Create and return the response distribution for the PyMC model.
"""Create and return the response distribution for the PyMC model
Parameters
----------
pymc_backend : bambi.backend.PyMCModel
The object with all the backend information
bmb_model : bambi.Model
The Bambi model instance
The Bambi model instance.
Returns
-------
dist : pm.Distribution
The response distribution
The response distribution.
"""
data = np.squeeze(self.term.data)
parent = self.family.likelihood.parent
Expand Down Expand Up @@ -504,7 +504,7 @@ def get_covariance_functions(self):
Returns
-------
Sequence[pm.gp.Covariance]
A covariance function that can be used with a GP in PyMC
A covariance function that can be used with a GP in PyMC.
"""

# Get the callable that creates the function
Expand Down
6 changes: 3 additions & 3 deletions bambi/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_linkinv(link, invlinks):
Returns
-------
callable
The link function
The link function.
"""
# If the name is in the backend, get it from there
if link.name in invlinks:
Expand Down Expand Up @@ -97,7 +97,7 @@ def make_weighted_logp(dist: pm.Distribution):
Returns
-------
A function that computes the weighted logp
A function that computes the weighted logp.
"""

def logp(value, *dist_params, weights):
Expand All @@ -108,7 +108,7 @@ def logp(value, *dist_params, weights):


def get_dist_args(dist: pm.Distribution) -> list[str]:
"""Get the argument names of a PyMC distribution.
"""Get the argument names of a PyMC distribution
The argument names are the names of the parameters of the distribution.
Expand Down
24 changes: 12 additions & 12 deletions bambi/families/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class Family:
"""A specification of model family.
"""A specification of model family
Parameters
----------
Expand Down Expand Up @@ -110,7 +110,7 @@ def posterior_predictive(self, model, posterior, **kwargs):
This function works for almost all the families. It grabs the draws for the parameters
needed in the response distribution, and then gets samples from the posterior predictive
distribution using `pm.draw()`. It won't work when the response distribution requires
distribution using ``pm.draw()``. It won't work when the response distribution requires
parameters that are not available in `posterior`.
Parameters
Expand All @@ -129,7 +129,7 @@ def posterior_predictive(self, model, posterior, **kwargs):
Returns
-------
xr.DataArray
A data array with the draws from the posterior predictive distribution
A data array with the draws from the posterior predictive distribution.
"""
response_dist = get_response_dist(model.family)
params = model.family.likelihood.params
Expand Down Expand Up @@ -213,12 +213,12 @@ def get_response_dist(family):
Parameters
----------
family : bambi.Family
The family for which the response distribution is wanted
The family for which the response distribution is wanted.
Returns
-------
pm.Distribution
The response distribution
The response distribution.
"""
mapping = {"Cumulative": pm.Categorical, "StoppingRatio": pm.Categorical}

Expand All @@ -234,25 +234,25 @@ def get_response_dist(family):
def expand_array(x, ndim):
"""Add dimensions to an array to match the number of desired dimensions
If x.ndim < ndim, it adds ndim - x.ndim dimensions after the last axis. If not, it is left
If ``x.ndim < ndim``, it adds ``ndim - x.ndim`` dimensions after the last axis. If not, it is left
untouched.
For example, if we have a normal regression model with n = 1000, chains = 2, and draws = 500
the shape of the draws of mu will be (2, 500, 1000) but the shape of the draws of sigma will be
(2, 500). This function makes sure the shape of the draws of sigma is (2, 500, 1) which is
comaptible with (2, 500, 1000).
For example, if we have a normal regression model with ``n = 1000``, ``chains = 2``, and
``draws = 500`` the shape of the draws of mu will be ``(2, 500, 1000)`` but the shape of the
draws of sigma will be ``(2, 500)``. This function makes sure the shape of the draws of
sigma is ``(2, 500, 1)`` which is comaptible with ``(2, 500, 1000)``.
Parameters
----------
x : np.ndarray
The array
ndim : int
The number of desired dimensions
The number of desired dimensions.
Returns
-------
np.ndarray
The array with the expanded dimensions
The array with the expanded dimensions.
"""
if x.ndim == ndim:
return x
Expand Down
2 changes: 1 addition & 1 deletion bambi/families/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


class Likelihood:
"""Representation of a Likelihood function for a Bambi model.
"""Representation of a Likelihood function for a Bambi model
Notes:
* ``parent`` must be in ``params``
Expand Down
16 changes: 8 additions & 8 deletions bambi/families/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@


def force_within_unit_interval(x):
"""Make sure data in unit interval is in (0, 1)"""
"""Make sure data in unit interval is in (0, 1)."""
eps = np.finfo(float).eps
x[x == 0] = eps
x[x == 1] = 1 - eps
return x


def force_greater_than_zero(x):
"""Make sure data in positive reals is in (0, infty)"""
"""Make sure data in positive reals is in (0, infty)."""
eps = np.finfo(float).eps
x[x == 0] = eps
return x
Expand All @@ -33,32 +33,32 @@ def cloglog(mu):


def invcloglog(eta):
"""Inverse of the cloglog function that ensures result is in (0, 1)"""
"""Inverse of the cloglog function that ensures result is in (0, 1)."""
result = 1 - np.exp(-np.exp(eta))
return force_within_unit_interval(result)


def probit(mu):
"""Probit function that ensures the input is in (0, 1)"""
"""Probit function that ensures the input is in (0, 1)."""
mu = force_within_unit_interval(mu)
return 2**0.5 * special.erfinv(2 * mu - 1) # pylint: disable=no-member


def invprobit(eta):
"""Inverse of the probit function that ensures result is in (0, 1)"""
"""Inverse of the probit function that ensures result is in (0, 1)."""
result = 0.5 + 0.5 * special.erf(eta / 2**0.5) # pylint: disable=no-member
return force_within_unit_interval(result)


def expit(eta):
"""Expit function that ensures result is in (0, 1)"""
"""Expit function that ensures result is in (0, 1)."""
result = special.expit(eta) # pylint: disable=no-member
result = force_within_unit_interval(result)
return result


def logit(mu):
"""Logit function that ensures the input is in (0, 1)"""
"""Logit function that ensures the input is in (0, 1)."""
mu = force_within_unit_interval(mu)
return special.logit(mu) # pylint: disable=no-member

Expand Down Expand Up @@ -115,7 +115,7 @@ def link_not_implemented(*args, **kwargs):


class Link:
"""Representation of a link function.
"""Representation of a link function
This object contains two main functions. One is the link function itself, the function
that maps values in the response scale to the linear predictor, and the other is the inverse
Expand Down
10 changes: 5 additions & 5 deletions bambi/families/univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_success_level(self, response):


class Beta(UnivariateFamily):
"""Beta Family
"""Beta family
It uses the mean (mu) and sample size (kappa) parametrization of the Beta distribution.
"""
Expand Down Expand Up @@ -465,9 +465,9 @@ class ZeroInflatedPoisson(UnivariateFamily):

# pylint: disable = protected-access
def get_success_level(term):
"""Returns the success level of a categorical term.
"""Returns the success level of a categorical term
Whenever the concept of "success level" does not apply, it returns None.
Whenever the concept of "success level" does not apply, it returns ``None``.
"""
if term.kind != "categoric":
return None
Expand All @@ -485,9 +485,9 @@ def get_success_level(term):

# pylint: disable = protected-access
def get_reference_level(term):
"""Returns the reference level of a categorical term.
"""Returns the reference level of a categorical term
Whenever the concept of "reference level" does not apply, it returns None.
Whenever the concept of "reference level" does not apply, it returns ``None``.
"""
if term.kind != "categoric":
return None
Expand Down
Loading

0 comments on commit e1a27ed

Please sign in to comment.