80def power_of_test(data1, data2, rvs_func = 'rvs_pairs', tests = ['chi2_2samp'], rvs_key = {}, test_key = {}, parallel=None, sync=True):
81 """Compute the corresponding p-values for each histrogram pairs from the random variates of the given 2 samples/frequencies for size_times.
82
83 Parameters
84 ----------
85 data1, data2 : sequence of 1-D ndarrays
86 Input data. Observed samples or frequencies.
87 rvs_func : [callable|str], optional, default : "rvs_pairs"
88 The random variates function. The rvs_func can be either a callable or one of the following strings::
89
90 String Description
91 "rvs_pairs" Compute the histogram pairs from the random
92 variates of the given 2 samples/frequencies
93 for size_times.
94
95 tests : ([callable|str],...), optional, default : ["chi2_2samp"]
96 A list of *test* statistical functions. The *test* can be either a callable or one of the following strings::
97
98 String Description
99 "chi2_2samp" Read TS.chi2_2samp for further information.
100 "BDM_2samp" Read TS.BDM_2samp for further information.
101 "likelihoodratio_ksamp" Read TS.likelihoodratio_ksamp for further information.
102 "likelihoodvalue_ksamp" Read TS.likelihoodvalue_ksamp for further information.
103 "ks_2samp" Read TS.ks_2samp for further information.
104 "anderson_ksamp" Read TS.anderson_ksamp for further information.
105 "CVM_2samp" Read TS.CVM_2samp for further information.
106
107 rvs_key : dict, optional, default : {}
108 Keyword arguments for the rvs function, rvs_func.
109 test_key : dict, optional
110 Keyword arguments for the test statistical function, test.
111 parallel : bool, optional, default : None
112 If True, import IPyParallel package to do the parallel computation. If parallel is None,
113 the global variable PARALLEL will be used instead.
114 sync : bool, optional, default : True
115 When sync is False, an IPyParallel AsyncResult Object will be returned instead. Onyl affect
116 when parallel is True.
117
118 Returns
119 -------
120 [p1, p2, ...] : 1-D array
121 The corresponding p-values for each histogram pairs.
122 """
123 if parallel is None: parallel = PARALLEL
124 if parallel:
125 try:
126 global client
127 client=Client(**ipp_profile)
128 size = rvs_key['size']
129 N = len(client)
130 jobs = []
131 for i in range(N):
132 rvs_key['size'] = (size//N + 1) if (i < size % N) else size//N
133 jobs.append(client[client.ids[i]].apply_async(power_of_test, data1, data2, rvs_func, tests, rvs_key, test_key, False))
134 ars = client._asyncresult_from_jobs(jobs)
135 if sync:
136 ars.wait_interactive()
137 ret = {}
138 for key, val in ars.get():
139 ret.setdefault(key, []).extend(val)
140 else:
141 return ars
142 finally:
143 client.close()
144 return ret
145 if type(rvs_func)
is str:
146 rvs_func = globals()[rvs_func]
147 if type(tests)
not in (list, tuple):
148 tests = [tests]
149 tests = [(t, getattr(TS, t))
if type(t)==str
else (str(t), t)
for t
in tests]
150 ret = {}
151 for rvs1, rvs2 in rvs_func(data1,data2,**rvs_key):
152 for tname, test in tests:
153 ret.setdefault(tname, []).append(test(rvs1, rvs2, binned=True, **test_key).pvalue)
154 return ret