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 ZeroSumNormal distribution #6121

Merged
merged 53 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6260c84
Use None as default value for zerosum_axes
AlexAndorra Sep 12, 2022
af96016
Add tests for ZSN
AlexAndorra Sep 12, 2022
71e5651
Reorder dispatched functions
AlexAndorra Sep 12, 2022
3cadb26
Test pylint
AlexAndorra Sep 12, 2022
a66c586
Ignore type check on normalize_axis_tuple
AlexAndorra Sep 12, 2022
e3be495
Disable mypy on import of normalize_axis_tuple
AlexAndorra Sep 12, 2022
759de36
Remove base class in tests
AlexAndorra Sep 12, 2022
a5a1e45
Use pytest parametrize
AlexAndorra Sep 15, 2022
c9eea6e
Add pm.draw to tests
AlexAndorra Sep 15, 2022
0582d7c
Test moment
AlexAndorra Sep 15, 2022
0bdcdd7
Add change size test
AlexAndorra Sep 15, 2022
854ef4c
Move ZSN to multivariate.py
AlexAndorra Sep 15, 2022
fd3aefa
Move ZSN tests to test_multivariate.py
AlexAndorra Sep 15, 2022
e94e4f1
Add check if zerosum_axes is iterable in dist method
AlexAndorra Sep 17, 2022
dec4a9f
Improve test_zsn_change_dist_size
AlexAndorra Sep 17, 2022
f7a55c5
Improve docstrings
AlexAndorra Sep 18, 2022
da6eaab
Refactor get_steps to work with multivariate support shapes
AlexAndorra Sep 27, 2022
a5ed1f0
Refactor ZSN dist and logp for rightmost zerosum_axes
AlexAndorra Sep 27, 2022
126e76b
Start writing __new__ method
AlexAndorra Sep 28, 2022
3a8d898
Handle single output and fix transform
AlexAndorra Sep 28, 2022
4c52737
Fix indexing of at.stack in get_support_shape
AlexAndorra Sep 28, 2022
7e4ed0a
Fix examples in ZSN docstrings
AlexAndorra Sep 28, 2022
44b5b91
Refactor test_zsn_dims_shape
AlexAndorra Sep 28, 2022
99dbb38
Refactor test_zsn_fail_axis
AlexAndorra Sep 28, 2022
e3dc1d4
Refactor test_zsn_change_dist_size
AlexAndorra Sep 29, 2022
09f0d91
Simplify test_zsn_dims_shape
AlexAndorra Sep 29, 2022
cf5b384
Refactor test_zsn_dims_shape
AlexAndorra Sep 29, 2022
3e86a3e
Fix get_support_shape
AlexAndorra Sep 29, 2022
ce68f02
Test support_shape handling
AlexAndorra Sep 29, 2022
09d849c
Merge branch 'main' into add-zerosumnormal
AlexAndorra Sep 29, 2022
b50909e
Remove TODO list comment
AlexAndorra Sep 29, 2022
c204131
Merge branch 'add-zerosumnormal' of https://github.com/pymc-devs/pymc…
AlexAndorra Sep 29, 2022
7ba1d0f
Add test of ZSN variance
AlexAndorra Sep 29, 2022
5ee950a
Remove unused imports
AlexAndorra Sep 30, 2022
95ffc94
Merge branch 'main' into add-zerosumnormal
AlexAndorra Sep 30, 2022
13a54e6
Replace get_steps by get_support_shape_1d in timeseries.py
AlexAndorra Sep 30, 2022
ca655bc
Split dims and shape test
AlexAndorra Sep 30, 2022
9d419ef
Fix test_get_support_shape_1d
AlexAndorra Sep 30, 2022
85da56c
Add test_get_support_shape
AlexAndorra Sep 30, 2022
f363118
Add ZSN logp test
AlexAndorra Oct 5, 2022
64eca5c
Fix test_inconsistent_steps_and_shape
AlexAndorra Oct 5, 2022
c5e76c9
Integrate review comments
AlexAndorra Oct 5, 2022
08c9df0
Solve freaking pre-commit issues
AlexAndorra Oct 5, 2022
c120f7e
Put assert_zerosum_axes at top of test class
AlexAndorra Oct 5, 2022
ba5f3a1
Improve error message of get_support_shape
AlexAndorra Oct 5, 2022
48dafe9
Nicer format for ZSN logp test
AlexAndorra Oct 5, 2022
6612a24
Increase tolerance for test_zsn_variance
AlexAndorra Oct 6, 2022
6b07a2a
Add ZSN to docs
AlexAndorra Oct 6, 2022
135ed47
Refactor ZSN docs
AlexAndorra Oct 6, 2022
cba0187
Better latex in ZSN docs
AlexAndorra Oct 6, 2022
566f308
Add ZeroSumTransform to docs
AlexAndorra Oct 7, 2022
5954e65
Remove mention of default value in ZS transform docs
AlexAndorra Oct 7, 2022
3e72922
Update pymc/distributions/transforms.py
ricardoV94 Oct 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/api/distributions/multivariate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Multivariate

