Calculating the probability of incorrect events within independent groups

79 views Asked by At

I have the following structure:

T = struct('Time',{20, 40, 50, 80, 120, 150, 190, 210, 250, 260, 270, 320, 350, 380, 385, 390, 395},...
           'Trial',{'correct','incorrect','incorrect','correct','correct','correct','incorrect','incorrect','correct','correct','correct','incorrect','incorrect','correct','correct','incorrect','incorrect'});

I would like to perform the following two tasks:

  1. I want to get the probability of having an 'incorrect' per each 100 ms time window (interval).

    For example, for the first time window, the first 100 ms, there is 4 trials and 2 are 'incorrect' out of 4 so it would be 2/4 = 0.5

  2. I want to plot a bar graph of the probabilities for each 100 ms time window. The x axis would be time and each bar's width would be 100 ms and its height is the probability for that specific window.

I really appreciate any help.

1

There are 1 answers

4
rayryeng On BEST ANSWER

This goes against my policy in answering questions without any effort made by the question poser, but this seems like an interesting question, so I'll make an exception.


First, split up each of the Time and Trial fields so that they're in separate arrays. For the Trial fields, I'm going to convert them into labels 1 and 2 to denote correct and incorrect for ease of implementation:

time = [T.Time].';
trial = {T.Trial}.';
[~,~,trial_ID] = unique(trial);

Next, what you can do is take each entry in the time array and divide by 100 while taking the floor. Values that belong to the same ID mean that they belong to a group of 100 ms. Note that we also need to add 1 for the next step... you'll see why:

groups = floor(time/100) + 1;

Now, here's probably one of the most beautiful functions you can ever use in MATLAB: accumarray. accumarray groups portions of an array based on an ID and you apply a function to all of the values per group. In our case, we want to group all of the correct and incorrect IDs based on the groups array, then from there we determine the total fraction of values that are incorrect per group.

Specifically, what we're going to do is for each group of values specified in groups, we will take a look at the correct and incorrect numeric labels and determine how many were incorrect by summing over how many were equal to 2 for each group, then dividing by how many there were per group. The groups need to start at index 1, which is why we had to add 1 to groups. Without it, the first group would actually start at 0, and MATLAB starts indexing at 1, hence the offset:

per = accumarray(groups, trial_ID, [], @(x) sum(x == 2) / numel(x));

per contains the fraction that were correct per group, and we get:

>> per

per =

    0.5000
    0.3333
    0.2500
    0.6667

Very nice! Doing a quick hand calculation will demonstrate that you get the correct results.


Now the last part is to plot the probabilities on a bar graph. That's very simply:

bar(100*(1:numel(per)), per);
xlabel('Time (ms)');
ylabel('Probability');

I create a vector that starts from 100 and goes up in multiples of 100 up until as many groups as we have. In our case, we have 4 as the time goes up to 395 ms.

As such, we get:

enter image description here