<jupyter_start><jupyter_text>Notebook to generate figure 2 and 3 from the paper<jupyter_code>import time
import random
import numpy as np
from BradleyTerry import *

def get_features(n=5):
    feats = []
    for i in range(n):
        feats.append('f'+str(i+1))
    return feats

def gen_strengths(features):
    return {feature: random.random() for feature in features}

def gen_comp(f1, f2, s1, s2):
    if random.random() < abs(s1 - s2):
        f1, f2 = f2, f1
    return '{} > {}\n'.format(f1, f2)

def gen_min_comps(filename, features):
    with open(filename, 'w') as f:
        for feat1 in features:
            for feat2 in features:
                if feat1 != feat2:
                    f.write('{} > {}\n'.format(feat1, feat2))

def add_comps(filename, features, n=None):
    if n is None:
        n = len(features) * 50
    feature_names = list(features.keys())
    with open(filename, 'w') as f:
        for _ in range(n):
            f1, f2 = tuple(random.choices(feature_names, k=2))
            f.write(gen_comp(f1, f2, features[f1], features[f2]))

def populate(nfeats, fname, ncomps=100, verbose=False):
    fts = get_features(nfeats)
    st = gen_strengths(fts)
    #gen_min_comps('./data/{}-survey.txt'.format(dataset), st)
    if verbose:
        print(st)
    add_comps('./data/{}-survey.txt'.format(fname), st, n=len(st)*ncomps)
    return fts, st

def extract(features, fname, verbose=False):
    data = FeatureSet(features)
    data.fit('./data/{}-survey.txt'.format(fname))
    if verbose:
        print(data)
    result = []
    for feat in data.features:
        result.append(feat.strength)
    return result

def main(params = 5, factor=100):
    random.seed(0)
    fts, st = populate(params, 'titanic', ncomps=factor)
    gst = np.array([st[ft] for ft in fts])
    n_gst = gst*params / np.sum(gst)
    ist = np.array(extract(fts, 'titanic'))
    n_ist = ist*params / np.sum(ist)
    err = (n_gst - n_ist)
    return((np.mean(err ** 2)))


if __name__ == '__main__':
    main(factor=500)


p5 = []
p5_times = []
for f in [1,2,3,5,10,20,50,100,200,500]:
    start = time.time()
    p5.append(main(factor=f))
    p5_times.append(time.time()-start)
    
p5

p10 = []
p10_times = []
for f in [1,2,3,5,10,20,50,100,200,500]:
    start = time.time()
    p10.append(main(params=10,factor=f))
    p10_times.append(time.time()-start)
    
p10

p20 = []
p20_times = []
for f in [1,2,3,5,10,20,50,100,200,500]:
    start = time.time()
    p20.append(main(params=20,factor=f))
    p20_times.append(time.time()-start)
    
p20

p15 = []
p15_times = []
for f in [1,2,3,5,10,20,50,100,200,500]:
    start = time.time()
    p15.append(main(params=15,factor=f))
    p15_times.append(time.time()-start)
    
p15
import matplotlib.pyplot as plt
plt.plot([i for i in range(len(p5))], p5, marker='x', label='5 feature models')
plt.plot([i for i in range(len(p5))], p10, marker='o', label='10 feature models')
plt.plot([i for i in range(len(p5))], p15, marker='+', label='15 feature models')
plt.plot([i for i in range(len(p5))], p20, marker='.', label='20 feature models')
plt.legend(fontsize=12)
plt.xticks([i for i in range(len(p5))], [1,2,3,5,10,20,50,100,200,500])
plt.xlabel('Ratio of Number of Comparisons to Features', fontsize=14)
plt.ylabel('MSE of Inferred Parameters', fontsize=14)
plt.yscale('log')
# plt.title('Mean Squared Error of Estimated Bradley-Terry Parameters with increasing ')
plt.savefig('fig1.png', dpi=200)
plt.plot([i for i in range(len(p5))], p5_times, marker='x', label='5 feature models')
plt.plot([i for i in range(len(p5))], p10_times, marker='o', label='10 feature models')
plt.plot([i for i in range(len(p5))], p15_times, marker='+', label='15 feature models')
plt.plot([i for i in range(len(p5))], p20_times, marker='.', label='20 feature models')
plt.legend(fontsize=12)
plt.xticks([i for i in range(len(p5))], [1,2,3,5,10,20,50,100,200,500])
plt.xlabel('Ratio of Number of Comparisons to Features', fontsize=14)
plt.ylabel('Inference Running Time (s)', fontsize=14)
# plt.title('Mean Squared Error of Estimated Bradley-Terry Parameters with increasing ')
plt.savefig('fig2.png', dpi=200)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17118847539015605, 'fraction_numerical': 0.061464585834333736, 'mean_word_length': 3.849825378346915, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '306993', 'license': 'MIT', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1596, 'n_words': 369, 'path': 'Plots1and2.ipynb', 'repo_name': 'kaivalyar/RealWorldRecourse', 'size': '4165'}