Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MATH 16B: Homework 9

2022

Solve the following problems.  Implement the functions in the file hw9 .py, replacing the ellipses with your implementation of the function. Test your implementation before submitting on LATTE.                  You must submit the Python script,  exactly  in the form of the file hw9.py with the same filename.  Do not submit a Jupyter notebook.  Do not include extra code that executes your functions.

Note: Some functions this week expect a file path string as an argument. You are also given an example of a data file that might be used.  You can rely on all aspects of the formatting of this example data file(such as column names or data format), but your function should work correctly even if all the values are changed.

1.  (Least squares predictions). In this problem, we perform straightforward least-squares data fitting. Implement the function predict from model, with the following behavior:

• Accepts three numpy array arguments: features has shape (n,m), target has shape (n,), and unlabeled has shape (k,m) for some integers n,m,k .

• Fits an affine model to approximate the entries of target as a function of the rows of features using least squares.

• Returns the values predicted by model on the unlabeled data, as a numpy array of shape (k,).

That is, if A denotes the matrix given by features, b denotes the vector given by target, and B denotes the matrix given by unlabeled, then the function should first find θ ∈ Rm+1  minimizing

  A   1  θ b

and then return the vector

B   1  θ .

For example, if features and unlabeled both happen to equal to

 2(1)   1(0)

4(3)   0(1)

while target is

 4(2)

4

then the function would return

 

 

2.  (Moore’s law). Moore’s law is the observation that the number of transistors in a newly manufactured integrated circuit tends to double roughly every 18-24 months.  In this problem, we will attempt to precisely quantify the doubling time based on historical data.

Implement the function moore, with the following behavior:

• Accept a single string argument, file path, indicating the path of a CSV file

• Returns as a float the doubling time (in years) for transistor counts estimated via least squares for the data points in the file, as described below.

The file indicated by the file path argument will be a CSV file of a similar format as the transistors .csv file available on LATTE, but possibly with different values for the data. Each row gives data about        a specific integrated circuit that was produced. The  Year field indicates when the year the chip was        made, and the Transistors field indicates the number of transistors on the chip.

To estimate the doubling time, imagine there is a function f(t) giving the number of transistors per chip in year t.  If we had log2 (f(t)) = γt + β (where log2  indicates the base-2 logarithm), then the doubling time (in years) would be 1/γ, because

f(t0+ 1/γ) = 2γ(t0 +1/γ)+β  = 2γt0 +β+1 = 2f(t0 )

So, we can estimate the doubling time γ and β that give the best (least squares) approximation log2 (f(t)) ≈ γt + β

and then returning 1/γ . To do this, first compute the base-2 logarithm of the transistor counts (e.g., using the numpy .log2 function) and then fit the data to an affine function γt + β using the least squares method.

3.  (Minimizing validation error).  Suppose we wish to model a particular quantity based on a dataset with many features (say, m). We want to avoid overfitting caused by using too many of the features. One way to achieve this is by using out-of-sample validation decide how many features to include. A somewhat simplistic approach would be to fit a model using the first k features, with k ranging from

0 to m.  For each model, we compute the sum of squared residuals on a validation set, and choose the k with the smallest validation error. In this problem, you will find that number k for given data. Implement the function min validation error, with the following behavior:

• Accepts two arguments:  a numpy array called features of shape  (n,m), and a numpy array called b of shape (n,).

• Returns the integer k such that the following holds.  Let A be the matrix given by the first k columns of features.  Suppose the first 50% of the rows of A (rounded down and without shuffling) are selected to be the training set” and used to fit an affine model for predicting the values in (the first 50% of) b. The remaining 50% of the dataset should be used as a validation set.  Then the value k returned is the one that minimizes the sum of squared residuals on the

validation set, over all possible choices of k .

For example, if the features matrix was

  

2      1     3.01

1     0        0

   

and b was

 2 01(2)

3

2

  1(1)  

then the function would return 1.   (Using only the first column has smaller sum of squared residuals on the validation set, compared to using the first two columns or all three columns.)