Rolling OLS
by Filipe Rodrigues da Costa
Hi All,
I have been trying to solve a few problems regarding the rolling
regression. In order to separate them, I am using a simple example here,
in which I have two financial assets: Amazon (AMZN) and the market
(SP500). I'm trying to estimate "beta", which can be roughly obtained
from regressing the returns of AMZN on the returns of SP500.
I found a very simple code from a previous issue on this list, which is
quite straightforward:
> T = $nobs
>
> scalar window_size = 20
> scalar k = $nobs - window_size + 1
> series b = NA
>
> smpl 1 window_size
> loop i = window_size .. T
> ols AMZN const SP500
> if i < T
> smpl +1 +1
> endif
> endloop
>
> smpl full
So far so good, this works quite well. But let's say the data covers 100
periods for SP500 but only 60 for AMZN (no data for the last 40).
Because I'm using a rolling window of 20 data points, a point will come
when the routine will use only 19 data points, 18, 17, and so on until
reaching 2 (which is the technical minimum). The reason for this is
because the routine will still identify datapoints in the full sample,
even though there are less for AMZN.
My question is as follows: Is there a simple way of imposing the routine
to only estimate OLS when we have the full 20 data points for AMZN and
20 for SP500? When I have a very large dataset with 400 or even 500
assets, there are many cases where some just went out of the market and
then I should not be estimating betas. I believe the program checks for
the $t1 and if it exists it computes OLS. I would like it to check for
everything between $t1 and $t2 and if some missing, in particular at the
end, just don't compute.
Hope I could make myself clear! Thanks all!
--
Filipe Rodrigues da Costa
Send me an email to: filipe(a)pobox.io
Reach me through Telegram at: https://t.me/rodriguesdacosta
7 years, 4 months
Sequential naming of variables
by Filipe Rodrigues da Costa
Hi All,
I'm trying to find a way of generating variable names automatically.
I have a list of share prices: share1, share2, ...., share12. I want to
compute returns in a loop. Here is what I was doing:> # Compute returns for all shares
> loop foreach i list_share
> ret_$i = 100 * (log(list_share.$i) - log(list_share.$i(-1)))
> endloop
The above one would return the following variables: ret_share1,
ret_share2,..., ret_share12.
My question is simple: is there a way of controlling the name of the
output variable in a different way than what is used above? I mean, how
can I get output in variables with name: ret1, ret2,..., ret12? Is there
any sequential command that can be added to ret*?
Thanks.
--
Filipe Rodrigues da Costa
Send me an email to: filipe(a)pobox.io
Reach me through Telegram at: https://t.me/rodriguesdacosta
7 years, 4 months