Coverage for tinytroupe / experimentation / randomization.py: 0%
42 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-28 17:48 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-28 17:48 +0000
1import random
2import pandas as pd
3from tinytroupe.agent import TinyPerson
5class ABRandomizer():
7 def __init__(self, real_name_1="control", real_name_2="treatment",
8 blind_name_a="A", blind_name_b="B",
9 passtrough_name=[],
10 random_seed=42):
11 """
12 An utility class to randomize between two options, and de-randomize later.
13 The choices are stored in a dictionary, with the index of the item as the key.
14 The real names are the names of the options as they are in the data, and the blind names
15 are the names of the options as they are presented to the user. Finally, the passtrough names
16 are names that are not randomized, but are always returned as-is.
18 Args:
19 real_name_1 (str): the name of the first option
20 real_name_2 (str): the name of the second option
21 blind_name_a (str): the name of the first option as seen by the user
22 blind_name_b (str): the name of the second option as seen by the user
23 passtrough_name (list): a list of names that should not be randomized and are always
24 returned as-is.
25 random_seed (int): the random seed to use
26 """
28 self.choices = {}
29 self.real_name_1 = real_name_1
30 self.real_name_2 = real_name_2
31 self.blind_name_a = blind_name_a
32 self.blind_name_b = blind_name_b
33 self.passtrough_name = passtrough_name
34 self.random_seed = random_seed
36 def randomize(self, i, a, b):
37 """
38 Randomly switch between a and b, and return the choices.
39 Store whether the a and b were switched or not for item i, to be able to
40 de-randomize later.
42 Args:
43 i (int): index of the item
44 a (str): first choice
45 b (str): second choice
46 """
47 # use the seed
48 if random.Random(self.random_seed).random() < 0.5:
49 self.choices[i] = (0, 1)
50 return a, b
52 else:
53 self.choices[i] = (1, 0)
54 return b, a
56 def derandomize(self, i, a, b):
57 """
58 De-randomize the choices for item i, and return the choices.
60 Args:
61 i (int): index of the item
62 a (str): first choice
63 b (str): second choice
64 """
65 if self.choices[i] == (0, 1):
66 return a, b
67 elif self.choices[i] == (1, 0):
68 return b, a
69 else:
70 raise Exception(f"No randomization found for item {i}")
72 def derandomize_name(self, i, blind_name):
73 """
74 Decode the choice made by the user, and return the choice.
76 Args:
77 i (int): index of the item
78 choice_name (str): the choice made by the user
79 """
81 # was the choice i randomized?
82 if self.choices[i] == (0, 1):
83 # no, so return the choice
84 if blind_name == self.blind_name_a:
85 return self.real_name_1
86 elif blind_name == self.blind_name_b:
87 return self.real_name_2
88 elif blind_name in self.passtrough_name:
89 return blind_name
90 else:
91 raise Exception(f"Choice '{blind_name}' not recognized")
93 elif self.choices[i] == (1, 0):
94 # yes, it was randomized, so return the opposite choice
95 if blind_name == self.blind_name_a:
96 return self.real_name_2
97 elif blind_name == self.blind_name_b:
98 return self.real_name_1
99 elif blind_name in self.passtrough_name:
100 return blind_name
101 else:
102 raise Exception(f"Choice '{blind_name}' not recognized")
103 else:
104 raise Exception(f"No randomization found for item {i}")