It seems to work well! After making all arrangements, my code looks like
that:
smpl roll_firstobs roll_firstobs+roll_window_int
loop j = roll_firstobs + roll_window_int .. roll_firstobs +
roll_window_int + roll_nr
loop foreach i excshare
string temp=sprintf("beta%d",i)
if sum(missing(excshare.$i)) # This avoids calculating betas
when not all data points are available
loop m = $t2 .. roll_lastobs
series @temp[m] = NA
endloop
else
ols excshare.$i const excindex@ind
loop m = $t2 .. roll_lastobs
if ok(excshare.$i[m])==0 # This avoids having betas for
dead periods
series @temp[m] = NA
else
series @temp[m] = $coeff[2]
endif
endloop
endif
endloop
if j<roll_firstobs + roll_window_int + roll_nr
smpl +roll_step +roll_step
endif
endloop
This rolling regression works really well. It loops into a list of
excess returns and computes sequential OLS, keeping the main coefficient
(which is beta) in a series for each stock. The only problem I still
have is on the time taken when the list of stocks is large...
--
Filipe Rodrigues da Costa
Send me an email to: filipe(a)pobox.io
Reach me through Telegram at:
https://t.me/rodriguesdacosta
On Fri, 27 Oct 2017, at 12:34, Sven Schreiber wrote:
Am 27.10.2017 um 13:27 schrieb Filipe Rodrigues da Costa:
>
>> 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).
> 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?
There are various possibilities. For example, check for any missings in
the sample like this (untested, bugs likely!):
<hansl>
open denmark
T = $nobs
window_size = 20
list all = LRM LRY
loop i=window_size..T
start = i - window_size + 1
smpl start i
if sum(missing(all)) == 0
ols LRM const LRY
endif
endloop
</hansl>
hth,
sven
_______________________________________________
Gretl-users mailing list
Gretl-users(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users