Skip to content

autora.experiment_runner.synthetic.psychology.luce_choice_ratio

luce_choice_ratio(name='Luce-Choice-Ratio', resolution=8, maximum_similarity=10, focus=0.8)

Luce-Choice-Ratio

Parameters:

Name Type Description Default
name

name of the experiment

'Luce-Choice-Ratio'
added_noise

standard deviation of normally distributed noise added to y-values

required
resolution

number of allowed values for stimulus DVs

8
maximum_similarity

upperbound for DVs

10
focus

parameter measuring participant focus

0.8
random_state

integer used to seed the random number generator

required
Shepard-Luce Choice Rule according to
  • Equation (4) in Logan, G. D., & Gordon, R. D. (2001).
  • and in Executive control of visual attention in dual-task situations. Psychological review, 108(2), 393.
  • Equation (5) in Luce, R. D. (1963). Detection and recognition.

Examples:

We can instantiate a Shepard-Cue Choice Experiment. We use a seed to get replicable results:

>>> l_s_experiment = luce_choice_ratio()

We can look at the name of the experiment:

>>> l_s_experiment.name
'Luce-Choice-Ratio'

To call the ground truth, we can use an attribute of the experiment:

>>> l_s_experiment.ground_truth(np.array([[1,2,3,4]]))
   similarity_category_A1  ...  choose_A1
0                       1  ...   0.210526

[1 rows x 5 columns]

We can also run an experiment:

>>> l_s_experiment.run(np.array([[1,2,3,4]]), random_state=42)
   similarity_category_A1  ...  choose_A1
0                       1  ...   0.211328

[1 rows x 5 columns]

To plot the experiment use:

