{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 0
   },
   "source": [
    "# Linear Regression\n",
    ":label:`sec_linear_regression`\n",
    "\n",
    "*Regression* refers to a set of methods for modeling\n",
    "the relationship between one or more independent variables\n",
    "and a dependent variable.\n",
    "In the natural sciences and social sciences,\n",
    "the purpose of regression is most often to\n",
    "*characterize* the relationship between the inputs and outputs.\n",
    "Machine learning, on the other hand,\n",
    "is most often concerned with *prediction*.\n",
    "\n",
    "Regression problems pop up whenever we want to predict a numerical value.\n",
    "Common examples include predicting prices (of homes, stocks, etc.),\n",
    "predicting length of stay (for patients in the hospital),\n",
    "demand forecasting (for retail sales), among countless others.\n",
    "Not every prediction problem is a classic regression problem.\n",
    "In subsequent sections, we will introduce classification problems,\n",
    "where the goal is to predict membership among a set of categories.\n",
    "\n",
    "\n",
    "## Basic Elements of Linear Regression\n",
    "\n",
    "*Linear regression* may be both the simplest\n",
    "and most popular among the standard tools to regression.\n",
    "Dating back to the dawn of the 19th century,\n",
    "linear regression flows from a few simple assumptions.\n",
    "First, we assume that the relationship between\n",
    "the independent variables $\\mathbf{x}$ and the dependent variable $y$ is linear,\n",
    "i.e., that $y$ can be expressed as a weighted sum\n",
    "of the elements in $\\mathbf{x}$,\n",
    "given some noise on the observations.\n",
    "Second, we assume that any noise is well-behaved\n",
    "(following a Gaussian distribution).\n",
    "\n",
    "To motivate the approach, let us start with a running example.\n",
    "Suppose that we wish to estimate the prices of houses (in dollars)\n",
    "based on their area (in square feet) and age (in years).\n",
    "To actually develop a model for predicting house prices,\n",
    "we would need to get our hands on a dataset\n",
    "consisting of sales for which we know\n",
    "the sale price, area, and age for each home.\n",
    "In the terminology of machine learning,\n",
    "the dataset is called a *training dataset* or *training set*,\n",
    "and each row (here the data corresponding to one sale)\n",
    "is called an *example* (or *data point*, *data instance*, *sample*).\n",
    "The thing we are trying to predict (price)\n",
    "is called a *label* (or *target*).\n",
    "The independent variables (age and area)\n",
    "upon which the predictions are based\n",
    "are called *features* (or *covariates*).\n",
    "\n",
    "Typically, we will use $n$ to denote\n",
    "the number of examples in our dataset.\n",
    "We index the data examples by $i$, denoting each input\n",
    "as $\\mathbf{x}^{(i)} = [x_1^{(i)}, x_2^{(i)}]^\\top$\n",
    "and the corresponding label as $y^{(i)}$.\n",
    "\n",
    "\n",
    "### Linear Model\n",
    ":label:`subsec_linear_model`\n",
    "\n",
    "The linearity assumption just says that the target (price)\n",
    "can be expressed as a weighted sum of the features (area and age):\n",
    "\n",
    "$$\\mathrm{price} = w_{\\mathrm{area}} \\cdot \\mathrm{area} + w_{\\mathrm{age}} \\cdot \\mathrm{age} + b.$$\n",
    ":eqlabel:`eq_price-area`\n",
    "\n",
    "In :eqref:`eq_price-area`, $w_{\\mathrm{area}}$ and $w_{\\mathrm{age}}$\n",
    "are called *weights*, and $b$ is called a *bias*\n",
    "(also called an *offset* or *intercept*).\n",
    "The weights determine the influence of each feature\n",
    "on our prediction and the bias just says\n",
    "what value the predicted price should take\n",
    "when all of the features take value 0.\n",
    "Even if we will never see any homes with zero area,\n",
    "or that are precisely zero years old,\n",
    "we still need the bias or else we will\n",
    "limit the expressivity of our model.\n",
    "Strictly speaking, :eqref:`eq_price-area` is an *affine transformation*\n",
    "of input features,\n",
    "which is characterized by\n",
    "a *linear transformation* of features via weighted sum, combined with\n",
    "a *translation* via the added bias.\n",
    "\n",
    "Given a dataset, our goal is to choose\n",
    "the weights $\\mathbf{w}$ and the bias $b$ such that on average,\n",
    "the predictions made according to our model\n",
    "best fit the true prices observed in the data.\n",
    "Models whose output prediction\n",
    "is determined by the affine transformation of input features\n",
    "are *linear models*,\n",
    "where the affine transformation is specified by the chosen weights and bias.\n",
    "\n",
    "\n",
    "In disciplines where it is common to focus\n",
    "on datasets with just a few features,\n",
    "explicitly expressing models long-form like this is common.\n",
    "In machine learning, we usually work with high-dimensional datasets,\n",
    "so it is more convenient to employ linear algebra notation.\n",
    "When our inputs consist of $d$ features,\n",
    "we express our prediction $\\hat{y}$ (in general the \"hat\" symbol denotes estimates) as\n",
    "\n",
    "$$\\hat{y} = w_1  x_1 + ... + w_d  x_d + b.$$\n",
    "\n",
    "Collecting all features into a vector $\\mathbf{x} \\in \\mathbb{R}^d$\n",
    "and all weights into a vector $\\mathbf{w} \\in \\mathbb{R}^d$,\n",
    "we can express our model compactly using a dot product:\n",
    "\n",
    "$$\\hat{y} = \\mathbf{w}^\\top \\mathbf{x} + b.$$\n",
    ":eqlabel:`eq_linreg-y`\n",
    "\n",
    "In :eqref:`eq_linreg-y`, the vector $\\mathbf{x}$ corresponds to features of a single data example.\n",
    "We will often find it convenient\n",
    "to refer to features of our entire dataset of $n$ examples\n",
    "via the *design matrix* $\\mathbf{X} \\in \\mathbb{R}^{n \\times d}$.\n",
    "Here, $\\mathbf{X}$ contains one row for every example\n",
    "and one column for every feature.\n",
    "\n",
    "For a collection of features $\\mathbf{X}$,\n",
    "the predictions $\\hat{\\mathbf{y}} \\in \\mathbb{R}^n$\n",
    "can be expressed via the matrix-vector product:\n",
    "\n",
    "$${\\hat{\\mathbf{y}}} = \\mathbf{X} \\mathbf{w} + b,$$\n",
    "\n",
    "where broadcasting (see :numref:`subsec_broadcasting`) is applied during the summation.\n",
    "Given features of a training dataset $\\mathbf{X}$\n",
    "and corresponding (known) labels $\\mathbf{y}$,\n",
    "the goal of linear regression is to find\n",
    "the weight vector $\\mathbf{w}$ and the bias term $b$\n",
    "that given features of a new data example\n",
    "sampled from the same distribution as $\\mathbf{X}$,\n",
    "the new example's label will (in expectation) be predicted with the lowest error.\n",
    "\n",
    "\n",
    "Even if we believe that the best model for\n",
    "predicting $y$ given $\\mathbf{x}$ is linear,\n",
    "we would not expect to find a real-world dataset of $n$ examples where\n",
    "$y^{(i)}$ exactly equals $\\mathbf{w}^\\top \\mathbf{x}^{(i)}+b$\n",
    "for all $1 \\leq i \\leq n$.\n",
    "For example, whatever instruments we use to observe\n",
    "the features $\\mathbf{X}$ and labels $\\mathbf{y}$\n",
    "might suffer small amount of measurement error.\n",
    "Thus, even when we are confident\n",
    "that the underlying relationship is linear,\n",
    "we will incorporate a noise term to account for such errors.\n",
    "\n",
    "Before we can go about searching for the best *parameters* (or *model parameters*) $\\mathbf{w}$ and $b$,\n",
    "we will need two more things:\n",
    "(i) a quality measure for some given model;\n",
    "and (ii) a procedure for updating the model to improve its quality.\n",
    "\n",
    "\n",
    "### Loss Function\n",
    "\n",
    "Before we start thinking about how to *fit* data with our model,\n",
    "we need to determine a measure of *fitness*.\n",
    "The *loss function* quantifies the distance\n",
    "between the *real* and *predicted* value of the target.\n",
    "The loss will usually be a non-negative number\n",
    "where smaller values are better\n",
    "and perfect predictions incur a loss of 0.\n",
    "The most popular loss function in regression problems\n",
    "is the squared error.\n",
    "When our prediction for an example $i$ is $\\hat{y}^{(i)}$\n",
    "and the corresponding true label is $y^{(i)}$,\n",
    "the squared error is given by:\n",
    "\n",
    "$$l^{(i)}(\\mathbf{w}, b) = \\frac{1}{2} \\left(\\hat{y}^{(i)} - y^{(i)}\\right)^2.$$\n",
    ":eqlabel:`eq_mse`\n",
    "\n",
    "The constant $\\frac{1}{2}$ makes no real difference\n",
    "but will prove notationally convenient,\n",
    "canceling out when we take the derivative of the loss.\n",
    "Since the training dataset is given to us, and thus out of our control,\n",
    "the empirical error is only a function of the model parameters.\n",
    "To make things more concrete, consider the example below\n",
    "where we plot a regression problem for a one-dimensional case\n",
    "as shown in :numref:`fig_fit_linreg`.\n",
    "\n",
    "![Fit data with a linear model.](../img/fit-linreg.svg)\n",
    ":label:`fig_fit_linreg`\n",
    "\n",
    "Note that large differences between\n",
    "estimates $\\hat{y}^{(i)}$ and observations $y^{(i)}$\n",
    "lead to even larger contributions to the loss,\n",
    "due to the quadratic dependence.\n",
    "To measure the quality of a model on the entire dataset of $n$ examples,\n",
    "we simply average (or equivalently, sum)\n",
    "the losses on the training set.\n",
    "\n",
    "$$L(\\mathbf{w}, b) =\\frac{1}{n}\\sum_{i=1}^n l^{(i)}(\\mathbf{w}, b) =\\frac{1}{n} \\sum_{i=1}^n \\frac{1}{2}\\left(\\mathbf{w}^\\top \\mathbf{x}^{(i)} + b - y^{(i)}\\right)^2.$$\n",
    "\n",
    "When training the model, we want to find parameters ($\\mathbf{w}^*, b^*$)\n",
    "that minimize the total loss across all training examples:\n",
    "\n",
    "$$\\mathbf{w}^*, b^* = \\operatorname*{argmin}_{\\mathbf{w}, b}\\  L(\\mathbf{w}, b).$$\n",
    "\n",
    "\n",
    "### Analytic Solution\n",
    "\n",
    "Linear regression happens to be an unusually simple optimization problem.\n",
    "Unlike most other models that we will encounter in this book,\n",
    "linear regression can be solved analytically by applying a simple formula.\n",
    "To start, we can subsume the bias $b$ into the parameter $\\mathbf{w}$\n",
    "by appending a column to the design matrix consisting of all ones.\n",
    "Then our prediction problem is to minimize $\\|\\mathbf{y} - \\mathbf{X}\\mathbf{w}\\|^2$.\n",
    "There is just one critical point on the loss surface\n",
    "and it corresponds to the minimum of the loss over the entire domain.\n",
    "Taking the derivative of the loss with respect to $\\mathbf{w}$\n",
    "and setting it equal to zero yields the analytic (closed-form) solution:\n",
    "\n",
    "$$\\mathbf{w}^* = (\\mathbf X^\\top \\mathbf X)^{-1}\\mathbf X^\\top \\mathbf{y}.$$\n",
    "\n",
    "While simple problems like linear regression\n",
    "may admit analytic solutions,\n",
    "you should not get used to such good fortune.\n",
    "Although analytic solutions allow for nice mathematical analysis,\n",
    "the requirement of an analytic solution is so restrictive\n",
    "that it would exclude all of deep learning.\n",
    "\n",
    "\n",
    "### Minibatch Stochastic Gradient Descent\n",
    "\n",
    "Even in cases where we cannot solve the models analytically,\n",
    "it turns out that we can still train models effectively in practice.\n",
    "Moreover, for many tasks, those difficult-to-optimize models\n",
    "turn out to be so much better that figuring out how to train them\n",
    "ends up being well worth the trouble.\n",
    "\n",
    "The key technique for optimizing nearly any deep learning model,\n",
    "and which we will call upon throughout this book,\n",
    "consists of iteratively reducing the error\n",
    "by updating the parameters in the direction\n",
    "that incrementally lowers the loss function.\n",
    "This algorithm is called *gradient descent*.\n",
    "\n",
    "The most naive application of gradient descent\n",
    "consists of taking the derivative of the loss function,\n",
    "which is an average of the losses computed\n",
    "on every single example in the dataset.\n",
    "In practice, this can be extremely slow:\n",
    "we must pass over the entire dataset before making a single update.\n",
    "Thus, we will often settle for sampling a random minibatch of examples\n",
    "every time we need to compute the update,\n",
    "a variant called *minibatch stochastic gradient descent*.\n",
    "\n",
    "In each iteration, we first randomly sample a minibatch $\\mathcal{B}$\n",
    "consisting of a fixed number of training examples.\n",
    "We then compute the derivative (gradient) of the average loss\n",
    "on the minibatch with regard to the model parameters.\n",
    "Finally, we multiply the gradient by a predetermined positive value $\\eta$\n",
    "and subtract the resulting term from the current parameter values.\n",
    "\n",
    "We can express the update mathematically as follows\n",
    "($\\partial$ denotes the partial derivative):\n",
    "\n",
    "$$(\\mathbf{w},b) \\leftarrow (\\mathbf{w},b) - \\frac{\\eta}{|\\mathcal{B}|} \\sum_{i \\in \\mathcal{B}} \\partial_{(\\mathbf{w},b)} l^{(i)}(\\mathbf{w},b).$$\n",
    "\n",
    "\n",
    "To summarize, steps of the algorithm are the following:\n",
    "(i) we initialize the values of the model parameters, typically at random;\n",
    "(ii) we iteratively sample random minibatches from the data,\n",
    "updating the parameters in the direction of the negative gradient.\n",
    "For quadratic losses and affine transformations,\n",
    "we can write this out explicitly as follows:\n",
    "\n",
    "$$\\begin{aligned} \\mathbf{w} &\\leftarrow \\mathbf{w} -   \\frac{\\eta}{|\\mathcal{B}|} \\sum_{i \\in \\mathcal{B}} \\partial_{\\mathbf{w}} l^{(i)}(\\mathbf{w}, b) = \\mathbf{w} - \\frac{\\eta}{|\\mathcal{B}|} \\sum_{i \\in \\mathcal{B}} \\mathbf{x}^{(i)} \\left(\\mathbf{w}^\\top \\mathbf{x}^{(i)} + b - y^{(i)}\\right),\\\\ b &\\leftarrow b -  \\frac{\\eta}{|\\mathcal{B}|} \\sum_{i \\in \\mathcal{B}} \\partial_b l^{(i)}(\\mathbf{w}, b)  = b - \\frac{\\eta}{|\\mathcal{B}|} \\sum_{i \\in \\mathcal{B}} \\left(\\mathbf{w}^\\top \\mathbf{x}^{(i)} + b - y^{(i)}\\right). \\end{aligned}$$\n",
    ":eqlabel:`eq_linreg_batch_update`\n",
    "\n",
    "\n",
    "Note that $\\mathbf{w}$ and $\\mathbf{x}$ are vectors in :eqref:`eq_linreg_batch_update`.\n",
    "Here, the more elegant vector notation makes the math\n",
    "much more readable than expressing things in terms of coefficients,\n",
    "say $w_1, w_2, \\ldots, w_d$.\n",
    "The set cardinality\n",
    "$|\\mathcal{B}|$ represents\n",
    "the number of examples in each minibatch (the *batch size*)\n",
    "and $\\eta$ denotes the *learning rate*.\n",
    "We emphasize that the values of the batch size and learning rate\n",
    "are manually pre-specified and not typically learned through model training.\n",
    "These parameters that are tunable but not updated\n",
    "in the training loop are called *hyperparameters*.\n",
    "*Hyperparameter tuning* is the process by which hyperparameters are chosen,\n",
    "and typically requires that we adjust them\n",
    "based on the results of the training loop\n",
    "as assessed on a separate *validation dataset* (or *validation set*).\n",
    "\n",
    "After training for some predetermined number of iterations\n",
    "(or until some other stopping criteria are met),\n",
    "we record the estimated model parameters,\n",
    "denoted $\\hat{\\mathbf{w}}, \\hat{b}$.\n",
    "Note that even if our function is truly linear and noiseless,\n",
    "these parameters will not be the exact minimizers of the loss\n",
    "because, although the algorithm converges slowly towards the minimizers\n",
    "it cannot achieve it exactly in a finite number of steps.\n",
    "\n",
    "Linear regression happens to be a learning problem where there is only one minimum\n",
    "over the entire domain.\n",
    "However, for more complicated models, like deep networks,\n",
    "the loss surfaces contain many minima.\n",
    "Fortunately, for reasons that are not yet fully understood,\n",
    "deep learning practitioners seldom struggle to find parameters\n",
    "that minimize the loss *on training sets*.\n",
    "The more formidable task is to find parameters\n",
    "that will achieve low loss on data\n",
    "that we have not seen before,\n",
    "a challenge called *generalization*.\n",
    "We return to these topics throughout the book.\n",
    "\n",
    "\n",
    "### Making Predictions with the Learned Model\n",
    "\n",
    "\n",
    "Given the learned linear regression model\n",
    "$\\hat{\\mathbf{w}}^\\top \\mathbf{x} + \\hat{b}$,\n",
    "we can now estimate the price of a new house\n",
    "(not contained in the training data)\n",
    "given its area $x_1$ and age $x_2$.\n",
    "Estimating targets given features is\n",
    "commonly called *prediction* or *inference*.\n",
    "\n",
    "We will try to stick with *prediction* because\n",
    "calling this step *inference*,\n",
    "despite emerging as standard jargon in deep learning,\n",
    "is somewhat of a misnomer.\n",
    "In statistics, *inference* more often denotes\n",
    "estimating parameters based on a dataset.\n",
    "This misuse of terminology is a common source of confusion\n",
    "when deep learning practitioners talk to statisticians.\n",
    "\n",
    "\n",
    "## Vectorization for Speed\n",
    "\n",
    "When training our models, we typically want to process\n",
    "whole minibatches of examples simultaneously.\n",
    "Doing this efficiently requires that (**we**) (~~should~~) (**vectorize the calculations\n",
    "and leverage fast linear algebra libraries\n",
    "rather than writing costly for-loops in Python.**)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "origin_pos": 3,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import math\n",
    "import time\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from d2l import tensorflow as d2l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 4
   },
   "source": [
    "To illustrate why this matters so much,\n",
    "we can (**consider two methods for adding vectors.**)\n",
    "To start we instantiate two 10000-dimensional vectors\n",
    "containing all ones.\n",
    "In one method we will loop over the vectors with a Python for-loop.\n",
    "In the other method we will rely on a single call to `+`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "origin_pos": 5,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [],
   "source": [
    "n = 10000\n",
    "a = tf.ones(n)\n",
    "b = tf.ones(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 6
   },
   "source": [
    "Since we will benchmark the running time frequently in this book,\n",
    "[**let us define a timer**].\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "origin_pos": 7,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [],
   "source": [
    "class Timer:  #@save\n",
    "    \"\"\"Record multiple running times.\"\"\"\n",
    "    def __init__(self):\n",
    "        self.times = []\n",
    "        self.start()\n",
    "\n",
    "    def start(self):\n",
    "        \"\"\"Start the timer.\"\"\"\n",
    "        self.tik = time.time()\n",
    "\n",
    "    def stop(self):\n",
    "        \"\"\"Stop the timer and record the time in a list.\"\"\"\n",
    "        self.times.append(time.time() - self.tik)\n",
    "        return self.times[-1]\n",
    "\n",
    "    def avg(self):\n",
    "        \"\"\"Return the average time.\"\"\"\n",
    "        return sum(self.times) / len(self.times)\n",
    "\n",
    "    def sum(self):\n",
    "        \"\"\"Return the sum of time.\"\"\"\n",
    "        return sum(self.times)\n",
    "\n",
    "    def cumsum(self):\n",
    "        \"\"\"Return the accumulated time.\"\"\"\n",
    "        return np.array(self.times).cumsum().tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 8
   },
   "source": [
    "Now we can benchmark the workloads.\n",
    "First, [**we add them, one coordinate at a time,\n",
    "using a for-loop.**]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "origin_pos": 10,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'9.03769 sec'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = tf.Variable(tf.zeros(n))\n",
    "timer = Timer()\n",
    "for i in range(n):\n",
    "    c[i].assign(a[i] + b[i])\n",
    "f'{timer.stop():.5f} sec'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 11
   },
   "source": [
    "(**Alternatively, we rely on the reloaded `+` operator to compute the elementwise sum.**)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "origin_pos": 12,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.00023 sec'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "timer.start()\n",
    "d = a + b\n",
    "f'{timer.stop():.5f} sec'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 13
   },
   "source": [
    "You probably noticed that the second method\n",
    "is dramatically faster than the first.\n",
    "Vectorizing code often yields order-of-magnitude speedups.\n",
    "Moreover, we push more of the mathematics to the library\n",
    "and need not write as many calculations ourselves,\n",
    "reducing the potential for errors.\n",
    "\n",
    "## The Normal Distribution and Squared Loss\n",
    ":label:`subsec_normal_distribution_and_squared_loss`\n",
    "\n",
    "While you can already get your hands dirty using only the information above,\n",
    "in the following we can more formally motivate the squared loss objective\n",
    "via assumptions about the distribution of noise.\n",
    "\n",
    "Linear regression was invented by Gauss in 1795,\n",
    "who also discovered the normal distribution (also called the *Gaussian*).\n",
    "It turns out that the connection between\n",
    "the normal distribution and linear regression\n",
    "runs deeper than common parentage.\n",
    "To refresh your memory, the probability density\n",
    "of a normal distribution with mean $\\mu$ and variance $\\sigma^2$ (standard deviation $\\sigma$)\n",
    "is given as\n",
    "\n",
    "$$p(x) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}} \\exp\\left(-\\frac{1}{2 \\sigma^2} (x - \\mu)^2\\right).$$\n",
    "\n",
    "Below [**we define a Python function to compute the normal distribution**].\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "origin_pos": 14,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [],
   "source": [
    "def normal(x, mu, sigma):\n",
    "    p = 1 / math.sqrt(2 * math.pi * sigma**2)\n",
    "    return p * np.exp(-0.5 / sigma**2 * (x - mu)**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 15
   },
   "source": [
    "We can now (**visualize the normal distributions**).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "origin_pos": 17,
    "tab": [
     "tensorflow"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"302.08125pt\" height=\"180.65625pt\" viewBox=\"0 0 302.08125 180.65625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2022-03-24T13:09:05.359293</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.5.1, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 180.65625 \n",
       "L 302.08125 180.65625 \n",
       "L 302.08125 0 \n",
       "L 0 0 \n",
       "L 0 180.65625 \n",
       "z\n",
       "\" style=\"fill: none\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 43.78125 143.1 \n",
       "L 294.88125 143.1 \n",
       "L 294.88125 7.2 \n",
       "L 43.78125 7.2 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <path d=\"M 71.511736 143.1 \n",
       "L 71.511736 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_2\">\n",
       "      <defs>\n",
       "       <path id=\"m2dca1e8f88\" d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"71.511736\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- −6 -->\n",
       "      <g transform=\"translate(64.140642 157.698438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-2212\" d=\"M 678 2272 \n",
       "L 4684 2272 \n",
       "L 4684 1741 \n",
       "L 678 1741 \n",
       "L 678 2272 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \n",
       "Q 1688 2584 1439 2293 \n",
       "Q 1191 2003 1191 1497 \n",
       "Q 1191 994 1439 701 \n",
       "Q 1688 409 2113 409 \n",
       "Q 2538 409 2786 701 \n",
       "Q 3034 994 3034 1497 \n",
       "Q 3034 2003 2786 2293 \n",
       "Q 2538 2584 2113 2584 \n",
       "z\n",
       "M 3366 4563 \n",
       "L 3366 3988 \n",
       "Q 3128 4100 2886 4159 \n",
       "Q 2644 4219 2406 4219 \n",
       "Q 1781 4219 1451 3797 \n",
       "Q 1122 3375 1075 2522 \n",
       "Q 1259 2794 1537 2939 \n",
       "Q 1816 3084 2150 3084 \n",
       "Q 2853 3084 3261 2657 \n",
       "Q 3669 2231 3669 1497 \n",
       "Q 3669 778 3244 343 \n",
       "Q 2819 -91 2113 -91 \n",
       "Q 1303 -91 875 529 \n",
       "Q 447 1150 447 2328 \n",
       "Q 447 3434 972 4092 \n",
       "Q 1497 4750 2381 4750 \n",
       "Q 2619 4750 2861 4703 \n",
       "Q 3103 4656 3366 4563 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-36\" x=\"83.789062\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <path d=\"M 104.145435 143.1 \n",
       "L 104.145435 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"104.145435\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- −4 -->\n",
       "      <g transform=\"translate(96.774342 157.698438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \n",
       "L 825 1625 \n",
       "L 2419 1625 \n",
       "L 2419 4116 \n",
       "z\n",
       "M 2253 4666 \n",
       "L 3047 4666 \n",
       "L 3047 1625 \n",
       "L 3713 1625 \n",
       "L 3713 1100 \n",
       "L 3047 1100 \n",
       "L 3047 0 \n",
       "L 2419 0 \n",
       "L 2419 1100 \n",
       "L 313 1100 \n",
       "L 313 1709 \n",
       "L 2253 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-34\" x=\"83.789062\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <path d=\"M 136.779135 143.1 \n",
       "L 136.779135 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"136.779135\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- −2 -->\n",
       "      <g transform=\"translate(129.408041 157.698438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-32\" x=\"83.789062\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <path d=\"M 169.412834 143.1 \n",
       "L 169.412834 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"169.412834\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(166.231584 157.698438)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <path d=\"M 202.046534 143.1 \n",
       "L 202.046534 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"202.046534\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(198.865284 157.698438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <path d=\"M 234.680233 143.1 \n",
       "L 234.680233 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"234.680233\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- 4 -->\n",
       "      <g transform=\"translate(231.498983 157.698438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path d=\"M 267.313932 143.1 \n",
       "L 267.313932 7.2 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2dca1e8f88\" x=\"267.313932\" y=\"143.1\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 6 -->\n",
       "      <g transform=\"translate(264.132682 157.698438)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_8\">\n",
       "     <!-- x -->\n",
       "     <g transform=\"translate(166.371875 171.376563)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-78\" d=\"M 3513 3500 \n",
       "L 2247 1797 \n",
       "L 3578 0 \n",
       "L 2900 0 \n",
       "L 1881 1375 \n",
       "L 863 0 \n",
       "L 184 0 \n",
       "L 1544 1831 \n",
       "L 300 3500 \n",
       "L 978 3500 \n",
       "L 1906 2253 \n",
       "L 2834 3500 \n",
       "L 3513 3500 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_15\">\n",
       "      <path d=\"M 43.78125 136.922727 \n",
       "L 294.88125 136.922727 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_16\">\n",
       "      <defs>\n",
       "       <path id=\"m8cc394e759\" d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m8cc394e759\" x=\"43.78125\" y=\"136.922727\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 0.0 -->\n",
       "      <g transform=\"translate(20.878125 140.721946)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-2e\" d=\"M 684 794 \n",
       "L 1344 794 \n",
       "L 1344 0 \n",
       "L 684 0 \n",
       "L 684 794 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_17\">\n",
       "      <path d=\"M 43.78125 105.954474 \n",
       "L 294.88125 105.954474 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m8cc394e759\" x=\"43.78125\" y=\"105.954474\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <!-- 0.1 -->\n",
       "      <g transform=\"translate(20.878125 109.753693)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-31\" d=\"M 794 531 \n",
       "L 1825 531 \n",
       "L 1825 4091 \n",
       "L 703 3866 \n",
       "L 703 4441 \n",
       "L 1819 4666 \n",
       "L 2450 4666 \n",
       "L 2450 531 \n",
       "L 3481 531 \n",
       "L 3481 0 \n",
       "L 794 0 \n",
       "L 794 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-31\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path d=\"M 43.78125 74.986221 \n",
       "L 294.88125 74.986221 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m8cc394e759\" x=\"43.78125\" y=\"74.986221\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <!-- 0.2 -->\n",
       "      <g transform=\"translate(20.878125 78.78544)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-32\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_21\">\n",
       "      <path d=\"M 43.78125 44.017968 \n",
       "L 294.88125 44.017968 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_22\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m8cc394e759\" x=\"43.78125\" y=\"44.017968\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <!-- 0.3 -->\n",
       "      <g transform=\"translate(20.878125 47.817187)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \n",
       "Q 3050 2419 3304 2112 \n",
       "Q 3559 1806 3559 1356 \n",
       "Q 3559 666 3084 287 \n",
       "Q 2609 -91 1734 -91 \n",
       "Q 1441 -91 1130 -33 \n",
       "Q 819 25 488 141 \n",
       "L 488 750 \n",
       "Q 750 597 1062 519 \n",
       "Q 1375 441 1716 441 \n",
       "Q 2309 441 2620 675 \n",
       "Q 2931 909 2931 1356 \n",
       "Q 2931 1769 2642 2001 \n",
       "Q 2353 2234 1838 2234 \n",
       "L 1294 2234 \n",
       "L 1294 2753 \n",
       "L 1863 2753 \n",
       "Q 2328 2753 2575 2939 \n",
       "Q 2822 3125 2822 3475 \n",
       "Q 2822 3834 2567 4026 \n",
       "Q 2313 4219 1838 4219 \n",
       "Q 1578 4219 1281 4162 \n",
       "Q 984 4106 628 3988 \n",
       "L 628 4550 \n",
       "Q 988 4650 1302 4700 \n",
       "Q 1616 4750 1894 4750 \n",
       "Q 2613 4750 3031 4423 \n",
       "Q 3450 4097 3450 3541 \n",
       "Q 3450 3153 3228 2886 \n",
       "Q 3006 2619 2597 2516 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-33\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_23\">\n",
       "      <path d=\"M 43.78125 13.049715 \n",
       "L 294.88125 13.049715 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_24\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m8cc394e759\" x=\"43.78125\" y=\"13.049715\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_13\">\n",
       "      <!-- 0.4 -->\n",
       "      <g transform=\"translate(20.878125 16.848934)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-34\" x=\"95.410156\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_14\">\n",
       "     <!-- p(x) -->\n",
       "     <g transform=\"translate(14.798438 85.185156)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \n",
       "L 1159 -1331 \n",
       "L 581 -1331 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2969 \n",
       "Q 1341 3281 1617 3432 \n",
       "Q 1894 3584 2278 3584 \n",
       "Q 2916 3584 3314 3078 \n",
       "Q 3713 2572 3713 1747 \n",
       "Q 3713 922 3314 415 \n",
       "Q 2916 -91 2278 -91 \n",
       "Q 1894 -91 1617 61 \n",
       "Q 1341 213 1159 525 \n",
       "z\n",
       "M 3116 1747 \n",
       "Q 3116 2381 2855 2742 \n",
       "Q 2594 3103 2138 3103 \n",
       "Q 1681 3103 1420 2742 \n",
       "Q 1159 2381 1159 1747 \n",
       "Q 1159 1113 1420 752 \n",
       "Q 1681 391 2138 391 \n",
       "Q 2594 391 2855 752 \n",
       "Q 3116 1113 3116 1747 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-28\" d=\"M 1984 4856 \n",
       "Q 1566 4138 1362 3434 \n",
       "Q 1159 2731 1159 2009 \n",
       "Q 1159 1288 1364 580 \n",
       "Q 1569 -128 1984 -844 \n",
       "L 1484 -844 \n",
       "Q 1016 -109 783 600 \n",
       "Q 550 1309 550 2009 \n",
       "Q 550 2706 781 3412 \n",
       "Q 1013 4119 1484 4856 \n",
       "L 1984 4856 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-29\" d=\"M 513 4856 \n",
       "L 1013 4856 \n",
       "Q 1481 4119 1714 3412 \n",
       "Q 1947 2706 1947 2009 \n",
       "Q 1947 1309 1714 600 \n",
       "Q 1481 -109 1013 -844 \n",
       "L 513 -844 \n",
       "Q 928 -128 1133 580 \n",
       "Q 1338 1288 1338 2009 \n",
       "Q 1338 2731 1133 3434 \n",
       "Q 928 4138 513 4856 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-70\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-28\" x=\"63.476562\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-78\" x=\"102.490234\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-29\" x=\"161.669922\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_25\">\n",
       "    <path d=\"M 55.194886 136.922727 \n",
       "L 108.224648 136.813535 \n",
       "L 113.609208 136.566288 \n",
       "L 117.198915 136.184417 \n",
       "L 119.97278 135.668952 \n",
       "L 122.257139 135.025186 \n",
       "L 124.215161 134.257847 \n",
       "L 126.010014 133.330368 \n",
       "L 127.804868 132.138334 \n",
       "L 129.436552 130.779445 \n",
       "L 131.068237 129.113085 \n",
       "L 132.699922 127.093512 \n",
       "L 134.331607 124.67477 \n",
       "L 135.963292 121.812692 \n",
       "L 137.594977 118.467288 \n",
       "L 139.226662 114.605495 \n",
       "L 141.021516 109.733818 \n",
       "L 142.816369 104.197078 \n",
       "L 144.774391 97.412516 \n",
       "L 146.895582 89.247632 \n",
       "L 149.506278 78.224523 \n",
       "L 153.259153 61.239305 \n",
       "L 157.501534 42.275258 \n",
       "L 159.785893 33.113056 \n",
       "L 161.580746 26.820515 \n",
       "L 163.049263 22.424523 \n",
       "L 164.354611 19.173268 \n",
       "L 165.49679 16.884633 \n",
       "L 166.475801 15.362585 \n",
       "L 167.454812 14.263605 \n",
       "L 168.270655 13.679589 \n",
       "L 169.086497 13.401979 \n",
       "L 169.739171 13.401979 \n",
       "L 170.391845 13.599455 \n",
       "L 171.207688 14.122466 \n",
       "L 172.02353 14.948577 \n",
       "L 173.002541 16.331186 \n",
       "L 173.981552 18.12656 \n",
       "L 175.123732 20.717347 \n",
       "L 176.42908 24.286979 \n",
       "L 177.897596 29.000684 \n",
       "L 179.69245 35.615359 \n",
       "L 181.81364 44.367165 \n",
       "L 184.913841 58.245076 \n",
       "L 190.46157 83.161144 \n",
       "L 192.909098 93.115047 \n",
       "L 195.030288 100.899673 \n",
       "L 196.98831 107.299489 \n",
       "L 198.783164 112.473243 \n",
       "L 200.578017 116.986089 \n",
       "L 202.209702 120.534567 \n",
       "L 203.841387 123.5855 \n",
       "L 205.473072 126.176453 \n",
       "L 207.104757 128.35023 \n",
       "L 208.736442 130.152334 \n",
       "L 210.368127 131.628806 \n",
       "L 211.999812 132.824481 \n",
       "L 213.794665 133.865803 \n",
       "L 215.752687 134.73293 \n",
       "L 217.873878 135.421686 \n",
       "L 220.321405 135.97206 \n",
       "L 223.258438 136.389278 \n",
       "L 227.011314 136.67952 \n",
       "L 232.395874 136.850878 \n",
       "L 243.001826 136.917995 \n",
       "L 283.467614 136.922727 \n",
       "L 283.467614 136.922727 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_26\">\n",
       "    <path d=\"M 55.194886 136.7876 \n",
       "L 65.800839 136.522951 \n",
       "L 73.143421 136.126434 \n",
       "L 79.017487 135.590287 \n",
       "L 83.912542 134.926494 \n",
       "L 88.318091 134.105229 \n",
       "L 92.234135 133.153577 \n",
       "L 95.823842 132.06316 \n",
       "L 99.250381 130.798749 \n",
       "L 102.51375 129.367709 \n",
       "L 105.77712 127.695008 \n",
       "L 109.04049 125.764111 \n",
       "L 112.30386 123.563438 \n",
       "L 115.56723 121.087897 \n",
       "L 118.993769 118.196043 \n",
       "L 122.583476 114.860886 \n",
       "L 126.49952 110.903144 \n",
       "L 131.068237 105.948686 \n",
       "L 138.247651 97.770807 \n",
       "L 144.611223 90.644946 \n",
       "L 148.527267 86.589675 \n",
       "L 151.790637 83.530672 \n",
       "L 154.564501 81.224436 \n",
       "L 157.175197 79.344214 \n",
       "L 159.459556 77.957408 \n",
       "L 161.743915 76.832365 \n",
       "L 163.865105 76.036198 \n",
       "L 165.823127 75.522597 \n",
       "L 167.781149 75.227168 \n",
       "L 169.739171 75.153089 \n",
       "L 171.697193 75.301158 \n",
       "L 173.655215 75.66978 \n",
       "L 175.613237 76.254995 \n",
       "L 177.734428 77.126088 \n",
       "L 179.855618 78.233161 \n",
       "L 182.139977 79.673625 \n",
       "L 184.587504 81.48006 \n",
       "L 187.361369 83.820867 \n",
       "L 190.46157 86.751111 \n",
       "L 194.051277 90.469337 \n",
       "L 198.783164 95.721751 \n",
       "L 211.673475 110.215093 \n",
       "L 215.752687 114.383393 \n",
       "L 219.505563 117.90542 \n",
       "L 222.932101 120.82526 \n",
       "L 226.195471 123.328273 \n",
       "L 229.458841 125.556324 \n",
       "L 232.722211 127.513772 \n",
       "L 235.985581 129.211619 \n",
       "L 239.248951 130.665971 \n",
       "L 242.675489 131.95258 \n",
       "L 246.265196 133.063568 \n",
       "L 250.18124 134.034481 \n",
       "L 254.423621 134.846714 \n",
       "L 259.155508 135.514669 \n",
       "L 264.540068 136.040363 \n",
       "L 270.903639 136.432354 \n",
       "L 279.225233 136.707949 \n",
       "L 283.467614 136.785216 \n",
       "L 283.467614 136.785216 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_27\">\n",
       "    <path d=\"M 55.194886 136.922727 \n",
       "L 157.175197 136.813535 \n",
       "L 162.559757 136.566288 \n",
       "L 166.149464 136.184417 \n",
       "L 168.923329 135.668952 \n",
       "L 171.207688 135.025186 \n",
       "L 173.16571 134.257847 \n",
       "L 174.960563 133.330368 \n",
       "L 176.755417 132.138334 \n",
       "L 178.387102 130.779445 \n",
       "L 180.018787 129.113085 \n",
       "L 181.650472 127.093512 \n",
       "L 183.282156 124.67477 \n",
       "L 184.913841 121.812692 \n",
       "L 186.545526 118.467288 \n",
       "L 188.177211 114.605495 \n",
       "L 189.972065 109.733818 \n",
       "L 191.766918 104.197078 \n",
       "L 193.72494 97.412516 \n",
       "L 195.846131 89.247632 \n",
       "L 198.456827 78.224523 \n",
       "L 202.209702 61.239305 \n",
       "L 206.452083 42.275258 \n",
       "L 208.736442 33.113056 \n",
       "L 210.531295 26.820515 \n",
       "L 211.999812 22.424523 \n",
       "L 213.30516 19.173268 \n",
       "L 214.447339 16.884633 \n",
       "L 215.42635 15.362585 \n",
       "L 216.405361 14.263605 \n",
       "L 217.221204 13.679589 \n",
       "L 218.037046 13.401979 \n",
       "L 218.68972 13.401979 \n",
       "L 219.342394 13.599455 \n",
       "L 220.158237 14.122466 \n",
       "L 220.974079 14.948577 \n",
       "L 221.95309 16.331186 \n",
       "L 222.932101 18.12656 \n",
       "L 224.074281 20.717347 \n",
       "L 225.379629 24.286979 \n",
       "L 226.848145 29.000684 \n",
       "L 228.642999 35.615359 \n",
       "L 230.764189 44.367165 \n",
       "L 233.864391 58.245076 \n",
       "L 239.412119 83.161144 \n",
       "L 241.859647 93.115047 \n",
       "L 243.980837 100.899673 \n",
       "L 245.938859 107.299489 \n",
       "L 247.733713 112.473243 \n",
       "L 249.528566 116.986089 \n",
       "L 251.160251 120.534567 \n",
       "L 252.791936 123.5855 \n",
       "L 254.423621 126.176453 \n",
       "L 256.055306 128.35023 \n",
       "L 257.686991 130.152334 \n",
       "L 259.318676 131.628806 \n",
       "L 260.950361 132.824481 \n",
       "L 262.745215 133.865803 \n",
       "L 264.703236 134.73293 \n",
       "L 266.824427 135.421686 \n",
       "L 269.271954 135.97206 \n",
       "L 272.208987 136.389278 \n",
       "L 275.961863 136.67952 \n",
       "L 281.346423 136.850878 \n",
       "L 283.467614 136.879593 \n",
       "L 283.467614 136.879593 \n",
       "\" clip-path=\"url(#pb7b9c054bb)\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 43.78125 143.1 \n",
       "L 43.78125 7.2 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 294.88125 143.1 \n",
       "L 294.88125 7.2 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 43.78125 143.1 \n",
       "L 294.88125 143.1 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 43.78125 7.2 \n",
       "L 294.88125 7.2 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"legend_1\">\n",
       "    <g id=\"patch_7\">\n",
       "     <path d=\"M 50.78125 59.234375 \n",
       "L 152.05625 59.234375 \n",
       "Q 154.05625 59.234375 154.05625 57.234375 \n",
       "L 154.05625 14.2 \n",
       "Q 154.05625 12.2 152.05625 12.2 \n",
       "L 50.78125 12.2 \n",
       "Q 48.78125 12.2 48.78125 14.2 \n",
       "L 48.78125 57.234375 \n",
       "Q 48.78125 59.234375 50.78125 59.234375 \n",
       "z\n",
       "\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_28\">\n",
       "     <path d=\"M 52.78125 20.298438 \n",
       "L 62.78125 20.298438 \n",
       "L 72.78125 20.298438 \n",
       "\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "    </g>\n",
       "    <g id=\"text_15\">\n",
       "     <!-- mean 0, std 1 -->\n",
       "     <g transform=\"translate(80.78125 23.798438)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-6d\" d=\"M 3328 2828 \n",
       "Q 3544 3216 3844 3400 \n",
       "Q 4144 3584 4550 3584 \n",
       "Q 5097 3584 5394 3201 \n",
       "Q 5691 2819 5691 2113 \n",
       "L 5691 0 \n",
       "L 5113 0 \n",
       "L 5113 2094 \n",
       "Q 5113 2597 4934 2840 \n",
       "Q 4756 3084 4391 3084 \n",
       "Q 3944 3084 3684 2787 \n",
       "Q 3425 2491 3425 1978 \n",
       "L 3425 0 \n",
       "L 2847 0 \n",
       "L 2847 2094 \n",
       "Q 2847 2600 2669 2842 \n",
       "Q 2491 3084 2119 3084 \n",
       "Q 1678 3084 1418 2786 \n",
       "Q 1159 2488 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1356 3278 1631 3431 \n",
       "Q 1906 3584 2284 3584 \n",
       "Q 2666 3584 2933 3390 \n",
       "Q 3200 3197 3328 2828 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \n",
       "L 3597 1613 \n",
       "L 953 1613 \n",
       "Q 991 1019 1311 708 \n",
       "Q 1631 397 2203 397 \n",
       "Q 2534 397 2845 478 \n",
       "Q 3156 559 3463 722 \n",
       "L 3463 178 \n",
       "Q 3153 47 2828 -22 \n",
       "Q 2503 -91 2169 -91 \n",
       "Q 1331 -91 842 396 \n",
       "Q 353 884 353 1716 \n",
       "Q 353 2575 817 3079 \n",
       "Q 1281 3584 2069 3584 \n",
       "Q 2775 3584 3186 3129 \n",
       "Q 3597 2675 3597 1894 \n",
       "z\n",
       "M 3022 2063 \n",
       "Q 3016 2534 2758 2815 \n",
       "Q 2500 3097 2075 3097 \n",
       "Q 1594 3097 1305 2825 \n",
       "Q 1016 2553 972 2059 \n",
       "L 3022 2063 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \n",
       "Q 1497 1759 1228 1600 \n",
       "Q 959 1441 959 1056 \n",
       "Q 959 750 1161 570 \n",
       "Q 1363 391 1709 391 \n",
       "Q 2188 391 2477 730 \n",
       "Q 2766 1069 2766 1631 \n",
       "L 2766 1759 \n",
       "L 2194 1759 \n",
       "z\n",
       "M 3341 1997 \n",
       "L 3341 0 \n",
       "L 2766 0 \n",
       "L 2766 531 \n",
       "Q 2569 213 2275 61 \n",
       "Q 1981 -91 1556 -91 \n",
       "Q 1019 -91 701 211 \n",
       "Q 384 513 384 1019 \n",
       "Q 384 1609 779 1909 \n",
       "Q 1175 2209 1959 2209 \n",
       "L 2766 2209 \n",
       "L 2766 2266 \n",
       "Q 2766 2663 2505 2880 \n",
       "Q 2244 3097 1772 3097 \n",
       "Q 1472 3097 1187 3025 \n",
       "Q 903 2953 641 2809 \n",
       "L 641 3341 \n",
       "Q 956 3463 1253 3523 \n",
       "Q 1550 3584 1831 3584 \n",
       "Q 2591 3584 2966 3190 \n",
       "Q 3341 2797 3341 1997 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-2c\" d=\"M 750 794 \n",
       "L 1409 794 \n",
       "L 1409 256 \n",
       "L 897 -744 \n",
       "L 494 -744 \n",
       "L 750 256 \n",
       "L 750 794 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \n",
       "L 2834 2853 \n",
       "Q 2591 2978 2328 3040 \n",
       "Q 2066 3103 1784 3103 \n",
       "Q 1356 3103 1142 2972 \n",
       "Q 928 2841 928 2578 \n",
       "Q 928 2378 1081 2264 \n",
       "Q 1234 2150 1697 2047 \n",
       "L 1894 2003 \n",
       "Q 2506 1872 2764 1633 \n",
       "Q 3022 1394 3022 966 \n",
       "Q 3022 478 2636 193 \n",
       "Q 2250 -91 1575 -91 \n",
       "Q 1294 -91 989 -36 \n",
       "Q 684 19 347 128 \n",
       "L 347 722 \n",
       "Q 666 556 975 473 \n",
       "Q 1284 391 1588 391 \n",
       "Q 1994 391 2212 530 \n",
       "Q 2431 669 2431 922 \n",
       "Q 2431 1156 2273 1281 \n",
       "Q 2116 1406 1581 1522 \n",
       "L 1381 1569 \n",
       "Q 847 1681 609 1914 \n",
       "Q 372 2147 372 2553 \n",
       "Q 372 3047 722 3315 \n",
       "Q 1072 3584 1716 3584 \n",
       "Q 2034 3584 2315 3537 \n",
       "Q 2597 3491 2834 3397 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \n",
       "L 1172 3500 \n",
       "L 2356 3500 \n",
       "L 2356 3053 \n",
       "L 1172 3053 \n",
       "L 1172 1153 \n",
       "Q 1172 725 1289 603 \n",
       "Q 1406 481 1766 481 \n",
       "L 2356 481 \n",
       "L 2356 0 \n",
       "L 1766 0 \n",
       "Q 1100 0 847 248 \n",
       "Q 594 497 594 1153 \n",
       "L 594 3053 \n",
       "L 172 3053 \n",
       "L 172 3500 \n",
       "L 594 3500 \n",
       "L 594 4494 \n",
       "L 1172 4494 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-64\" d=\"M 2906 2969 \n",
       "L 2906 4863 \n",
       "L 3481 4863 \n",
       "L 3481 0 \n",
       "L 2906 0 \n",
       "L 2906 525 \n",
       "Q 2725 213 2448 61 \n",
       "Q 2172 -91 1784 -91 \n",
       "Q 1150 -91 751 415 \n",
       "Q 353 922 353 1747 \n",
       "Q 353 2572 751 3078 \n",
       "Q 1150 3584 1784 3584 \n",
       "Q 2172 3584 2448 3432 \n",
       "Q 2725 3281 2906 2969 \n",
       "z\n",
       "M 947 1747 \n",
       "Q 947 1113 1208 752 \n",
       "Q 1469 391 1925 391 \n",
       "Q 2381 391 2643 752 \n",
       "Q 2906 1113 2906 1747 \n",
       "Q 2906 2381 2643 2742 \n",
       "Q 2381 3103 1925 3103 \n",
       "Q 1469 3103 1208 2742 \n",
       "Q 947 2381 947 1747 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-6d\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-65\" x=\"97.412109\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"158.935547\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"220.214844\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"283.59375\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-30\" x=\"315.380859\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-2c\" x=\"379.003906\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"410.791016\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"442.578125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-74\" x=\"494.677734\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-64\" x=\"533.886719\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"597.363281\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-31\" x=\"629.150391\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_29\">\n",
       "     <path d=\"M 52.78125 34.976562 \n",
       "L 62.78125 34.976562 \n",
       "L 72.78125 34.976562 \n",
       "\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "    </g>\n",
       "    <g id=\"text_16\">\n",
       "     <!-- mean 0, std 2 -->\n",
       "     <g transform=\"translate(80.78125 38.476562)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-6d\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-65\" x=\"97.412109\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"158.935547\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"220.214844\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"283.59375\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-30\" x=\"315.380859\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-2c\" x=\"379.003906\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"410.791016\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"442.578125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-74\" x=\"494.677734\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-64\" x=\"533.886719\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"597.363281\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-32\" x=\"629.150391\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_30\">\n",
       "     <path d=\"M 52.78125 49.654688 \n",
       "L 62.78125 49.654688 \n",
       "L 72.78125 49.654688 \n",
       "\" style=\"fill: none; stroke-dasharray: 9.6,2.4,1.5,2.4; stroke-dashoffset: 0; stroke: #008000; stroke-width: 1.5\"/>\n",
       "    </g>\n",
       "    <g id=\"text_17\">\n",
       "     <!-- mean 3, std 1 -->\n",
       "     <g transform=\"translate(80.78125 53.154688)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-6d\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-65\" x=\"97.412109\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"158.935547\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"220.214844\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"283.59375\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-33\" x=\"315.380859\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-2c\" x=\"379.003906\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"410.791016\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"442.578125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-74\" x=\"494.677734\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-64\" x=\"533.886719\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"597.363281\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-31\" x=\"629.150391\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"pb7b9c054bb\">\n",
       "   <rect x=\"43.78125\" y=\"7.2\" width=\"251.1\" height=\"135.9\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 324x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Use numpy again for visualization\n",
    "x = np.arange(-7, 7, 0.01)\n",
    "\n",
    "# Mean and standard deviation pairs\n",
    "params = [(0, 1), (0, 2), (3, 1)]\n",
    "d2l.plot(x, [normal(x, mu, sigma) for mu, sigma in params], xlabel='x',\n",
    "         ylabel='p(x)', figsize=(4.5, 2.5),\n",
    "         legend=[f'mean {mu}, std {sigma}' for mu, sigma in params])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 18
   },
   "source": [
    "As we can see, changing the mean corresponds to a shift along the $x$-axis,\n",
    "and increasing the variance spreads the distribution out, lowering its peak.\n",
    "\n",
    "One way to motivate linear regression with the mean squared error loss function (or simply squared loss)\n",
    "is to formally assume that observations arise from noisy observations,\n",
    "where the noise is normally distributed as follows:\n",
    "\n",
    "$$y = \\mathbf{w}^\\top \\mathbf{x} + b + \\epsilon \\text{ where } \\epsilon \\sim \\mathcal{N}(0, \\sigma^2).$$\n",
    "\n",
    "Thus, we can now write out the *likelihood*\n",
    "of seeing a particular $y$ for a given $\\mathbf{x}$ via\n",
    "\n",
    "$$P(y \\mid \\mathbf{x}) = \\frac{1}{\\sqrt{2 \\pi \\sigma^2}} \\exp\\left(-\\frac{1}{2 \\sigma^2} (y - \\mathbf{w}^\\top \\mathbf{x} - b)^2\\right).$$\n",
    "\n",
    "Now, according to the principle of maximum likelihood,\n",
    "the best values of parameters $\\mathbf{w}$ and $b$ are those\n",
    "that maximize the *likelihood* of the entire dataset:\n",
    "\n",
    "$$P(\\mathbf y \\mid \\mathbf X) = \\prod_{i=1}^{n} p(y^{(i)}|\\mathbf{x}^{(i)}).$$\n",
    "\n",
    "Estimators chosen according to the principle of maximum likelihood\n",
    "are called *maximum likelihood estimators*.\n",
    "While, maximizing the product of many exponential functions,\n",
    "might look difficult,\n",
    "we can simplify things significantly, without changing the objective,\n",
    "by maximizing the log of the likelihood instead.\n",
    "For historical reasons, optimizations are more often expressed\n",
    "as minimization rather than maximization.\n",
    "So, without changing anything we can minimize the *negative log-likelihood*\n",
    "$-\\log P(\\mathbf y \\mid \\mathbf X)$.\n",
    "Working out the mathematics gives us:\n",
    "\n",
    "$$-\\log P(\\mathbf y \\mid \\mathbf X) = \\sum_{i=1}^n \\frac{1}{2} \\log(2 \\pi \\sigma^2) + \\frac{1}{2 \\sigma^2} \\left(y^{(i)} - \\mathbf{w}^\\top \\mathbf{x}^{(i)} - b\\right)^2.$$\n",
    "\n",
    "Now we just need one more assumption that $\\sigma$ is some fixed constant.\n",
    "Thus we can ignore the first term because\n",
    "it does not depend on $\\mathbf{w}$ or $b$.\n",
    "Now the second term is identical to the squared error loss introduced earlier,\n",
    "except for the multiplicative constant $\\frac{1}{\\sigma^2}$.\n",
    "Fortunately, the solution does not depend on $\\sigma$.\n",
    "It follows that minimizing the mean squared error\n",
    "is equivalent to maximum likelihood estimation\n",
    "of a linear model under the assumption of additive Gaussian noise.\n",
    "\n",
    "## From Linear Regression to Deep Networks\n",
    "\n",
    "So far we only talked about linear models.\n",
    "While neural networks cover a much richer family of models,\n",
    "we can begin thinking of the linear model\n",
    "as a neural network by expressing it in the language of neural networks.\n",
    "To begin, let us start by rewriting things in a \"layer\" notation.\n",
    "\n",
    "### Neural Network Diagram\n",
    "\n",
    "Deep learning practitioners like to draw diagrams\n",
    "to visualize what is happening in their models.\n",
    "In :numref:`fig_single_neuron`,\n",
    "we depict our linear regression model as a neural network.\n",
    "Note that these diagrams highlight the connectivity pattern\n",
    "such as how each input is connected to the output,\n",
    "but not the values taken by the weights or biases.\n",
    "\n",
    "![Linear regression is a single-layer neural network.](../img/singleneuron.svg)\n",
    ":label:`fig_single_neuron`\n",
    "\n",
    "For the neural network shown in :numref:`fig_single_neuron`,\n",
    "the inputs are $x_1, \\ldots, x_d$,\n",
    "so the *number of inputs* (or *feature dimensionality*) in the input layer is $d$.\n",
    "The output of the network in :numref:`fig_single_neuron` is $o_1$,\n",
    "so the *number of outputs* in the output layer is 1.\n",
    "Note that the input values are all *given*\n",
    "and there is just a single *computed* neuron.\n",
    "Focusing on where computation takes place,\n",
    "conventionally we do not consider the input layer when counting layers.\n",
    "That is to say,\n",
    "the *number of layers* for the neural network in :numref:`fig_single_neuron` is 1.\n",
    "We can think of linear regression models as neural networks\n",
    "consisting of just a single artificial neuron,\n",
    "or as single-layer neural networks.\n",
    "\n",
    "Since for linear regression, every input is connected\n",
    "to every output (in this case there is only one output),\n",
    "we can regard this transformation (the output layer in :numref:`fig_single_neuron`)\n",
    "as a *fully-connected layer* or *dense layer*.\n",
    "We will talk a lot more about networks composed of such layers\n",
    "in the next chapter.\n",
    "\n",
    "\n",
    "### Biology\n",
    "\n",
    "Since linear regression (invented in 1795)\n",
    "predates computational neuroscience,\n",
    "it might seem anachronistic to describe\n",
    "linear regression as a neural network.\n",
    "To see why linear models were a natural place to begin\n",
    "when the cyberneticists/neurophysiologists\n",
    "Warren McCulloch and Walter Pitts began to develop\n",
    "models of artificial neurons,\n",
    "consider the cartoonish picture\n",
    "of a biological neuron in :numref:`fig_Neuron`, consisting of\n",
    "*dendrites* (input terminals),\n",
    "the *nucleus* (CPU), the *axon* (output wire),\n",
    "and the *axon terminals* (output terminals),\n",
    "enabling connections to other neurons via *synapses*.\n",
    "\n",
    "![The real neuron.](../img/neuron.svg)\n",
    ":label:`fig_Neuron`\n",
    "\n",
    "Information $x_i$ arriving from other neurons\n",
    "(or environmental sensors such as the retina)\n",
    "is received in the dendrites.\n",
    "In particular, that information is weighted by *synaptic weights* $w_i$\n",
    "determining the effect of the inputs\n",
    "(e.g., activation or inhibition via the product $x_i w_i$).\n",
    "The weighted inputs arriving from multiple sources\n",
    "are aggregated in the nucleus as a weighted sum $y = \\sum_i x_i w_i + b$,\n",
    "and this information is then sent for further processing in the axon $y$,\n",
    "typically after some nonlinear processing via $\\sigma(y)$.\n",
    "From there it either reaches its destination (e.g., a muscle)\n",
    "or is fed into another neuron via its dendrites.\n",
    "\n",
    "Certainly, the high-level idea that many such units\n",
    "could be cobbled together with the right connectivity\n",
    "and right learning algorithm,\n",
    "to produce far more interesting and complex behavior\n",
    "than any one neuron alone could express\n",
    "owes to our study of real biological neural systems.\n",
    "\n",
    "At the same time, most research in deep learning today\n",
    "draws little direct inspiration in neuroscience.\n",
    "We invoke Stuart Russell and Peter Norvig who,\n",
    "in their classic AI text book\n",
    "*Artificial Intelligence: A Modern Approach* :cite:`Russell.Norvig.2016`,\n",
    "pointed out that although airplanes might have been *inspired* by birds,\n",
    "ornithology has not been the primary driver\n",
    "of aeronautics innovation for some centuries.\n",
    "Likewise, inspiration in deep learning these days\n",
    "comes in equal or greater measure from mathematics,\n",
    "statistics, and computer science.\n",
    "\n",
    "## Summary\n",
    "\n",
    "* Key ingredients in a machine learning model are training data, a loss function, an optimization algorithm, and quite obviously, the model itself.\n",
    "* Vectorizing makes everything better (mostly math) and faster (mostly code).\n",
    "* Minimizing an objective function and performing maximum likelihood estimation can mean the same thing.\n",
    "* Linear regression models are neural networks, too.\n",
    "\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. Assume that we have some data $x_1, \\ldots, x_n \\in \\mathbb{R}$. Our goal is to find a constant $b$ such that $\\sum_i (x_i - b)^2$ is minimized.\n",
    "    1. Find a analytic solution for the optimal value of $b$.\n",
    "    1. How does this problem and its solution relate to the normal distribution?\n",
    "1. Derive the analytic solution to the optimization problem for linear regression with squared error. To keep things simple, you can omit the bias $b$ from the problem (we can do this in principled fashion by adding one column to $\\mathbf X$ consisting of all ones).\n",
    "    1. Write out the optimization problem in matrix and vector notation (treat all the data as a single matrix, and all the target values as a single vector).\n",
    "    1. Compute the gradient of the loss with respect to $w$.\n",
    "    1. Find the analytic solution by setting the gradient equal to zero and solving the matrix equation.\n",
    "    1. When might this be better than using stochastic gradient descent? When might this method break?\n",
    "1. Assume that the noise model governing the additive noise $\\epsilon$ is the exponential distribution. That is, $p(\\epsilon) = \\frac{1}{2} \\exp(-|\\epsilon|)$.\n",
    "    1. Write out the negative log-likelihood of the data under the model $-\\log P(\\mathbf y \\mid \\mathbf X)$.\n",
    "    1. Can you find a closed form solution?\n",
    "    1. Suggest a stochastic gradient descent algorithm to solve this problem. What could possibly go wrong (hint: what happens near the stationary point as we keep on updating the parameters)? Can you fix this?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 21,
    "tab": [
     "tensorflow"
    ]
   },
   "source": [
    "[Discussions](https://discuss.d2l.ai/t/259)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}