{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Biostat M280 Homework 1\n", "\n", "#### Due Apr 19 @ 11:59PM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q1\n", "\n", "**No handwritten homework reports are accepted for this course.** We work with Git/GitHub. Efficient and abundant use of Git, e.g., **frequent and well-documented** commits, is an important criterion for grading your homework.\n", "\n", "1. If you don't have a GitHub account, apply for the [Student Developer Pack](https://education.github.com/pack) at GitHub using your UCLA email.\n", "\n", "2. Create a **private** repository `biostat-m280-2019-spring` and add `Hua-Zhou` and `chris-german` (TA) as your collaborators.\n", "\n", "3. Top directories of the repository should be `hw1`, `hw2`, ... You may create other branches for developing your homework solutions; but the `master` branch will be your presentation area. Put your homework submission files (IJulia notebook `.ipynb`, html converted from notebook, all code and data set to reproduce results) in `master` branch. \n", "\n", "4. After each homework due date, teaching assistant and instructor will check out your `master` branch for grading. Tag each of your homework submissions with tag names `hw1`, `hw2`, ... Tagging time will be used as your submission time. That means if you tag your hw1 submission after deadline, penalty points will be deducted for late submission. \n", "\n", "5. Read the [style guide](https://github.com/johnmyleswhite/Style.jl) for Julia programming by John Myles White. Following rules in the style guide will be strictly enforced when grading: (4), (6), (7), (8), (9), (12), (13) and (16)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q2\n", "\n", "Let's check whether floating-point numbers obey certain algebraic rules. \n", "1. Associative rule for addition says `(x + y) + z == x + (y + z)`. Check association rule using `x = 0.1`, `y = 0.1` and `z = 1.0` in Julia. Explain what you find.\n", "\n", "2. Do floating-point numbers obey the associative rule for multiplication: `(x * y) * z == x * (y * z)`?\n", "\n", "3. Do floating-point numbers obey the distributive rule: `a * (x + y) == a * x + a * y`? \n", "4. Is `0 * x == 0` true for all floating-point number `x`? \n", "\n", "5. Is `x / a == x * (1 / a)` always true?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q3\n", "\n", "Consider Julia function\n", "```julia\n", "function g(k)\n", " for i in 1:10\n", " k = 5k - 1\n", " end\n", " k\n", "end\n", "```\n", "1. Use `@code_llvm` to find the LLVM bitcode of compiled `g` with `Int64` input. \n", "2. Use `@code_llvm` to find the LLVM bitcode of compiled `g` with `Float64` input. \n", "3. Compare the bitcode from questions 1 and 2. What do you find? \n", "4. Read Julia documentation on `@fastmath` and repeat the questions 1-3 on the function \n", "\n", "```julia\n", "function g_fastmath(k) \n", " @fastmath for i in 1:10 \n", " k = 5k - 1\n", " end\n", " k\n", "end\n", "```\n", "Explain what does macro `@fastmath` do?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q4\n", "\n", "Create the vector `x = (0.988, 0.989, 0.990, ..., 1.010, 1.011, 1.012)`. \n", "0. Plot the polynomial `y = x^7 - 7x^6 + 21x^5 - 35x^4 + 35x^3 - 21x^2 + 7x -1` at points `x`. \n", "\n", "1. Plot the polynomial `y = (x - 1)^7` at points `x`. \n", "\n", "2. Explain what you found." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q5\n", "\n", "1. Show the **Sherman-Morrison formula**\n", "$$\n", "\t(\\mathbf{A} + \\mathbf{u} \\mathbf{u}^T)^{-1} = \\mathbf{A}^{-1} - \\frac{1}{1 + \\mathbf{u}^T \\mathbf{A}^{-1} \\mathbf{u}} \\mathbf{A}^{-1} \\mathbf{u} \\mathbf{u}^T \\mathbf{A}^{-1},\n", "$$\n", "where $\\mathbf{A} \\in \\mathbb{R}^{n \\times n}$ is nonsingular and $\\mathbf{u} \\in \\mathbb{R}^n$. This formula supplies the inverse of the symmetric, rank-one perturbation of $\\mathbf{A}$.\n", "\n", "2. Show the **Woodbury formula**\n", "$$\n", "\t(\\mathbf{A} + \\mathbf{U} \\mathbf{V}^T)^{-1} = \\mathbf{A}^{-1} - \\mathbf{A}^{-1} \\mathbf{U} (\\mathbf{I}_m + \\mathbf{V}^T \\mathbf{A}^{-1} \\mathbf{U})^{-1} \\mathbf{V}^T \\mathbf{A}^{-1},\n", "$$\n", "where $\\mathbf{A} \\in \\mathbb{R}^{n \\times n}$ is nonsingular, $\\mathbf{U}, \\mathbf{V} \\in \\mathbb{R}^{n \\times m}$, and $\\mathbf{I}_m$ is the $m \\times m$ identity matrix. In many applications $m$ is much smaller than $n$. Woodbury formula generalizes Sherman-Morrison and is valuable because the smaller matrix $\\mathbf{I}_m + \\mathbf{V}^T \\mathbf{A}^{-1} \\mathbf{U}$ is cheaper to invert than the larger matrix $\\mathbf{A} + \\mathbf{U} \\mathbf{V}^T$.\n", "\n", "3. Show the **binomial inversion formula**\n", "$$\n", "\t(\\mathbf{A} + \\mathbf{U} \\mathbf{B} \\mathbf{V}^T)^{-1} = \\mathbf{A}^{-1} - \\mathbf{A}^{-1} \\mathbf{U} (\\mathbf{B}^{-1} + \\mathbf{V}^T \\mathbf{A}^{-1} \\mathbf{U})^{-1} \\mathbf{V}^T \\mathbf{A}^{-1},\n", "$$\n", "where $\\mathbf{A}$ and $\\mathbf{B}$ are nonsingular.\n", "\n", "4. Show the identity\n", "$$\n", "\t\\text{det}(\\mathbf{A} + \\mathbf{U} \\mathbf{V}^T) = \\text{det}(\\mathbf{A}) \\text{det}(\\mathbf{I}_m + \\mathbf{V}^T \\mathbf{A}^{-1} \\mathbf{U}).\n", "$$\n", "This formula is useful for evaluating the density of a multivariate normal with covariance matrix $\\mathbf{A} + \\mathbf{U} \\mathbf{V}^T$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q6\n", "\n", "Show the following facts about triangular matrices. A unit triangular matrix is a triangular matrix with all diagonal entries being 1.\n", "\n", "1. The product of two upper (lower) triangular matrices is upper (lower) triangular.\n", "\n", "2. The inverse of an upper (lower) triangular matrix is upper (lower) triangular.\n", "\n", "3. The product of two unit upper (lower) triangular matrices is unit upper (lower) triangular.\n", "\n", "4. The inverse of a unit upper (lower) triangular matrix is unit upper (lower) triangular.\n", "\n", "5. An orthogonal upper (lower) triangular matrix is diagonal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q7\n", "\n", "1. Show that a symmetric matrix is positive semidefinite if and only if it is the covariance matrix of a random vector.\n", "\n", "2. Suppose two matrices $\\mathbf{A}, \\mathbf{B} \\in \\mathbb{R}^{n \\times n}$ are positive semidefinite. Show that their Hadamard product (elementwise product) $\\mathbf{A} \\circ \\mathbf{B} = (a_{ij} b_{ij})_{ij}$ is positive semidefinite.\n", "\n", "3. Suppose a symmetric matrix $\\mathbf{A} \\in \\mathbb{R}^{n \\times n}$ has entries $a_{ij} = i(n − j + 1)$ for $j \\ge i$. Show that $\\mathbf{A}$ is positive semidefinite." ] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.6.4", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.4" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "153px", "width": "252px" }, "navigate_menu": true, "number_sections": false, "sideBar": true, "skip_h1_title": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }