A question about averaging across files

Hi Francois,

When I conduct paired t-test, it seems that mean of difference is computed using the script ‘bst_avg_files.m’.

It seems like that the mean is not a simple arithmetic mean of difference, as the result is different from what I get by manually computing from input files.

If it is the case, could you explain what kind of mean values is computed by default when I use ‘process_ttest_paired.m’?

I am asking this question as when I conduct cluter-permutation test in brainstorm, the results of paired-ttest and cluster-permutation test does not completely match as fieldtrip computes arithmetic mean whereas brainstorm copmutes some weighted mean…

Thank you very much.


Hi Hyeong,

I guess you are processing averages.
The averaging processes are weighted by the number of trials used to create the averages. This corresponds to the field .nAvg in the file structures.
Process average: sum_trials(nAvg(i)*data(i)) / sum_trials(nAvg(i))

Something similar is used to compute the difference of means:
sum_trials(nAvgA(i) * (dataA-dataB)) / sum_trials(nAvg(i))

This works better if the number of trials for FilesA(i) and FilesB(i) is the same for all the pairs #i.
But then you can have different number of trials between averages #i and #j without any problem.
Does this make sense?

You are right to point this out, I will add a comment to following processes to indicate that when processing averages, the average is a weighted average:

  • Process1 > Average > Average files
  • Process2 > Test > Difference of means
  • Process2 > Test > Parametric t-test
  • Process2 > Test > Parametric t-test (paired)

But except for this, it is great that you’re getting similar outputs with FieldTrip functions. I haven’t done these tests myself yet.
Please note that this is work in progress, we’re still working on those functions. In particular, all the display of the clusters is still missing.
Make sure you update Brainstorm very frequently…


Hi Francois,

Thanks a lot for your reply,

I am trying to modify statfun_depsamplesT to make the result is consistent btw BST and FT.

So, the equation for weighted difference of mean is:

sum{(nAvgA(i)+nAvgB(i)) * (dataA(i) - dataB(i))} / sum{(nAvgA(i) + nAvgB(i))}

(i --> number of subject)

Is this right? Although I try this, the result is still different. Do you also compute weighted variance for computing t-value?



Hi Hyeong,

The function bst_avg_files implements the following algorithm:

This computes the mean and variance based on weighted values (weights=nAvg, values=difference A-B).

What I use for now is the following:

  1. sum{nAvgA(i) * (dataA(i) - dataB(i))} / sum{nAvgA(i)}
    not what you suggested:
  2. sum{(nAvgA(i)+nAvgB(i)) * (dataA(i) - dataB(i))} / sum{(nAvgA(i) + nAvgB(i))}

I’m not sure ones makes more sense than the other. It just depends on the data you are looking at.
But maybe I’m wrong, this was not much discussed before.
What do you think?


Thanks again.

Then, the weighted variance might be like below:

sum{nAvgA(i) * ((dataA(i) - dataB(i)) - weightedMean)^2} / sum{nAvgA(i)}

I just tested it, and now I can get the same t-value between BST and FT. Thanks a lot for your help!

Regarding the formula, I dont know which makes more sense…Thanks anyway!



Hi Heyong,

This is great! Thank you for this very valuable investigations and feedback.