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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve HSGP and ZeroInflated / Hurdle distributions docs #7189

Merged
merged 1 commit into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions pymc/distributions/mixture.py
Expand Up @@ -629,7 +629,7 @@ class ZeroInflatedPoisson:
Parameters
----------
psi : tensor_like of float
Expected proportion of Poisson variates (0 < psi < 1)
Expected proportion of Poisson draws (0 < psi < 1)
mu : tensor_like of float
Expected number of occurrences during the given interval
(mu >= 0).
Expand Down Expand Up @@ -692,7 +692,7 @@ class ZeroInflatedBinomial:
Parameters
----------
psi : tensor_like of float
Expected proportion of Binomial variates (0 < psi < 1)
Expected proportion of Binomial draws (0 < psi < 1)
n : tensor_like of int
Number of Bernoulli trials (n >= 0).
p : tensor_like of float
Expand Down Expand Up @@ -779,7 +779,7 @@ def ZeroInfNegBinom(a, m, psi, x):
Parameters
----------
psi : tensor_like of float
Expected proportion of NegativeBinomial variates (0 < psi < 1)
Expected proportion of NegativeBinomial draws (0 < psi < 1)
mu : tensor_like of float
Poisson distribution parameter (mu > 0).
alpha : tensor_like of float
Expand Down Expand Up @@ -867,7 +867,7 @@ class HurdlePoisson:
Parameters
----------
psi : tensor_like of float
Expected proportion of Poisson variates (0 < psi < 1)
Expected proportion of Poisson draws (0 < psi < 1)
mu : tensor_like of float
Expected number of occurrences (mu >= 0).
"""
Expand Down Expand Up @@ -911,7 +911,7 @@ class HurdleNegativeBinomial:
Parameters
----------
psi : tensor_like of float
Expected proportion of Negative Binomial variates (0 < psi < 1)
Expected proportion of Negative Binomial draws (0 < psi < 1)
alpha : tensor_like of float
Gamma distribution shape parameter (alpha > 0).
mu : tensor_like of float
Expand Down Expand Up @@ -963,7 +963,7 @@ class HurdleGamma:
Parameters
----------
psi : tensor_like of float
Expected proportion of Gamma variates (0 < psi < 1)
Expected proportion of Gamma draws (0 < psi < 1)
alpha : tensor_like of float, optional
Shape parameter (alpha > 0).
beta : tensor_like of float, optional
Expand Down Expand Up @@ -1015,7 +1015,7 @@ class HurdleLogNormal:
Parameters
----------
psi : tensor_like of float
Expected proportion of LogNormal variates (0 < psi < 1)
Expected proportion of LogNormal draws (0 < psi < 1)
mu : tensor_like of float, default 0
Location parameter.
sigma : tensor_like of float, optional
Expand Down
27 changes: 16 additions & 11 deletions pymc/gp/hsgp_approx.py
Expand Up @@ -92,15 +92,15 @@ class HSGP(Base):

The `gp.HSGP` class is an implementation of the Hilbert Space Gaussian process. It is a
reduced rank GP approximation that uses a fixed set of basis vectors whose coefficients are
random functions of a stationary covariance function's power spectral density. It's usage
random functions of a stationary covariance function's power spectral density. Its usage
is largely similar to `gp.Latent`. Like `gp.Latent`, it does not assume a Gaussian noise model
and can be used with any likelihood, or as a component anywhere within a model. Also like
`gp.Latent`, it has `prior` and `conditional` methods. It supports any sum of covariance
functions that implement a `power_spectral_density` method. (Note, this excludes the
`Periodic` covariance function, which uses a different set of basis functions for a
low rank approximation, as described in `HSGPPeriodic`.).

For information on choosing appropriate `m`, `L`, and `c`, refer Ruitort-Mayol et al. or to
For information on choosing appropriate `m`, `L`, and `c`, refer to Ruitort-Mayol et al. or to
the PyMC examples that use HSGP.

To work with the HSGP in its "linearized" form, as a matrix of basis vectors and a vector of
Expand All @@ -117,14 +117,14 @@ class HSGP(Base):
`active_dim`.
c: float
The proportion extension factor. Used to construct L from X. Defined as `S = max|X|` such
that `X` is in `[-S, S]`. `L` is the calculated as `c * S`. One of `c` or `L` must be
that `X` is in `[-S, S]`. `L` is calculated as `c * S`. One of `c` or `L` must be
provided. Further information can be found in Ruitort-Mayol et al.
drop_first: bool
Default `False`. Sometimes the first basis vector is quite "flat" and very similar to
the intercept term. When there is an intercept in the model, ignoring the first basis
vector may improve sampling. This argument will be deprecated in future versions.
parameterization: str
Whether to use `centred` or `noncentered` parameterization when multiplying the
Whether to use the `centered` or `noncentered` parameterization when multiplying the
basis by the coefficients.
cov_func: Covariance function, must be an instance of `Stationary` and implement a
`power_spectral_density` method.
Expand Down Expand Up @@ -245,16 +245,16 @@ def prior_linearized(self, Xs: TensorLike):
"""Linearized version of the HSGP. Returns the Laplace eigenfunctions and the square root
of the power spectral density needed to create the GP.

This function allows the user to bypass the GP interface and work directly with the basis
This function allows the user to bypass the GP interface and work with the basis
and coefficients directly. This format allows the user to create predictions using
`pm.set_data` similarly to a linear model. It also enables computational speed ups in
multi-GP models since they may share the same basis. The return values are the Laplace
multi-GP models, since they may share the same basis. The return values are the Laplace
eigenfunctions `phi`, and the square root of the power spectral density.

Correct results when using `prior_linearized` in tandem with `pm.set_data` and
`pm.MutableData` require two conditions. First, one must specify `L` instead of `c` when
the GP is constructed. If not, a RuntimeError is raised. Second, the `Xs` needs to be
zero-centered, so it's mean must be subtracted. An example is given below.
zero-centered, so its mean must be subtracted. An example is given below.

Parameters
----------
Expand Down Expand Up @@ -286,9 +286,9 @@ def prior_linearized(self, Xs: TensorLike):
# L = [10] means the approximation is valid from Xs = [-10, 10]
gp = pm.gp.HSGP(m=[200], L=[10], cov_func=cov_func)

# Order is important. First calculate the mean, then make X a shared variable,
# then subtract the mean. When X is mutated later, the correct mean will be
# subtracted.
# Order is important.
# First calculate the mean, then make X a shared variable, then subtract the mean.
# When X is mutated later, the correct mean will be subtracted.
X_mean = np.mean(X, axis=0)
X = pm.MutableData("X", X)
Xs = X - X_mean
Expand All @@ -301,9 +301,14 @@ def prior_linearized(self, Xs: TensorLike):
# as m_star.
beta = pm.Normal("beta", size=gp._m_star)

# The (non-centered) GP approximation is given by
# The (non-centered) GP approximation is given by:
f = pm.Deterministic("f", phi @ (beta * sqrt_psd))

# The centered approximation can be more efficient when
# the GP is stronger than the noise
# beta = pm.Normal("beta", sigma=sqrt_psd, size=gp._m_star)
# f = pm.Deterministic("f", phi @ beta)
Comment on lines +307 to +310
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity: is this something empirical, or is there a general statement about it? :)


...


Expand Down