-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathregression.py
executable file
·282 lines (216 loc) · 11.4 KB
/
regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
This module declares the Bayesian regression tree models:
* PerpendicularRegressionTree
* HyperplaneRegressionTree
"""
import numpy as np
from abc import ABC
from scipy.special import gammaln
from sklearn.base import RegressorMixin
from bayesian_decision_tree.base import BaseTree
from bayesian_decision_tree.base_hyperplane import BaseHyperplaneTree
from bayesian_decision_tree.base_perpendicular import BasePerpendicularTree
class BaseRegressionTree(BaseTree, ABC, RegressorMixin):
"""
Abstract base class of all Bayesian regression trees (perpendicular and hyperplane). Performs
medium-level fitting and prediction tasks and outsources the low-level work to subclasses.
"""
def __init__(self, partition_prior, prior, delta, prune, child_type, split_precision, level=0):
BaseTree.__init__(self, partition_prior, prior, delta, prune, child_type, True, split_precision, level)
def _check_target(self, y):
if y.ndim != 1:
raise ValueError('y should have 1 dimension but has {}'.format(y.ndim))
def _compute_log_p_data_no_split(self, y, prior):
y_sum = y.sum()
y_squared_sum = (y ** 2).sum()
n = len(y)
mu_post, kappa_post, alpha_post, beta_post = self._compute_posterior_internal(prior, n, y_sum, y_squared_sum)
log_p_prior = np.log(1 - self.partition_prior**(1 + self.level))
log_p_data = self._compute_log_p_data(prior, alpha_post, beta_post, kappa_post, n)
return log_p_prior + log_p_data
def _compute_log_p_data_split(self, y, prior, n_dim, split_indices):
n = len(y)
n1 = np.arange(1, n)
n2 = n - n1
y_sum1 = y.cumsum()[:-1]
y_sum2 = y.sum() - y_sum1
y_squared_sum1 = (y[:-1] ** 2).cumsum()
y_squared_sum2 = (y ** 2).sum() - y_squared_sum1
if len(split_indices) != len(y)-1:
# we are *not* splitting between all data points -> indexing necessary
split_indices_minus_1 = split_indices - 1
n1 = n1[split_indices_minus_1]
n2 = n2[split_indices_minus_1]
y_sum1 = y_sum1[split_indices_minus_1]
y_sum2 = y_sum2[split_indices_minus_1]
y_squared_sum1 = y_squared_sum1[split_indices_minus_1]
y_squared_sum2 = y_squared_sum2[split_indices_minus_1]
mu1, kappa1, alpha1, beta1 = self._compute_posterior_internal(prior, n1, y_sum1, y_squared_sum1)
mu2, kappa2, alpha2, beta2 = self._compute_posterior_internal(prior, n2, y_sum2, y_squared_sum2)
n_splits = len(split_indices)
log_p_prior = np.log(self.partition_prior**(1+self.level) / (n_splits * n_dim))
log_p_data1 = self._compute_log_p_data(prior, alpha1, beta1, kappa1, n1)
log_p_data2 = self._compute_log_p_data(prior, alpha2, beta2, kappa2, n2)
return log_p_prior + log_p_data1 + log_p_data2
def _get_prior(self, n_data, n_dim):
if self.prior is not None:
return self.prior
else:
# TODO: use actual data to compute mu and tau
prior_pseudo_observation_count = max(1, n_data//100)
mu = 0
tau = 1
kappa = prior_pseudo_observation_count
alpha = prior_pseudo_observation_count/2
beta = alpha/tau
return np.array([mu, kappa, alpha, beta])
def _compute_posterior(self, y, prior, delta=1):
if delta == 0:
return prior
n = len(y)
y_sum = y.sum()
y_squared_sum = (y ** 2).sum()
return self._compute_posterior_internal(prior, n, y_sum, y_squared_sum, delta)
def _compute_posterior_internal(self, prior, n, y_sum, y_squared_sum, delta=1):
mu, kappa, alpha, beta = prior
# see https://www.cs.ubc.ca/~murphyk/Papers/bayesGauss.pdf, equations (86) - (89)
n_delta = n*delta
kappa_post = kappa + n_delta
mu_post = (kappa * mu + n_delta * y_sum / n) / kappa_post
alpha_post = alpha + 0.5*n_delta
beta_post = beta + 0.5 * delta * (y_squared_sum - y_sum ** 2 / n) + 0.5 * kappa * n_delta * (
y_sum / n - mu) ** 2 / (kappa + n)
return mu_post, kappa_post, alpha_post, beta_post
def _compute_posterior_mean(self):
return self.posterior_[0] # mu is the posterior mean
def _compute_log_p_data(self, prior, alpha_new, beta_new, kappa_new, n_new):
mu, kappa, alpha, beta = prior
# see https://www.cs.ubc.ca/~murphyk/Papers/bayesGauss.pdf, equation (95)
return (gammaln(alpha_new) - gammaln(alpha)
+ alpha*np.log(beta) - alpha_new*np.log(beta_new)
+ 0.5*np.log(kappa/kappa_new)
- 0.5*n_new*np.log(2*np.pi))
def _predict_leaf(self):
# predict posterior mean
return self._compute_posterior_mean()
def _get_raw_leaf_data_internal(self):
# prior and posterior raw data
return np.array([self.prior, self.posterior_])
class PerpendicularRegressionTree(BasePerpendicularTree, BaseRegressionTree):
"""
Bayesian regression tree using axes-normal splits ("perpendicular").
Uses a Normal-gamma(mu, kappa, alpha, beta) prior assuming unknown mean and unknown variance.
Parameters
----------
partition_prior : float, must be > 0.0 and < 1.0, typical value: 0.9
The prior probability of splitting a node's data into two children.
Small values tend to reduce the tree depth, leading to less expressiveness
but also to less overfitting.
Large values tend to increase the tree depth and thus lead to the tree
better fitting the data, which can lead to overfitting.
prior : array_like, shape = [4]
The prior hyperparameters [mu, kappa, alpha, beta] of the Normal-gamma
distribution (see also [1], [2], [3]):
- mu: prior pseudo-observation sample mean
- kappa: prior pseudo-observation count used to compute mu
- alpha: (prior pseudo-observation count used to compute sample variance)/2
- beta: alpha * (prior pseudo-observation sample variance)
It is usually easier to compute these hyperparameters off more intuitive
base quantities, see examples section.
delta : float, default=0.0
Determines the strengthening of the prior as the tree grows deeper,
see [1]. Must be a value between 0.0 and 1.0.
split_precision : float, default=0.0
Determines the minimum distance between two contiguous points to consider a split. If the distance is below
this threshold, the points are considered to overlap along this direction.
level : DO NOT SET, ONLY USED BY SUBCLASSES
See also
--------
demo_regression_perpendicular.py
PerpendicularClassificationTree
HyperplaneRegressionTree
References
----------
.. [1] https://en.wikipedia.org/wiki/Normal-gamma_distribution
.. [2] https://en.wikipedia.org/wiki/Normal-gamma_distribution#Interpretation_of_parameters
.. [3] https://en.wikipedia.org/wiki/Conjugate_prior#Continuous_distributions
Examples
--------
It is usually convenient to compute the prior hyperparameters as follows:
>>> # prior mean; set to the mean of the target
>>> mu = ...
>>>
>>> # prior standard deviation; set to about 0.1 times the standard deviation of the target
>>> sd_prior = ...
>>>
>>> # the number of prior pseudo-observations; set to roughly 1 - 10 % of the number of training samples
>>> prior_pseudo_observations = ...
>>>
>>> # now compute the prior
>>> kappa = prior_pseudo_observations
>>> alpha = prior_pseudo_observations/2
>>> beta = alpha*sd_prior**2
>>> prior = [mu, kappa, alpha, beta]
See `demo_regression_perpendicular.py`.
"""
def __init__(self, partition_prior=0.99, prior=None, delta=0, prune=False, split_precision=0.0, level=0):
child_type = PerpendicularRegressionTree
BasePerpendicularTree.__init__(self, partition_prior, prior, delta, prune, child_type, True, split_precision, level)
BaseRegressionTree.__init__(self, partition_prior, prior, delta, prune, child_type, split_precision, level)
class HyperplaneRegressionTree(BaseHyperplaneTree, BaseRegressionTree):
"""
Bayesian regression tree using arbitrarily-oriented hyperplane splits.
Uses a Normal-gamma(mu, kappa, alpha, beta) prior assuming unknown mean and unknown variance.
Parameters
----------
partition_prior : float, must be > 0.0 and < 1.0, typical value: 0.9
The prior probability of splitting a node's data into two children.
Small values tend to reduce the tree depth, leading to less expressiveness
but also to less overfitting.
Large values tend to increase the tree depth and thus lead to the tree
better fitting the data, which can lead to overfitting.
prior : array_like, shape = [4]
The prior hyperparameters [mu, kappa, alpha, beta] of the Normal-gamma
distribution (see also [1], [2], [3]):
- mu: prior pseudo-observation sample mean
- kappa: prior pseudo-observation count used to compute mu
- alpha: (prior pseudo-observation count used to compute sample variance)/2
- beta: alpha * (prior pseudo-observation sample variance)
It is usually easier to compute these hyperparameters off more intuitive
base quantities, see examples section.
delta : float, default=0.0
Determines the strengthening of the prior as the tree grows deeper,
see [1]. Must be a value between 0.0 and 1.0.
optimizer : object
A global optimization algorithm object that performs optimal hyperparameter
orientation search. The available options are (in the order in which you should
try them):
- ScipyOptimizer: A wrapper around scipy global optimizers. See usages for examples.
- SimulatedAnnealingOptimizer: Experimental, but works well with n_scan=20, n_keep=10, spread_factor=0.95
- RandomHyperplaneOptimizer: Experimental, mediocre performance
- RandomTwoPointOptimizer: Experimental, mediocre performance
- GradientDescentOptimizer: Experimental, mediocre performance
split_precision : float, default=0.0
Determines the minimum distance between two contiguous points to consider a split. If the distance is below
this threshold, the points are considered to overlap along this direction.
level : DO NOT SET, ONLY USED BY SUBCLASSES
See also
--------
demo_regression_hyperplane.py
HyperplaneClassificationTree
PerpendicularRegressionTree
References
----------
.. [1] https://en.wikipedia.org/wiki/Normal-gamma_distribution
.. [2] https://en.wikipedia.org/wiki/Normal-gamma_distribution#Interpretation_of_parameters
.. [3] https://en.wikipedia.org/wiki/Conjugate_prior#Continuous_distributions
Examples
--------
It is usually convenient to compute the prior hyperparameters in the same manner as for
the perpendicular case, see PerpendicularRegressionTree.
See `demo_regression_hyperplane.py`.
"""
def __init__(self, partition_prior=0.99, prior=None, delta=0, prune=False, optimizer=None, split_precision=0.0, level=0):
child_type = HyperplaneRegressionTree
BaseHyperplaneTree.__init__(self, partition_prior, prior, delta, prune, child_type, True, optimizer, split_precision, level)
BaseRegressionTree.__init__(self, partition_prior, prior, delta, prune, child_type, split_precision, level)