-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmean_estimators.py
227 lines (165 loc) · 6 KB
/
mean_estimators.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
"""Robust Mean estimation."""
# Author: Timothee Mathieu
# License: BSD 3 clause
import numpy as np
from scipy.stats import iqr
from sklearn.metrics import mean_squared_error
def block_mom(X, k, random_state):
"""Sample the indices of 2k+1 blocks for data x using a random permutation
Parameters
----------
X : array like, length = n_sample
sample whose size correspong to the size of the sample we want to do
blocks for.
k : int
we use 2k+1 blocks
random_state : RandomState instance
The seed of the pseudo random number generator to use when shuffling
the data.
Returns
-------
list of size K containing the lists of the indices of the blocks,
the size of the lists are contained in [n_sample/K,2n_sample/K]
"""
x = X.flatten()
K = 2 * k + 1
# Sample a permutation to shuffle the data.
perm = random_state.permutation(len(x))
return np.array_split(perm, K)
def median_of_means_blocked(X, blocks):
"""Compute the median of means of X using the blocks blocks
Parameters
----------
X : array like, length = n_sample
sample from which we want an estimator of the mean
blocks : list of list, provided by the function blockMOM.
Return
------
The median of means of x using the block blocks, a float.
"""
x = X.flatten()
# Compute the mean of each block
means_blocks = [np.mean([x[f] for f in ind]) for ind in blocks]
# Find the indice for which the mean of block is the median-of-means.
indice = np.argsort(means_blocks)[int(np.floor(len(means_blocks) / 2))]
return means_blocks[indice], indice
def median_of_means(X, k, random_state=np.random.RandomState(42)):
"""Compute the median of means of X using 2k+1 blocks
Parameters
----------
X : array like, length = n_sample
sample from which we want an estimator of the mean
k : int.
random_state : RandomState instance
The seed of the pseudo random number generator to use when shuffling
the data.
Return
------
The median of means of x using 2k+1 random blocks, a float.
"""
x = X.flatten()
blocks = block_mom(x, k, random_state)
return median_of_means_blocked(x, blocks)[0]
def huber(X, c=None, n_iter=20, tol=1e-3):
"""Compute the Huber estimator of location of X with parameter c
Parameters
----------
X : array like, length = n_sample
sample from which we want an estimator of the mean
c : float >0, default = None
parameter that control the robustness of the estimator.
c going to zero gives a behavior close to the median.
c going to infinity gives a behavior close to sample mean.
if c is None, the interquartile range (IQR) is used
as heuristic.
n_iter : int, default = 20
Number of iterations of the algorithm.
tol : float, default=1e-3
Tolerance on stopping criterion.
Return
------
The Huber estimator of location on x with parameter c, a float.
"""
x = X.flatten()
# Initialize the algorithm with a robust first-guess : the median.
mu = np.median(x)
if c is None:
c_numeric = iqr(x)
else:
c_numeric = c
def psisx(x, c):
# Huber weight function.
res = np.zeros(len(x))
mask = np.abs(x) <= c_numeric
res[mask] = 1
res[~mask] = c_numeric / np.abs(x[~mask])
return res
# Create a list to keep the ten last values of mu
last_mu = mu
# Run the iterative reweighting algorithm to compute M-estimator.
for t in range(n_iter):
# Compute the weights
w = psisx(x - mu, c_numeric)
# Infinite coordinates in x gives zero weight, we take them out.
ind_pos = w > 0
# Update the value of the estimate with the new estimate using the
# new weights.
mu = np.sum(np.array(w[ind_pos]) * x[ind_pos]) / np.sum(w[ind_pos])
# Stopping criterion. The error is decreasing at each iteration
if np.abs(mu - last_mu) < tol:
break
else:
last_mu = mu
return mu
def make_huber_metric(
score_func=mean_squared_error, sample_weight=None, c=None, n_iter=20
):
"""
Make a robust metric using Huber estimator.
Read more in the :ref:`User Guide <make_huber_metric>`.
Parameters
----------
score_func : callable
Score function (or loss function) with signature
``score_func(y, y_pred, **kwargs)``.
sample_weight: array-like of shape (n_samples,), default=None
Sample weights.
c : float >0, default = None
parameter that control the robustness of the estimator.
c going to zero gives a behavior close to the median.
c going to infinity gives a behavior close to sample mean.
if c is None, the iqr (inter quartile range) is used as heuristic.
n_iter : int, default = 20
Number of iterations of the algorithm.
Return
------
Robust metric function, a callable with signature
``score_func(y, y_pred, **kwargs).
Examples
--------
>>> import numpy as np
>>> from sklearn.metrics import mean_squared_error
>>> from sklearn_extra.robust import make_huber_metric
>>> robust_mse = make_huber_metric(mean_squared_error, c=5)
>>> y_true = np.hstack([np.zeros(98), 20*np.ones(2)]) # corrupted test values
>>> np.random.shuffle(y_true) # shuffle them
>>> y_pred = np.zeros(100) # predicted values
>>> result = robust_mse(y_true, y_pred)
"""
def metric(y_true, y_pred):
# change size in order to use the raw multisample
# to have individual values
y1 = [y_true]
y2 = [y_pred]
values = score_func(
y1, y2, sample_weight=sample_weight, multioutput="raw_values"
)
if c is None:
c_ = iqr(values)
else:
c_ = c
if c_ == 0:
return np.median(values)
else:
return huber(values, c_, n_iter)
return metric