from otree.api import * c = cu doc = 'This is the end of the study.' class C(BaseConstants): NAME_IN_URL = 'end' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 RESULTS_TABLE_HEADER = ( 'Stock Name', 'Previous Month Scaled Price', 'Forecast Month', 'Actual Next Month Scaled Price', 'Human Analyst Name', "Human Analyst's Employer", 'Human Analyst Prediction', 'Was Human Analyst Accurate? ', 'Algorithmic Analyst Prediction', 'Was Algorithmic Analyst Accurate?' ) ACCURACY_TABLE_HEADER = ('', 'Human Analyst', 'Algorithmic Analyst') NUM_CHOICES = 20 FROM_AI = 'algorithmic' FROM_HUMAN = 'human' AMBIGUITY_PERCENTAGES_SINGLE = ( 0, 0.01, 0.02, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.85, 1) AMBIGUITY_PERCENTAGES_COMPOSITE = ( 0, 0.2, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.93, 0.95, 0.97, 0.98, 0.99, 1) SINGLE_PROB_TEXT = ('more than 0% and strictly less than 40%', 'more than 40% and less than 80%', 'strictly more than 80% and less than 100%') COMPOSITE_PROB_TEXT = ('more than 0% and strictly less than 40% or strictly more than 80% and less than 100%', 'more than 0% and less than 80%', 'more than 40% and less than 100%') class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): pass # FUNCTIONS def creating_session(subsession: Subsession): if subsession.round_number == 1: for p in subsession.get_players(): p.participant.vars['task_selected'] = False def live_task_selection(player: Player, data: dict) -> dict: participant = player.participant participant.vars['task_selected'] = participant.task_to_pay return {player.id_in_group: participant.vars['task_selected']} # PAGES class Start(Page): form_model = 'player' class TaskSelection(Page): @staticmethod def live_method(player: Player, data): return live_task_selection(player, data) @staticmethod def vars_for_template(player: Player): participant = player.participant records = participant.vars['records'] human_sum_accurate = sum([1 if r[-3] == 'TRUE' else 0 for r in records]) ai_sum_accurate = sum([1 if r[-1] == 'TRUE' else 0 for r in records]) human_rate = f'{human_sum_accurate / len(records): .2%}' ai_rate = f'{ai_sum_accurate / len(records): .2%}' if participant.task_to_pay == 'ambiguity': player_to_pay = participant.vars['ambiguity_player_to_pay'] ambiguity_percentages = C.AMBIGUITY_PERCENTAGES_SINGLE if player_to_pay[ 'event_type'] == 's' else C.AMBIGUITY_PERCENTAGES_COMPOSITE algorithm_statement = C.FROM_AI if player_to_pay['analyst_type'] == 'AI' else C.FROM_HUMAN prob_range = C.SINGLE_PROB_TEXT if player_to_pay['event_type'] == 's' else C.COMPOSITE_PROB_TEXT return dict( ambiguity_percentages=[f'{p:.0%}' for p in ambiguity_percentages], algorithm_statement=algorithm_statement, prob_range=prob_range[player_to_pay['event_type_num'] - 1], event=f'event/{player_to_pay["event_type"]}_{player_to_pay["event_type_num"]}.png', pay_option_b=participant.ambiguity_decision_to_pay >= participant.vars['ambiguity_player_to_pay'][ 'switch'], human_rate=human_rate, ai_rate=ai_rate, human_sum_accurate=human_sum_accurate, ai_sum_accurate=ai_sum_accurate ) else: return dict( human_rate=human_rate, ai_rate=ai_rate, human_sum_accurate=human_sum_accurate, ai_sum_accurate=ai_sum_accurate ) @staticmethod def js_vars(player: Player): participant = player.participant if participant.task_to_pay == 'ambiguity': switch = participant.vars['ambiguity_player_to_pay']['switch'] pay_option_b = participant.ambiguity_decision_to_pay >= switch _vars = dict( num_choices=C.NUM_CHOICES, switch=switch, pay_option_b=pay_option_b, ambiguity_draw=list(str(participant.ambiguity_draw)) if pay_option_b else None ) else: _vars = dict( choice=participant.vars['choice_player_to_pay'] ) return dict( task_to_pay=participant.task_to_pay, task_selected=participant.vars['task_selected'], **_vars ) @staticmethod def is_displayed(player: Player): return player.participant.agreement class FinalResults(Page): form_model = 'player' @staticmethod def vars_for_template(player: Player): participant = player.participant records = participant.vars['records'] human_sum_accurate = sum([1 if r[-3] == 'TRUE' else 0 for r in records]) ai_sum_accurate = sum([1 if r[-1] == 'TRUE' else 0 for r in records]) human_rate = f'{human_sum_accurate / len(records): .2%}' ai_rate = f'{ai_sum_accurate / len(records): .2%}' print(participant.task_to_pay) return dict( records=records, human_sum_accurate=human_sum_accurate, ai_sum_accurate=ai_sum_accurate, human_rate=human_rate, ai_rate=ai_rate ) @staticmethod def is_displayed(player: Player): return player.participant.agreement class EndExperiment(Page): form_model = 'player' page_sequence = [Start, FinalResults, TaskSelection, EndExperiment]