|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pymc_bart.split_rules import ContinuousSplitRule, OneHotSplitRule, SubsetSplitRule |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + argnames="Rule", |
| 9 | + argvalues=[ContinuousSplitRule, OneHotSplitRule, SubsetSplitRule], |
| 10 | + ids=["continuous", "one_hot", "subset"], |
| 11 | +) |
| 12 | +def test_split_rule(Rule): |
| 13 | + |
| 14 | + # Should return None if only one available value to pick from |
| 15 | + assert Rule.get_split_value(np.zeros(1)) is None |
| 16 | + |
| 17 | + # get_split should return a value divide can use |
| 18 | + available_values = np.arange(10).astype(float) |
| 19 | + sv = Rule.get_split_value(available_values) |
| 20 | + left = Rule.divide(available_values, sv) |
| 21 | + |
| 22 | + # divide should return a boolean numpy array |
| 23 | + # This de facto ensures it is a binary split |
| 24 | + assert len(left) == len(available_values) |
| 25 | + assert left.dtype == "bool" |
| 26 | + |
| 27 | + # divide should be deterministic |
| 28 | + left_repeated = Rule.divide(available_values, sv) |
| 29 | + assert (left == left_repeated).all() |
| 30 | + |
| 31 | + # Most elements should have a chance to go either direction |
| 32 | + # NB! This is not 100% necessary, but is a good proxy |
| 33 | + probs = np.array( |
| 34 | + [ |
| 35 | + Rule.divide(available_values, Rule.get_split_value(available_values)) |
| 36 | + for _ in range(10000) |
| 37 | + ] |
| 38 | + ).mean(axis=0) |
| 39 | + |
| 40 | + assert (probs > 0.01).sum() >= len(available_values) - 1 |
| 41 | + assert (probs < 0.99).sum() >= len(available_values) - 1 |
0 commit comments