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

st: running mean and s.d.


From   Christopher F Baum <[email protected]>
To   [email protected]
Subject   st: running mean and s.d.
Date   Mon, 10 May 2004 21:04:48 -0400

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



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