-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
49 lines (36 loc) · 1.24 KB
/
block.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
__doc__ = """
Block to determine auto-correlation time.
"""
import numpy as np
from tqdm import tqdm
def coarsen(arr):
return np.array([(arr[i<<1]+arr[i<<1|1])/2. for i in range(len(arr)//2)])
def exponential_blocking(arr):
''' Should not use this one. Too few information '''
if len(arr) == 1:
return []
return [arr.std()/len(arr)] + exponential_blocking(coarsen(arr))
def linear_blocking(arr):
return \
[
np.array(
[arr[k*n:k*n+n].mean() for k in range(len(arr)//n-1)]
).var()/(len(arr)//n-2)
for n in tqdm(range(1, min(len(arr)//20 + 1, 400)))
]
def linear_blocking_sanity(arr):
for n in range(1, min(len(arr)//20 + 1, 400)):
print(n)
buf = []
for k in range(len(arr)//n-1):
buf.append(arr[k*n:k*n+n].mean())
buf = np.array(buf)
m = buf.mean()
print(sum((x-m)*(x-m) for x in buf)/len(buf))
print(buf.var())
print(len(buf))
print(len(arr)//n-2)
print(arr[:-1].var()/(len(arr)-1))
input()
if __name__ == '__main__':
print(linear_blocking_sanity(np.array(list(range(100)))))