2
$\begingroup$

Let's say I have 2 series of data:

  • series A has 1M samples, out of which 1000 are "1", the rest is 0
  • series B has 100 samples, all 0s

I'm trying to use a statistical test to check if boths series have same mean or not. To me, these series could have the same mean (ie 0.001).

Using scipy:

from scipy import stats
A = [0.0]*100
B = [1.0]*1000 + [0.0]*(1000000-1000)
scipy.stats.ttest_ind(A,B, equal_var=False)

Returns a pvalue=1.399064173683964e-219, which means that both series are different. It seems that the size of A is not taken into account, ie I get the same results if I switch A to be 1 million zeroes.

Is this expected? To me, this p-value is inacurate. Have I made an incorrect assumption somewhere?

$\endgroup$
3
  • $\begingroup$ To me they would be different. Have you tried manually calculating the T-Statistic? I did and got a value of approx. 31.63, which is of course larger that the critical value of 1.96, and you reject the null hypothesis (assuming that your null hypothesis is that the means are equal). $\endgroup$
    – Caio C.
    Oct 29, 2020 at 13:46
  • $\begingroup$ Please post your code and result when you "switch A to be 1 million zeroes". // pvalue=1.399064173683964e-219 is the computer's way of telling you that the p-value is basically zero. I wouldn't take too literally the numerical integration of that small of an area. $\endgroup$
    – Dave
    Oct 29, 2020 at 14:22
  • $\begingroup$ The t test is not applicable to these data: they manifestly do not meet even the broadest assumptions required for this p-value to be meaningful. $\endgroup$
    – whuber
    Jan 7, 2022 at 19:03

2 Answers 2

5
$\begingroup$

Student's t-test is based on the normality of the data. You have manually generated binary data and I think it cannot be assumed they are normal.

You should test the probability of being 0 or 1, instead:

$$H_0: p_A = p_B, \text{ } H_A:p_a \neq p_B$$

You can estimate the probability of being 1 in the sample A and B as

$$\hat{p}_A = \frac{\text{number of ones}}{\text{sample size}} = 0$$

$$\hat{p}_B = \frac{\text{number of ones}}{\text{sample size}} = 0.001$$

Using the Moivre-Laplace central limit theorem you get

$$\frac{\hat{p}_A - \hat{p}_B}{\sqrt{\frac{\hat{p}_A (1 - \hat{p}_A)}{n_A} + \frac{\hat{p}_B (1 - \hat{p}_B)}{n_B}}}$$

is approximately $N(0,1)$ standard normal ($n_A$ and $n_B$ are the corresponding sample sizes).

So calculating the above test statistic, if it is greater than $1.96$ or less than $-1.96$, you should reject $H_0$ at significance level $0.05$ (the calculation left as an exercise to the reader :-) ).

$\endgroup$
5
  • $\begingroup$ thanks ! I think indeed this is why the results I observe with my t-test make no sense, the assumption that I "broke" in my example is probably due to non-normality $\endgroup$
    – lezebulon
    Oct 29, 2020 at 16:49
  • 1
    $\begingroup$ @lezebulon I deleted my answer because this answer has pointed out the biggest problem in the assumptions. The non-normality problem will affect the series with 100 zeros much more than that with 1M elements. Though it is worth noting the test statistic the Moivre-Laplace CLT gives is the same the t-test statistic you have calculated, and you will still get a virtually zero p-value. $\endgroup$
    – B.Liu
    Oct 29, 2020 at 18:17
  • $\begingroup$ @B.Liu thanks! Indeed if i compute the statistic it still give me something like 31, so much higher than 1.6 In that case I'm not sure exactly why we reject the null hypothesis when to me, we can't $\endgroup$
    – lezebulon
    Oct 30, 2020 at 9:21
  • $\begingroup$ @lezebulon, why dou you think we can't reject the null hypothesis? In the sense of the test, a difference of 0.001 can be a statistically significant difference. $\endgroup$
    – jumpini
    Oct 30, 2020 at 10:30
  • $\begingroup$ @jumpini as I explained, this is because series A has only 100 points. With a proba of 0.001 of having a 1, there's basically 90% chance that series A contains only zeroes. So in that sense, the estimation of pA based on the data is completely incorrect (ie pA being 0 instead of 0.001). I thought the test would account for this, but apparently not. I still dont really understand why all the tests confidently conclude, when A and B could have been generated by the same random process $\endgroup$
    – lezebulon
    Oct 30, 2020 at 13:15
2
$\begingroup$

Assuming $$H_0: \mu_1 = \mu_2, H_A: \mu_1 \neq \mu_2 $$ and $\alpha = 0.05$.

The t-statistics is calculated by: $$t_{obs} = \frac{\hat{\mu_1}- \hat{\mu_2}}{\sqrt{\frac{s_1^2}{n_1}+\frac{s_2^2}{n_2}}}$$

In your example, let's calculate the variances: $$\sigma^2_1 = p(1-p)=0.001(0.999) = 0.000999$$ $$\sigma^2_2 = 0$$

Therefore, the t-statistic would be: $$t_{obs} = \frac{0.001- 0}{\sqrt{\frac{0.000999}{1000000}+\frac{0}{100}}} \approx 31.64$$

(Notice that, because you know the variance, this is actually a z-test because $s^2 = \sigma^2$).

Because you have a two-sided test, your critical region is: $$CR = [t < -1.96; t>1.96]$$ Therefore, you reject the null hypothesis and you can't say that the means are equal.

$\endgroup$
1
  • $\begingroup$ Thanks I think I understand the computation, which indeed are correct in scipy and in your post. What I dont understand is why this test rejects the null hypothesis, when for instance here both my data series could have been generated by the same distribution of having a proba of being 1 be 0.001 $\endgroup$
    – lezebulon
    Oct 29, 2020 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.