def test_init_pmf(): """ Expect to create a new PMF with given hypotheses. """ test = {0: 0.25, 0.5: 0.25, 0.75: 0.25, 1: 0.25} hypotheses_a = init_pmf(tuple(test.keys())) assert hypotheses_a == test hypotheses_b = init_pmf(test) assert hypotheses_b == test hypotheses_c = init_pmf() assert hypotheses_c == {}
def test_get_pmf_value(): """ Expect to get a single number representing the best fit for the PMF. """ hypotheses = init_pmf((0, 0.5, 0.75, 1)) assert get_pmf_value(hypotheses) == 0.5625
def test_update_pmf(): """ Expect to update a PMF with given data, require and use a likelihood function to update. """ def likelihood(data, hypothesis): return hypothesis hypotheses = init_pmf((0, 0.5, 0.75, 1)) hypotheses = update_pmf(hypotheses, {}, likelihood) assert hypotheses == {0: 0.0, 0.5: 2 / 9, 0.75: 1 / 3, 1: 4 / 9}
def get_distribution(card_parameters, kind): """ Parse own distribution hypotheses, changing the keys back into numbers. """ key = '{kind}_distribution'.format(kind=kind) if key in card_parameters: distribution = card_parameters[key] distribution = deliver_distribution(distribution) else: init = init_guess if kind == 'guess' else init_slip distribution = { h: 1 - (init - h)**2 for h in [h / precision for h in range(1, precision)] } card_parameters[key] = distribution if kind == 'guess': return init_pmf(distribution) if kind == 'slip': return init_pmf(distribution)
def main(num_learners=1000, num_cards=50): d = create_responses(num_learners, num_cards) responses, learners, cards = d['responses'], d['learners'], d['cards'] precision = 20 my_cards = [{ 'name': card['name'], 'guess_distribution': init_pmf({ h: 1 - (init_guess - h) ** 2 for h in [h / precision for h in range(1, precision)]}), 'slip_distribution': init_pmf({ h: 1 - (init_slip - h) ** 2 for h in [h / precision for h in range(1, precision)]}), 'transit': init_transit, } for card in cards] my_learners = [{ 'name': learner['name'], 'learned': init_learned, } for learner in learners] latest_response_per_learner = {} for i, response in enumerate(responses): # response keys: learner, card, time, score if response['learner'] in latest_response_per_learner: prev_response = latest_response_per_learner[response['learner']] else: prev_response = { 'time': response['time'], 'prev_learned': init_learned, } my_learner = get_learner(response['learner'], my_learners) my_card = get_card(response['card'], my_cards) response['prev_learned'] = my_learner['learned'] c = update(learned=my_learner['learned'], guess_distribution=my_card['guess_distribution'], slip_distribution=my_card['slip_distribution'], score=response['score'], time_delta=response['time'] - prev_response['time']) my_learner['learned'] = c['learned'] my_card['guess_distribution'] = c['guess_distribution'] my_card['slip_distribution'] = c['slip_distribution'] latest_response_per_learner[response['learner']] = response # Compute the error rates. # Error should sqrt(sum( (o - x)^2 for o in list )) guess_error, slip_error, transit_error = 0, 0, 0 for card in cards: my_card = get_card(card['name'], my_cards) my_card['guess'] = get_guess_pmf_value(my_card['guess_distribution']) my_card['slip'] = get_slip_pmf_value(my_card['slip_distribution']) guess_error += (my_card['guess'] - card['guess']) ** 2 slip_error += (my_card['slip'] - card['slip']) ** 2 transit_error += (my_card['transit'] - card['transit']) ** 2 print('guess_error', sqrt(guess_error / len(my_cards))) print('slip_error', sqrt(slip_error / len(my_cards))) print('transit_error', sqrt(transit_error / len(my_cards))) guess_error, slip_error, transit_error = 0, 0, 0 for card in cards: guess_error += (init_guess - card['guess']) ** 2 slip_error += (init_slip - card['slip']) ** 2 transit_error += (init_transit - card['transit']) ** 2 print('CONTROL guess_error', sqrt(guess_error / len(my_cards))) print('CONTROL slip_error', sqrt(slip_error / len(my_cards))) print('CONTROL transit_error', sqrt(transit_error / len(my_cards)))