from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import numpy import random import csv import jellyfish import datetime from datetime import date from datetime import timedelta import random import csv import numpy as np author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'Valuations' players_per_group = None num_choices = 10 lottery_hi = 150.00 lottery_lo = 0.00 probability = 10 sure_payoff = 5.00 step_size = 2.50 endowment = 0.00 enforce_consistency = True num_rounds = 1 class Subsession(BaseSubsession): def creating_session(self): # randomize to treatments for player in self.get_players(): player.rewardchosen = random.choice( ['Sure payment of $15 to you on', '10% chance of payment of $150 to you on ', '10 trees ordered for planting by the Arbor Day Foundation on your behalf on ', '10% chance of 100 trees ordered for planting by the Arbor Day Foundation on your behalf on ' ] ) player.taskdistchosen = random.choice(['tasks1.jpg', 'tasks2.jpg']) if self.round_number == 1: n = Constants.num_choices for p in self.get_players(): # create list of lottery indices # ---------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n + 1)] # create list corresponding to form_field variables including all choices # ---------------------------------------------------------------------------------------------------- form_fields = ['val1choice_' + str(k) for k in indices] form_fields2 = ['val2choice_' + str(k) for k in indices] form_fields3 = ['val3choice_' + str(k) for k in indices] form_fields4 = ['val4choice_' + str(k) for k in indices] # create list of probabilities # ---------------------------------------------------------------------------------------------------- probabilities = [Constants.probability for k in indices] # create list of high lottery payoffs # ---------------------------------------------------------------------------------------------------- lottery_hi = [c(Constants.lottery_hi) for k in indices] # create list of low lottery payoffs # ---------------------------------------------------------------------------------------------------- lottery_lo = [c(Constants.lottery_lo) for k in indices] # create list of sure payoffs # ---------------------------------------------------------------------------------------------------- sure_payoffs = [c(Constants.sure_payoff + (k-1) * Constants.step_size) for k in indices] # create list of choices # ---------------------------------------------------------------------------------------------------- p.participant.vars['val1_choices'] = list( zip( indices, form_fields, probabilities, lottery_hi, lottery_lo, sure_payoffs ) ) p.participant.vars['val2_choices'] = list( zip( indices, form_fields2, probabilities, lottery_hi, lottery_lo, sure_payoffs ) ) p.participant.vars['val3_choices'] = list( zip( indices, form_fields3, probabilities, lottery_hi, lottery_lo, sure_payoffs ) ) p.participant.vars['val4_choices'] = list( zip( indices, form_fields4, probabilities, lottery_hi, lottery_lo, sure_payoffs ) ) # initiate list for choices made # ---------------------------------------------------------------------------------------------------- p.participant.vars['val1_choices_made'] = [None for j in range(1, n + 1)] p.participant.vars['val2_choices_made'] = [None for j in range(1, n + 1)] p.participant.vars['val3_choices_made'] = [None for j in range(1, n + 1)] p.participant.vars['val4_choices_made'] = [None for j in range(1, n + 1)] class Group(BaseGroup): pass class Player(BasePlayer): ### Expectation and Random Reward exp_date2 = models.IntegerField(min=0, max=100, label="(Chance Out of 100)") exp_failure = models.IntegerField(min=0, max=100, label="(Chance Out of 100)") taskdistchosen = models.StringField() rewardchosen = models.StringField() #### First Valuation Variables for j in range(1, Constants.num_choices + 1): locals()['val1choice_' + str(j)] = models.StringField() del j val1inconsistent = models.IntegerField() val1switching_row = models.IntegerField() # determine consistency # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val1consistency(self): n = Constants.num_choices # replace A's by 1's and B's by 0's self.participant.vars['val1_choices_made'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['val1_choices_made'] ] # check for multiple switching behavior for j in range(1, n): choices = self.participant.vars['val1_choices_made'] self.val1inconsistent = 1 if choices[j] > choices[j - 1] else 0 if self.val1inconsistent == 1: break # determine switching row # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val1switching_row(self): # set switching point to row number of first 'B' choice if self.val1inconsistent == 0: self.val1switching_row = sum(self.participant.vars['val1_choices_made']) + 1 #### Second Valuation Variables for j in range(1, Constants.num_choices + 1): locals()['val2choice_' + str(j)] = models.StringField() del j val2inconsistent = models.IntegerField() val2switching_row = models.IntegerField() # determine consistency # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val2consistency(self): n = Constants.num_choices # replace A's by 1's and B's by 0's self.participant.vars['val2_choices_made'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['val2_choices_made'] ] # check for multiple switching behavior for j in range(1, n): choices = self.participant.vars['val2_choices_made'] self.val2inconsistent = 1 if choices[j] > choices[j - 1] else 0 if self.val2inconsistent == 1: break # determine switching row # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val2switching_row(self): # set switching point to row number of first 'B' choice if self.val2inconsistent == 0: self.val2switching_row = sum(self.participant.vars['val2_choices_made']) + 1 #### Third Valuation Variables for j in range(1, Constants.num_choices + 1): locals()['val3choice_' + str(j)] = models.StringField() del j val3inconsistent = models.IntegerField() val3switching_row = models.IntegerField() # determine consistency # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val3consistency(self): n = Constants.num_choices # replace A's by 1's and B's by 0's self.participant.vars['val3_choices_made'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['val3_choices_made'] ] # check for multiple switching behavior for j in range(1, n): choices = self.participant.vars['val3_choices_made'] self.val3inconsistent = 1 if choices[j] > choices[j - 1] else 0 if self.val3inconsistent == 1: break # determine switching row # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val3switching_row(self): # set switching point to row number of first 'B' choice if self.val3inconsistent == 0: self.val3switching_row = sum(self.participant.vars['val3_choices_made']) + 1 #### Fourth Valuation Variables for j in range(1, Constants.num_choices + 1): locals()['val4choice_' + str(j)] = models.StringField() del j val4inconsistent = models.IntegerField() val4switching_row = models.IntegerField() # determine consistency # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val4consistency(self): n = Constants.num_choices # replace A's by 1's and B's by 0's self.participant.vars['val4_choices_made'] = [ 1 if j == 'A' else 0 for j in self.participant.vars['val4_choices_made'] ] # check for multiple switching behavior for j in range(1, n): choices = self.participant.vars['val4_choices_made'] self.val4inconsistent = 1 if choices[j] > choices[j - 1] else 0 if self.val4inconsistent == 1: break # determine switching row # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_val4switching_row(self): # set switching point to row number of first 'B' choice if self.val4inconsistent == 0: self.val4switching_row = sum(self.participant.vars['val4_choices_made']) + 1