├── test3.py ├── RandomTestModule.pyc ├── __pycache__ ├── RandomGenerator.cpython-35.pyc └── RandomTestModule.cpython-35.pyc ├── README.md ├── test2.py ├── testT.py ├── main.py ├── choose.py ├── RandomGenerator.py ├── average.py ├── autocor.py └── RandomTestModule.py /test3.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /RandomTestModule.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MatrixAINetwork/MATRIX_RANDOMNUMTEST/HEAD/RandomTestModule.pyc -------------------------------------------------------------------------------- /__pycache__/RandomGenerator.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MatrixAINetwork/MATRIX_RANDOMNUMTEST/HEAD/__pycache__/RandomGenerator.cpython-35.pyc -------------------------------------------------------------------------------- /__pycache__/RandomTestModule.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MatrixAINetwork/MATRIX_RANDOMNUMTEST/HEAD/__pycache__/RandomTestModule.cpython-35.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MATRIX_RANDOMNUMTEST 2 | 3 | 4 | This is used to test the performance of MATRIX random number design from a dimension of indicators: Chi-square indicator, KSTest indicator, autocorrelation, multi-dimensional Chi-square indicator, Kullback-Leibler Divergence, monobit test, in-block frequency test. 5 | 6 | Various random number algorithms are used, including: Mersenne Twister, well2024, xorshift, etc. 7 | 8 | -------------------------------------------------------------------------------- /test2.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | import warnings 4 | import matplotlib.pyplot as plt 5 | from scipy.stats import chisquare 6 | from functools import reduce 7 | 8 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 9 | 10 | 11 | if __name__=="__main__": 12 | 13 | a = [498,520,538,501,475,525,493,474,448,500,471,495,512,532,510,553,499,456 14 | ,523,477] 15 | b = [500 for i in range(20)] 16 | chisq = sum([(e-f)**2/f for e,f in zip(a,b)]) 17 | print(chisq) 18 | -------------------------------------------------------------------------------- /testT.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | import warnings 4 | from contextlib import contextmanager 5 | import time 6 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 7 | 8 | @contextmanager 9 | def timer(title): 10 | t0 = time.time() 11 | yield 12 | print("{} - done in {:.3f}s".format(title, time.time() - t0)) 13 | 14 | groups = 200 15 | seedlist = [i for i in range(groups)] 16 | with timer('elapsed time for mt19937'): 17 | for i in range(groups): 18 | rng = MT19937(i) 19 | res = rng.GenerateRandomU01(N=10000) 20 | 21 | with timer('elapsed time for pythonsys'): 22 | for i in range(groups): 23 | rng = pythonsytem(i) 24 | res = rng.GenerateRandomU01(N=10000) 25 | 26 | with timer('elapsed time for Well1024a'): 27 | for i in range(groups): 28 | rng = Well1024a(i) 29 | res = rng.GenerateRandomU01(N=10000) 30 | 31 | with timer('elapsed time for xorshift'): 32 | for i in range(groups): 33 | rng = xorshift1024(i) 34 | res = rng.GenerateRandomU01(N=10000) 35 | 36 | with timer('elapsed time for MyThreeFry'): 37 | for i in range(groups): 38 | rng = MyThreeFry(i) 39 | res = rng.GenerateRandomU01(N=10000) 40 | 41 | with timer('elapsed time for LCG64'): 42 | for i in range(groups): 43 | rng = MyPCG64(i) 44 | res = rng.GenerateRandomU01(N=10000) 45 | 46 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | import warnings 4 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 5 | 6 | def GeneratemdFile(columnName = [], rowname = [], content = [], outputpath = None): 7 | with open(outputpath,'w') as f: 8 | f.write('|'.join(columnName)+'\n') 9 | f.write('|'.join(['---' for i in range(len(columnName))])+'\n') 10 | for c,e in zip(rowname,content): 11 | temp = [c]+e 12 | f.write('|'.join(temp)+'\n') 13 | return 14 | 15 | 16 | def FromData2Index(datalist=None,bins=10,params=None): 17 | rndtest = randomtest(datalist=datalist) 18 | index = rndtest.GeneratemdTable(bins=bins,params = params) 19 | return ["%.5f"%e if type(e) != str else e for e in index] 20 | 21 | def GetRngDpFunc(func='mt19937',seed=0): 22 | rng = MT19937(0) 23 | if func == 'mt19937': 24 | rng = MT19937(seed) 25 | elif func == 'pythonsys': 26 | rng = pythonsytem(seed) 27 | elif func == 'well1024': 28 | rng = Well1024a(seed) 29 | elif func == 'xorshift': 30 | rng = xorshift1024(seed) 31 | elif func == 'ThreeFry': 32 | rng = MyThreeFry(seed) 33 | elif func == 'LCG64': 34 | rng = MyPCG64(seed) 35 | return rng 36 | 37 | def GenerateRandom01Arr(groups = 5, N = 10000, seed=0, func='mt19937'): 38 | seedlist = PrimeLessThanN(100000) 39 | #seedlist = [i for i in range(groups)] 40 | #print(len(seedlist)) 41 | seedlist = seedlist[-groups:] 42 | #print(len(seedlist)) 43 | #seedlist = [13,17,19,23,29] 44 | res = [] 45 | for i in range(groups): 46 | print(seedlist[i]) 47 | rng = GetRngDpFunc(func=func,seed=seedlist[i]) 48 | res.append(rng.GenerateRandomU01()) 49 | return res 50 | 51 | def PrimeLessThanN(N): 52 | ls = [True for i in range(N+1)] 53 | for i in range(2,N,1): 54 | if ls[i] is not True: 55 | continue 56 | for j in range(2*i,N+1,i): 57 | ls[j] = False 58 | res = [] 59 | for i in range(2,N+1,1): 60 | if ls[i] == True: 61 | res.append(i) 62 | return res 63 | 64 | if __name__=="__main__": 65 | # MT19937 66 | #res=MT19937(autocorrelation_tests0x12341).GenerateRandomU01(N=100000) 67 | # python 68 | N=1000 69 | bins=10 70 | seed=0x123451 71 | rowname = [] 72 | data=[] 73 | res = [] 74 | columnName = ["randomtype","chisquare-pvalue","kstest-pvalue","autocor","serial2D","kl/bins","monobit","blockfreq"] 75 | #columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue"] 76 | #rngtypes = ['mt19937']#,'pythonsys','well1024','xorshift','ThreeFry','LCG64'] 77 | rngtypes = ['pythonsys','well1024','xorshift','ThreeFry','LCG64'] 78 | for rngtype in rngtypes: 79 | datalist = GenerateRandom01Arr(groups=20,N=N,seed=seed,func=rngtype) 80 | rndtest = randomtest(datalist=datalist) 81 | passratio, res = rndtest.ChiSquare(bins=bins,confidence=0.8) 82 | print(passratio) 83 | #print(res) 84 | #print("%s chisq pv is %f"%(rngtype,rndtest.ChiSquare(bins=bins,confidence=0.8))) 85 | #index = rndtest.GeneratemdTable(bins=bins,params = columnName) 86 | 87 | #GeneratemdFile(columnName=columnName,rowname=rowname,content=data,outputpath='output.md') 88 | 89 | 90 | -------------------------------------------------------------------------------- /choose.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | from scipy.stats import chisquare 4 | import warnings 5 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 6 | 7 | def GeneratemdFile(columnName = [], rowname = [], content = [], outputpath = None): 8 | with open(outputpath,'w') as f: 9 | f.write('|'.join(columnName)+'\n') 10 | f.write('|'.join(['---' for i in range(len(columnName))])+'\n') 11 | for c,e in zip(rowname,content): 12 | temp = [c]+e 13 | f.write('|'.join(temp)+'\n') 14 | return 15 | 16 | 17 | def FromData2Index(datalist=None,bins=10,params=None): 18 | rndtest = randomtest(datalist=datalist) 19 | index = rndtest.GeneratemdTable(bins=bins,params = params) 20 | return ["%.5f"%e if type(e) != str else e for e in index] 21 | 22 | def GetRngDpFunc(func='mt19937',seed=0): 23 | rng = MT19937(0) 24 | if func == 'mt19937': 25 | rng = MT19937(seed) 26 | elif func == 'pythonsys': 27 | rng = pythonsytem(seed) 28 | elif func == 'well1024': 29 | rng = Well1024a(seed) 30 | elif func == 'xorshift': 31 | rng = xorshift1024(seed) 32 | elif func == 'ThreeFry': 33 | rng = MyThreeFry(seed) 34 | elif func == 'LCG64': 35 | rng = MyPCG64(seed) 36 | return rng 37 | 38 | def GenerateRandom01Arr(groups = 5, N = 10000, seed=0, func='mt19937'): 39 | seedlist = PrimeLessThanN(100000) 40 | #seedlist = [i for i in range(groups)] 41 | #print(len(seedlist)) 42 | seedlist = seedlist[-groups:] 43 | #print(len(seedlist)) 44 | #seedlist = [13,17,19,23,29] 45 | res = [] 46 | for i in range(groups): 47 | rng = GetRngDpFunc(func=func,seed=seedlist[i]) 48 | res.append(rng.GenerateRandomU01(N=N)) 49 | return res 50 | 51 | def PrimeLessThanN(N): 52 | ls = [True for i in range(N+1)] 53 | for i in range(2,N,1): 54 | if ls[i] is not True: 55 | continue 56 | for j in range(2*i,N+1,i): 57 | ls[j] = False 58 | res = [] 59 | for i in range(2,N+1,1): 60 | if ls[i] == True: 61 | res.append(i) 62 | return res 63 | 64 | 65 | def ChiSquare(datalist, bins=10, confidence = 0.8): 66 | def BinCountForData(data,bins = 10): 67 | bincount,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = False) 68 | return bincount 69 | 70 | def ExpectedCountForData(data,bins = 10): 71 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / bins 72 | return expectedcount 73 | # uniformity 74 | # p-value is the probability of assert H0 is true 75 | # H0 hypethesis: bincount Obey expected distribution 76 | res = [] 77 | passcnt = 0 78 | bincountSum = np.zeros((bins)) 79 | for data in datalist: 80 | bincount = BinCountForData(data,bins=bins) 81 | expectedcount = ExpectedCountForData(data,bins=bins) 82 | chisq,pvalue = chisquare(bincount,expectedcount) 83 | #print(bincount) 84 | #print(expectedcount) 85 | res.append(pvalue) 86 | if pvalue > confidence: 87 | passcnt += 1 88 | bincountSum = bincountSum + bincount 89 | #print(bincount) 90 | #print(expectedcount) 91 | # sum([(a-e)**2/e for a,e in zip(list(bincount), list(expectedcount))]) 92 | return 1.0*passcnt/len(datalist),res,bincountSum 93 | 94 | if __name__=="__main__": 95 | # MT19937 96 | #res=MT19937(autocorrelation_tests0x12341).GenerateRandomU01(N=100000) 97 | # python 98 | N=10000 99 | bins=10 100 | seed=0x123451 101 | rowname = [] 102 | data=[] 103 | res = [] 104 | #columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue","kstest-pvalue","autocor","serial2D","kl/bins","monobit","blockfreq"] 105 | columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue"] 106 | rngtypes = ['pythonsys']#,'pythonsys','well1024','xorshift','ThreeFry','LCG64'] 107 | for rngtype in rngtypes: 108 | datalist = GenerateRandom01Arr(groups=2000,N=N,seed=seed,func=rngtype) 109 | passratio, pvalueres, bincountsum = ChiSquare(datalist,bins=bins,confidence=0.8) 110 | print(passratio) 111 | print(pvalueres) 112 | print(bincountsum) 113 | -------------------------------------------------------------------------------- /RandomGenerator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | #coding=utf-8 3 | import random as rnd 4 | from randomgen import RandomGenerator, ThreeFry, PCG64, Xorshift1024 5 | from wellrng.well1024a import WELL1024a 6 | 7 | def _int32(x): 8 | return int(0xFFFFFFFF & x) 9 | 10 | class MT19937: 11 | def __init__(self, seed): 12 | self.mt = [0] * 624 13 | self.mt[0] = seed 14 | for i in range(1, 624): 15 | self.mt[i] = _int32(1812433253 * (self.mt[i - 1] ^ self.mt[i - 1] >> 30) + i) 16 | 17 | 18 | def extract_number(self): 19 | self.twist() 20 | y = self.mt[0] 21 | y = y ^ y >> 11 22 | y = y ^ y << 7 & 2636928640 23 | y = y ^ y << 15 & 4022730752 24 | y = y ^ y >> 18 25 | return _int32(y) 26 | 27 | def Uniform(self,low=0.0,high=1.0): 28 | return (high-low)*self.extract_number()/(2**32) 29 | 30 | 31 | def twist(self): 32 | for i in range(0, 624): 33 | y = _int32((self.mt[i] & 0x80000000) + (self.mt[(i + 1) % 624] & 0x7fffffff)) 34 | self.mt[i] = y ^ self.mt[(i + 397) % 624] >> 1 35 | if y % 2 != 0: 36 | self.mt[i] = self.mt[i] ^ 0x9908b0df 37 | 38 | def GenerateRandomU01(self,N=1000): 39 | res = [] 40 | for i in range(N): 41 | res.append(self.Uniform()) 42 | return res 43 | 44 | def Generate01Sequence(self, N=1000): 45 | res = self.GenerateRandomU01(N=N) 46 | return [0 if e < 0.5 else 1 for e in res] 47 | 48 | class pythonsytem: 49 | def __init__(self,seed = 0): 50 | self.rnd = rnd 51 | self.rnd.seed(seed) 52 | 53 | def GenerateRandomU01(self,N=1000): 54 | res = [] 55 | for i in range(N): 56 | res.append(self.rnd.uniform(0.0,1.0)) 57 | return res 58 | 59 | def Generate01Sequence(self, N=1000): 60 | res = self.GenerateRandomU01(N=N) 61 | return [0 if e < 0.5 else 1 for e in res] 62 | 63 | 64 | class Well1024a: 65 | def __init__(self,seed=0): 66 | self.rng = WELL1024a(seed) 67 | 68 | def GenerateRandomU01(self,N=1000): 69 | res = [] 70 | for i in range(N): 71 | res.append(self.rng.random()) 72 | return res 73 | 74 | def Generate01Sequence(self, N=1000): 75 | res = self.GenerateRandomU01(N=N) 76 | return [0 if e < 0.5 else 1 for e in res] 77 | 78 | 79 | class xorshift1024: 80 | def __init__(self,seed=0): 81 | self.rng = RandomGenerator(Xorshift1024(seed)) 82 | #self.rng.seed(seed) 83 | 84 | def GenerateRandomU01(self,N=1000): 85 | res = [] 86 | for i in range(N): 87 | res.append(self.rng.random_sample()) 88 | return res 89 | 90 | def Generate01Sequence(self, N=1000): 91 | res = self.GenerateRandomU01(N=N) 92 | return [0 if e < 0.5 else 1 for e in res] 93 | 94 | class MyPCG64: 95 | def __init__(self,seed=0): 96 | self.rng = RandomGenerator(PCG64(seed)) 97 | #self.rng.seed(seed) 98 | 99 | def GenerateRandomU01(self,N=1000): 100 | res = [] 101 | for i in range(N): 102 | res.append(self.rng.random_sample()) 103 | return res 104 | 105 | def Generate01Sequence(self, N=1000): 106 | res = self.GenerateRandomU01(N=N) 107 | return [0 if e < 0.5 else 1 for e in res] 108 | 109 | class MyThreeFry: 110 | def __init__(self,seed=0): 111 | self.rng = RandomGenerator(ThreeFry(seed)) 112 | #self.rng.seed(seed) 113 | 114 | def GenerateRandomU01(self,N=1000): 115 | res = [] 116 | for i in range(N): 117 | res.append(self.rng.random_sample()) 118 | return res 119 | 120 | def Generate01Sequence(self, N=1000): 121 | res = self.GenerateRandomU01(N=N) 122 | return [0 if e < 0.5 else 1 for e in res] 123 | 124 | 125 | 126 | class ReadFromPath: 127 | def __init__(self, path): 128 | self.path = path 129 | 130 | def readbyline(self): 131 | res = [] 132 | with open(self.path,'r') as f: 133 | for line in f.readlines(): 134 | res.append(float(line)) 135 | return res 136 | 137 | def GenerateRandomU01(self,N=1000): 138 | res = self.readbyline() 139 | assert len(res) >= N 140 | res = res[0:N] 141 | return res 142 | 143 | def Generate01Sequence(self, N=1000): 144 | res = self.GenerateRandomU01() 145 | return [0 if e < 0.5 else 1 for e in res] 146 | 147 | if __name__=="__main__": 148 | print('#'*20+' Test MT19937 '+'#'*20) 149 | #rand=MT19937(0x12341) 150 | #for i in range(30): 151 | # print(rand.Uniform()) 152 | 153 | rand = MyPCG64(1010104) 154 | print(rand.GenerateRandomU01(N=30)) 155 | -------------------------------------------------------------------------------- /average.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | import warnings 4 | import numpy as np 5 | from itertools import islice 6 | from scipy.stats import chisquare 7 | from scipy.stats import kstest 8 | from scipy.stats import norm 9 | import scipy.special as spc 10 | import math 11 | import warnings 12 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 13 | 14 | def GeneratemdFile(columnName = [], rowname = [], content = [], outputpath = None): 15 | with open(outputpath,'w') as f: 16 | f.write('|'.join(columnName)+'\n') 17 | f.write('|'.join(['---' for i in range(len(columnName))])+'\n') 18 | for c,e in zip(rowname,content): 19 | temp = [c]+e 20 | f.write('|'.join(temp)+'\n') 21 | return 22 | 23 | 24 | def FromData2Index(datalist=None,bins=10,params=None): 25 | rndtest = randomtest(datalist=datalist) 26 | index = rndtest.GeneratemdTable(bins=bins,params = params) 27 | return ["%.5f"%e if type(e) != str else e for e in index] 28 | 29 | def GetRngDpFunc(func='mt19937',seed=0): 30 | rng = MT19937(0) 31 | if func == 'mt19937': 32 | rng = MT19937(seed) 33 | elif func == 'pythonsys': 34 | rng = pythonsytem(seed) 35 | elif func == 'well1024': 36 | rng = Well1024a(seed) 37 | elif func == 'xorshift': 38 | rng = xorshift1024(seed) 39 | elif func == 'ThreeFry': 40 | rng = MyThreeFry(seed) 41 | elif func == 'LCG64': 42 | rng = MyPCG64(seed) 43 | return rng 44 | 45 | def GenerateRandom01Arr(groups = 5, N = 10000, seed=0, func='mt19937'): 46 | seedlist = PrimeLessThanN(500000) 47 | #seedlist = [i for i in range(groups)] 48 | #print(len(seedlist)) 49 | seedlist = seedlist[-groups:] 50 | print(len(seedlist)) 51 | print(seedlist) 52 | #seedlist=[i for i in range(groups)] 53 | #print(len(seedlist)) 54 | #seedlist = [13,17,19,23,29] 55 | res = [] 56 | for i in range(groups): 57 | rng = GetRngDpFunc(func=func,seed=seedlist[i]) 58 | res.append(rng.GenerateRandomU01(N=N)) 59 | return res 60 | 61 | def PrimeLessThanN(N): 62 | ls = [True for i in range(N+1)] 63 | for i in range(2,N,1): 64 | if ls[i] is not True: 65 | continue 66 | for j in range(2*i,N+1,i): 67 | ls[j] = False 68 | res = [] 69 | for i in range(2,N+1,1): 70 | if ls[i] == True: 71 | res.append(i) 72 | return res 73 | 74 | 75 | def ChiSquare(datalist, bins=10, confidence = 0.8): 76 | def BinCountForData(data,bins = 10): 77 | bincount,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = False) 78 | return bincount 79 | 80 | def ExpectedCountForData(data,bins = 10): 81 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / bins 82 | return expectedcount 83 | # uniformity 84 | # p-value is the probability of assert H0 is true 85 | # H0 hypethesis: bincount Obey expected distribution 86 | chisqlist = [] 87 | res = [] 88 | bincountSum = np.zeros((bins)) 89 | for data in datalist: 90 | bincount = BinCountForData(data,bins=bins) 91 | expectedcount = ExpectedCountForData(data,bins=bins) 92 | chisq,pvalue = chisquare(bincount,expectedcount) 93 | chisqlist.append(chisq) 94 | res.append(pvalue) 95 | return sum(chisqlist)/len(datalist),sum(res)/len(datalist) 96 | 97 | def KSTest(datalist): 98 | passcnt = 0 99 | chisqlist = [] 100 | pvaluelist = [] 101 | for data in datalist: 102 | chisq,pvalue = kstest(data,'uniform',args=(0,1)) 103 | 104 | chisqlist.append(chisq) 105 | pvaluelist.append(pvalue) 106 | #if pvalue > alpha: 107 | # passcnt += 1 108 | return sum(chisqlist)/len(datalist),sum(pvaluelist)/len(datalist) 109 | 110 | def Serial2DTest(datalist, binsX = 10, binsY = 10, alpha = 0.8): 111 | def Bin2DCountForData(data,binsX = 10,binsY = 10): 112 | oddlist = data[::2] 113 | evenlist = data[1::2] 114 | H,xedges,yedges = np.histogram2d(oddlist,evenlist,bins=[binsX,binsY],range=[[0.0,1.0],[0.0,1.0]]) 115 | return H.ravel() 116 | 117 | def Expected2DCountForData(data, binsX = 10, binsY = 10): 118 | bins = binsX * binsY 119 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / 2 / bins 120 | return expectedcount 121 | 122 | passcnt = 0 123 | chisqlist = [] 124 | pvaluelist = [] 125 | for data in datalist: 126 | binCount = Bin2DCountForData(data, binsX = binsX, binsY = binsY) 127 | expectedCount = Expected2DCountForData(data, binsX = binsX, binsY = binsY) 128 | chisq,pvalue = chisquare(binCount,expectedCount) 129 | chisqlist.append(chisq) 130 | pvaluelist.append(pvalue) 131 | return sum(chisqlist)/len(chisqlist),sum(pvaluelist)/len(pvaluelist) 132 | 133 | def kldistance(datalist,bins = 10, epsilon = 0.00001): 134 | def BinProbForData(data,bins = 10): 135 | binprob,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = True) 136 | return binprob 137 | 138 | def ExpectedProbForData(data, bins = 10): 139 | expectedProb = np.ones((bins), dtype=np.float) / bins 140 | return expectedProb 141 | kl_dissum = 0.0 142 | for data in datalist: 143 | binProb = BinProbForData(data,bins=bins) + epsilon 144 | expectedProb = ExpectedProbForData(data,bins=bins) + epsilon 145 | kl_dissum += np.sum(binProb*np.log(binProb/expectedProb))/bins 146 | return kl_dissum/bins/len(datalist) 147 | 148 | def MonobitTest(datalist, alpha = 0.01): 149 | def get01Sequence(data): 150 | return [0 if e < 0.5 else 1 for e in data] 151 | 152 | passcnt = 0 153 | Sobslist = [] 154 | pvaluelist = [] 155 | for data in datalist: 156 | sequence01 = get01Sequence(data) 157 | Sn = sum([2*e-1 for e in sequence01]) 158 | Sobs = math.fabs(Sn)/math.sqrt(len(datalist)) 159 | p_value = spc.erfc(Sobs / math.sqrt(2.0)) 160 | Sobslist.append(Sobs) 161 | pvaluelist.append(p_value) 162 | #if p_value > alpha: 163 | # passcnt += 1 164 | return sum(Sobslist)/len(Sobslist),sum(pvaluelist)/len(pvaluelist) 165 | 166 | 167 | def BlockFrequency(datalist, blocksize=1000, alpha=0.01): 168 | def get01Sequence(data): 169 | return [0 if e < 0.5 else 1 for e in data] 170 | 171 | def PropotionOneInList(data): 172 | return 1.0*sum(data)/len(data) 173 | passcnt = 0 174 | for data in datalist: 175 | sequence01 = get01Sequence(data) 176 | block_num = int(len(sequence01) / blocksize) 177 | # calc chisq for all blocks 178 | chisq_obs = 0.0 179 | for i in range(block_num): 180 | pi_i = PropotionOneInList(sequence01[(i*blocksize):(i+1)*blocksize]) 181 | chisq_obs += 4.0*blocksize*(pi_i-0.5)**2 182 | pvalue = spc.gammaincc(block_num/2.0,chisq_obs/2.0) 183 | return chisq_obs,pvalue 184 | 185 | 186 | 187 | if __name__=="__main__": 188 | # MT19937 189 | #res=MT19937(autocorrelation_tests0x12341).GenerateRandomU01(N=100000) 190 | # python 191 | N=10000 192 | bins=10 193 | seed=0x123451 194 | rowname = [] 195 | data=[] 196 | res = [] 197 | #columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue","kstest-pvalue","autocor","serial2D","kl/bins","monobit","blockfreq"] 198 | columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue"] 199 | rngtypes = ['well1024'] 200 | for rngtype in rngtypes: 201 | datalist = GenerateRandom01Arr(groups=2000,N=N,seed=seed,func=rngtype) 202 | """ 203 | chisq,pvalue = ChiSquare(datalist) 204 | print('chisq') 205 | print(chisq) 206 | print(pvalue) 207 | chisq,pvalue = KSTest(datalist) 208 | print('kstest') 209 | print(chisq) 210 | print(pvalue) 211 | chisq,pvalue = Serial2DTest(datalist) 212 | print('serial 2d test') 213 | print(chisq) 214 | print(pvalue) 215 | kldis = kldistance(datalist) 216 | print('kl dis') 217 | print(kldis) 218 | Echisq,pvalue = MonobitTest(datalist) 219 | print('monobit') 220 | print(Echisq) 221 | print(pvalue) 222 | """ 223 | Echisq,pvalue = BlockFrequency(datalist) 224 | print(Echisq) 225 | print(pvalue) 226 | #Echisq,Epvlue = ChiSquare(datalist,bins=bins,confidence=0.8) 227 | -------------------------------------------------------------------------------- /autocor.py: -------------------------------------------------------------------------------- 1 | from RandomTestModule import * 2 | from RandomGenerator import * 3 | import warnings 4 | import numpy as np 5 | from itertools import islice 6 | from scipy.stats import chisquare 7 | from scipy.stats import kstest 8 | from scipy.stats import norm 9 | import scipy.special as spc 10 | import math 11 | import warnings 12 | warnings.filterwarnings("ignore", message="numpy.dtype size changed") 13 | 14 | def GeneratemdFile(columnName = [], rowname = [], content = [], outputpath = None): 15 | with open(outputpath,'w') as f: 16 | f.write('|'.join(columnName)+'\n') 17 | f.write('|'.join(['---' for i in range(len(columnName))])+'\n') 18 | for c,e in zip(rowname,content): 19 | temp = [c]+e 20 | f.write('|'.join(temp)+'\n') 21 | return 22 | 23 | 24 | def FromData2Index(datalist=None,bins=10,params=None): 25 | rndtest = randomtest(datalist=datalist) 26 | index = rndtest.GeneratemdTable(bins=bins,params = params) 27 | return ["%.5f"%e if type(e) != str else e for e in index] 28 | 29 | def GetRngDpFunc(func='mt19937',seed=0): 30 | rng = MT19937(0) 31 | if func == 'mt19937': 32 | rng = MT19937(seed) 33 | elif func == 'pythonsys': 34 | rng = pythonsytem(seed) 35 | elif func == 'well1024': 36 | rng = Well1024a(seed) 37 | elif func == 'xorshift': 38 | rng = xorshift1024(seed) 39 | elif func == 'ThreeFry': 40 | rng = MyThreeFry(seed) 41 | elif func == 'LCG64': 42 | rng = MyPCG64(seed) 43 | return rng 44 | 45 | def GenerateRandom01Arr(groups = 5, N = 10000, seed=0, func='mt19937'): 46 | seedlist = PrimeLessThanN(100000) 47 | #seedlist = [i for i in range(groups)] 48 | #print(len(seedlist)) 49 | seedlist = seedlist[-groups:] 50 | #print(len(seedlist)) 51 | #seedlist = [13,17,19,23,29] 52 | res = [] 53 | for i in range(groups): 54 | rng = GetRngDpFunc(func=func,seed=seedlist[i]) 55 | res.append(rng.GenerateRandomU01(N=N)) 56 | return res 57 | 58 | def PrimeLessThanN(N): 59 | ls = [True for i in range(N+1)] 60 | for i in range(2,N,1): 61 | if ls[i] is not True: 62 | continue 63 | for j in range(2*i,N+1,i): 64 | ls[j] = False 65 | res = [] 66 | for i in range(2,N+1,1): 67 | if ls[i] == True: 68 | res.append(i) 69 | return res 70 | 71 | 72 | def ChiSquare(datalist, bins=10, confidence = 0.8): 73 | def BinCountForData(data,bins = 10): 74 | bincount,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = False) 75 | return bincount 76 | 77 | def ExpectedCountForData(data,bins = 10): 78 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / bins 79 | return expectedcount 80 | # uniformity 81 | # p-value is the probability of assert H0 is true 82 | # H0 hypethesis: bincount Obey expected distribution 83 | chisqlist = [] 84 | res = [] 85 | bincountSum = np.zeros((bins)) 86 | for data in datalist: 87 | bincount = BinCountForData(data,bins=bins) 88 | expectedcount = ExpectedCountForData(data,bins=bins) 89 | chisq,pvalue = chisquare(bincount,expectedcount) 90 | chisqlist.append(chisq) 91 | res.append(pvalue) 92 | return sum(chisqlist)/len(datalist),sum(res)/len(datalist) 93 | 94 | def KSTest(datalist): 95 | passcnt = 0 96 | chisqlist = [] 97 | pvaluelist = [] 98 | for data in datalist: 99 | chisq,pvalue = kstest(data,'uniform',args=(0,1)) 100 | 101 | chisqlist.append(chisq) 102 | pvaluelist.append(pvalue) 103 | #if pvalue > alpha: 104 | # passcnt += 1 105 | return sum(chisqlist)/len(datalist),sum(pvaluelist)/len(datalist) 106 | 107 | def Serial2DTest(datalist, binsX = 10, binsY = 10, alpha = 0.8): 108 | def Bin2DCountForData(data,binsX = 10,binsY = 10): 109 | oddlist = data[::2] 110 | evenlist = data[1::2] 111 | H,xedges,yedges = np.histogram2d(oddlist,evenlist,bins=[binsX,binsY],range=[[0.0,1.0],[0.0,1.0]]) 112 | return H.ravel() 113 | 114 | def Expected2DCountForData(data, binsX = 10, binsY = 10): 115 | bins = binsX * binsY 116 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / 2 / bins 117 | return expectedcount 118 | 119 | passcnt = 0 120 | chisqlist = [] 121 | pvaluelist = [] 122 | for data in datalist: 123 | binCount = Bin2DCountForData(data, binsX = binsX, binsY = binsY) 124 | expectedCount = Expected2DCountForData(data, binsX = binsX, binsY = binsY) 125 | chisq,pvalue = chisquare(binCount,expectedCount) 126 | chisqlist.append(chisq) 127 | pvaluelist.append(pvalue) 128 | return sum(chisqlist)/len(chisqlist),sum(pvaluelist)/len(pvaluelist) 129 | 130 | def kldistance(datalist,bins = 10, epsilon = 0.00001): 131 | def BinProbForData(data,bins = 10): 132 | binprob,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = True) 133 | return binprob 134 | 135 | def ExpectedProbForData(data, bins = 10): 136 | expectedProb = np.ones((bins), dtype=np.float) / bins 137 | return expectedProb 138 | kl_dissum = 0.0 139 | for data in datalist: 140 | binProb = BinProbForData(data,bins=bins) + epsilon 141 | expectedProb = ExpectedProbForData(data,bins=bins) + epsilon 142 | kl_dissum += np.sum(binProb*np.log(binProb/expectedProb))/bins 143 | return kl_dissum/bins/len(datalist) 144 | 145 | def MonobitTest(datalist, alpha = 0.01): 146 | def get01Sequence(data): 147 | return [0 if e < 0.5 else 1 for e in data] 148 | 149 | passcnt = 0 150 | Sobslist = [] 151 | pvaluelist = [] 152 | for data in datalist: 153 | sequence01 = get01Sequence(data) 154 | Sn = sum([2*e-1 for e in sequence01]) 155 | Sobs = math.fabs(Sn)/math.sqrt(len(datalist)) 156 | p_value = spc.erfc(Sobs / math.sqrt(2.0)) 157 | Sobslist.append(Sobs) 158 | pvaluelist.append(p_value) 159 | #if p_value > alpha: 160 | # passcnt += 1 161 | return sum(Sobslist)/len(Sobslist),sum(pvaluelist)/len(pvaluelist) 162 | 163 | 164 | def BlockFrequency(datalist, blocksize=1000, alpha=0.01): 165 | def get01Sequence(data): 166 | return [0 if e < 0.5 else 1 for e in data] 167 | 168 | def PropotionOneInList(data): 169 | return 1.0*sum(data)/len(data) 170 | passcnt = 0 171 | for data in datalist: 172 | sequence01 = get01Sequence(data) 173 | block_num = int(len(sequence01) / blocksize) 174 | # calc chisq for all blocks 175 | chisq_obs = 0.0 176 | for i in range(block_num): 177 | pi_i = PropotionOneInList(sequence01[(i*blocksize):(i+1)*blocksize]) 178 | chisq_obs += 4.0*blocksize*(pi_i-0.5)**2 179 | pvalue = spc.gammaincc(block_num/2.0,chisq_obs/2.0) 180 | return chisq_obs,pvalue 181 | 182 | def AutoCorTest(datalist, lagklist = [], confidence = 0.8): 183 | def AutoCovariance(data,lagk = 10): 184 | Rksum = 0.0 185 | for i in range(len(data)-lagk): 186 | Rksum += (data[i]-0.5)*(data[i+lagk]-0.5) 187 | Rk = Rksum/(len(data)-lagk) 188 | return Rk 189 | passcnt = 0 190 | assert confidence <= 1.0 and confidence > 0.5 191 | alpha = 1-confidence 192 | x0 = norm(0,1).ppf(1-alpha/2) 193 | for e in lagklist: 194 | for data in datalist: 195 | Rk = AutoCovariance(data,lagk=e) 196 | if Rk < x0: 197 | passcnt+=1 198 | return 1.0*passcnt/len(lagklist)/len(datalist) 199 | 200 | 201 | 202 | 203 | if __name__=="__main__": 204 | # MT19937 205 | #res=MT19937(autocorrelation_tests0x12341).GenerateRandomU01(N=100000) 206 | # python 207 | N=10000 208 | bins=10 209 | seed=0x123451 210 | rowname = [] 211 | data=[] 212 | res = [] 213 | #columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue","kstest-pvalue","autocor","serial2D","kl/bins","monobit","blockfreq"] 214 | columnName = ["N=%d,seed=%d"%(N,seed),"chisquare-pvalue"] 215 | rngtypes = ['pythonsys','well1024','xorshift','ThreeFry','LCG64'] 216 | for rngtype in rngtypes: 217 | datalist = GenerateRandom01Arr(groups=2000,N=N,seed=seed,func=rngtype) 218 | passratio = AutoCorTest(datalist,lagklist=[5,7,11,13,17,23,25,50,100,200,500]) 219 | print(rngtype) 220 | print(passratio) 221 | #Echisq,Epvlue = ChiSquare(datalist,bins=bins,confidence=0.8) 222 | -------------------------------------------------------------------------------- /RandomTestModule.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from itertools import islice 3 | from scipy.stats import chisquare 4 | from scipy.stats import kstest 5 | from scipy.stats import norm 6 | import scipy.special as spc 7 | import math 8 | import warnings 9 | warnings.filterwarnings("ignore",message="numpy.dtype size changed") 10 | 11 | """ 12 | all metric: 13 | 14 | """ 15 | 16 | class randomtest: 17 | """ 18 | chi-square: 19 | Most commonly used test 20 | Can be used for any distribution 21 | It's none fo bin k 22 | prepare a hist for the obeserved data 23 | compare obeseved frequencies with theoretical one 24 | D = \sum_{i=1}^k\frac{(o_i-e_i)^2}{e_i} 25 | D = 0 => Exact fit 26 | D has a chi-square distribution with k-1 degrees of freedom 27 | 28 | """ 29 | def __init__(self,datalist = None): 30 | self.datalist = datalist 31 | self.groups = len(datalist) 32 | # every datapoint must belong to [0.0,1.0) 33 | assert self.isUniform() is True 34 | 35 | def isUniform(self): 36 | for e in self.datalist: 37 | for f in e: 38 | if f < 0.0 or f >= 1.0: 39 | return False 40 | return True 41 | 42 | """ 43 | The below 3 function implemented for Chi-square Test 44 | Most commonly ,so ... 45 | """ 46 | 47 | 48 | def ChiSquare(self, bins=10, confidence = 0.8): 49 | def BinCountForData(data,bins = 10): 50 | bincount,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = False) 51 | return bincount 52 | 53 | def ExpectedCountForData(data,bins = 10): 54 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / bins 55 | return expectedcount 56 | # uniformity 57 | # p-value is the probability of assert H0 is true 58 | # H0 hypethesis: bincount Obey expected distribution 59 | res = [] 60 | passcnt = 0 61 | for data in self.datalist: 62 | bincount = BinCountForData(data,bins=bins) 63 | expectedcount = ExpectedCountForData(data,bins=bins) 64 | chisq,pvalue = chisquare(bincount,expectedcount) 65 | #print(bincount) 66 | #print(expectedcount) 67 | res.append(chisq) 68 | if pvalue > confidence: 69 | passcnt += 1 70 | #print(bincount) 71 | #print(expectedcount) 72 | # sum([(a-e)**2/e for a,e in zip(list(bincount), list(expectedcount))]) 73 | return 1.0*passcnt/len(self.datalist),res 74 | 75 | """ 76 | the below function implemented for Kolmogorov-Smirnov Test 77 | K^{+} is maximum .. 78 | transfer data to cdf compared with uniform/u(0,1) 79 | """ 80 | def KSTest(self, alpha): 81 | passcnt = 0 82 | for data in self.datalist: 83 | chisq,pvalue = kstest(data,'uniform',args=(0,1)) 84 | if pvalue > alpha: 85 | passcnt += 1 86 | return 1.0*passcnt/len(self.datalist) 87 | 88 | """ 89 | the below 2 function implemented for Serial-Correlation Test 90 | Rk = Autocovariance at lag 91 | R_k = \frac{1}{n-k}\sum_{i=1}^{n-k}(U_i-0.5)(U_{i+k}-0.5) 92 | R_k is normally distributed with a mean of 0 and a variance of 1/(144*(n-k)) 93 | when all Rk less than z_{1-\alpha/2}/(12*\sqrt{n-k}) 94 | """ 95 | 96 | 97 | def AutoCorTest(self, lagklist = [], confidence = 0.8): 98 | def AutoCovariance(data,lagk = 10): 99 | Rksum = 0.0 100 | for i in range(len(data)-lagk): 101 | Rksum += (data[i]-0.5)*(data[i+lagk]-0.5) 102 | Rk = Rksum/(len(data)-lagk) 103 | return Rk 104 | passcnt = 0 105 | assert confidence <= 1.0 and confidence > 0.5 106 | alpha = 1-confidence 107 | x0 = norm(0,1).ppf(1-alpha/2) 108 | for e in lagklist: 109 | for data in self.datalist: 110 | Rk = AutoCovariance(lagk=e) 111 | if Rk < x0: 112 | passcnt+=1 113 | return 1.0/passcnt/len(lagklist)/len(self.datalist) 114 | 115 | """ 116 | Two level Test, 117 | when teh sample size is too small, the test results may apply to locally, but not 118 | globally, to the complete cycle. 119 | Use Chi-square test on n samples of size k each and use a chi-square test on \\ 120 | the set of n Chi-square statistics so obtained 121 | Chi-square on Chi-square test 122 | """ 123 | def TwoLevelTest(self): 124 | return 125 | 126 | #def kdistributivity(self): 127 | # return 128 | 129 | """ 130 | the below 3 function implemented for serial test 131 | the odd element as X-Axis, the even element as Y-Axis 132 | In 2D dimensions, divide the space between 0 and 1 into K^2 cell of equal area 133 | obey to the chi-square distribution with the freedom of K^2-1 134 | """ 135 | 136 | 137 | def Serial2DTest(self, binsX = 10, binsY = 10, alpha = 0.8): 138 | def Bin2DCountForData(data,binsX = 10,binsY = 10): 139 | oddlist = data[::2] 140 | evenlist = data[1::2] 141 | H,xedges,yedges = np.histogram2d(oddlist,evenlist,bins=[binsX,binsY],range=[[0.0,1.0],[0.0,1.0]]) 142 | return H.ravel() 143 | 144 | def Expected2DCountForData(data, binsX = 10, binsY = 10): 145 | bins = binsX * binsY 146 | expectedcount = np.ones((bins), dtype=np.float) * len(data) / 2 / bins 147 | return expectedcount 148 | 149 | passcnt = 0 150 | for data in self.datalist: 151 | binCount = Bin2DCountForData(data, binsX = binsX, binsY = binsY) 152 | expectedCount = Expected2DCountForData(data, binsX = binsX, binsY = binsY) 153 | chisq,pvalue = chisquare(binCount,expectedCount) 154 | if pvalue >= alpha: 155 | passcnt += 1 156 | return 1.0*passcnt/len(self.datalist) 157 | 158 | """ 159 | the below function implemented for 160 | maybe every lcgs has some hyperplane. 161 | necessary? 162 | """ 163 | #def SpectralTest(self): 164 | # return 165 | 166 | """ 167 | def autocorrelationForStm(self, st=0, m=5): 168 | # Independence 169 | # refer to http://www.aritzhaupt.com/resource/autocorrelation/ 170 | # z_score and norm? 171 | N = len(self.datalist) 172 | M = int((N-1-st+0.5)/m) 173 | # Ri,Ri+m,... 174 | Rm1 = self.datalist[0::m] 175 | rho_im = sum([e*f for e,f in zip(Rm1[0:M],Rm1[1:M+1])])/(M+1) - 0.25 176 | sigma_rhoim = np.sqrt(13*M+7)/(12*(M+1)) 177 | z_0 = rho_im / sigma_rhoim 178 | return z_0 179 | def autocorrelation_test(self): 180 | maxzvalue = 0.0 181 | for m in range(1,50,1): 182 | thiszvalue = self.autocorrelationForStm(st=0,m=m) 183 | if math.fabs(thiszvalue) > maxzvalue: 184 | maxzvalue = math.fabs(thiszvalue) 185 | return maxzvalue 186 | """ 187 | 188 | """ 189 | The below function is implemented for kl distance divided by bins 190 | kl_distance(binprob,expectedprob)/bins 191 | """ 192 | 193 | 194 | def kldistance(self,bins = 10, epsilon = 0.00001): 195 | def BinProbForData(data,bins = 10): 196 | binprob,temp= np.histogram(data, bins = bins, range = (0.0,1.0), density = True) 197 | return binprob 198 | 199 | def ExpectedProbForData(data, bins = 10): 200 | expectedProb = np.ones((bins), dtype=np.float) / bins 201 | return expectedProb 202 | kl_dissum = 0.0 203 | for data in self.datalist: 204 | binProb = BinProbForData(data,bins=bins) + epsilon 205 | expectedProb = ExpectedProbForData(data,bins=bins) + epsilon 206 | kl_dissum += np.sum(binProb*np.log(binProb/expectedProb))/bins 207 | return kl_dissum/bins/len(self.datalist) 208 | 209 | """ 210 | The below code implemented nist test codes 211 | monobit Test 212 | """ 213 | def MonobitTest(self, alpha = 0.01): 214 | def get01Sequence(data): 215 | return [0 if e < 0.5 else 1 for e in data] 216 | 217 | passcnt = 0 218 | for data in self.datalist: 219 | sequence01 = get01Sequence(data) 220 | Sn = sum([2*e-1 for e in sequence01]) 221 | Sobs = math.fabs(Sn)/math.sqrt(len(self.datalist)) 222 | p_value = spc.erfc(Sobs / math.sqrt(2.0)) 223 | if p_value > alpha: 224 | passcnt += 1 225 | return 1.0*passcnt/len(self.datalist) 226 | 227 | """ 228 | Block frequency 229 | """ 230 | def BlockFrequency(self, blocksize=1000, alpha=0.01): 231 | def get01Sequence(data): 232 | return [0 if e < 0.5 else 1 for e in data] 233 | 234 | def PropotionOneInList(data): 235 | return 1.0*sum(data)/len(data) 236 | passcnt = 0 237 | for data in self.datalist: 238 | sequence01 = get01Sequence(data) 239 | block_num = int(len(sequence01) / blocksize) 240 | # calc chisq for all blocks 241 | chisq_obs = 0.0 242 | for i in range(block_num): 243 | pi_i = PropotionOneInList(sequence01[(i*blocksize):(i+1)*blocksize]) 244 | chisq_obs += 4.0*blocksize*(pi_i-0.5)**2 245 | pvalue = spc.gammaincc(block_num/2.0,chisq_obs/2.0) 246 | passcnt = 0 247 | if pvalue > alpha: 248 | passcnt += 1 249 | return 1.0*passcnt/len(self.datalist) 250 | 251 | """ 252 | runtest 253 | """ 254 | def RunsTest(self): 255 | def get01Sequence(data): 256 | return [0 if e < 0.5 else 1 for e in data] 257 | chisq_obslist = [] 258 | pvaluelist = [] 259 | for data in self.datalist: 260 | seq01 = get01Sequence(data) 261 | n = len(seq01) 262 | pi = 1.0*sum(seq01)/len(seq01) 263 | chisq_obs = sum([0 if e == f else 1 for e,f in zip(seq01[0:len(seq01)-1],seq01[1:])]) + 1 264 | pvalue = spc.erfc(math.fabs(chisq_obs-2*n*pi*(1-pi))/(2*pi*(1-pi)*np.sqrt(2*n))) 265 | chisq_obslist.append(chisq_obs) 266 | pvaluelist.append(pvalue) 267 | return sum(chisq_obslist)/len(chisq_obslist),sum(pvaluelist)/len(pvaluelist) 268 | 269 | """ 270 | CumultiveSumsTest 271 | """ 272 | """ 273 | def CumulativeSumsTest(self): 274 | def GetNPSequence(data): 275 | return [-1 if e < 0.5 else 1 for e in data] 276 | 277 | def ForwardMaxCumlativeSum(data): 278 | seq01 = ForwardMaxCumlativeSum(data) 279 | lastmax = 0 280 | lastsum = 0 281 | for e in seq01: 282 | lastsum += e 283 | if math.fabs(lastsum) > lastmax: 284 | lastmax = math.fabs(lastsum) 285 | return lastmax 286 | for data in self.datalist: 287 | Sobs = max(ForwardMaxCumlativeSum(data),ForwardMaxCumlativeSum(reversed(data))) 288 | """ 289 | 290 | 291 | def GeneratemdTable(self,bins=10,params=[]): 292 | res = [] 293 | chisq,pvalue = self.ChiSquare(bins=bins) 294 | #if "chisquare" in params: 295 | # res.append(chisq) 296 | if "chisquare-pvalue" in params: 297 | res.append(pvalue) 298 | ks,pvalue = self.KSTest() 299 | #if "kstest" in params: 300 | # res.append(ks) 301 | if "kstest-pvalue" in params: 302 | res.append(pvalue) 303 | passornot,passcnt = self.AutoCorTest(lagklist = [5,8,10,15,20,30,60], confidence = 0.9) 304 | if "autocor" in params: 305 | res.append(passornot) 306 | 307 | chisq,pvalue = self.Serial2DTest(binsX = 10, binsY = 10) 308 | if "serial2D" in params: 309 | res.append(pvalue) 310 | 311 | kldis = self.kldistance(bins=bins) 312 | if "kl/bins" in params: 313 | res.append(kldis) 314 | 315 | passornot = self.MonobitTest(alpha=0.01) 316 | if "monobit" in params: 317 | res.append('Pass' if passornot else 'Failed') 318 | 319 | pvalue = self.BlockFrequency(blocksize=100) 320 | if "blockfreq" in params: 321 | res.append(pvalue) 322 | return res 323 | --------------------------------------------------------------------------------