>>> l_s_experiment.plotter()
>>> plt.show()
Source code in autora/experiment_runner/synthetic/psychology/luce_choice_ratio.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def luce_choice_ratio(
    name="Luce-Choice-Ratio",
    resolution=8,
    maximum_similarity=10,
    focus=0.8,
):
    """
    Luce-Choice-Ratio

    Args:
        name: name of the experiment
        added_noise: standard deviation of normally distributed noise added to y-values
        resolution: number of allowed values for stimulus DVs
        maximum_similarity: upperbound for DVs
        focus: parameter measuring participant focus
        random_state: integer used to seed the random number generator

    Shepard-Luce Choice Rule according to:
        - Equation (4) in Logan, G. D., & Gordon, R. D. (2001).
        - and in Executive control of visual attention in dual-task situations.
            Psychological review, 108(2), 393.
        - Equation (5) in Luce, R. D. (1963). Detection and recognition.

    Examples:
        We can instantiate a Shepard-Cue Choice Experiment. We use a seed to get replicable results:
        >>> l_s_experiment = luce_choice_ratio()

        We can look at the name of the experiment:
        >>> l_s_experiment.name
        'Luce-Choice-Ratio'

        To call the ground truth, we can use an attribute of the experiment:
        >>> l_s_experiment.ground_truth(np.array([[1,2,3,4]]))
           similarity_category_A1  ...  choose_A1
        0                       1  ...   0.210526
        <BLANKLINE>
        [1 rows x 5 columns]

        We can also run an experiment:
        >>> l_s_experiment.run(np.array([[1,2,3,4]]), random_state=42)
           similarity_category_A1  ...  choose_A1
        0                       1  ...   0.211328
        <BLANKLINE>
        [1 rows x 5 columns]

        To plot the experiment use:
        >>> l_s_experiment.plotter()
        >>> plt.show()  # doctest: +SKIP

    """

    minimum_similarity = 1 / maximum_similarity

    params = dict(
        name=name,
        maximum_similarity=maximum_similarity,
        minimum_similarity=minimum_similarity,
        resolution=resolution,
        focus=focus,
    )

    similarity_category_A1 = IV(
        name="similarity_category_A1",
        allowed_values=np.linspace(minimum_similarity, maximum_similarity, resolution),
        value_range=(minimum_similarity, maximum_similarity),
        units="similarity",
        variable_label="Similarity with Category A1",
        type=ValueType.REAL,
    )

    similarity_category_A2 = IV(
        name="similarity_category_A2",
        allowed_values=np.linspace(minimum_similarity, maximum_similarity, resolution),
        value_range=(minimum_similarity, maximum_similarity),
        units="similarity",
        variable_label="Similarity with Category A2",
        type=ValueType.REAL,
    )

    similarity_category_B1 = IV(
        name="similarity_category_B1",
        allowed_values=np.linspace(minimum_similarity, maximum_similarity, resolution),
        value_range=(minimum_similarity, maximum_similarity),
        units="similarity",
        variable_label="Similarity with Category B1",
        type=ValueType.REAL,
    )

    similarity_category_B2 = IV(
        name="similarity_category_B2",
        allowed_values=np.linspace(minimum_similarity, maximum_similarity, resolution),
        value_range=(minimum_similarity, maximum_similarity),
        units="similarity",
        variable_label="Similarity with Category B2",
        type=ValueType.REAL,
    )

    choose_A1 = DV(
        name="choose_A1",
        value_range=(0, 1),
        units="probability",
        variable_label="Probability of Choosing A1",
        type=ValueType.PROBABILITY,
    )

    variables = VariableCollection(
        independent_variables=[
            similarity_category_A1,
            similarity_category_A2,
            similarity_category_B1,
            similarity_category_B2,
        ],
        dependent_variables=[choose_A1],
    )

    def run(
        conditions: Union[pd.DataFrame, np.ndarray, np.recarray],
        focus_: float = focus,
        added_noise=0.01,
        random_state: Optional[int] = None,
    ):
        rng = np.random.default_rng(random_state)
        X = np.array(conditions)
        Y = np.zeros((X.shape[0], 1))
        for idx, x in enumerate(X):
            similarity_A1 = x[0]
            similarity_A2 = x[1]
            similarity_B1 = x[2]
            similarity_B2 = x[3]

            y = (similarity_A1 * focus + rng.normal(0, added_noise)) / (
                similarity_A1 * focus
                + similarity_A2 * focus
                + similarity_B1 * (1 - focus_)
                + similarity_B2 * (1 - focus_)
            )
            # probability can't be negative or larger than 1 (the noise can make it so)
            if y <= 0:
                y = 0.0001
            elif y >= 1:
                y = 0.9999
            Y[idx] = y
        experiment_data = pd.DataFrame(conditions)
        experiment_data.columns = [v.name for v in variables.independent_variables]
        experiment_data[choose_A1.name] = Y
        return experiment_data

    ground_truth = partial(run, added_noise=0.0)

    def domain():
        similarity_A1 = variables.independent_variables[0].allowed_values
        similarity_A2 = variables.independent_variables[1].allowed_values
        similarity_B1 = variables.independent_variables[2].allowed_values
        similarity_B2 = variables.independent_variables[3].allowed_values

        X = np.array(
            np.meshgrid(
                similarity_A1,
                similarity_A2,
                similarity_B1,
                similarity_B2,
            )
        ).T.reshape(-1, 4)

        # remove all conditions from X where the focus is 0 and the similarity of A1 is 0
        # or the similarity of A2 is 0
        X = X[~((X[:, 0] == 0) & (X[:, 1] == 0) & (X[:, 2] == 0) & (X[:, 3] == 0))]
        return X

    def plotter(
        model=None,
    ):
        import matplotlib.colors as mcolors
        import matplotlib.pyplot as plt

        similarity_A1 = np.linspace(
            variables.independent_variables[0].value_range[0],
            variables.independent_variables[0].value_range[1],
            100,
        )

        similarity_A2 = 0.5  # 1 - similarity_A1

        similarity_B1_list = [0.5, 0.75, 1]
        similarity_B2 = 0

        colors = mcolors.TABLEAU_COLORS
        col_keys = list(colors.keys())
        for idx, similarity_B1 in enumerate(similarity_B1_list):
            # similarity_B2 = 1 - similarity_B1
            X = np.zeros((len(similarity_A1), 4))

            X[:, 0] = similarity_A1
            X[:, 1] = similarity_A2
            X[:, 2] = similarity_B1
            X[:, 3] = similarity_B2

            y = ground_truth(X)
            plt.plot(
                similarity_A1.reshape((len(similarity_A1), 1)),
                y,
                label=f"Similarity to B1 = {similarity_B1} (Original)",
                c=colors[col_keys[idx]],
            )

            if model is not None:
                y = model.predict(X)
                plt.plot(
                    similarity_A1,
                    y,
                    label=f"Similarity to B1 = {similarity_B1} (Recovered)",
                    c=colors[col_keys[idx]],
                    linestyle="--",
                )

        x_limit = [np.min(similarity_A1), np.max(similarity_A1)]
        y_limit = [0, 1]
        x_label = "Similarity to Category A1"
        y_label = "Probability of Selecting Category A1"

        plt.xlim(x_limit)
        plt.ylim(y_limit)
        plt.xlabel(x_label, fontsize="large")
        plt.ylabel(y_label, fontsize="large")
        plt.legend(loc=4, fontsize="medium")
        plt.title("Shepard-Luce Choice Ratio", fontsize="x-large")

    collection = SyntheticExperimentCollection(
        name=name,
        description=luce_choice_ratio.__doc__,
        variables=variables,
        run=run,
        ground_truth=ground_truth,
        domain=domain,
        plotter=plotter,
        params=params,
        factory_function=luce_choice_ratio,
    )
    return collection