On Tue, 24 Oct 2017, Allin Cottrell wrote:
If that's right, here's faster code:
[...]
I took a different approach, which avoids loops completely. If I
understand the logic correctly, basically resampling is done by putting
together a series of "runs" of random length (b on average) which start at
random points between 1 and n. So I re-wrote the function as follows:
<hansl>
function matrix SB2 (matrix x "Data to be resampled",
int b[0::] "Block size")
scalar n = rows(x)
if n == 0
funcerr "data input error, check data"
endif
if b==0 # if required set mean block size to rule-of-thumb value
b = round(1.75*(n^(1/3)))
endif
p = 1/b # probability of a new run
s = seq(1,n)' # sequence
u = 1 | (muniform(n-1,1) .< p) # run starts
r = cum(u) # id of each run
nr = r[n] # how many runs ?
sub = selifr(r ~ s, u) # starting row for each run
sub[,2] -= mrandgen(i, 1, n, nr, 1) # adjust starting points
# --- create mini-trends ----------------
s -= replace(r, sub[,1], sub[,2])
# roll over if necessary
s = ((s-1)%n) + 1
return x[s,]
end function
</hansl>
This appears to be quite fast.
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------