Separately, I would like to calculate the standard deviation of returns
for
a particular year, e.g. egen stdev = stdev(nret), by(year), but there
doesn’t seem to be a stdev function (or any variants). The “summarize”
function gives me standard deviations but doesn’t store these as a
variable.
I’ve been able to calculate the standard deviation manually, by
squaring
the normalized returns and dividing them by the total number of
observations
less one, but thought that Stata must have a function to do this.
help egen turns up sd()...and from that help file,
count(exp)
(allows by varlist)
creates a constant (within varlist) containing the number of
nonmissing
observations of exp. Also see robs() and rmiss() below.
so I don't understand " I’m using Stata 8, which doesn’t seem to allow
me to do a egen temp = count(x) like previous Stata version".
Not clear that any egens are needed, though. If you want the results
stored in variables:
clear
local nn 10000
set obs `nn'
g ret = invnorm(uniform())
g sigma = .
g res = .
g sd = .
local j 0
qui forv i= -3/3 {
count if ret < `i'
local ++j
replace sigma = `i' in `j'
replace res = r(N)/`nn' in `j'
replace sd = sqrt(res[`j']*(1-res[`j'])/`nn') in `j'
}
l sigma res sd in 1/7