Problem 1Fibonacci numbers and eigenvalues
100 ptsSummaryWrite the Fibonacci recurrence in matrix form, prove that eigenvectors of a symmetric matrix are orthogonal, derive a closed form through spectral decomposition, and implement it in NumPy without loops.
- Linear algebraEigendecompositionNumPy
Let us consider the following sequence:
Before starting this problem, make sure to run the following code first without any change:
# DO NOT CHANGE
import numpy as np
import matplotlib.pyplot as plt
""" END OF THIS PART """
-
Beyond importing libraries/modules/classes/functions in the preceeding cell, you are NOT allowed to import anything else for the following purposes:
-
As a part of your final solution. For instance, if a problem asks you to build a model without using sklearn but you use it, then you will not earn points.
-
Temporarily import something to assist you to get a solution. For instance, if a problem asks you to manually compute eigenvalues but you temporarily use
np.linalg.eigto get an answer and then delete your code, then you violate the rule.
Rule of thumb: Each part has its particular purpose to intentionally test you something. Do not attempt to find a shortcut to circumvent the rule.
-
-
All coding tasks shall run on CPUs, not GPUs.
Let , .
Manually write down for .
- Reasoning is not required.
Show official solutionHide solution
Official solution
The recursive equation that defines the sequence in this problem can be written in a matrix form:
where .
Compute .
-
Reasoning is not required.
-
The value of each entry in must be exact. For instance, the following values are exact: , , , , , . However, their float approximations are not exact.
Show official solutionHide solution
Official solution
Explain why is a symmetric matrix.
Show official solutionHide solution
Official solution
Because
matrix is symmetric.
In this part, you are asked to prove a general result that holds for any real-valued symmetric matrix.
Let be a real-valued symmetric matrix.
Consider the following eigenvalue equation
where and .
Let and be two distinct real eigenvalues and be two eigenvectors associated with them, respectively.
Prove that
That is, vectors and are orthogonal.
Show official solutionHide solution
Official solution
We use two approaches to compute
Approach 1
We have
Approach 1
We have
where the second equality follows from the property that is a symmetric matrix.
Two approaches should yield the same result. Equating (1) and (2), we have
Because , we must have
This completes the proof.
Let us go back to our matrix .
In the following eigenvalue equation
compute two eigenvalues and whose values are in a descending order.
- Reasoning is required.
Show official solutionHide solution
Official solution
We solve the following characteristic equation:
Thus, we compute that satisfies
Hence, all eigenvalues are
In the remaining parts of this problem, for all non-coding tasks, when you need to use those eigenvalues,
Since matrix is symmetric, let us do the spectral decomposition of it.
That is, you should write in the following form:
where
-
is an orthonormal matrix.
-
is a diagnomal matrix with
In this task, you need to compute . Reasoning is required.
Show official solutionHide solution
Official solution
To compute an eigenvector assocaited with the eigenvalue for , we solve the following equation:
Since the determintant of the 2-by-2 matrix on the L.H.S. is 0, the rank of this matrix is 1. Thus, it is sufficient to only solve the following equation:
One solution is
Thus, the th orthonormal vector is
Therefore,
Use the spectral decomposition result to derive a closed form of for any .
-
Reasoning is required.
-
Your answer shall be written in terms of , , and and .
Show official solutionHide solution
Official solution
For , we have
Thus, for , we have
For , we can
and
Therefore, the above answer holds for all .
Compute
-
Reasoning is required.
-
Your answer shall be written in terms of one or two eigenvalues.
Show official solutionHide solution
Official solution
We notice that and .
Hence,
Define a class called My_Fib.
-
Attributes:
-
Q: This attribute is matrix computed above. It is a numpy array with shape(2,2). -
lambdas: This attribute is a numpy array with shape(2,)that includes two eigenvalues computed above.
-
-
Method
__init__:- All atttribute values shall be initialized when an object in this class is constructed.
-
Method
compute_fib:-
This method computes the sequence values on designated indices.
-
You must use the spectral decomposition result to write this method.
-
You are not allowed to use any loop.
-
Inputs:
-
f0: The value of -
f1: The value of -
indices: a list/tuple/range object that includes indices with which the sequence values shall be computed.
For instance, if
indicestakes the value[3, 5, 9], we need to compute , , .In our test cases, you are guaranteed that
len(indices)is at least 1. You do not need to worry about the size of the test cases or whether the sequence values are too big (that is, on your side, you do not need to worry about those corner cases). -
-
Outputs:
-
Return a numpy array
fib_valueswith shape(len(indices),)and datatypeint32. -
fib_values[i]takes the value of .
For instance, if
indicestakes the value[3, 5, 9], thenfib_valueshas shape(3,). The values offib_values[0],fib_values[1],fib_values[2]are , , , respectively. -
-
Inside this method:
- Print
fib_values.
- Print
-
-
Method
plot_fib:-
This method plots indices vs. sequence values on those indices.
-
Inputs: The same as the method
compute_fib(f0, f1, indices). -
Outputs: None.
-
In your plot,
-
All data points with input indices are with marker
x. -
The linestyle is
--. -
The color is red.
-
The x-lable is: .
-
The y-label is: .
-
The title is: Fibounacci sequence.
-
-
Show official solutionHide solution
Official solution
class My_Fib:
def __init__(self):
self.lambdas = np.array([(1 + 5**.5)/2, (1 - 5**.5)/2])
self.Q = np.stack([self.lambdas / np.sqrt(1 + self.lambdas**2), np.ones(2) / np.sqrt(1 + self.lambdas**2)])
def compute_fib(self, f0, f1, indices):
fib_values = (np.power(self.lambdas, np.array(indices)[:, np.newaxis]) / np.sqrt(1 + self.lambdas**2)) \
@ (self.Q.T @ np.array([f1, f0]))
fib_values = fib_values.astype(np.int32)
print(fib_values)
return fib_values
def plot_fib(self, f0, f1, indices):
fib_values = self.compute_fib(f0, f1, indices)
plt.plot(indices, fib_values, marker = "x", linestyle = "--", color = "red")
plt.xlabel('$n$')
plt.ylabel('$F_n$')
plt.title('Fibounacci sequence')
Do the following tasks in this part.
-
Define an object of the class
My_Fibcalledmy_fib. -
Set
f0 = 0,f1 = 1,indices = range(10). -
Call method
my_fib.plot_fib(f0, f1, indices).
Show official solutionHide solution
Official solution
my_fib = My_Fib()
f0 = 0
f1 = 1
indices = range(10)
my_fib.plot_fib(f0, f1, indices)