## bootstrapping

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.

### 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
wardb

Posts: 15
Joined: Fri Nov 02, 2007 3:46 pm

### 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.
TomDoan

Posts: 2727
Joined: Wed Nov 01, 2006 5:36 pm

### Re: bootstrapping

Thank you, Tom.
wardb

Posts: 15
Joined: Fri Nov 02, 2007 3:46 pm

### 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
wardb

Posts: 15
Joined: Fri Nov 02, 2007 3:46 pm

### Re: bootstrapping

You would need to put the draws into a VECT[SERIES] so they would all be available at once. For instance:

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 iopen copy r_star.xlscopy(format=xls,org=col) 1 4697 r_staropen copy p_star.xlscopy(format=xls,org=col) 1 4697 p_staropen copy price.xlscopy(format=xls,org=col) 1 4697 price`

Note, by the way, that you can't use the XLS format with more than 256 draws since that's the column limit.

Code: 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}`

is precisely what the FIRST option on SET is for. This could be replaced by:

Code: Select all
`set(first=505.92) p_star(i) 1 4697 = (1+r_star(i))*p_star(i){1}`
TomDoan

Posts: 2727
Joined: Wed Nov 01, 2006 5:36 pm