if __name__ == "__main__":
    mw = MicroWriter(write_key=WRITE_KEY)
    LONG = '~' + str(mw.DELAYS[-1])
    SHORT = '~' + str(mw.DELAYS[0])

    # Give up if things are going badly ... do it nicely
    for _ in range(5):
        mw.cancel_worst_active(stop_loss=STOP_LOSS, num=1)
        time.sleep(1)

    for name in mw.get_streams():
        if 'z2~' in name and (LONG in name or SHORT in name):
            num = mw.num_predictions
            z11 = '~'.join(['z1', name.split('~')[1], name.split('~')[3]])
            z12 = '~'.join(['z1', name.split('~')[2], name.split('~')[3]])
            lagged1 = mw.get_lagged_values(z11)
            lagged2 = mw.get_lagged_values(z12)
            if len(lagged1) > 10:
                z_samples = list()
                for z1, z2 in zip(lagged1, lagged2):
                    try:
                        p1 = mw.normcdf(z1)
                        p2 = mw.normcdf(z2)
                        z = mw.to_zcurve(prctls=[p1, p2])
                        z_samples.append(z)
                    except:
                        pass

                samples = exponential_bootstrap(z_samples,
                                                decay=0.001,
                                                num=mw.num_predictions,
from microprediction import MicroWriter, new_key
import random
from pprint import pprint
from collections import Counter

# Crush the die.json contest and some others using the new discrete_pdf_lagged functionality

NAMES = ['die.json']

if __name__ == "__main__":
    write_key = new_key(difficulty=8)
    print(write_key)
    mw = MicroWriter(write_key=write_key, base_url='https://devapi.microprediction.org')
    for name in NAMES:
        for delay in mw.DELAYS:
            lagged_values = mw.get_lagged_values(name=name)
            pdf = mw.get_discrete_pdf_lagged(name=name, delay=mw.DELAYS[1],
                                             lagged_values=lagged_values)  # "market measure"
            empirical_pdf = Counter(lagged_values)
            market_pdf = dict(zip(pdf['x'], pdf['y']))
            weights = [empirical_pdf[x] / (0.01 + market_pdf[x]) for x in pdf['x']]
            samples = random.choices(population=pdf['x'], weights=weights, k=mw.num_predictions)
            res = mw.submit(name=name, delay=delay, values=samples, verbose=True)
            pprint(res)
    print('Punch ' + write_key + ' into dashboard at https://www.microprediction.org/dashboard.html')