Last active
May 21, 2016 22:15
-
-
Save abarth/4331ae47fd0f85ff10e4eaf260d6f0be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import numpy | |
| from scipy import stats | |
| def sample_left(): | |
| return random.uniform(0, 1000) | |
| def sample_right(): | |
| return random.uniform(0, 1000) | |
| def get_observation(): | |
| return (sample_left(), sample_right()) | |
| population = [get_observation() for i in range(100000)] | |
| adversary_map = {} | |
| for p in population: | |
| left = p[0] | |
| right = p[1] | |
| record = adversary_map.get(right, (0, 0)) | |
| adversary_map[right] = (record[0] + left, record[1] + 1) | |
| def get_score_adversarial(left, right): | |
| record = adversary_map.get(right) | |
| if record is None: | |
| return int(left) | |
| return int(record[0] / record[1]) | |
| def get_score(left, right): | |
| #### Even weight #### | |
| # return left * 0.5 + right * 0.5 | |
| #### Biased right #### | |
| # return left * 0.25 + right * 0.75 | |
| #### Adversarial behavior #### | |
| return get_score_adversarial(left, right) | |
| def estimate_left_cause(p, samples=1000): | |
| right = p[1] | |
| real_score = get_score(p[0], right) | |
| sampled_scores = [get_score(sample_left(), right) for i in range(samples)] | |
| return real_score - numpy.mean(sampled_scores) | |
| def estimate_right_cause(p, samples=1000): | |
| left = p[0] | |
| real_score = get_score(left, p[1]) | |
| sampled_scores = [get_score(left, sample_right()) for i in range(samples)] | |
| return real_score - numpy.mean(sampled_scores) | |
| example_person = random.choice(population) | |
| left_cause = estimate_left_cause(example_person) | |
| right_cause = estimate_right_cause(example_person) | |
| print '==== Example person:', example_person, '====' | |
| print 'left_cause:', left_cause | |
| print 'right_cause:', right_cause | |
| sample = random.sample(population, 1000) | |
| mean_abs_left_cause = numpy.mean([abs(estimate_left_cause(p)) for p in sample]) | |
| mean_abs_right_cause = numpy.mean([abs(estimate_right_cause(p)) for p in sample]) | |
| print '==== Sample mean ====' | |
| print 'mean_abs_left_cause:', mean_abs_left_cause | |
| print 'mean_abs_right_cause:', mean_abs_right_cause | |
| left = [p[0] for p in population] | |
| right = [p[1] for p in population] | |
| score = [get_score(p[0], p[1]) for p in population] | |
| left_score_rvalue = stats.linregress(left, score)[2] | |
| right_score_rvalue = stats.linregress(right, score)[2] | |
| print '==== Summary stats ====' | |
| print 'R^2_{left, score}: ', left_score_rvalue * left_score_rvalue | |
| print 'R^2_{right, score}: ', right_score_rvalue * right_score_rvalue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment