|
| 1 | +# # Exploring a credibility-based approach for tree-gain estimation |
| 2 | + |
| 3 | +include(joinpath(@__DIR__, "utils.jl")); #hide |
| 4 | + |
| 5 | +#= |
| 6 | +> The motivation for this experiment was to explore an alternative to gradient-based gain measure by integrating the volatility of split candidates to identity the best node split. |
| 7 | +=# |
| 8 | + |
| 9 | +#= |
| 10 | +
|
| 11 | +## Review of key gradient-based MSE characteristics |
| 12 | +
|
| 13 | +The figures below illustrate the behavior of vanilla gradient-based approach using a mean-squarred error (MSE) loss. |
| 14 | +The 2 colors represent the observations belonging to the left and right children. |
| 15 | +
|
| 16 | +Key observations: |
| 17 | +- **the gain is invariant to the volatility**: the top vs bottom figures differs only by the std dev of the observations. |
| 18 | + The associated gain is identical, which is aligned with the gradient-based approach to gain: the gain matches the reduction in the MSE, which is identical regardless of the dispersion. It's strictly driven by their mean. |
| 19 | +- **the gain scales linearly with the number of observations**: the right vs left figures contrasts different number of observations (100 vs 10k), and show that gain is directly proportional. |
| 20 | +- **the gain scales quadratically with the spread**: moving from a spread of 1.0 to 0.1 between the 2nd and 3rd row results in a drop by 100x of the gain: from 50.0 to 0.5. |
| 21 | +=# |
| 22 | + |
| 23 | +loss = :mse#hide |
| 24 | +f = get_dist_figure(; loss, nobs=100, spread=1.0, sd=1.0)#hide |
| 25 | +save(joinpath(@__DIR__, "assets", "dist-mse-1A.png"), f)#hide |
| 26 | +f = get_dist_figure(; loss, nobs=1_000, spread=1.0, sd=1.0)#hide |
| 27 | +save(joinpath(@__DIR__, "assets", "dist-mse-1B.png"), f)#hide |
| 28 | +f = get_dist_figure(; loss, nobs=100, spread=1.0, sd=0.1)#hide |
| 29 | +save(joinpath(@__DIR__, "assets", "dist-mse-2A.png"), f)#hide |
| 30 | +f = get_dist_figure(; loss, nobs=1_000, spread=1.0, sd=0.1)#hide |
| 31 | +save(joinpath(@__DIR__, "assets", "dist-mse-2B.png"), f);#hide |
| 32 | +f = get_dist_figure(; loss, nobs=100, spread=0.1, sd=0.1)#hide |
| 33 | +save(joinpath(@__DIR__, "assets", "dist-mse-3A.png"), f)#hide |
| 34 | +f = get_dist_figure(; loss, nobs=1_000, spread=0.1, sd=0.1)#hide |
| 35 | +save(joinpath(@__DIR__, "assets", "dist-mse-3B.png"), f);#hide |
| 36 | + |
| 37 | +#= |
| 38 | +|  |  | |
| 39 | +|:----------------------:|:----------------------:| |
| 40 | +|  |  | |
| 41 | +=# |
| 42 | + |
| 43 | +#= |
| 44 | +## Credibility-based gains |
| 45 | +=# |
| 46 | + |
| 47 | +#= |
| 48 | +The idea is for *gain* to reflect varying uncertainty levels for observations associated to each of the tree-split candidates. |
| 49 | +For tree-split candidates with an identical spread, the intuition is that candidates with a lower volatility, all other things being equal, should be preferred. |
| 50 | +The original inspiration comes from credibility theory, a foundational notion in actuarial science with direct connexion mixed effect models and bayesian theory. |
| 51 | +Key concept is that the credibility associated with a set of observations is driven by the relative effect of 2 components: |
| 52 | + - **Variance of the Hypothetical Means (VHM)**: if large differences between candidates means are expected, a greater credibility is assigned. |
| 53 | + - **Expected Value of the Process Variance (EVPV)**: if the data generation process of a given candidate has a large volatility, a smaller credibility is assigned. |
| 54 | +The Buhlmann credibility states that the optimal linear posterior estimator of a group mean is: |
| 55 | + - `Z * X̄ + (1 - Z) * μ`, where `X̄` is the group mean and `μ` the population mean. |
| 56 | +=# |
| 57 | + |
| 58 | +#= |
| 59 | +This approach results in a shift of perspective in how the gain is derived. |
| 60 | +Classical gradient based is about deriving a second-order approximation of the loss curve for a tre-split candidate. |
| 61 | +The gain corresponds to the reduction in this approximated loss by taking the prediciton that minimises the quadratic loss curve. |
| 62 | +The credibility-based takes a loss function agnostic approach, and view the gain as the total absolute change in the credibility-adjusted predicted value. |
| 63 | +Example, if a child has a mean residual of *2.0*, credibility of 0.5 and 100 observations, the resulting gain is: `2.0 * 0.5 * 100 = 100.0`, where `2.0 * 0.5` corresponds to the credibility adjusted prediction. |
| 64 | +
|
| 65 | +VHM is estimated as the square of the mean of the spread between observed values and predictions: |
| 66 | +- `VHM = E[X] = mean(y - p)` |
| 67 | +
|
| 68 | +EVPV is estimated as the variance of the observations. This value can be derived from the aggregation of the first and second moment of the individual observations: |
| 69 | +- `EVPV = E[(x - μ)²] = E[X²] - E²[X]` |
| 70 | +=# |
| 71 | + |
| 72 | +#= |
| 73 | +## Credibility-based losses in EvoTrees |
| 74 | +Two credibility-based losses are supported with `EvoTreeRegressor`: |
| 75 | + - **cred_var**: `VHM / (VHM + EVPV)` |
| 76 | + - **cred_std**: `sqrt(VHM) / (sqrt(VHM) + sqrt(EVPV))` |
| 77 | +=# |
| 78 | + |
| 79 | + |
| 80 | +#= |
| 81 | +Just like the gradient-based MSE error, the gain grows linearly with the number of observations, all other things being equal. |
| 82 | +However, a smaller volatility results in an increased gain, as shown in 2nd vs 1st row. |
| 83 | +=# |
| 84 | + |
| 85 | +loss = :cred_std#hide |
| 86 | +f = get_dist_figure(; loss, nobs=100, spread=1.0, sd=1.0)#hide |
| 87 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-1A.png"), f);#hide |
| 88 | +f = get_dist_figure(; loss, nobs=1_000, spread=1.0, sd=1.0)#hide |
| 89 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-1B.png"), f);#hide |
| 90 | +f = get_dist_figure(; loss, nobs=100, spread=1.0, sd=0.1)#hide |
| 91 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-2A.png"), f);#hide |
| 92 | +f = get_dist_figure(; loss, nobs=1_000, spread=1.0, sd=0.1)#hide |
| 93 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-2B.png"), f);#hide |
| 94 | +f = get_dist_figure(; loss, nobs=100, spread=0.1, sd=0.1)#hide |
| 95 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-3A.png"), f);#hide |
| 96 | +f = get_dist_figure(; loss, nobs=1_000, spread=0.1, sd=0.1)#hide |
| 97 | +save(joinpath(@__DIR__, "assets", "dist-cred_std-3B.png"), f);#hide |
| 98 | + |
| 99 | +#= |
| 100 | +|  |  | |
| 101 | +|:----------------------:|:----------------------:| |
| 102 | +|  |  | |
| 103 | +=# |
| 104 | + |
| 105 | +# ### Simulation grid |
| 106 | + |
| 107 | +#= |
| 108 | +The chart below show the associated credibility and gain for a given node split candidate for various spreads and standards deviations. |
| 109 | +=# |
| 110 | + |
| 111 | +nobs = 1000 |
| 112 | +sd_list = [0.01, 0.05, 0.1, 0.2, 0.5, 1, 2, 5] |
| 113 | +spread_list = [0.01, 0.05, 0.1, 0.2, 0.5, 1] |
| 114 | +metric_name = "cred"#hide |
| 115 | +f = get_cred_figureB(; metric_name, loss=:cred_std, nobs, sd_list, spread_list)#hide |
| 116 | +save(joinpath(@__DIR__, "assets", "heatmap-$metric_name-cred_std.png"), f);#hide |
| 117 | +metric_name = "gain"#hide |
| 118 | +f = get_cred_figureB(; metric_name, loss=:cred_std, nobs, sd_list, spread_list)#hide |
| 119 | +save(joinpath(@__DIR__, "assets", "heatmap-$metric_name-cred_std.png"), f);#hide |
| 120 | +#= |
| 121 | +|  |  | |
| 122 | +|:----------------------:|:----------------------:| |
| 123 | +=# |
| 124 | + |
| 125 | +# ### Illustration of different cred-based decision between `cred_std` to `MSE` |
| 126 | + |
| 127 | +#= |
| 128 | +Despite both `mse` and `cred_std` resulting in the same prediction, which matches the mean of the observations, the associated gain differs due to the volatility penalty. |
| 129 | +
|
| 130 | +The following illustrates a minimal scenario of 2 features, each with only 2 levels. |
| 131 | +=# |
| 132 | + |
| 133 | +#= |
| 134 | +|  |  | |
| 135 | +|:----------------------:|:----------------------:| |
| 136 | +=# |
| 137 | + |
| 138 | +#= |
| 139 | +```julia |
| 140 | +config = EvoTreeRegressor(loss=:mse, nrounds=1, max_depth=2) |
| 141 | +model_mse = EvoTrees.fit(config, dtrain; target_name="y") |
| 142 | +
|
| 143 | +EvoTrees.Tree{EvoTrees.MSE, 1} |
| 144 | + - feat: [2, 0, 0] |
| 145 | + - cond_bin: UInt8[0x01, 0x00, 0x00] |
| 146 | + - gain: Float32[12113.845, 0.0, 0.0] |
| 147 | + - pred: Float32[0.0 -0.017858343 0.3391479] |
| 148 | + - split: Bool[1, 0, 0] |
| 149 | +``` |
| 150 | +=# |
| 151 | + |
| 152 | +#= |
| 153 | +```julia |
| 154 | +config = EvoTreeRegressor(loss=:cred_std, nrounds=1, max_depth=2) |
| 155 | +model_std = EvoTrees.fit(config, dtrain; target_name="y") |
| 156 | +
|
| 157 | +EvoTrees.Tree{EvoTrees.CredStd, 1} |
| 158 | + - feat: [1, 0, 0] |
| 159 | + - cond_bin: UInt8[0x02, 0x00, 0x00] |
| 160 | + - gain: Float32[8859.706, 0.0, 0.0] |
| 161 | + - pred: Float32[0.0 0.07375729 -0.07375729] |
| 162 | + - split: Bool[1, 0, 0] |
| 163 | +``` |
| 164 | +=# |
| 165 | + |
| 166 | +#= |
| 167 | +## Benchmarks |
| 168 | +
|
| 169 | +From [MLBenchmarks.jl](https://github.com/Evovest/MLBenchmarks.jl). |
| 170 | +
|
| 171 | +| **model** | **metric** | **mse** | **cred_var** | **cred_std** | |
| 172 | +|:---------:|:----------:|:-------:|:------------:|:------------:| |
| 173 | +| boston | mse | 6.3 | 5.95 | 5.43 | |
| 174 | +| boston | gini | 0.945 | 0.947 | 0.952 | |
| 175 | +| year | mse | 74.9 | 74.6 | 74.2 | |
| 176 | +| year | gini | 0.662 | 0.664 | 0.661 | |
| 177 | +| msrank | mse | 0.55 | 0.551 | 0.549 | |
| 178 | +| msrank | ndcg | 0.511 | 0.509 | 0.51 | |
| 179 | +| yahoo | mse | 0.565 | 0.589 | 0.568 | |
| 180 | +| yahoo | ndcg | 0.795 | 0.787 | 0.794 | |
| 181 | +
|
| 182 | +=# |
0 commit comments