QBUS1040-python代写
时间:2023-11-24
CONFIDENTIAL EXAM PAPER
This paper is not to be removed from the exam venue
Business Analytics
EXAMINATION
Semester 2 - Final, 2023
QBUS1040 Foundations of Business Analytics
EXAM WRITING TIME: 2 hours
READING TIME: 10 minutes
EXAM CONDITIONS:
This is a RESTRICTED OPEN book exam - specified materials permitted
MATERIALS PERMITTED IN THE EXAM VENUE:
(No electronic aids are permitted e.g. laptops, phones)
Non-programmable calculators
Physical language translation dictionaries
One A4 sheet of HANDWRITTEN notes SINGLE-SIDED
MATERIALS TO BE SUPPLIED TO STUDENTS:
Formula sheet
INSTRUCTIONS TO STUDENTS:
• Detach the formula sheet from the middle of the exam booklet
• You must use the mathematical notation from this course. We will not
accept other notation.
• You must only rely on concepts you have learnt from this course in your
solutions. We will deduct points for solutions that refer to outside material.
• Show all working. Marks may be deducted for careless or untidy work.
Please tick the box to confirm that your examination paper is complete.
(25 pages including cover sheet)
Room Number ________
Seat Number ________
Student Number |__|__|__|__|__|__|__|__|__|
ANONYMOUSLY MARKED
(Please do not write your name on this exam paper)
For Examiner Use Only
Question Mark
1
30 mins 24 marks
2
35 mins 24 marks
3
25 mins 16 marks
4
20 mins 16 marks
5
10 mins 10 marks
Total ________/90
This page has been intentionally left blank.
2
QBUS1040 Practice Exam S1 2023 SID:
Question 1. (24 marks) Short Calculations and Multiple Choice
(a) (1 mark) A matrix X is a right inverse of A if AX = I. You want to prove that a matrix is
right-invertible if and only if its AT is left-invertible, i.e. XTAT = I. Which of the following is the
most appropriate method of proof? Tick one ⇤:
⇤ induction proof
⇤ proof by counter example, i.e. choose A such that there is no X that satisfies AX = I
⇤ proof by counter example, i.e. choose A such that there is no X that satisfies XTAT = I
⇤ show that XTAT = I
⇤ show that AX = I implies XTAT = I and that XTAT = I implies AX = I
(b) (1 mark) Which one of the following notations is valid for a scalar ↵, n-vector x and an m ⇥ n
matrix A? Tick one ⇤:
⇤ |x|kAk
⇤ 2↵ |x|
⇤ ↵kATAk
⇤ A0 x
⇤ (Ix)2
(c) (1 mark) Given the vector a = (2, 7, 4), what is (e2 e1)Ta?
(d) (1 mark) Consider the function f(x) = (1, 1, 1, 1)Tx+2. Which of the following best describes f(x)?
Tick one ⇤:
⇤ linear
⇤ f : R4 ! R
⇤ quartic polynomial
⇤ least squares objective function
⇤ rf(x) = (0, 0, 0, 0)T
(e) (1 mark) Which of the following implies that the angle between the vectors x and y is 0? Tick one ⇤:
⇤ xT y = 0
⇤ xT y = kxkkyk
⇤ The correlation coecient between x and y is 0
⇤ 1kxT yk = 0
⇤ std(xT y) = 0
Page 3 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(f) (1 mark) Given the centroids z1 = (1, 1), z2 = (4, 4) and z3 = (1,5), which group (1, 2 or 3)
would the data point x1 = (1, 1) be assigned to? Provide a brief explanation.
-6
-6
-4
-4
-2
-2
0
0
2
2
4
4
6
6
x1⇥


z1
z2
z3
(g) (1 mark) Write down two 2-vectors x and y that are linearly independent, or state whether no such
pair of vectors exist.
(h) (1 mark) Suppose you have a 3⇥ 3 matrix X, which is a diagonal matrix. You construct the block
matrix Y =

X XT
X XT

. Draw the spy plot associated with the matrix Y .
6
1
5
2
4
3
3
4
2
5
1
6
6
1
5
2
4
3
3
4
2
5
1
6
We have provided you a second set of axes in case you make a mistake on your first set of axes.
Page 4 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(i) (1 mark) The following system of linear equations can be represented in the form Ax = b for
x = (x1, x2) and b = 1.
x1 = 1
x1 + x2 = 1
Write out the matrix A.
(j) (1 mark) Write down a matrix A that would lead to an under-determined system of linear equations
Ax = b.
(k) (1 mark) What is AB given A =

3 1
1 4

and B =

1 1
2 2

?
(l) (1 mark) Given that ATA = I, what do you know about the matrix A? Tick one ⇤:
⇤ A must be wide
⇤ A = A1
⇤ A must be a triangular matrix
⇤ The columns of A must be orthonormal (mutually orthogonal and normalised)
⇤ A = AT
Page 5 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(m) (1 mark) Given that the matrix A =
242 4 11 1 0
3 5 2
35 has the inverse A1 =
241 1.5 0.51 0.5 0.5
1 1 1
35, write
down the inverse of the matrix B =
242 1 34 1 5
1 0 2
35.
(n) (1 mark) Which of the following matrices would have both a left and right inverse? Tick one ⇤:
⇤ A =

