Skip to content

Instantly share code, notes, and snippets.

@mtambos
Last active November 24, 2017 11:40
Show Gist options
  • Save mtambos/4dcac06ce287f751a03aa3071f064c6a to your computer and use it in GitHub Desktop.
Save mtambos/4dcac06ce287f751a03aa3071f064c6a to your computer and use it in GitHub Desktop.
Implementation of the threshouldout method
# Copyright (c) 2017 Mario Tambos
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def thresholdout(scorer: Callable,
y_train: np.ndarray, y_train_pred: np.ndarray,
y_test: np.ndarray, y_test_pred: np.ndarray,
T: float=-1, tau: float=-1):
"""
Based on:
The reusable holdout: Preserving validity in adaptive data analysis
BY CYNTHIA DWORK, VITALY FELDMAN, MORITZ HARDT, TONIANN PITASSI, OMER REINGOLD, AARON ROTH
SCIENCE07 AUG 2015 : 636-638
"""
if T <= 0:
T = 1/len(y_train)
if tau <= 0:
tau = 4/len(y_train)
T_hat = T + np.random.laplace(scale=tau)
xi = np.random.laplace(scale=tau)
eta = np.random.laplace(scale=tau)
ESh = scorer(y_test, y_test_pred)
ESt = scorer(y_train, y_train_pred)
if np.abs(ESh - ESt) > T_hat + eta:
return ESh + xi
else:
return ESt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment