{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "769c3ac9",
            "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": "7cf6d872",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import stats\nrng = np.random.default_rng()",
            "outputs": []
        },
        {
            "id": "fa867c74",
            "cell_type": "markdown",
            "source": "Generate some data:\n",
            "metadata": {}
        },
        {
            "id": "cc657a73",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = rng.random(10)\ny = 1.6*x + rng.random(10)",
            "outputs": []
        },
        {
            "id": "8ac13275",
            "cell_type": "markdown",
            "source": "Perform the linear regression:\n",
            "metadata": {}
        },
        {
            "id": "9be756dd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res = stats.linregress(x, y)",
            "outputs": []
        },
        {
            "id": "f770cf27",
            "cell_type": "markdown",
            "source": "Coefficient of determination (R-squared):\n",
            "metadata": {}
        },
        {
            "id": "36b8a3ee",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(f\"R-squared: {res.rvalue**2:.6f}\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "R-squared: 0.717533"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "73d4305d",
            "cell_type": "markdown",
            "source": "Plot the data along with the fitted line:\n",
            "metadata": {}
        },
        {
            "id": "12e86ac5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(x, y, 'o', label='original data')\nplt.plot(x, res.intercept + res.slope*x, 'r', label='fitted line')\nplt.legend()\nplt.show()",
            "outputs": []
        },
        {
            "id": "0706635c",
            "cell_type": "markdown",
            "source": "Calculate 95% confidence interval on slope and intercept:\n",
            "metadata": {}
        },
        {
            "id": "2ec90f9c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Two-sided inverse Students t-distribution\n# p - probability, df - degrees of freedom\nfrom scipy.stats import t\ntinv = lambda p, df: abs(t.ppf(p/2, df))",
            "outputs": []
        },
        {
            "id": "1b8a7231",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ts = tinv(0.05, len(x)-2)\nprint(f\"slope (95%): {res.slope:.6f} +/- {ts*res.stderr:.6f}\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "slope (95%): 1.453392 +/- 0.743465"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4406f9d5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "print(f\"intercept (95%): {res.intercept:.6f}\"\n      f\" +/- {ts*res.intercept_stderr:.6f}\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "intercept (95%): 0.616950 +/- 0.544475"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}