from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from math import ceil import random import pandas as pd import itertools import numpy as np from sklearn.utils import shuffle doc = """ This is my rendition of the trust game, hopefully it works """ # This is the function I use to determine if offer was accepted or not def response(p): return True if random.random() < p else False class Constants(BaseConstants): name_in_url = 'my_trust_ToM' players_per_group = None instructions_template = 'my_trust_ToM/Instructions.html' endowment = c(20) payoff_if_rejected = c(0) offer_increment = c(1) offer_choices = currency_range(1, endowment, offer_increment) num_rounds = len(offer_choices) * 2 # num_rounds = 2 offer_choices_count = len(offer_choices) keep_give_amounts = [] for offer in offer_choices: keep_give_amounts.append((offer, endowment - offer)) # assign the amount the trustor sends to the trustee # trustor_transfers = currency_range(0, 20, 1) # # these probabilities are currently generated from my MATLAB script and are meant to control for crossing point # # while changing the relative payout of the different opponents # opponent_dict = dict(opp1 = [0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000], # opp2 = [0.2200, 0.2400, 0.2600, 0.2800, 0.3000, 0.3200, 0.3400, 0.3600, 0.3800, 0.4000, 0.4200, 0.4400, 0.4600, 0.4800, 0.5000, 0.5200, 0.5400, 0.5600, 0.5800, 0.6000, 0.6200], # opp3 = [0.1280, 0.1710, 0.2140, 0.2570, 0.3000, 0.3430, 0.3860, 0.4290, 0.4720, 0.5150, 0.5580, 0.6010, 0.6440, 0.6870, 0.7300, 0.7730, 0.8160, 0.8590, 0.9020, 0.9450, 0.9880]) # opponent = "opp1", "opp2", "opp3" # opponentb1 = list(opponent * (int((num_rounds / len(opponent)) / 2))) # opponentb2 = list(opponent * (int((num_rounds / len(opponent)) / 2))) # random.shuffle((opponentb1)) # random.shuffle((opponentb2)) # opponent_all = opponentb1 + opponentb2 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): gender = models.CharField(choices=["Male", "Female", "Other", "Decline to answer"], widget=widgets.RadioSelect()) age = models.IntegerField() current_opponent = models.CharField() payout = models.IntegerField() soc_or_no = models.CharField() current_offer = models.IntegerField() current_offer_mult_3 = models.IntegerField() amount_returned = models.IntegerField(widget=widgets.RadioSelect()) amount_offered = models.IntegerField() def shuffle_once(self): if self.round_number == 1: tmp_all_offers = Constants.offer_choices * 2 random.shuffle(tmp_all_offers) self.participant.vars['all_offers'] = tmp_all_offers def set_responder_identity(self): # self.current_offer = (int(Constants.offer_choices[self.round_number - 1])) # self.current_offer_mult_3 = (int(Constants.offer_choices[self.round_number - 1]) * 3) self.current_offer = (int(self.participant.vars['all_offers'][self.round_number - 1])) self.current_offer_mult_3 = (int(self.participant.vars['all_offers'][self.round_number - 1]) * 3) final_payout = models.CurrencyField() trial_to_pay = models.IntegerField() amount_earned_on_priors_nonsocial = models.IntegerField() amount_earned_total = models.IntegerField() def set_payoffs(self): self.payoff = ceil(3 * int(self.amount_offered) * Constants.opponent_dict[self.current_opponent][int(self.amount_offered)]) # if self.offer_accepted: # self.payoff = int(Constants.endowment) - int(self.amount_offered) # # self.payout = Constants.endowment - self.amount_offered # else: # self.payoff = int(Constants.payoff_if_rejected) # # self.payout = Constants.payoff_if_rejected def round_to_pay(self): self.round_list = range(1, self.round_number + 1) self.payout_chosen_trial = shuffle(self.round_list) # self.participant.vars['payout_chosen_trial'] = self.payout_chosen_trial[0] self.participant.vars['payout_chosen_trial'] = random.choice(range(1, self.round_number + 1)) self.trial_to_pay = self.participant.vars['payout_chosen_trial'] self.participant.vars['payout_UG'] = self.group.in_round(self.trial_to_pay).payout self.amount_earned_on_priors_nonsocial = self.participant.vars['payout_UG'] # self.amount_earned_total = self.participant.vars['payout_UG'] + self.participant.vars[ # 'payout_social_priors'] def calculate_final_payout(self): self.final_payout = 0.125 * (self.participant.vars['payout_UG'] + self.participant.vars['risk_payout'] + self.participant.vars['payout_dictator'] + self.participant.vars['payout_social_priors']) # this is commented out because I'm not sure if I should pay them for both prior estimation sessions or just 1 # + self.participant.vars['payout_nonsocial_priors'] test_question_1 = models.IntegerField( choices=(5, 10, 35, 40, 45), widget=widgets.RadioSelectHorizontal() ) test_question_2 = models.IntegerField( choices=(5, 10, 35, 40, 45), widget=widgets.RadioSelectHorizontal() ) test_question_3 = models.IntegerField( choices=(5, 10, 35, 40, 45), widget=widgets.RadioSelectHorizontal() ) test_question_4 = models.StringField(choices = ('True', 'False'), widget=widgets.RadioSelectHorizontal() ) def amount_returned_choices(self): # choices = [0, self.group.current_offer] choices = [ii for ii in range(0, self.current_offer_mult_3+1)] return choices