-
Notifications
You must be signed in to change notification settings - Fork 1
/
stationary_bootstrap_calibrate.py
218 lines (172 loc) · 6.81 KB
/
stationary_bootstrap_calibrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import numpy as np
def OptimalLength(data: np.ndarray) ->float:
"""
Function calculates the optimal parameter value when using a stationary bootstraping algorithm.
The method is based on the 2004 paper by Politis & White:
Dimitris N. Politis & Halbert White (2004) Automatic Block-Length Selection for the Dependent Bootstrap, Econometric Reviews,
23:1, 53-70, DOI: 10.1081/ETC-120028836
The code was modified compared to Patton's implementation in that it takes as input a one dimensional time-series
and returns the optimalblock size only for the stationary bootstrap algorithm.
Warning! The minimal size of the time series is 9 elements.
Parameters
----------
data ... ndarray array containing the time-series that we wish to bootstrap.
Ex. np.array([-1,0.2,0.3,0.7,0.5,0.1,0.4,0.3,0.5])
Returns
-------
Bstar ... optimal value of the parameter m Ex. 1
Example of use:
>>> import numpy as np
>>> 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])
>>> OptimalLength(data)
Out[0]: 4.0
Original Matlab version written by:
James P. LeSage, Dept of Economics
University of Toledo
2801 W. Bancroft St,
Toledo, OH 43606
jpl@jpl.econ.utoledo.edu
This Python implementation is based on Andrew J. Patton's Matlab code avalible at:
http://public.econ.duke.edu/~ap172/
Implemented by Gregor Fabjan from Qnity Consultants on 12/11/2021.
"""
n = data.shape[0]
kn = max(5,np.sqrt(np.log10(n)))
mmax = int(np.ceil(np.sqrt(n))+kn)
bmax = np.ceil(min(3*np.sqrt(n),n/3))
c = 2
temp = mlag(data, mmax)
temp = np.delete(temp,range(mmax), 0) # Remove first rows where there are 0`s
corcoef = np.zeros(mmax)
for iCor in range(0, mmax):
corcoef[iCor] = np.corrcoef(data[mmax:],temp[:,iCor])[0,1]
temp2 = np.transpose(mlag(corcoef,kn))
temp3 = np.zeros((kn,corcoef.shape[0]+1-kn))
for iRow in range(kn):
temp3[iRow,:] = np.append(temp2[iRow,kn:corcoef.shape[0]],corcoef[len(corcoef)-kn+iRow-1])
treshold = abs(temp3) < (c* np.sqrt(np.log10(n)/n)) #Test if coeff bigger than triger
treshold = np.sum(treshold,axis = 0 )
count = 0
mhat = None
for x in treshold:
if (x==kn):
mhat = count
break
count +=1
if (mhat is None):
# largest lag that is still significant
seccrit = corcoef >(c* np.sqrt(np.log10(n)/n))
for iLag in range(seccrit.shape[0]-1,0,-1):
if (seccrit[iLag]):
mhat = iLag+1
break
if(mhat is None):
M = 0
elif (2*mhat > mmax):
M = mmax
else:
M = 2*mhat
# Computing the inputs to the function for Bstar
kk = np.arange(-M, M+1, 1)
if (M>0):
temp = mlag(data,M)
temp = np.delete(temp,range(M),0)
temp2 = np.zeros((temp.shape[0],temp.shape[1]+1))
for iRow in range(len(data)-M):
temp2[iRow,:] = np.hstack((data[M+iRow],temp[iRow,:]))
temp2 = np.transpose(temp2)
temp3 = np.cov(temp2)
acv = temp3[:,0]
acv2 = np.zeros((len(acv)-1,2))
acv2[:,0] = np.transpose(-np.linspace(1,M,M))
acv2[:,1] = acv[1:len(acv)]
if acv2.shape[0]>1:
acv2 =acv2[::-1]
acv3 = np.zeros((acv2.shape[0]+acv.shape[0],1))
Counter = 0
for iEl in range(acv2.shape[0]):
acv3[Counter,0] = acv2[iEl,1]
Counter +=1
for iEl in range(acv.shape[0]):
acv3[Counter,0] = acv[iEl]
Counter +=1
Ghat = 0
DSBhat = 0
LamTemp =lam(kk/M)
for iHat in range(acv3.shape[0]):
Ghat += LamTemp[iHat]* np.absolute(kk[iHat])*acv3[iHat,0]
DSBhat += LamTemp[iHat]*acv3[iHat,0]
DSBhat = 2* np.square(DSBhat)
Bstar = np.power(2*np.square(Ghat)/DSBhat,1/3)*np.power(n,1/3)
if Bstar>bmax:
Bstar = bmax
else:
Bstar = 1
return Bstar
def mlag(x: np.ndarray,n)-> np.ndarray:
"""
Returns a numpy array in which the k-th column is the series x pushed down (lagged) by k places.
Parameters
----------
x ... ndarray array for which the lagged matrix is calculated. np.array([1,2,3,4])
n ... integer specifying how many lags does the function consider
Returns
-------
xlag... ndarray contining the k-th lagged values in the k-th column of the matrix
Example of use
>>> import numpy as np
>>> x = np.array([1,2,3,4])
>>> n = 2
>>> mlag(x,n)
Out[0]: array([[0, 0],
[1, 0],
[2, 1],
[3, 2]])
The function was tested passing a numpy array (ndarray) as input and requires numpy to run.
Original Matlab version written by:
James P. LeSage, Dept of Economics
University of Toledo
2801 W. Bancroft St,
Toledo, OH 43606
jpl@jpl.econ.utoledo.edu
This Python implementation is based on Andrew J. Patton's Matlab code avalible at:
http://public.econ.duke.edu/~ap172/
Implemented by Gregor Fabjan from Qnity Consultants on 12/11/2021
"""
nobs = x.shape[0]
out = np.zeros((nobs,n))
for iLag in range(1,n+1):
for iCol in range(nobs-iLag):
out[iCol+iLag, iLag-1] = x[iCol]
return out
def lam(x: np.ndarray)-> np.ndarray:
"""
Returns the value at points x of the Trapezoidal function. Trapezoidal funcion maps all numbers bigger than 1 or smaller than -1 to zero.
Values between -1/2 to 1/2 to 1 and the rest either on the line connecting (-1,0) to (-1/2,1) or (1/2,1) to (1,0).
Parameters
----------
x ... ndarray array of points on which we wish to apply the trapezoidal mapping.
Ex. np.array([-1,0.2,0.3,0.7])
Returns
-------
out ... ndarray of mapped points Ex. array([0. , 1. , 1. , 0.6])
Example of use:
>>> import numpy as np
>>> x = np.array([0.55])
>>> lam(x)
Out[0]: array([0.9])
Original Matlab version written by:
James P. LeSage, Dept of Economics
University of Toledo
2801 W. Bancroft St,
Toledo, OH 43606
jpl@jpl.econ.utoledo.edu
This Python implementation is based on Andrew J. Patton's Matlab code avalible at:
http://public.econ.duke.edu/~ap172/
Implemented by Gregor Fabjan from Qnity Consultants on 12/11/2021.
"""
nrow = x.shape[0]
out = np.zeros(nrow)
for row in range(nrow):
out[row] = (abs(x[row])>=0) * (abs(x[row])<0.5) + 2 * (1-abs(x[row])) * (abs(x[row])>=0.5) * (abs(x[row])<=1)
return out