Biostat M280 Homework 1

Due Apr 21 @ 11:59PM

Q1

No handwritten homework reports are accepted for this course. We work with git and github. Efficient and abundant use of Git, e.g., frequent and well-documented commits, is an important criterion for grading your homework.

  1. Register an account on github.com using your UCLA email. Email your github username to teaching assistant Juhyun Kim juhkim111@ucla.edu, who will create a private repository for you.

  2. Top directories of the repository should be hw1, hw2, ... Create two branches master and develop. The develop branch will be your main playground, the place where you develop solution (code) to homework problems and write up report. 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.

  3. 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.

  4. Read the style guide 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).

Q2

Let's check whether floating-point numbers obey certain algebraic rules.

  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.

  2. Do floating-point numbers obey the associative rule for multiplication: (x * y) * z == x * (y * z)?

  3. Do floating-point numbers obey the distributive rule: a * (x + y) == a * x + a * y?

  4. Is 0 * x == 0 true for all floating-point number x?

  5. Is x / a == x * (1 / a) always true?

Q3

Consider Julia function

function g(k)
    for i = 1:10
        k = 5k - 1
    end
    k
end
  1. Use @code_llvm to find the LLVM bitcode of compiled g with Int64 input. Does the result surprise you?
  2. Use @code_llvm to find the LLVM bitcode of compiled g with Float64 input. Does the result surprise you?
  3. Read Julia documentation on @fastmath and repeat the last question on the function
function g_fastmath(k)  
    @fastmath for i = 1:10  
        k = 5k - 1
    end
    k
end

Q4

Create the vector x = (0.988, 0.989, 0.990, ..., 1.010, 1.011, 1.012).

  1. Plot the polynomial y = x^7 - 7x^6 + 21x^5 - 35x^4 + 35x^3 - 21x^2 + 7x -1 at points x.

  2. Plot the polynomial y = (x - 1)^7 at points x.

  3. Explain what you found.

Q5

Read in the matrix in the file longley.txt on the macroeconomic data from 1947 to 1962. The columns are Employment, Prices, GNP, Jobless, Military, PopSize and Year. Plot the pairwise scatter plot between these variables. What do you observe?

Q6

Let the $n \times n$ matrix H have elements H[i, j] = 1 / (i + j - 1).

  1. Write a function h(n) that outputs $n \times n$ matrix H. Try at least 3 ways, e.g., looping, comprehension, and vectorization. Compute and print H for n = 5.
  2. Compare their efficiencies at n = 1000.
  3. Try different rounding modes (use function setrounding(Float64, RoundingMode)) and report the entry inv(H)[1, 1] for n = 15.