{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c0d6d14-a97f-41f2-8b3e-673399418107",
   "metadata": {},
   "source": [
    "# Basic statistics\n",
    "\n",
    "Here, the goal is to get used to several concepts in basic statistics needed later on for Machine Learning.\n",
    "We are going to generate a simple dataset ourselves and try to calculate some standard quantities to describe them.\n",
    "\n",
    "First, let's import some standard libraries and generate the dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5a29abd-6af1-4b18-8bec-8a8402b97dc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f4bed5ac-0828-4e5f-a166-2e161557b49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "rng = np.random.RandomState(0)\n",
    "n_samples = 10000\n",
    "cov = [[1,       0.7*1*1.3,   0.3*1.3*1],\n",
    "       [0.7*1*1.3,    1.3**2,      0],\n",
    "       [0.3*1.3*1,   0, 1.5**2]]\n",
    "data = rng.multivariate_normal(mean=[1, -1, 0], cov=cov, size=n_samples)\n",
    "data = pd.DataFrame(data, columns=[\"x1\", \"x2\", \"x3\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87d5cd39-4720-4eba-a268-0c38a561976a",
   "metadata": {},
   "source": [
    "First, make a scatter plot of this dataset using matplotlib to visualise what are the possible values of $x_1$, $x_2$ and $x_3$.\n",
    "One can make a 2D plot of $x_1$ versus $x_2$, $x_2$ versus $x_3$ or $x_1$ versus $x_3$ to understand how they relate to one another.\n",
    "\n",
    "Often this is cumbersome if one has too many variables to plot, but we are going to discuss methods to get around this later in the lecture.\n",
    "\n",
    "One tip: one may use `plt.scatter` for every pair of variables to do this, or use the seaborn module to plot all pairs in one go. Look for documentation on `sns.pairplot`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de735cf-3466-41e9-bcd5-fcc756958019",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "89a1c59f-9ff7-4c16-a85e-c390b9f77b31",
   "metadata": {},
   "source": [
    "What are the means and covariances of each variable?\n",
    "Can you explain how they can be geometrically interpreted from the plots above?\n",
    "\n",
    "Tip 1: Try subtracting the mean from the variables, dividing by the square root of the variance, and plotting the variables again. If you fit a line to the data, what is the slope? What is the correlation coefficient?\n",
    "\n",
    "Tip 2: Try using `np.linalg.eig(covariance_matrix)` to decompose the covariance matrix into eigenvectors and try interpreting it.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee885793-46a8-424c-be66-7b9c642c296a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df9f429b-e218-49fd-bc72-fca2e77ba770",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54373271-dd1a-4fdd-8f49-b7db7a595695",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
