{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7186e81c",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "e71e3381",
            "cell_type": "markdown",
            "source": "Suppose we have the summary data for two samples, as follows (with the\nSample Variance being the corrected sample variance)\n```\n\nSample   Sample\nSize   Mean   Variance\nSample 1    13    15.0     87.5\nSample 2    11    12.0     39.0\n\n```\nApply the t-test to this data (with the assumption that the population\nvariances are equal):\n",
            "metadata": {}
        },
        {
            "id": "482b3b3b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.stats import ttest_ind_from_stats\nttest_ind_from_stats(mean1=15.0, std1=np.sqrt(87.5), nobs1=13,\n                     mean2=12.0, std2=np.sqrt(39.0), nobs2=11)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Ttest_indResult(statistic=0.9051358093310269, pvalue=0.3751996797581487)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "89dcba1c",
            "cell_type": "markdown",
            "source": "For comparison, here is the data from which those summary statistics\nwere taken.  With this data, we can compute the same result using\n`scipy.stats.ttest_ind`:\n",
            "metadata": {}
        },
        {
            "id": "54a8e6db",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = np.array([1, 3, 4, 6, 11, 13, 15, 19, 22, 24, 25, 26, 26])\nb = np.array([2, 4, 6, 9, 11, 13, 14, 15, 18, 19, 21])\nfrom scipy.stats import ttest_ind\nttest_ind(a, b)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "TtestResult(statistic=0.905135809331027,\n            pvalue=0.3751996797581486,\n            df=22.0)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2e55874f",
            "cell_type": "markdown",
            "source": "Suppose we instead have binary data and would like to apply a t-test to\ncompare the proportion of 1s in two independent groups\n```\n\nNumber of    Sample     Sample\nSize    ones        Mean     Variance\nSample 1    150      30         0.2        0.161073\nSample 2    200      45         0.225      0.175251\n\n```\nThe sample mean $\\hat{p}$ is the proportion of ones in the sample\nand the variance for a binary observation is estimated by\n$\\hat{p}(1-\\hat{p})$.\n",
            "metadata": {}
        },
        {
            "id": "d6775204",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ttest_ind_from_stats(mean1=0.2, std1=np.sqrt(0.161073), nobs1=150,\n                     mean2=0.225, std2=np.sqrt(0.175251), nobs2=200)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Ttest_indResult(statistic=-0.5627187905196761, pvalue=0.5739887114209541)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "de6d60f5",
            "cell_type": "markdown",
            "source": "For comparison, we could compute the t statistic and p-value using\narrays of 0s and 1s and `scipy.stat.ttest_ind`, as above.\n",
            "metadata": {}
        },
        {
            "id": "ccbd33c2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "group1 = np.array([1]*30 + [0]*(150-30))\ngroup2 = np.array([1]*45 + [0]*(200-45))\nttest_ind(group1, group2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "TtestResult(statistic=-0.5627179589855622,\n            pvalue=0.573989277115258,\n            df=348.0)"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}