# How to derive t-test statistics for one-sample and independent tests?

I know that I can compute t-statistic as following.

for one-sample T-Test:

$$t = \frac{E(x) - \mu}{\sigma / \sqrt{(n)}}$$

for independent sample T-Test:

$$t =\frac{E(x_1) - E(x_2)}{\sqrt{\sigma_1 / (n_1) + \sigma_2 / (n_2)}}$$

But I can't understand how we obtain denominators for both equations.

The only source I found is here but it is not clear enough, at least for me. https://www.math.arizona.edu/~jwatkins/ttest.pdf

Could you explain how we can derive these equations?

Also, there is this answer but it does not explain much. Denominator in t Test formula for 2 independent samples

It is still not clear how we got this expression there:

$$\operatorname{var}(\bar X-\bar Y)=\operatorname{var}(\bar X)+\operatorname{var}(\bar Y)=\frac{\sigma_x^2}{n}+\frac{\sigma_y^2}{m}\approx \frac{S_x^2}{n}+\frac{S_y^2}{m}$$

I would discuss only the $$t$$ test for difference of means, which was touched in this post too.

Let $$\mathcal H_0:\mu_X-\mu_Y=\Delta.$$ Assumption is that variances are equal.

Define $$T:=\frac{Z}{\sqrt{\frac {\chi^2}{m+n-2}}}\tag 1\label 1$$ where $$Z:=\frac{(\bar x-\bar y) -\mathbb E(\bar x-\bar y) }{\sqrt{\operatorname{Var}(\bar x -\bar y) }}\sim\mathrm N(0,1)$$ and $$\chi^2:=\frac{ms_X^2}{\sigma^2}+\frac{ns_Y^2}{\sigma^2}\sim\chi^2_{m+n-2}.$$

Since the samples are independent and variances are equal by assumption, $$\operatorname{Var}(\bar x-\bar y) =\sigma^2\left(\frac1m +\frac1n\right)$$

Substituting in $$\eqref 1$$ under $$\mathcal H_0,$$

\begin{align}T&= \frac{Z}{\sqrt{\frac {\chi^2}{m+n-2}}}\\&= \frac{(\bar x-\bar y) -\Delta}{\sigma\sqrt{\left(\frac1m +\frac1n\right)}}\times \frac1{\sqrt{\left[\frac1{m+n-2}\left(\frac{ms_X^2}{\sigma^2}+\frac{ns_Y^2}{\sigma^2}\right)\right]}}\\&= \frac{(\bar x-\bar y) -\Delta}{ S\sqrt{\left(\frac1m+\frac1n\right)}}\\&\sim\mathrm t_{m+n-2},\tag 2\label 2\end{align}

where $$S^2:=\frac{\sum_i(x_i-\bar x)^2+\sum_j(y_j-\bar y) ^2}{m+n-2}.$$

Now $$\eqref 2$$ is based on the assumption of equal variance; if this is not, then the statistic $$T=\frac{(\bar x-\bar y) -\Delta}{ \sqrt{\left(\frac{S_1^2}m+\frac{S_2^2}n\right)}}$$ can be used in Welch approximate $$t$$ test.