bootstrapping
bootstrapping
As part of a project to test the efficacy of alternative trading rules, I'm need to save results from 500 draws of a bootstrap for subsequent use. In the following code (based on BOOTSIMP.PRG), the COPY command saves results only for the final draw. I've tried placing the COPY command inside and outside the DO loop, to no avail. If anyone has a helpful suggestion, I'd certainly be most grateful if they would pass it on to me. Kind regards.
*
* BOOTSIMP.PRG
* Bootstrapping with an AR(1) model.
*
all 4697
open data f:/Gerry/KLSE_returns.xls
data(format=xls,org=obs) / price returns
*
stats returns
*
*Estimate AR(1) model for returns; obtain resids and coeffs.
*
linreg returns
# constant returns{1}
set uhat = %resids
compute B1 = %beta(1)
compute B2 = %beta(2)
*
* For the bootstrap, make 500 draws from the residuals,
* which, together with coeffs, will produce 500 series of returns and price.
* Save the bootstrapped series in Excel file.
compute ndraws = 500
*
open copy returnstar.xls
/*
do draws=1,ndraws
boot entries 1 4697
set ustar1 1 4697 = uhat(entries(t))
set r_star1 = B1 + B2*returns{1} + ustar1
*/
do i=1,ndraws
set ustar = uhat(fix(%uniform(1,4698)))
set r_star = B1 + B2*returns{1} + ustar
* Transform bootstrapped returns series back into price level equivalents.
smpl 1 1 ; set p_star = 505.92
smpl 2 4697 ; set p_star = (1+r_star)*p_star{1}
end do i
*print 1 4697 r_star p_star
copy(format=xls,org=col) 1 4697 r_star p_star price
*
* BOOTSIMP.PRG
* Bootstrapping with an AR(1) model.
*
all 4697
open data f:/Gerry/KLSE_returns.xls
data(format=xls,org=obs) / price returns
*
stats returns
*
*Estimate AR(1) model for returns; obtain resids and coeffs.
*
linreg returns
# constant returns{1}
set uhat = %resids
compute B1 = %beta(1)
compute B2 = %beta(2)
*
* For the bootstrap, make 500 draws from the residuals,
* which, together with coeffs, will produce 500 series of returns and price.
* Save the bootstrapped series in Excel file.
compute ndraws = 500
*
open copy returnstar.xls
/*
do draws=1,ndraws
boot entries 1 4697
set ustar1 1 4697 = uhat(entries(t))
set r_star1 = B1 + B2*returns{1} + ustar1
*/
do i=1,ndraws
set ustar = uhat(fix(%uniform(1,4698)))
set r_star = B1 + B2*returns{1} + ustar
* Transform bootstrapped returns series back into price level equivalents.
smpl 1 1 ; set p_star = 505.92
smpl 2 4697 ; set p_star = (1+r_star)*p_star{1}
end do i
*print 1 4697 r_star p_star
copy(format=xls,org=col) 1 4697 r_star p_star price
Re: bootstrapping
XLS, WKS and several other formats like that create the entire file in a single go, so you can't append to them. The best choice for a whole string of output like that is FORMAT=FREE,ORG=VAR, which will just have a separate block of otherwise unlabeled data for each COPY instruction. You can read that back into RATS or almost anything else relatively easily.
Re: bootstrapping
Hello Tom,
Is it possible to format the COPY command in a manner that will store the transformed series (p_star) as a matrix with the results from each of the 500 draws stored in a separate column, each having
number of rows equal to the number of observations? Thanks very much.
Bert
Is it possible to format the COPY command in a manner that will store the transformed series (p_star) as a matrix with the results from each of the 500 draws stored in a separate column, each having
number of rows equal to the number of observations? Thanks very much.
Bert
Re: bootstrapping
You would need to put the draws into a VECT[SERIES] so they would all be available at once. For instance:
Note, by the way, that you can't use the XLS format with more than 256 draws since that's the column limit.
is precisely what the FIRST option on SET is for. This could be replaced by:
Code: Select all
dec vect[series] ustar(ndraws) r_star(ndraws) p_star(ndraws)
do i=1,ndraws
set ustar(i) = uhat(fix(%uniform(1,4698)))
set r_star(i) = B1 + B2*returns{1} + ustar(i)
* Transform bootstrapped returns series back into price level equivalents.
smpl 1 1 ; set p_star(i) = 505.92
smpl 2 4697 ; set p_star(i) = (1+r_star(i))*p_star(i){1}
end do i
open copy r_star.xls
copy(format=xls,org=col) 1 4697 r_star
open copy p_star.xls
copy(format=xls,org=col) 1 4697 p_star
open copy price.xls
copy(format=xls,org=col) 1 4697 priceCode: Select all
smpl 1 1 ; set p_star(i) = 505.92
smpl 2 4697 ; set p_star(i) = (1+r_star(i))*p_star(i){1}Code: Select all
set(first=505.92) p_star(i) 1 4697 = (1+r_star(i))*p_star(i){1}