{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline\n", "\n", "import sympy as sp\n", "import fmt" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Homework Set 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 0:\n", "\n", "I have read and understood the [class policy]( https://nbviewer.jupyter.org/github/yadongli/nyumath2048/blob/master/ClassPolicies.ipynb), especially the section about cheating. Please type your full names below to acknowledge:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1:\n", "\n", "Set up your Python and IPython notebook environment and show case the following IPython functionalities:\n", "\n", "2. use ? or help() to show comments (docstring) of a python function\n", "3. use %pdb to debug the following code, show how to print out the value of b inside function bug() using the debugger" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.6.7'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from platform import python_version\n", "python_version()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Automatic pdb calling has been turned ON\n" ] }, { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdiv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mbug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mbug\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mdiv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mbug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mdiv\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdiv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] }, { "name": "stdout", "output_type": "stream", "text": [ "> \u001b[0;32m\u001b[0m(4)\u001b[0;36mdiv\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m 2 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 3 \u001b[0;31m\u001b[0;32mdef\u001b[0m \u001b[0mdiv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m----> 4 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 5 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 6 \u001b[0;31m\u001b[0;32mdef\u001b[0m \u001b[0mbug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\n", "ipdb> q\n" ] } ], "source": [ "%pdb on \n", "\n", "def div(a, b) :\n", " return a/b\n", "\n", "def bug(a, b) :\n", " return div(a, b)\n", "\n", "bug(3, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2:\n", "\n", "For the first term of the conditional variance expression of the CIR process discused in the class:\n", "\n", "$$f(\\kappa) = \\frac{r_0\\sigma^2}{\\kappa}(e^{-\\kappa t} - e^{-2\\kappa t})$$\n", "\n", "1. compute its condition number, assuming $\\kappa$ is the only variable, comment on whether the computation is stable around $\\kappa = 0$\n", "2. implement a function to compute the above expression, which is numerically stable around $\\kappa = 0$. A typical approach is to switch to an approximation when $\\kappa$ is smaller than a threshold $h$. Explain your choice of $h$, is the $h$ different for double or single precision calculation? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3:\n", "\n", "Implement a matrix product function using hand coded loops in python, please feel free to use numpy array and numpy vector multiplications, but obviously you should not just call numpy matrix multiplication.\n", "\n", "Compare the speed of your implementation to the numpy matrix product version, plot the difference in computational time with the size of the matrix.\n", "\n", "\n", "Hint: \n", "\n", "* %timeit is a useful timing tool in IPython, or you can use time.clock() function\n", "* numpy has two ways of representing matrix, one is array of arrays, another is matrix type. For array types, the matrix production is ```a.dot(b)```, for matrix type, the matrix production is ```a*b```. We recommend you to stick with array type to avoid any potential confusions. The ```a*b``` on array type is element wise multiplication. A common mistake is to use ```a*b``` for matrix production on array type. Please see the following code section as an example.\n", "* numpy.allclose is a useful function to compare floating point calculation results\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a=\n", " [[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n", "\n", "b=\n", " [[ 2 3 4]\n", " [ 5 6 7]\n", " [ 8 9 10]]\n", "\n", "c=a.dot(b)\n", " [[ 36 42 48]\n", " [ 81 96 111]\n", " [126 150 174]]\n", "\n", "d=a*b\n", " [[ 2 6 12]\n", " [20 30 42]\n", " [56 72 90]]\n" ] } ], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n", "b = a + 1\n", "print(\"a=\\n\", a)\n", "print(\"\\nb=\\n\", b)\n", "c = a.dot(b) # matrix product\n", "d = a*b #elment wise multiplication\n", "\n", "print(\"\\nc=a.dot(b)\\n\", c)\n", "print(\"\\nd=a*b\\n\", d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Book problems (from Goodman and Bindel Book)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.8\n", "\n", "Hint: \n", "\n", "* The extra credit part of this problem is rather involved, you are encouraged to try but there is no need to spend too much time.\n", "* In python, floating point numbers are double precision by default, but you can force them to be single precision by calling numpy.float32, please see the following example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "a = np.float32(1.)\n", "b = np.float64(1.)\n", "\n", "print(type(a), type(b))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.7" } }, "nbformat": 4, "nbformat_minor": 1 }