1 2
2 1

⇤ A =

3 2

⇤ A =

1 1
1 1

⇤ A =
244 21 1
2 5
35 ⇤ A =
240 0 00 0 0
0 0 0
35
(o) (1 mark) The pseudo-inverse of the matrix A =
248 40 5
4 2
35 is A† = 0.1 0.1 0.05
0 0.2 0

. What is the
solution to the least squares problem:
minimise kAx bk2
where b = (2, 2, 1).
(p) (1 mark) Which of the following is a correct interpretation of the least squares problem where our
goal is to minimise kAx bk2? Tick one ⇤:
⇤ find a linear combination of columns of A that is closest to b
⇤ find A such that the columns of A are orthonormal
⇤ find a square matrix A such that A1 exists
⇤ find b such that b1 exists
⇤ find b such that b is normalised
Page 6 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(q) (1 mark) Suppose that you want to fit a straight line model to predict a person’s salary from their
age. You have the following data:
Age (years) Salary ($)
21 45 000
25 70 000
32 110 000
44 150 0000
Write out the matrix A required to formulate this as a least squares problem where your goal is to
minimise kAx bk2.
(r) (1 mark) You are fitting a polynomial model of the following form to your data:
fˆ(x) = ✓1 + ✓2x+ · · · + ✓p + xp1
Draw a sketch illustrating how you would expect the training and test root-mean-squared (RMS)
error to change as p increases. As indicated by the provided legend draw the RMS error on the
training data with a solid line and the RMS error on the test data with a dashed line.
Training Test
p
R
M
S
E
rr
or
Training Test
p
R
M
S
E
rr
or
We have provided you a second set of axes in case you make a mistake on your first set of axes.
Page 7 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(s) (1 mark) What does it mean for a model to have good generalisation ability?
(t) (1 mark) The goal of the bi-criterion problem is to minimise:
J = kA1x b1k2 + kA2x b2k
Suppose you calculated the residuals associated with each objective function separately so that
r1 = A1xˆ b1 and r2 = A2xˆ b2, where xˆ is the solution to the solution to the multi-objective least
squares problem. As you increased , what would you expect to happen to kr2k.
(u) (1 mark) Suppose that your are attempting to solve the constrained least squares problem:
minimise kAx bk2
subject to Cx = d
Suppose C =
2 0 1
0 1 1

and d =

1
2

. Write down a vector x, which is feasible.
Page 8 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(v) (1 mark) Suppose you are attempting to solve the least squares problem:
minimise x1 + x2 + x
2
3
subject to x1 + x2 = 1
x2 x3 = 0
What is the Lagrangian function associated with this constrained problem?
(w) (1 mark) What do you expect to be the output of the following code?
import numpy as np
n = 5
A = np.zeros(5)
for i in range(n):
A[i] = n
print(A)
(x) (1 mark) What do you expect to be the output of the following code? Tick one ⇤:
import numpy as np
A = np.array([[1, 0],
[0, 1]])
X = np.linalg.pinv(A)
print(X)
⇤ [1, 0, 0, 1]
⇤ [[1, 0]
[0, 1]]
⇤ numpy.linalg.LinAlgError: 1-dimensional array given.
Array must be at least two-dimensional
⇤ numpy.linalg.LinAlgError: Singular matrix
⇤ numpy.linalg.LinAlgError: AttributeError: module 'numpy.linalg' has no attribute 'pinv'.
Did you mean inv?
Page 9 of 25.
QBUS1040 Practice Exam S1 2023 SID:
Question 2. (24 marks) Proofs and Calculations
(a) (4 marks) Consider the matrix A =

5 1 2
0 1 2

. What is A†?
(b) (3 marks) Write down a matrix A which is a 3⇥ 4 matrix and nnz(A) = 2 and kAk = 5.
Page 10 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(c) You want to find a 4-vector x, which is closest to 1 among all 4-vectors that have an average value
of 2.
i. (2 marks) Formulate this as a constrained least squares problem of the form:
minimise kAx bk2
subject to Cx = d
i.e. state A, b, C and d.
ii. (4 marks) Find xˆ, which is a solution to the constrained least squares problem you formulated
in part i.
Page 11 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(d) i. (6 marks) Using induction show that for n-vectors a1, . . . , am and b that
(a1 + · · · + am)T b = aT1 b+ · · · + aTmb
for m 2.
Page 12 of 25.
QBUS1040 Practice Exam S1 2023 SID:
ii. (5 marks) Suppose a1, . . . , ak are orthonormal n-vectors, and x = 1a1 + · · · + kak, where
1, . . . ,k are scalars. Express kxk in terms of = (1, . . . ,k).
Page 13 of 25.
QBUS1040 Practice Exam S1 2023 SID:
Question 3. (16 marks) Written Responses
(a) (3 marks) Given A an n ⇥ n matrix with non-zero entries, is it computationally more ecient to
calculate A I or I A? Explain your answer.
(b) (3 marks) Given that the correlation coecient ⇢ between the vectors a and b is less than 0 where
a 6= 0, b 6= 0 and avg(a) = avg(b) = 0, what can you say about the angle between a and b? Explain
your answer.
Page 14 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(c) (4 marks) Suppose you are performing k-means clustering. Describe a scenario which achieves
Jclust = 0.
Page 15 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(d) (4 marks) You run the Gram-Schmidt algorithm on the vectors a1, . . . , ak and the algorithm runs
successfully so that it gives you the vectors q1, . . . , qk. You run your Gram-Schmidt algorithm again,
but this time on the set of vectors q1, . . . , qk. Describe what happens the second time you run the
Gram-Schmidt algorithm. Does the algorithm run to completion or does it terminate early? If
it runs to completion describe the vectors it outputs. If it terminates, at what iteration does it
terminate?
Page 16 of 25.
QBUS1040 Practice Exam S1 2023 SID:
(e) (2 marks) We proved in lectures that the Gram matrix G = ATA is invertible if and only if A has
linearly independent columns. Explain why it then follows that G = AAT is invertible if and only
if A has linearly independent rows.
Page 17 of 25.
QBUS1040 Practice Exam S1 2023 SID:
Question 4. (16 marks) Python Comprehension
(a) You have the following data, and your goal is to fit a polynomial model to this data. Your data
consists of 24 training samples and 6 test samples.
You write the following code:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 def vandermonde(t, n):
5 m = t.shape[0]
6 A = np.zeros((m, n))
7
8 for j in range(m):
9 for i in range(n):
10 A[j, i] = t[j]**i
11
12 return A
13
14 train = np.load('train.npy')
15 test = np.load('test.npy')
16
17 x_train = train[:, 0]; y_train = train[:, 1]
18 x_test = test[:, 0]; y_test = test[:, 1]
19
20 degree = 8
21
22 A_train = vandermonde(x_train, degree + 1)
23 theta_hat = np.linalg.pinv(A_train) @ y_train
24
Page 18 of 25.
QBUS1040 Practice Exam S1 2023 SID:
i. (2 marks) Write out the function fˆ(x), which describes the model that is being fitted.
ii. (2 marks) You add a line of code to print the shape of A_train on line 24:
24 print(A_train.shape)
What would be the result of this print statement. Briefly explain your answer.
Page 19 of 25.
QBUS1040 Practice Exam S1 2023 SID:
iii. (4 marks) You want to perform regularisation so that your goal is now to minimise
kA✓ yk2 + k✓2:pk2
To formulate this as a ‘bi-criterion’ least squares problem of the form
minimise kA1✓ b1k2 + kA2✓ b2k2
where the second objective correspond to the regularisation term, what would the matrix A2
look like?
You aim to implement regularisation as described in part iii. Your new code is as follows:
1 import numpy as np
2
3 def vandermonde(t, n):
4 m = t.shape[0]
5 A = np.zeros((m, n))
6
7 for j in range(m):
8 for i in range(n):
9 A[j, i] = t[j]**i
10 return A
11
12 train = np.load('train.npy'); test = np.load('test.npy')
13
14 x_train = train[:, 0]; y_train = train[:, 1]
15 x_test = test[:, 0]; y_test = test[:, 1]
16 degree = 8; lamb = 4
17
18 A_train = vandermonde(x_train, degree + 1)
19 A_reg = np.sqrt(lamb)*np.eye(degree+1)
20 A_tilde = np.concatenate((A_train, A_reg), axis=0)
21
22 zeros = np.zeros(degree)
23 y_tilde = np.concatenate((y_train, zeros))
24
25 theta_hat = np.linalg.pinv(A_tilde) @ y_tilde
26
Page 20 of 25.
QBUS1040 Practice Exam S1 2023 SID:
iv. (3 marks) If you were to print(A_reg), what would be the result of this print statement.
Briefly explain your answer.
v. (2 marks) Is the matrix A_reg (from part iv.) the same as A2 (from part iii.)? Briefly explain
your answer.
vi. (3 marks) Suppose you ran the code given to you in part iv. but you change line 14 from:
14 degree = 8; lamb = 4
to:
14 degree = 8; lamb = 10e10
If you added a print to line 26:
26 print(theta_hat)
What would be the result of this print statement. Explain your answer.
Page 21 of 25.
QBUS1040 Practice Exam S1 2023 SID:
Question 5. (10 marks) Python Code
Write a function called is upper triangular in the file funcs.py, which determines whether a given
matrix A is an upper triangular matrix. Your function should return True if it is an upper triangular
matrix, otherwise it should return False.
Example 1
import numpy as np
import funcs
A = np.array([[1, 1, 1],
[0, 1, 1],
[0, 0, 1]])
print(funcs.is_upper_triangular(A))
Output: True
Example 2
import numpy as np
import funcs
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(funcs.is_upper_triangular(A))
Output: False
Page 22 of 25.
QBUS1040 Practice Exam S1 2023 SID:
Write your code in the box below and follow the provided indentation lines.
funcs.py
Page 23 of 25.
This page has been intentionally left blank.
24
This page has been intentionally left blank.
essay、essay代写