python实现堆栈_python - Bootstrap方法Python实现 - 堆栈内存溢出

如果我理解正确的话,应该这样做。

from itertools import product

import numpy as np

# Generate fake data.

keys = np.arange(100, 130)

populations = {}

samples = {}

for k in keys:

loc = np.random.uniform(-9.0, +9.0)

scale = np.random.uniform(0.4, 4.0)

n = np.random.randint(400, 800)

m = np.random.randint(20, 100)

populations[k] = np.random.normal(loc, scale, n)

samples[k] = np.random.choice(populations[k], m, replace=False)

print('data: key={} pop={} samp={}'.format(k, len(populations[k]), len(samples[k])))

def ttest_(d, p, n=1000):

result = {}

percentiles = (np.arange(n) + 0.5) / n

for k, (pop, sample) in d.items():

size_sample = len(sample)

mean_sample = np.mean(sample)

sd_sample = np.std(sample, ddof=1)

# Generate a distribution of t values.

tvalues = np.zeros(n)

for i in range(n):

sample2 = np.random.choice(pop, size=size_sample, replace=True)

size_sample2 = len(sample2)

mean_sample2 = np.mean(sample2)

sd_sample2 = np.std(sample2, ddof=1)

# Welch's t-test for sample and sample2.

tvalues[i] = (mean_sample - mean_sample2) / \

np.sqrt((sd_sample / np.sqrt(size_sample))**2 +

(sd_sample2 / np.sqrt(size_sample2))**2)

# Interpolate the quantile function at p.

tvalues.sort()

result[k] = round(np.interp(p, percentiles, tvalues), 2)

return result

pairs = {}

for (k, v), (k2, v2) in product(populations.items(), samples.items()):

if k == k2:

pairs[k] = (v, v2)

result = ttest_(pairs, p=0.5)

for k, v in result.items():

print('result: key={} t={}'.format(k, v))

你可能感兴趣的:(python实现堆栈)