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_2' players_per_group = None num_rounds = 2 endowment = 6.0 # 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): 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() lottery_equivalent = models.FloatField(initial=9999) indicator_never_always_switcher = models.IntegerField() def set_current_lottery(self): if self.participant.vars['sequence'][0] == 'wta': setattr(self, 'choice_name', self.participant.vars['sequence_wtp'][self.round_number - 1]) else: setattr(self, 'choice_name', self.participant.vars['sequence_wta'][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 def set_payoff(self): wta = Constants.choices[self.participant.vars['pay_wta']] wta_rhs = list(self.frange(wta[0], wta[1], wta[2]))[self.participant.vars['pay_row']][1] wtp = Constants.choices[self.participant.vars['pay_wtp']] wtp_rhs = list(self.frange(wtp[0], wtp[1], wtp[2]))[self.participant.vars['pay_row']][1] switch_wta = self.participant.vars[self.participant.vars['pay_wta']] switch_wtp = self.participant.vars[self.participant.vars['pay_wtp']] if switch_wta <= wta_rhs: pay_wta = wta_rhs pay_left_wta = 0 else: pay_wta = Constants.choices[self.participant.vars['pay_wta']][ self.participant.vars['lottery']] pay_left_wta = 1 if switch_wtp <= wtp_rhs: pay_wtp = Constants.endowment pay_left_wtp = 1 else: pay_wtp = Constants.endowment - wtp_rhs + \ Constants.choices[self.participant.vars['pay_wtp']][ self.participant.vars['lottery']] pay_left_wtp = 0 if self.participant.vars['pay'] == "wta": lottery_pay = pay_wta pay_left = pay_left_wta rhs = wta_rhs min = wta[0] max = wta[1] else: lottery_pay = pay_wtp pay_left = pay_left_wtp rhs = wtp_rhs min = wtp[0] max = wtp[1] if self.participant.vars['lucky'] == 0: self.participant.vars['lottery_pay'] = lottery_pay else: self.participant.vars['lottery_pay'] = 0 self.participant.vars['pay_left'] = pay_left self.participant.vars['rhs'] = rhs self.participant.vars['pay_min'] = min self.participant.vars['pay_max'] = max