Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Fwd: running mean and s.d.


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: Fwd: running mean and s.d.
Date   Tue, 11 May 2004 17:28:05 -0400

Begin forwarded message:

From: Kit Baum <[email protected]>
Date: May 11, 2004 4:52:10 PM EDT
To: "de la Garza, Adrian" <[email protected]>
Subject: Re: running mean and s.d.

Adrian,

the shortcut formula (which we used to use in the days of punch cards, to avoid holding the entire dataset in 'core'!) is

s^2 = [ sum (x_i^2) - n (\bar{x})^2 ] / (n -1)

If you knew the mean, the \sigma^2 formula would have n \mu^2 in the numerator and n in the denominator.

I believe that my shortcut s.d. formula works perfectly well for any number of issues per issuer.

Please note that in the mail message the if _n == `i' clause appeared on the next line. That is part of the bys borrname: replace sd statement.

Thanks
Kit

On May 11, 2004, at 4:40 PM, de la Garza, Adrian wrote:


Chris,

Can you please explain your procedure? I see that you try to use the standard deviation formula but I don't know how your forvalues loop work, especially this part:

`i'*meanmat[`i']^2)/(`i'-1) if _n==`i'

what is that `i' multiplying meanmat[`i']^2?

Thank you.
Adrian


-----Original Message-----
From: Christopher F Baum [mailto:[email protected]]
Sent: Monday, May 10, 2004 9:05 PM
To: [email protected]
Subject: st: running mean and s.d.



Adrian asked how to calculate a running mean and s.d. within bygroups: e.g.


borrower closedate maturity mean sdev cvar

18 Thermal Po~s 23-Dec-02 5 5.00 0.00 0.00

1st Silicon (~c 19-Jun-01 7 7.00 0.00 0.00

AB Capital Ma~d 25-Jul-97 2 2.00 0.00 0.00

AB Lietuvos D~a 25-Aug-00 3 3.00 0.00 0.00

ABC Roma Bank~d 13-Mar-97 1 1.00 0.00 0.00

ABN AMRO Bank~V 6-Apr-01 1 1.00 0.00 0.00

ABN AMRO Bank~V 9-May-01 1 1.00 0.00 0.00

ABN AMRO Bank~) 25-Oct-95 3 3.00 0.00 0.00

AC Internatio~d 30-May-96 6 6.00 0.00 0.00

AC Internatio~d 29-Nov-96 5 5.50 0.71 0.13

AC Internatio~d 16-Jul-99 6 5.67 0.58 0.10

ACINDAR Indus~A 16-Jul-93 2 2.00 0.00 0.00

ACINDAR Indus~A 12-Nov-93 5 3.50 2.12 0.61

ACINDAR Indus~A 14-Feb-97 7 4.67 2.52 0.54

ADI Cayman Corp 21-Dec-95 5 5.00 0.00 0.00

AECI Ltd 19-Jul-91 4 4.00 0.00

0.00

AIG Finance (~d 12-Apr-01 3 3.00 0.00 0.00

ANACAFE - Aso~e 31-Aug-93 5 5.00 0.00 0.00

AO IrkutskEne~o 20-Apr-98 3 3.00 0.00 0.00

AO Moscow Cit~k 19-Mar-98 3 3.00 0.00 0.00

AO Siberian O~) 15-Aug-97 3 3.00 0.00 0.00

APP Global Fi~ 17-Apr-97 5 5.00 0.00 0.00

APP Global Fi~d 4-Oct-96 5 5.00 0.00 0.00

APP Internati~V 18-Sep-95 5 5.00 0.00 0.00

APP Internati~V 18-Sep-95 10 7.50 3.54 0.47

APP Internati~V 28-Jun-96 3 6.00 3.61 0.60

APP Internati~V 12-Dec-96 3 5.25 3.30 0.63



bys borrname:g totmat=sum(mat)

bys borrname: g iss=_n

g meanmat = totmat/iss

g matsq = mat^2

bys borrname: g sqmat = sum(matsq)

g sd = 0


su iss,meanonly

local maxiss = r(max)

forv i=2/`maxiss' {

bys borrname: replace sd = (sqmat[`i'] - `i'*meanmat[`i']^2)/(`i'-1) if _n==`i'

}

replace sd=sqrt(sd) if sd>0

g cv = sd/meanmat

l borrname issdate meanmat sd cv


Kit




*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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