RANTEST: Two_mean_simulation


In [1]:
import math
import random
import numpy as np
import scipy.stats as stats

In [2]:
#START INPUTS
nsim = 10000  #number of re-amplings to run
#define the two samples
n = 10
sampA = np.array([0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2])
sampB = np.array([1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4])
#END OF INPUTS

In [3]:
meanA = np.mean(sampA)
meanB = np.mean(sampB)
obsdif = meanA - meanB
sdA = np.std(sampA)
sdB = np.std(sampB)

In [4]:
# Do t test
t, p = stats.ttest_ind(sampA, sampB, equal_var=False, nan_policy='omit')
#low, high = stats.t.interval(0.95, len(sd)-1, loc=np.mean(sd), scale=stats.sem(sd))

In [5]:
#put all obs into one array
allobs = np.concatenate((sampA, sampB))
totobs = np.sum(allobs)

In [6]:
#random.seed(1984)
difmean = np.zeros(nsim)
for i in range(nsim):
    random.shuffle(allobs)
    sumB = sum(allobs[n : ])
    difmean[i] = (totobs - sumB) / float(n) - sumB / float(n)
nranlo = difmean[np.fabs(difmean) <= math.fabs(obsdif)].size
nranhi = difmean[np.fabs(difmean) >= -math.fabs(obsdif)].size
print(nranlo, nranhi)


9195 10000