[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: RE: Sum product of each element with each other element except itself?

From   "Nick Cox" <>
To   <>
Subject   RE: st: RE: RE: Sum product of each element with each other element except itself?
Date   Sun, 16 Aug 2009 17:53:48 +0100

Your -egen- call 

bysort period               :  egen components_sum       =

is incorrect. You cannot complicate -egen- function calls with arbitrary extra expressions. Look again at the help for -egen-. You must put other stuff inside the () or do it separately. 

I can't advise on whether this code is correct otherwise. 


Stata Chris

Thanks Martin and Nick!

I've tried that but there seems to remain at least one problem, as
Stata tells me that I have invalid Syntax and I'm not sure where the
problem is. Below is my code (I've tried to spare you of the parts
which I feel confident are not the reason for the invalidity):

Explanation in words: What I want to get is the sum of the deviation1
for each fund multiplied by the deviation2 of each other fund. So I
have created two loops. The first generates deviation1 and deviation2
for each fund. The second is supposed to multiply for each fund j its
deviation1 with the deviation2 of every other fund (I'm multiplying it
with every possible fund, and then subtracting its own deviation2,
following the principle you mentioned). Finally, I wanted to sum up
these products for all 19 funds. However, when I run this Stata tells
me after the second loop that there is invalid syntax:

xtset obs_fundasset period, monthly
sort obs_fundasset period
bysort obs_fundasset period:   gen deviation1 = (buying     - mean_pit    )
bysort obs_fundasset period:   gen deviation2 = (buying_lag - mean_pit_lag)

forvalues i=1/19{
  capture noisily {
    bysort assetid period:  gen    deviation1_`i' = deviation1   if fundid==`i'
    bysort assetid period:  gen    deviation2_`i' = deviation1   if fundid==`i'
    bysort assetid period: replace deviation1_`i' = 0              if
deviation1_`i' == .
    bysort assetid period: replace deviation2_`i' = 0              if
deviation2_`i' == .


forvalues j=1/19{
  capture noisily {
       bysort obs_fundasset period: gen beta_other_component_`j' =
deviation1_`j' * ///
                               (deviation2_1  + deviation2_2  +
deviation2_3  + deviation2_4  + deviation2_5  + ///
                                deviation2_6  + deviation2_7  +
deviation2_8  + deviation2_9  + deviation2_10 + ///
                                deviation2_11 + deviation2_12 +
deviation2_13 + deviation2_14 + deviation2_15 + ///
                                deviation2_16 + deviation2_17 +
deviation2_18 + deviation2_19 - deviation2_`j')

bysort period               :  egen components_sum       =
bysort period               :   gen beta_other           =
components_sum /    ( (I-1) * sd_pit * sd_pit_lag)

2009/8/16 Nick Cox <>:

> It sounds as if you just keep going, using the principle that sum of
> (everything - somethings) = sum(everything) - sum(somethings).

Martin Weiss

> This FAQ may be helpful:
> Apart from that, give an excerpt of your data, and show exactly what you
> want to happen to it...

Stata Chris

> I would like to compute the following in Stata:
> Sum over all n of:  Sum over all m unequal n of:
> (D_nt - p)*(D_mt - p),
> where m, n, t are subindices.
> If it was with only a single sum and without the unequal condition, I
> would just have used sth like this:
> bysort m n t:  gen factor1 = (D_nt - p)
> bysort m n t:  gen factor2 = (D_mt - p)
> bysort t:        egen result = sum(factor1*factor2)
> But I'm not sure how to get the product for each element n with each
> other element from the same set except itself.
> Should I do this with a loop or sth?

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index