Autograd, Optimization, Visualization

Prerequisite homework

Prerequisite homework

  • Total marks: 130
  • Due on 01/23 before class. Submit on brightspace. Submit the code (cpp/py/ipynb) files separately on brightspace. No screenshots embedded in pdf please.

Problem 1

(20 marks)

The factorial of a non-negative integer n , denoted by $n!$, is the product of all positive integers less than or equal to n . The factorial of n also equals the product of n with the next smaller factorial:

$n ! = n × ( n − 1 ) × ( n − 2 ) × ( n − 3 ) × ⋯ × 3 × 2 × 1 = n × ( n − 1 ) !$

For example, $5 ! = 5 × 4 ! = 5 × 4 × 3 × 2 × 1 = 120.$ The value of 0! is 1, according to the convention for an empty product.

Write a recursive C or Python function to compute factorial of a natural number n. It should pass the test_factorial function 10 times. test_factorial function is given.

C programmers: download and edit the file test_factorial.c .

Python programmers: write functions similar to test_factorial.c including the test functions.

Complete the funciton and submit the file as a separate file to brightspace.

Problem 2

(20 marks)

A natural number (1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if it is greater than 1 and cannot be written as the product of two smaller natural numbers.

Write a C or Python function to calculate if a number is prime. Return 1 if it is prime and 0 if it is not a prime. If the number is not a prime number, then a factor exists. Return the factor as through a pointer.

C programmers: Download and edit the file test_prime.c. Complete the funciton and submit the file as a separate file to brightspace.

Python programmers: write functions similar to test_factorial.c including the test functions.

Complete the funciton and submit the file as a separate file to brightspace.

Problem 3

(20 marks)

Write a C or Python function named days() that determines the number of days from the turn of the century (01/01/2000) for any date within the century passed as a structure. The date structure should use the template

struct date {
    int month;
    int day;
    int year;
}

In writing the days() function, use the convention that all years have 360 days and each month consists of 30 days. The function should return the number of days for any date structure passed to it. Make sure to declare the returned variable a long integer to reserve sufficient room for dates such as 12/19/2099.

Problem 4: Probability and statistics

(30 marks)

  1. Write the definitions of Random Variable, Expectation, Variance, a joint probability distribution and a conditional probability distribtion? (5 marks)
  2. What is the difference between Expectation and a sample mean? When do they converge? (5 marks)
  3. Write the three axioms (properties) of a Probability distribution (measure). (5 marks)
  4. What is the difference between Probability density function, Probability mass function and Cumulative distribution function. (5 marks)
  5. Prove that for any Random Variable $X$, the variance $V(X) = E[X^2] - E[X]^2$. (5 marks)
  6. Prove that $E[X^2] > 0$ (5 marks)

Problem 5

(20 marks)

Resources: Khan academy

  1. Find the mininimum point the function $f(x) = x^2 - 6x + 33$ using calculus. Write the property of derivatives that made you reach the answer? (10 marks)

  2. Find the mininimum point the function $f(x, y) = x^2 - 6x + y^2 - 8y - xy + 33$ using calculus. Write the property of derivatives that made you reach the answer? (10 marks)

Problem 6

(20 marks)

  1. Given two matrices $A$ of size $m \times n$ and $B$ of size $p \times q$, when is the matrix multiplication $AB$ valid? When is the matrix multiplication $BA$ valid? When is the addition $A + B$ valid? (5 marks)

  2. Define dot product for two vectors? How to test when two given vectors are perpendicular? Assume you have two n-dimensional vectors $\vec{a} = [a_1, a_2, \dots, a_n]$ and $\vec{b} = [b_1, b_2, \dots, b_n]$. Denote dot product as, $\vec{a} \cdot \vec{b}$? (5 marks)

  3. Define cross product for two vectors? How to test when two vectors are parallel to other? (5 marks)

  4. What is a unit vector? How to can you find the magnitude of a vector? (5 marks)