MvNormal
MvStudentT
ZeroSumNormal
Dirichlet
Multinomial
DirichletMultinomial
Expand Down
1 change: 1 addition & 0 deletions docs/source/api/distributions/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Specific Transform Classes
LogExpM1
Ordered
SumTo1
ZeroSumTransform


Transform Composition Classes
Expand Down
4 changes: 3 additions & 1 deletion pymc/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
StickBreakingWeights,
Wishart,
WishartBartlett,
ZeroSumNormal,
)
from pymc.distributions.simulator import Simulator
from pymc.distributions.timeseries import (
Expand All @@ -116,8 +117,8 @@
"Uniform",
"Flat",
"HalfFlat",
"TruncatedNormal",
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved
"Normal",
"TruncatedNormal",
"Beta",
"Kumaraswamy",
"Exponential",
Expand Down Expand Up @@ -160,6 +161,7 @@
"Continuous",
"Discrete",
"MvNormal",
"ZeroSumNormal",
"MatrixNormal",
"KroneckerNormal",
"MvStudentT",
Expand Down
207 changes: 206 additions & 1 deletion pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import warnings

from functools import reduce
from typing import Optional

import aesara
import aesara.tensor as at
Expand Down Expand Up @@ -63,15 +64,17 @@
_change_dist_size,
broadcast_dist_samples_to,
change_dist_size,
get_support_shape,
rv_size_is_none,
to_tuple,
)
from pymc.distributions.transforms import Interval, _default_transform
from pymc.distributions.transforms import Interval, ZeroSumTransform, _default_transform
from pymc.math import kron_diag, kron_dot
from pymc.util import check_dist_not_registered

__all__ = [
"MvNormal",
"ZeroSumNormal",
"MvStudentT",
"Dirichlet",
"Multinomial",
Expand Down Expand Up @@ -2380,3 +2383,205 @@ def logp(value, alpha, K):
K > 0,
msg="alpha > 0, K > 0",
)


class ZeroSumNormalRV(SymbolicRandomVariable):
"""ZeroSumNormal random variable"""

_print_name = ("ZeroSumNormal", "\\operatorname{ZeroSumNormal}")
default_output = 0


class ZeroSumNormal(Distribution):
r"""
ZeroSumNormal distribution, i.e Normal distribution where one or
several axes are constrained to sum to zero.
By default, the last axis is constrained to sum to zero.
See `zerosum_axes` kwarg for more details.
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved

.. math::

\begin{align*}
ZSN(\sigma) = N \Big( 0, \sigma^2 (I - \tfrac{1}{n}J) \Big) \\
\text{where} \ ~ J_{ij} = 1 \ ~ \text{and} \\
n = \text{nbr of zero-sum axes}
\end{align*}

Parameters
----------
sigma : tensor_like of float
Scale parameter (sigma > 0).
It's actually the standard deviation of the underlying, unconstrained Normal distribution.
Defaults to 1 if not specified.
For now, ``sigma`` has to be a scalar, to ensure the zero-sum constraint.
zerosum_axes: int, defaults to 1
Number of axes along which the zero-sum constraint is enforced, starting from the rightmost position.
Defaults to 1, i.e the rightmost axis.
dims: sequence of strings, optional
Dimension names of the distribution. Works the same as for other PyMC distributions.
Necessary if ``shape`` is not passed.
shape: tuple of integers, optional
Shape of the distribution. Works the same as for other PyMC distributions.
Necessary if ``dims`` or ``observed`` is not passed.

Warnings
--------
``sigma`` has to be a scalar, to ensure the zero-sum constraint.
The ability to specifiy a vector of ``sigma`` may be added in future versions.

``zerosum_axes`` has to be > 0. If you want the behavior of ``zerosum_axes = 0``,
just use ``pm.Normal``.

Examples
--------
Define a `ZeroSumNormal` variable, with `sigma=1` and
`zerosum_axes=1` by default::

COORDS = {
"regions": ["a", "b", "c"],
"answers": ["yes", "no", "whatever", "don't understand question"],
}
with pm.Model(coords=COORDS) as m:
# the zero sum axis will be 'answers'
v = pm.ZeroSumNormal("v", dims=("regions", "answers"))

with pm.Model(coords=COORDS) as m:
# the zero sum axes will be 'answers' and 'regions'
v = pm.ZeroSumNormal("v", dims=("regions", "answers"), zerosum_axes=2)

with pm.Model(coords=COORDS) as m:
# the zero sum axes will be the last two
v = pm.ZeroSumNormal("v", shape=(3, 4, 5), zerosum_axes=2)
"""
rv_type = ZeroSumNormalRV

