# [FieldTrip] Different way of calculating the covariance for LCM

Yuval Harpaz yuvharpaz at gmail.com
Thu Mar 24 16:58:44 CET 2011

```Thanks Michael for the detailed explanation. sorry for misunderstanding you
before, making you work hard to explain yourself.
thanks again, yuval

On 24 March 2011 16:24, Michael Wibral <michael.wibral at web.de> wrote:

> Dear Yuval,
>
>  you are completely right with what you wrote but you might have
> misunderstood my post (as I say exactly the same):
>
> What I wrote is exactly this: concatenation of trials followed by cov
> computation or trial wise cov computation followed by averaging is the same,
> BUT different from averaging and then computing cov (just as you point out).
>
> Also note that i did NOT write that cov computation is linear BUT bi-linear
> (see also the wikipedia article on covariance), hence you have to honor
> distributivity in both arguments (and since you average both arguments,
> that's were the problem comes in!).
>
> Let's denote cov computation by C<.|.>, then
>
> (1a)    C<a+b|c>=(C<a|c> + C<b|c> )      (this is  linearity in first
> argument)
> (1b)    C<a+b|c+d>=(C<a|c> + C<b|c> + C<a|d> + C<b|d> )     (this is
> bi-linearity when considering both arguments)
>
> concatenating signal vectors a, b and c,d into …[a b] and [c d] means:
> (1c) C<[a b]|[c d]> = (C<a|c> + C<b|d> )
>
> For numerical factors 1/m, 1/n as they arise in averaging the following
> holds:
> C<1/m*a| 1/n*b> =1/m*1/m*C<a|b>
>
>
> (i.e. concatenation or single trial cov computation and later averaging are
> the same. This is because the dot product that is used to compute the
> covariance for two signals vectors reads like this: [a b c d] * [e f g
> h]=ae+bf+cg+dh=[a b] * [e f] + [c d]*[gh], so whether you multiply the
> vectors in pieces and later add results or rather multiply them as a whole
> doesn't matter. What matters is that a, b, ... stay exactly the same in both
> cases)
>
>
> Now to trial averging and cov computation (see 3) versus cov computation
> and averaging afterwards (see 4):
>
> for two trials a,b and two channels c1,c2:
>
> (4)
> C<AVG(c1a|c1b)|AVG(c2a,c2b)>=1/2*1/2*(C<c1a|c2a>+C<c1b|c2b>+C<c1a|c2b>+C<c1b|c2a>)
>    (here things get multiplied across trials, the two factors 1/2 come from
> each separate averaging in the first and second argument)
> (5)    AVG(C<c1a|c2a>,C<c1b|c2b>)  =1/2*(C<c1a|c2a>+C<c1b|c2b>)  (here
> things do only get multiplied within trials, we have only the outer
> averaging, hence only a factor of 1/2)
>
> Michael
>
>
>
>  ------------------------------
> *Von:* "Yuval Harpaz" <yuvharpaz at gmail.com>
> *Gesendet:* Mar 24, 2011 10:41:02 AM
>
> *An:* "Email discussion list for the FieldTrip project" <
> fieldtrip at donders.ru.nl>
> *Betreff:* Re: [FieldTrip] Different way of calculating the covariance for
> LCM
>
> Dear Michael, Jean-Michel and FieldTrip users.
> To my experience and understanding it does matter if you first asses the
> covariance and then average or the other way around.
> First, as far as my understanding goes, if we have some brain activity such
> as alpha, which is not phase locked to the event then in the covariance will
> not 'catch' too much covariability there after averaging, compared to
> unaveraged data where it should. if the process of calculating covariance
> and averaging id linear then I must be wrong here.
> anyway, beamforming on averaged and unaveraged base covariance matrices
> give different results in fieldtrip. this is because the covariance matrices
> are not the same with or without averaging.
>
> I ran the following test to check it all. I took the same structure as in
> the example above, and I tested the covariance matrix after
> 1. running it with cfg.keeptrials='yes'; then I averaged  (across trials)
> the 3d matrix to get 2d matrix.
> 2. running it without keeping trials; it gives a 2d covariance matrix.
> 3. averaging the data with this command:
> mD1st=ft_timelockanalysis([],D1st). no filters baseline correction or
> anything.
> then I ran timelockanalysis again to compute the covariance (no keeptrials
> of course).
>
> the result is that 1. and 2. gave the same result but not 3.
>
> It seems to me that the process is not linear, at least in fieldtrip, and
> that one doesn't need to specify keeptrials for unaveraged data because it
> calculates the covariance for every trial and then averages the cov
> matrices.
>
> If I am wrong here I will appreciate further help with this issue.
>
> thanks
> yuval
>
>
> On 23 March 2011 21:27, Michael Wibral <michael.wibral at web.de> wrote:
>
>>
>> Dear Fieldtrip users interested in covariance computation,
>> dear Yuval,
>>
>> I would like to my opinion on covariance computation up for discussion
>> here.
>>
>> Covariance is a (bi-)linear measure (like an inner (scalar) product) and
>> should in principle be commutative mathematically with other linear
>> procedures as long as you respect the distributative law from elementary
>> math. Hence,
>>
>> (1) averaging single trial covariances or computing the covariance of
>> concatenated trials should give the same result if a am not mistaken. In
>> these two approaches samples from your two time course are in the end
>> multiplied with each other one by one. Whichever route of these two you take
>> does not matter at all.
>> This holds as long as you treat baseline correction and filtering (!)
>> exactly the same way in both cases - otherwise you'll additionally get the
>> baseline covariance over trials as a term in the covariance matrix or
>> filtering related differences. This latter point maybe important if you post
>> hoc decide on a band width to confine your beamformer analysis to and do not
>> go back to the raw data for the band pass filtering.
>>
>> (2) In contrast, when you compute covariance from a precomputed trial
>> average then every sample at time t in one trial on channel A will in the
>> course of the calculations be implicitely multiplied with samples at time t
>> at EVERY other trial on channel B. So this covariances focuses on covariance
>> structures that are consistent irrespective of the correct pairings of
>> trials.
>>
>> It's a little bit like first computing the power spectrum and averaging to
>> get total activity (induced+evoked) - this would correspond to procedure (1)
>> , or first averaging and then computing spectral power in order to get the
>> power of evoked activity alone - this would correspond to procedure (2).
>>
>> This said, when you're interested in the sources of all task related
>> oscillatory activity - not only the activity phase locked to a stimulus you
>> should pick option (1).
>>
>>
>> Please let me know If I overlooked something important.
>> Michael
>>
>>
>>
>>
>> ---------------------
>> Gesendet: Mar 23, 2011 9:42:53 AM
>> An: "Email discussion list for the FieldTrip project" <
>> fieldtrip at donders.ru.nl>
>> Betreff: Re: [FieldTrip] Different way of calculating the covariance for
>> LCM
>>
>>
>> Thanks Yuval,
>>
>>
>>  Le 22/03/11 10:55, Yuval Harpaz a écrit :
>>
>> So just run the commands on an unaveraged dataset.
>>
>>
>>
>> Yes but that would be correct if there was only one trial in the     data
>> set (see the message from Luisa).
>>
>>
>>
>> Another option to consider is the one used by Dr.         Robinson when
>> performing SAMerf (we have his tool [here], works         for our 4D
>> machine).
>>
>>
>>  Thanks for it I will test it.
>>
>>
>> The idea is to calculate the covariance on all trials, calculate
>> weights by this covariance (keep filter in LCMV) and then apply
>> these weights on the averaged data. I found it useful because         the
>> covariance is better for longer datasets, and the averaging         in the
>> end increases the signal to noise ratio. I do not know         exactly how
>> to do it in fieldtrip.
>>
>>
>> On 22 March 2011 10:43, Jean-Michel           Badier <[
>>
>>
>> Dear Yuval,
>>
>>  I have to admit that I did not look at the matlab               routines.
>>  In item 2 I suppose that the covariance is calculated for
>> each trial then averaged. In item 3 I would like to               calculate
>> the covariance from all the signal (the trials               being
>> concatenated).
>>
>>  Jean-Michel
>>
>>  Le 22/03/11 05:47, Yuval Harpaz a écrit :
>>
>>
>>
>> Dear Jean Michel
>> As far as I know you can do it on an averaged                         data
>> structure (item 1) or do the same with the                         data
>> structure before averaging (3). I did not                         understand
>> what you meant by 2.
>>
>> Yuval
>>
>>
>> On 21 March 2011 22:58,                           Jean-Michel Badier <[
>>
>>
>> Dear                               fieldtrip users,
>>
>>  There are different ways of estimating the
>> covariance for LCMV calculation.
>>  If I am correct:
>>
>>  1. As suggested in one of the tutorial one
>> can apply the calculation of the                               covariance
>> directly on the average data                               (for the
>> different periods of interest                               that are at
>> least a base line and the                               period of interest).
>>
>>  2. Estimate the covariance from the                               average
>> of the covariance rather than the                               covariance
>> of the average using                               cfg.keeptrials = "yes"
>>
>>  3. Estimate the covariance from the whole
>> trials concatenated together.
>>  Is there an easy way to do that in
>> fieldtrip (beside create a new data set of                               one
>> trial constituted of all the trials)?
>>
>>  Thanks
>>
>>  Jean-Michel
>>
>>
>>  Laboratoire de
>> MagnétoEncéphaloGraphie INSERM U751. Aix
>>     Marseille Université 33 (0)4 91 38 55 62  [
>>  Service de                                               Neurophysiologie
>> Clinique. CHU                                         Timone 264 Rue
>>                                 Saint-Pierre, 13005
>>                 Marseille-France
>>
>>
>>
>>
>>
>>  _______________________________________________
>>  fieldtrip mailing list
>> [fieldtrip at donders.ru.nl]
>> [http://mailman.science.ru.nl/mailman/listinfo/fieldtrip]
>>
>>
>>
>>
>>
>>  --
>>
>> Y.Harpaz
>>
>>  a link to the BIU MEG lab:
>> [http://faculty.biu.ac.il/~goldsa/index.html]
>>
>>
>>
>>  _______________________________________________ fieldtrip mailing list [
>> fieldtrip at donders.ru.nl] [
>> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip]
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>  _______________________________________________
>>  fieldtrip mailing list
>> [fieldtrip at donders.ru.nl]
>> [http://mailman.science.ru.nl/mailman/listinfo/fieldtrip]
>>
>>
>>
>>
>>
>>  --
>>
>> Y.Harpaz
>>
>>  a link to the BIU MEG lab:
>> [http://faculty.biu.ac.il/~goldsa/index.html]
>>
>>
>>
>>
>>   _______________________________________________ fieldtrip mailing list
>> [fieldtrip at donders.ru.nl] [
>> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip]
>>
>>
>>
>> --
>>
>>
>>
>> <!--
>>                @page { size: 21cm 29.7cm; margin: 2cm }
>>                P { margin-bottom: 0.21cm }
>> -->
>>
>> Laboratoire               de MagnétoEncéphaloGraphie
>> INSERM               U751. Aix Marseille Université
>> 33               (0)4 91 38 55 62
>>
>> Service                     de Neurophysiologie Clinique. CHU
>>   Timone
>> 264               Rue Saint-Pierre, 13005 Marseille-France
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> fieldtrip mailing list
>> fieldtrip at donders.ru.nl
>> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>>
>
>
> _______________________________________________
> fieldtrip mailing list
> fieldtrip at donders.ru.nl
> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20110324/9aa619d6/attachment-0002.html>
```