from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import itertools import numpy as np from django import forms from django.forms import widgets as django_widgets import math author = 'Jindi Huang' doc = """ WTA-WTP Gap """ class Constants(BaseConstants): name_in_url = 'wta_wtp_1' players_per_group = None num_rounds = 2 endowment = 6 # The first element is the minimum payment of the lottery; # The second element is the maximum payment of the lottery; # The third element is the step size of the right hand side; choices = { 'wta1': [0, 5, 0.5], 'wta2': [1, 6, 0.5], 'wtp1': [0, 5, 0.5], 'wtp2': [1, 6, 0.5] } list1 = np.arange(1 * 20, - 1, -1) list2 = [] list2.append(0) for x in range(20): list2.append((x+1)*5) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['failed_comprehension'] = False p.participant.vars['sequence'] = ['wta', 'wtp'] p.participant.vars['sequence_wta'] = random.sample(list(Constants.choices)[:2], Constants.num_rounds) p.participant.vars['sequence_wtp'] = random.sample(list(Constants.choices)[2:], Constants.num_rounds) p.participant.vars['pay_wta'] = random.choice(list(Constants.choices)[:2]) p.participant.vars['pay_wtp'] = random.choice(list(Constants.choices)[2:]) p.participant.vars['pay_row'] = random.choice(range(0, 11)) p.participant.vars['pay'] = random.choice(['wta', 'wtp']) p.participant.vars['wta1'] = 9999 p.participant.vars['wta2'] = 9999 p.participant.vars['wtp1'] = 9999 p.participant.vars['wtp2'] = 9999 p.participant.vars['lottery'] = random.choice([0,1]) p.participant.vars['lottery_pay'] = 0 p.participant.vars['pay_left'] = 0 p.participant.vars['rhs'] = 0 p.participant.vars['pay_min'] = 0 p.participant.vars['pay_max'] = 0 p.participant.vars['lucky'] = random.choice(range(0,10)) for p in self.get_players(): p.set_current_lottery() class Group(BaseGroup): pass class Player(BasePlayer): choice_name = models.StringField() switching_point = models.FloatField() confidence = models.FloatField() lottery_min = models.FloatField() lottery_max = models.FloatField() step_size = models.FloatField() choice_num = models.IntegerField() prolific_id = models.StringField() consent = models.BooleanField() lottery_equivalent = models.FloatField(initial=9999) indicator_never_always_switcher = models.IntegerField() question_failed = models.IntegerField() failed_comprehension = models.BooleanField(initial=False) qn_lottery_got_wrong = models.BooleanField(initial=False) qn_list_got_wrong = models.BooleanField(initial=False) qn_confidence_got_wrong = models.BooleanField(initial=False) qn_lottery = models.IntegerField( choices=[ [0, 'It would be possible that I get BOTH the lottery ticket and $7.00.'], [1, 'I would receive EITHER the lottery ticket OR $7.00.'], [0, 'If I choose to keep the lottery ticket, I would receive $10.00 for sure.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_list = models.IntegerField( choices=[ [0, 'This person indicated that the lottery ticket would be worth more to them than $7.00.'], [0, 'This person indicated that the lottery ticket would be worth between $8.00 and $8.50 to them.'], [1, 'This person indicated that the lottery ticket would be worth between $6.50 and $7.00 to them.'], ], widget=widgets.RadioSelect, blank=False, label="" ) qn_confidence = models.FloatField( blank=False, label="" ) def set_current_lottery(self): if self.participant.vars['sequence'][0] == 'wta': setattr(self, 'choice_name', self.participant.vars['sequence_wta'][self.round_number-1]) else: setattr(self, 'choice_name', self.participant.vars['sequence_wtp'][self.round_number-1]) choice = Constants.choices[self.choice_name] self.lottery_min = choice[0] self.lottery_max = choice[1] self.step_size = choice[2] if self.choice_name == "wtp1" or self.choice_name == "wtp2": setattr(self, "choice_num", 1) else: setattr(self, "choice_num", 0) def frange(self, start, stop, step): j = 1 while start <= stop: start = round(start, 3) left = round(Constants.endowment - start, 3) yield (j, start, left, c(start), c(left)) start += step j += 1 def set_switching_point_and_indicator(self): if self.switching_point == 9999: self.indicator_never_always_switcher = 2 elif self.switching_point == 0: self.indicator_never_always_switcher = 3 else: self.indicator_never_always_switcher = 1 self.lottery_equivalent = self.switching_point - self.step_size/2