bashtage / arch

ARCH models in Python
Other
1.29k stars 247 forks source link

Time series bootstrapping issues #697

Closed msh855 closed 8 months ago

msh855 commented 8 months ago

Hi,

I am trying to replicate some of your examples.

I using the Stationary Bootstrapping method, but I have noticed the following

import yfinance
data = yfinance.download('^GSPC')
bench_name = 'SP500'
ret_bench = pd.Series(data['Adj Close'].pct_change(), name = bench_name)
seed_n = 123 

bs_opt = StationaryBootstrap(opt.loc[bench_name, "stationary"], ret_bench, seed=seed)
bs_opt
Out[62]: Stationary Bootstrap(block size: 0.35387158048858935, no. pos. inputs: 1, no. keyword inputs: 0, ID: 0x7fbacd380bb0)
bs_opt.bootstrap(n_samples)
Out[63]: <generator object IIDBootstrap.bootstrap at 0x7fbacca5f4a0>

As you can see the bs_opt.bootstrap(n_samples) seems to be calling an IID boostrapping. is this supposed to be correct?

I also have issues using the optimal block for the circular bootstrapping:


opt = optimal_block_length(ret_bench)
block_size = opt.loc[bench_name, 'circular']

bs = CircularBlockBootstrap(block_size, ret_bench, seed=seed_n)
ps_list = []
for data in bs.bootstrap(n_samples):
    bs_x = data[0][0]
    ps_list.append(bs_x)

The error that I get is this:


    return prng.integers(upper, size=size, dtype=np.int64)
  File "_generator.pyx", line 543, in numpy.random._generator.Generator.integers
  File "_bounded_integers.pyx", line 1256, in numpy.random._bounded_integers._rand_int64
TypeError: expected a sequence of integers or a single integer, got '45892.0'
bashtage commented 8 months ago

45892.0 is not an integer. Make sure size is an integer, e.g., size=int(size).

bashtage commented 8 months ago

As you can see the bs_opt.bootstrap(n_samples) seems to be calling an IID boostrapping. is this supposed to be correct?

Yes. The bootstrap method calls private internal methods that differ across classes, so that the bootstrap never needs to be overwritten.

bashtage commented 8 months ago

Going to close as answered, but please fell free to continue to post if you this isn't clean.

msh855 commented 8 months ago

Thanks. There is an issue with this solution (i.e. size=int(size)). Very often I use the optimal block size provided by the library and the result is a float, like 0.40. This solution will make the optimal size block zero and the bootstrap will fail again.

bashtage commented 8 months ago

Block size has to be at least 1. int(max(size, 1)) would work.