def __new__(cls, *args, zerosum_axes=None, support_shape=None, dims=None, **kwargs):
if dims is not None or kwargs.get("observed") is not None:
zerosum_axes = cls.check_zerosum_axes(zerosum_axes)

support_shape = get_support_shape(
support_shape=support_shape,
shape=None, # Shape will be checked in `cls.dist`
dims=dims,
observed=kwargs.get("observed", None),
ndim_supp=zerosum_axes,
)

return super().__new__(
cls, *args, zerosum_axes=zerosum_axes, support_shape=support_shape, dims=dims, **kwargs
)

@classmethod
def dist(cls, sigma=1, zerosum_axes=None, support_shape=None, **kwargs):
zerosum_axes = cls.check_zerosum_axes(zerosum_axes)

sigma = at.as_tensor_variable(floatX(sigma))
if sigma.ndim > 0:
raise ValueError("sigma has to be a scalar")

support_shape = get_support_shape(
support_shape=support_shape,
shape=kwargs.get("shape"),
ndim_supp=zerosum_axes,
)

if support_shape is None:
if zerosum_axes > 0:
raise ValueError("You must specify dims, shape or support_shape parameter")
# TODO: edge-case doesn't work for now, because at.stack in get_support_shape fails
# else:
# support_shape = () # because it's just a Normal in that case
support_shape = at.as_tensor_variable(intX(support_shape))

assert zerosum_axes == at.get_vector_length(
support_shape
), "support_shape has to be as long as zerosum_axes"

return super().dist(
[sigma], zerosum_axes=zerosum_axes, support_shape=support_shape, **kwargs
)

@classmethod
def check_zerosum_axes(cls, zerosum_axes: Optional[int]) -> int:
if zerosum_axes is None:
zerosum_axes = 1
if not isinstance(zerosum_axes, int):
raise TypeError("zerosum_axes has to be an integer")
if not zerosum_axes > 0:
raise ValueError("zerosum_axes has to be > 0")
return zerosum_axes

@classmethod
def rv_op(cls, sigma, zerosum_axes, support_shape, size=None):

shape = to_tuple(size) + tuple(support_shape)
normal_dist = ignore_logprob(pm.Normal.dist(sigma=sigma, shape=shape))

if zerosum_axes > normal_dist.ndim:
raise ValueError("Shape of distribution is too small for the number of zerosum axes")

normal_dist_, sigma_, support_shape_ = (
normal_dist.type(),
sigma.type(),
support_shape.type(),
)

# Zerosum-normaling is achieved by substracting the mean along the given zerosum_axes
zerosum_rv_ = normal_dist_
for axis in range(zerosum_axes):
zerosum_rv_ -= zerosum_rv_.mean(axis=-axis - 1, keepdims=True)

return ZeroSumNormalRV(
inputs=[normal_dist_, sigma_, support_shape_],
outputs=[zerosum_rv_, support_shape_],
ndim_supp=zerosum_axes,
)(normal_dist, sigma, support_shape)


@_change_dist_size.register(ZeroSumNormalRV)
def change_zerosum_size(op, normal_dist, new_size, expand=False):

normal_dist, sigma, support_shape = normal_dist.owner.inputs

if expand:
original_shape = tuple(normal_dist.shape)
old_size = original_shape[: len(original_shape) - op.ndim_supp]
new_size = tuple(new_size) + old_size

return ZeroSumNormal.rv_op(
sigma=sigma, zerosum_axes=op.ndim_supp, support_shape=support_shape, size=new_size
)


@_moment.register(ZeroSumNormalRV)
def zerosumnormal_moment(op, rv, *rv_inputs):
return at.zeros_like(rv)


@_default_transform.register(ZeroSumNormalRV)
def zerosum_default_transform(op, rv):
zerosum_axes = tuple(np.arange(-op.ndim_supp, 0))
return ZeroSumTransform(zerosum_axes)


@_logprob.register(ZeroSumNormalRV)
def zerosumnormal_logp(op, values, normal_dist, sigma, support_shape, **kwargs):
(value,) = values
shape = value.shape
zerosum_axes = op.ndim_supp

_deg_free_support_shape = at.inc_subtensor(shape[-zerosum_axes:], -1)
_full_size = at.prod(shape)
_degrees_of_freedom = at.prod(_deg_free_support_shape)

zerosums = [
at.all(at.isclose(at.mean(value, axis=-axis - 1), 0, atol=1e-9))
for axis in range(zerosum_axes)
]

out = at.sum(
pm.logp(normal_dist, value) * _degrees_of_freedom / _full_size,
axis=tuple(np.arange(-zerosum_axes, 0)),
)

return check_parameters(out, *zerosums, msg="at.mean(value, axis=zerosum_axes) == 0")
Loading