Two-Sample t-Test
Calculate a two-sample t-test for a difference in mean column between every unordered pair of tickers in the Toolkit instance.
The test checks the null hypothesis that two samples come from distributions with the same mean, against the two-sided alternative that the means differ. Applied to returns it answers whether one asset has had a genuinely different average return than another, or whether the difference could be down to chance. By default this is Welch’s t-test, which does not assume the two tickers have the same variance.
The formula is as follows:
\[\text{Welch (the default):}\;\; t = (\text{mean}_{a} - \text{mean}_{b}) / \sqrt{s_{a} ^{2} / n_{a} + s_{b} ^{2} / n_{b}}\] \[\text{Welch-Satterthwaite:}\;\; \text{df} = (s_{a} ^{2} / n_{a} + s_{b} ^{2} / n_{b}) ^{2} / ((s_{a} ^{2} / n_{a}) ^{2} / (n_{a} - 1) + (s_{b} ^{2} / n_{b}) ^{2} / (n_{b} - 1))\] \[\text{Student} (\text{equal\_variance} = \text{True}) \text{:} t = (\text{mean}_{a} - \text{mean}_{b}) / (s_{p} \cdot \sqrt{1 / n_{a} + 1 / n_{b}}),\;\; \text{with}\; s_{p} ^{2} = ((n_{a} - 1) \cdot s_{a} ^{2} + (n_{b} - 1) \cdot s_{b} ^{2}) / (n_{a} + n_{b} - 2) \text{and df} = n_{a} + n_{b} - 2\] \[s_{a} ^{2} \text{and}\; s_{b} ^{2} \text{are sample variances} (\text{ddof} = 1) \text{of the two tickers'}\; \text{column} (\text{Return by default}),\;\; \text{with a two-sided p-value}\]Setting equal_variance=True pools the two variances instead (Student’s t-test), which is slightly more powerful but only valid when the variances are truly equal. Welch’s version is the safer default since assets rarely share the same volatility. A low p-value (e.g. below 0.05) means the difference in means is statistically significant.
For more information about the method, see the following papers:
- Student (1908). “The Probable Error of a Mean.” Biometrika, 6(1), 1-25.
- Welch, B.L. (1947). “The Generalization of ‘Student’s’ Problem when Several Different Population Variances are Involved.” Biometrika, 34(1/2), 28-35.
Also known as: independent samples t-test, Welch’s t-test (default), Student’s t-test (equal_variance=True).
Two-Sample t-Test in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_two_sample_t_test as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["MSFT", "ORCL", "SAP"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.econometrics.get_two_sample_t_test()
Or, after setting up the Finance Toolkit MCP, ask:
Were the mean daily returns of Microsoft, Oracle and SAP different from each other from 2021 to 2025?
Which returns:
| T-Statistic | Degrees of Freedom | P-Value | Mean A | Mean B | |
|---|---|---|---|---|---|
| (‘MSFT’, ‘ORCL’) | -0.528 | 2176.24 | 0.5975 | 0.0008 | 0.0012 |
| (‘MSFT’, ‘SAP’) | 0.1389 | 2504.05 | 0.8895 | 0.0008 | 0.0007 |
| (‘ORCL’, ‘SAP’) | 0.6326 | 2210.92 | 0.5271 | 0.0012 | 0.0007 |
Using Your Own Data
get_two_sample_t_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:
from financetoolkit import Economics, Econometrics
economics = Economics(start_date="2000-01-01")
unemployment = economics.get_unemployment_rate(
countries=["United States", "Germany"], period="monthly"
)
econometrics = Econometrics(data=unemployment, changes="difference")
econometrics.get_two_sample_t_test()
Parameters
get_two_sample_t_test accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to compare. Defaults to “Return”. | "Return" |
equal_variance | bool | Whether to assume the two samples share a common variance (Student’s pooled t-test) instead of Welch’s (unequal-variance) t-test. Defaults to False. | True |
include_benchmark | bool | Whether to include “Benchmark” among the tickers paired up. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |