Skip to content

add moment for BART distribution #5211

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

Merged
merged 2 commits into from
Nov 20, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pymc/bart/bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from aesara.tensor.random.op import RandomVariable, default_shape_from_params
from pandas import DataFrame, Series

from pymc.distributions.distribution import NoDistribution
from pymc.distributions.distribution import NoDistribution, _get_moment

__all__ = ["BART"]

Expand Down Expand Up @@ -110,6 +110,10 @@ def __new__(

NoDistribution.register(BARTRV)

@_get_moment.register(BARTRV)
def get_moment(rv, size, *rv_inputs):
return cls.get_moment(rv, size, *rv_inputs)

cls.rv_op = bart_op
params = [X, Y, m, alpha, k]
return super().__new__(cls, name, *params, **kwargs)
Expand All @@ -132,6 +136,11 @@ def logp(x, *inputs):
"""
return at.zeros_like(x)

@classmethod
def get_moment(cls, rv, size, *rv_inputs):
Copy link
Member

Choose a reason for hiding this comment

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

Could BART shape be determined by the shape of the rv_inputs? Or does the mean already integrate this info?

Copy link
Member Author

Choose a reason for hiding this comment

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

The shape of BART is always the shape of observed data Y.

Copy link
Member

Choose a reason for hiding this comment

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

And do you do anything with the size variable or is that branch below unnecessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

everything is a lie :-) Initially I tough about having a size argument for future extension. But probably that will not be very useful.

mean = at.fill(size, rv.Y.mean())
return mean


def preprocess_XY(X, Y):
if isinstance(Y, (Series, DataFrame)):
Expand Down
16 changes: 16 additions & 0 deletions pymc/tests/test_bart.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import pymc as pm

from pymc.tests.test_distributions_moments import assert_moment_is_expected


def test_split_node():
split_node = pm.bart.tree.SplitNode(index=5, idx_split_variable=2, split_value=3.0)
Expand Down Expand Up @@ -97,3 +99,17 @@ def test_predict(self):
)
def test_pdp(self, kwargs):
pm.bart.utils.plot_dependence(self.idata, X=self.X, Y=self.Y, **kwargs)


@pytest.mark.parametrize(
"size, expected",
[
(None, np.zeros(50)),
],
)
def test_bart_moment(size, expected):
X = np.zeros((50, 2))
Y = np.zeros(50)
with pm.Model() as model:
pm.BART("x", X=X, Y=Y, size=size)
assert_moment_is_expected(model, expected)