Statalist


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

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


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: RE: RE: Sum product of each element with each other element except itself?
Date   Sun, 16 Aug 2009 20:31:10 +0200

<> 

"(I've tried to spare you of the parts
which I feel confident are not the reason for the invalidity):"


Why not be certain and -set trace on- and show the whole world the output?


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Stata Chris
Gesendet: Sonntag, 16. August 2009 18:45
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: RE: RE: Sum product of each element with each other element
except itself?

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       =
sum(beta_other_comp)/T_it
bysort period               :   gen beta_other           =
components_sum /    ( (I-1) * sd_pit * sd_pit_lag)







2009/8/16 Nick Cox <n.j.cox@durham.ac.uk>:
> It sounds as if you just keep going, using the principle that sum of
> (everything - somethings) = sum(everything) - sum(somethings).
>
> Nick
> n.j.cox@durham.ac.uk
>
> Martin Weiss
>
> This FAQ may be helpful:
> http://www.stata.com/support/faqs/data/members.html
>
> 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:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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