Skip to content

Commit 6f8cbeb

Browse files
Transferred from Matlab repository
0 parents commit 6f8cbeb

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Gregor
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
A block resampling method used for weakly-dependent stationary time-series data proposed in the 1994 paper by [Politis & Romano](https://www.researchgate.net/publication/254287565_The_Stationary_Bootstrap).
2+
3+
## Problem
4+
When using non-parametric tools to generate counterfactual scenarios or empirical distributions, bootstrapping methods proved to be a powerful and easy-to-use tools. However, the bootstrap in its simplest implementation assumes a time-series in which observations are independent. In a lot of applications this is not the case.
5+
6+
An example of this is interest rate modelling when business cycles need to be considered. The presence of business cycles makes the time-series weakly time dependent. To account for this, block-resampling techniques are used.
7+
8+
## Solution
9+
10+
Stationary bootstrap is a block-resampling technique that relaxes the assumption of a fixed length of a sampling block. The user still needs to specify an average length, but because this is true only on average, shorter/longer blocks are also present in the final sample.
11+
The algorithm works by randomly selecting a starting point in the time-series and at each step it either increases the block size by one or selects a new block with a new starting point. This choice happens with a fixed probability governed by the parametrisation.
12+
13+
### Input
14+
- A time-series that you want to bootstrap
15+
- The parameter m describing the average duration of the blocks in the sample
16+
- The length of the output sample
17+
18+
### Output
19+
- Vector of bootstrapped values of specified length
20+
21+
## Getting started
22+
23+
Given the time-series with observed values 0.4, 0.2, 0.1, 0.4, 0.3, 0.1, 0.3, 0.4, 0.2, 0.5, 0.1, and 0.2, the user is looking to bootstrap a new sample of length 9 where the average block is of size 4.
24+
25+
```python
26+
import numpy as np
27+
from StationaryBootstrap import StationaryBootstrap
28+
29+
# Original time-series
30+
data = np.array([0.4,0.2,0.1,0.4,0.3,0.1,0.3,0.4,0.2,0.5,0.1,0.2])
31+
32+
# Average length of the block
33+
m = 4
34+
35+
# Length of output sample
36+
sampleLength = 12
37+
38+
ans = StationaryBootstrap(data, m, sampleLength)
39+
40+
print(ans)
41+
```

StationaryBootstrap.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import numpy as np
2+
3+
def StationaryBootstrap(data: np.ndarray, m, sampleLength)-> np.ndarray:
4+
"""
5+
Returns a bootstraped sample of the time-series "data" of length "sampleLength.
6+
The algorithm used is stationary bootstrap from 1994 Politis & Romano.
7+
8+
Args:
9+
data ... ndarray array. A single vector of numbers containing the time-series.
10+
m ... floating number. Parameter to stationary bootstrap indicating the average length of each block in the sample.
11+
sampleLength ... integer. Length of the bootstrapped sample returned as output.
12+
13+
Returns:
14+
sample ... ndarray array containing the final bootstraped sample.
15+
16+
Example of use:
17+
>>> import numpy as np
18+
>>> data = np.array([1,2,3,4,5,6,7,8,9,10])
19+
>>> m = 4
20+
>>> sampleLength = 12
21+
>>> StationaryBootstrap(data, m, sampleLength)
22+
Out[0]: array([[9.],
23+
[3.],
24+
[4.],
25+
[5.],
26+
[6.],
27+
[7.],
28+
[8.],
29+
[7.],
30+
[2.],
31+
[3.],
32+
[4.],
33+
[2.]])
34+
35+
Original paper about stationary bootstrap:
36+
Dimitris N. Politis & Joseph P. Romano (1994) The Stationary Bootstrap, Journal of the American Statistical
37+
Association, 89:428, 1303-1313, DOI: 10.1080/01621459.1994.10476870
38+
39+
Implemented by Gregor Fabjan from Qnity Consultants on 12/11/2021.
40+
41+
"""
42+
accept = 1/m
43+
lenData = data.shape[0]
44+
45+
sampleIndex = np.random.randint(0,high =lenData,size=1);
46+
sample = np.zeros((sampleLength,1))
47+
for iSample in range(sampleLength):
48+
if np.random.uniform(0,1,1)>=accept:
49+
sampleIndex += 1
50+
if sampleIndex >= lenData:
51+
sampleIndex=0
52+
else:
53+
sampleIndex = np.random.randint(0,high = lenData,size=1)
54+
55+
sample[iSample,0] = data[sampleIndex]
56+
return sample

0 commit comments

Comments
 (0)