<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 0.10\n",
    "Over the next couple of exercises we will make use of the Galton dataset, a dataset of heights of fathers\n",
    "and sons from the 1877 paper that first discussed the “regression to the mean” phenomenon. This dataset has 928 pairs\n",
    "of numbers.\n",
    "* Use the ``load()`` function in the ``galton.py`` file to load the dataset. The file is located under the lxmls/readers folder. Type the following in your Python interpreter:\n",
    "\n",
    "``import galton as galton\n",
    "galton_data = galton.load()``\n",
    "\n",
    "* What are the mean height and standard deviation of all the people in the sample? What is the mean height of the\n",
    "fathers and of the sons?\n",
    "* Plot a histogram of all the heights (you might want to use the plt.hist function and the ravel method on\n",
    "arrays).\n",
    "* Plot the height of the father versus the height of the son.\n",
    "* You should notice that there are several points that are exactly the same (e.g., there are 21 pairs with the values 68.5\n",
    "and 70.2). Use the ``?`` command in ipython to read the documentation for the numpy.random.randn function\n",
    "and add random jitter (i.e., move the point a little bit) to the points before displaying them. Does your impression\n",
    "of the data change?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxmls.readers import galton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "galton_data = galton.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(galton_data.mean(0))\n",
    "print(galton_data.std(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.hist(galton_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(galton_data[:,0], galton_data[:,1], '.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.random.randn?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "galton_data_randn = galton_data + 0.5*np.random.randn(len(galton_data), 2)\n",
    "plt.plot(galton_data_randn[:,0], galton_data_randn[:,1], '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Exercise 0.11 \n",
    "Consider the function $f(x) = x^2$ and its derivative \n",
    "$ \\frac{\\partial f}{\\partial x} $.\n",
    "Look at the derivative of that function at points ``[-2,0,2]``, draw the tangent to the graph in that point \n",
    "$ \\frac{\\partial f}{\\partial x}(-2)=-4 $, \n",
    "$ \\frac{\\partial f}{\\partial x}(0)=0 $ and \n",
    "$ \\frac{\\partial f}{\\partial x}(2)=4 $. \n",
    "\n",
    "For example, the tangent\n",
    "equation for $x = −2$ is $y = −4x − b$, where $b = f(−2)$. The following code plots the function and the derivatives on\n",
    "those points using matplotlib (See Figure 4)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "a = np.arange(-5,5,0.01)\n",
    "f_x = np.power(a,2)\n",
    "plt.plot(a,f_x)\n",
    "plt.xlim(-5,5)\n",
    "plt.ylim(-5,15)\n",
    "k = np.array([-2,0,2])\n",
    "plt.plot(k,k**2,\"bo\")\n",
    "for i in k:\n",
    "    plt.plot(a, (2*i)*a - i**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 0.12 \n",
    "Consider the function $f(x) = (x + 2)^2 − 16 \\text{exp}(−(x − 2)^2)$\n",
    ". Make a function that computes the\n",
    "function value given x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_y(x):\n",
    "    return (x+2)**2 - 16*np.exp(-((x-2)**2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw a plot around $x \\in [−8, 8]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.arange(-8,8,0.001)\n",
    "y = list(map(lambda u: get_y(u),x))\n",
    "plt.plot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the derivative of the function $f(x)$, implement the function ``get grad(x)``."
   ]
  },
  {
   "cell_type": "code",<commit_msg>erase lambda when drawing function<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 0.10\n",
    "Over the next couple of exercises we will make use of the Galton dataset, a dataset of heights of fathers\n",
    "and sons from the 1877 paper that first discussed the “regression to the mean” phenomenon. This dataset has 928 pairs\n",
    "of numbers.\n",
    "* Use the ``load()`` function in the ``galton.py`` file to load the dataset. The file is located under the lxmls/readers folder. Type the following in your Python interpreter:\n",
    "\n",
    "``import galton as galton\n",
    "galton_data = galton.load()``\n",
    "\n",
    "* What are the mean height and standard deviation of all the people in the sample? What is the mean height of the\n",
    "fathers and of the sons?\n",
    "* Plot a histogram of all the heights (you might want to use the plt.hist function and the ravel method on\n",
    "arrays).\n",
    "* Plot the height of the father versus the height of the son.\n",
    "* You should notice that there are several points that are exactly the same (e.g., there are 21 pairs with the values 68.5\n",
    "and 70.2). Use the ``?`` command in ipython to read the documentation for the numpy.random.randn function\n",
    "and add random jitter (i.e., move the point a little bit) to the points before displaying them. Does your impression\n",
    "of the data change?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxmls.readers import galton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "galton_data = galton.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(galton_data.mean(0))\n",
    "print(galton_data.std(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.hist(galton_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(galton_data[:,0], galton_data[:,1], '.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "np.random.randn?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "galton_data_randn = galton_data + 0.5*np.random.randn(len(galton_data), 2)\n",
    "plt.plot(galton_data_randn[:,0], galton_data_randn[:,1], '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Exercise 0.11 \n",
    "Consider the function $f(x) = x^2$ and its derivative \n",
    "$ \\frac{\\partial f}{\\partial x} $.\n",
    "Look at the derivative of that function at points ``[-2,0,2]``, draw the tangent to the graph in that point \n",
    "$ \\frac{\\partial f}{\\partial x}(-2)=-4 $, \n",
    "$ \\frac{\\partial f}{\\partial x}(0)=0 $ and \n",
    "$ \\frac{\\partial f}{\\partial x}(2)=4 $. \n",
    "\n",
    "For example, the tangent\n",
    "equation for $x = −2$ is $y = −4x − b$, where $b = f(−2)$. The following code plots the function and the derivatives on\n",
    "those points using matplotlib (See Figure 4)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "a = np.arange(-5,5,0.01)\n",
    "f_x = np.power(a,2)\n",
    "plt.plot(a,f_x)\n",
    "plt.xlim(-5,5)\n",
    "plt.ylim(-5,15)\n",
    "k = np.array([-2,0,2])\n",
    "plt.plot(k,k**2,\"bo\")\n",
    "for i in k:\n",
    "    plt.plot(a, (2*i)*a - i**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 0.12 \n",
    "Consider the function $f(x) = (x + 2)^2 − 16 \\text{exp}(−(x − 2)^2)$\n",
    ". Make a function that computes the\n",
    "function value given x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_y(x):\n",
    "    return (x+2)**2 - 16*np.exp(-((x-2)**2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw a plot around $x \\in [−8, 8]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.arange(-8,8,0.001)\n",
    "y = list(map(get_y,x))\n",
    "plt.plot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate the derivative of the function $f(x)$, implement the function ``get grad(x)``."
   ]
  },
  {
   "cell_type": "code",
{'fraction_non_alphanumeric': 0.207698396991886, 'fraction_numerical': 0.016623787848802692, 'mean_word_length': 2.6981339187705817, 'pattern_counts': {'":': 144, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4227976', 'n_tokens_mistral': 3904, 'n_tokens_neox': 3770, 'n_words': 989}