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 jitter to duplicated values for continuous splitting rule #129

Merged
merged 2 commits into from
Nov 21, 2023
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
22 changes: 18 additions & 4 deletions pymc_bart/pgbart.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ def __init__(
else:
self.split_rules = [ContinuousSplitRule] * self.X.shape[1]

jittered = np.random.normal(self.X, self.X.std(axis=0) / 12)
min_values = np.min(self.X, axis=0)
max_values = np.max(self.X, axis=0)
self.X = np.clip(jittered, min_values, max_values)
for idx, rule in enumerate(self.split_rules):
if rule is ContinuousSplitRule:
self.X[:, idx] = jitter_duplicated(self.X[:, idx], np.std(self.X[:, idx]))

init_mean = self.bart.Y.mean()
self.num_observations = self.X.shape[0]
Expand Down Expand Up @@ -693,6 +692,21 @@ def inverse_cdf(
return new_indices


@njit
def jitter_duplicated(array: npt.NDArray[np.float_], std: float) -> npt.NDArray[np.float_]:
"""
Jitter duplicated values.
"""
seen = []
for idx, num in enumerate(array):
if num in seen:
array[idx] = num + np.random.normal(0, std / 12)
else:
seen.append(num)

return array


def logp(point, out_vars, vars, shared): # pylint: disable=redefined-builtin
"""Compile PyTensor function of the model and the input and output variables.

Expand Down
2 changes: 1 addition & 1 deletion pymc_bart/split_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def divide(available_splitting_values, split_value):
class SubsetSplitRule(SplitRule):
"""
Choose a random subset of the categorical values and branch on belonging to that set.
This is the approach taken by Sameer K. Deshpande.
This is the approach taken by Sameer K. Deshpande.
flexBART: Flexible Bayesian regression trees with categorical predictors. arXiv,
`link <https://arxiv.org/abs/2211.04459>`__
"""
Expand Down
Loading