Bird
Raised Fist0
SciPydata~10 mins

Cholesky decomposition in SciPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Cholesky decomposition function from scipy.linalg.

SciPy
from scipy.linalg import [1]

# Now you can use the function for decomposition
Drag options to blanks, or click blank then click option'
Aeig
Binv
Ccholesky
Dsvd
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong function like 'inv' or 'eig'.
Using 'svd' which is for singular value decomposition, not Cholesky.
2fill in blank
medium

Complete the code to perform Cholesky decomposition on matrix 'A'.

SciPy
import numpy as np
from scipy.linalg import cholesky

A = np.array([[4, 2], [2, 3]])
L = cholesky([1], lower=True)
print(L)
Drag options to blanks, or click blank then click option'
AA
BL
Cnp
Dcholesky
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name instead of the matrix.
Passing the result variable 'L' before it is assigned.
3fill in blank
hard

Fix the error in the code to correctly compute the Cholesky decomposition.

SciPy
import numpy as np
from scipy.linalg import cholesky

B = np.array([[1, 2], [2, 1]])
L = cholesky([1], lower=True)
print(L)
Drag options to blanks, or click blank then click option'
Anp
BB
Ccholesky
DL
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name or other variables instead of the matrix.
Using a matrix that is not positive definite (this matrix is not positive definite, so decomposition will fail).
4fill in blank
hard

Fill both blanks to create a dictionary of Cholesky decompositions for matrices with keys 'X' and 'Y'.

SciPy
matrices = {'X': np.array([[9, 3], [3, 5]]), 'Y': np.array([[16, 4], [4, 10]])}
decompositions = {key: [1](mat, lower=True) for key, mat in matrices.items() if mat.shape[0] == [2]
print(decompositions)
Drag options to blanks, or click blank then click option'
Acholesky
B2
C1
Dnp.linalg.cholesky
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong function name like 'np.linalg.cholesky' when importing from scipy.linalg.
Checking for wrong matrix size.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores the upper triangular Cholesky decomposition for each matrix in 'data'.

SciPy
data = {'A': np.array([[25, 15], [15, 18]]), 'B': np.array([[36, 12], [12, 20]])}
results = { [1]: cholesky(mat, lower=[2]) for [3], mat in data.items() }
print(results)
Drag options to blanks, or click blank then click option'
A'A'
BFalse
Ckey
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable for keys.
Setting lower=True instead of False.
Not unpacking the dictionary items correctly.

Practice

(1/5)
1. What does Cholesky decomposition do to a matrix?
easy
A. It breaks the matrix into a lower-triangular matrix and its transpose.
B. It converts the matrix into a diagonal matrix only.
C. It finds the inverse of the matrix directly.
D. It changes the matrix into a symmetric matrix.

Solution

  1. Step 1: Understand Cholesky decomposition

    Cholesky decomposition splits a symmetric, positive definite matrix into a product of a lower-triangular matrix and its transpose.
  2. Step 2: Compare options with definition

    Only It breaks the matrix into a lower-triangular matrix and its transpose. correctly describes this decomposition as lower-triangular matrix times its transpose.
  3. Final Answer:

    It breaks the matrix into a lower-triangular matrix and its transpose. -> Option A
  4. Quick Check:

    Cholesky = Lower-triangular x Transpose [OK]
Hint: Cholesky = lower-triangular matrix x its transpose [OK]
Common Mistakes:
  • Confusing Cholesky with diagonalization
  • Thinking it finds matrix inverse
  • Assuming it changes matrix symmetry
2. Which of the following is the correct way to import and use Cholesky decomposition from scipy?
easy
A. from scipy.linalg import cholesky L = cholesky(A, lower=True)
B. import scipy.cholesky as chol L = chol.decompose(A)
C. from scipy.linalg import cholesky L = cholesky(A, upper=True)
D. import cholesky from scipy L = cholesky(A)

Solution

  1. Step 1: Recall correct import syntax

    The correct import is from scipy.linalg import cholesky.
  2. Step 2: Check usage parameters

    To get the lower-triangular matrix, use cholesky(A, lower=True). from scipy.linalg import cholesky L = cholesky(A, lower=True) matches this exactly.
  3. Final Answer:

    from scipy.linalg import cholesky\nL = cholesky(A, lower=True) -> Option A
  4. Quick Check:

    Correct import and lower=True parameter [OK]
Hint: Use from scipy.linalg import cholesky and lower=True [OK]
Common Mistakes:
  • Using wrong import path
  • Confusing upper and lower parameters
  • Incorrect function call syntax
3. Given the matrix A = [[4, 2], [2, 3]], what is the output of scipy.linalg.cholesky(A, lower=True)?
medium
A. [[4.0, 2.0], [2.0, 3.0]]
B. [[2.0, 1.0], [0.0, 1.73205081]]
C. [[2.0, 0.0], [1.0, 1.41421356]]
D. [[1.0, 0.0], [0.5, 1.0]]

Solution

  1. Step 1: Understand Cholesky on matrix A

    Matrix A is symmetric and positive definite, so cholesky(A, lower=True) returns lower-triangular L such that A = L x L.T.
  2. Step 2: Calculate L manually or verify

    L = [[2.0, 0.0], [1.0, 1.41421356]] satisfies A = L x L.T.
  3. Final Answer:

    [[2.0, 0.0], [1.0, 1.41421356]] -> Option C
  4. Quick Check:

    Lower-triangular matrix matches [[2.0, 0.0], [1.0, 1.41421356]] [OK]
Hint: Check lower-triangular matrix matches A = L x L.T [OK]
Common Mistakes:
  • Confusing upper and lower matrices
  • Using original matrix as output
  • Incorrect decimal approximations
4. What is the error in this code snippet?
import numpy as np
from scipy.linalg import cholesky
A = np.array([[1, 2], [3, 4]])
L = cholesky(A, lower=True)
medium
A. Parameter 'lower' is invalid in cholesky.
B. Missing import for numpy.linalg.
C. Matrix A must be integer type.
D. Matrix A is not symmetric positive definite, so cholesky fails.

Solution

  1. Step 1: Check matrix properties

    Matrix A = [[1,2],[3,4]] is not symmetric (A != A.T) and not positive definite.
  2. Step 2: Understand cholesky requirements

    Cholesky decomposition requires symmetric positive definite matrices; otherwise, it raises an error.
  3. Final Answer:

    Matrix A is not symmetric positive definite, so cholesky fails. -> Option D
  4. Quick Check:

    Cholesky needs symmetric positive definite matrix [OK]
Hint: Check matrix symmetry and positive definiteness before cholesky [OK]
Common Mistakes:
  • Ignoring matrix symmetry requirement
  • Assuming any matrix works
  • Misunderstanding parameter names
5. You have a covariance matrix C = [[2, -1], [-1, 2]]. How can you use Cholesky decomposition to generate correlated random samples X from independent standard normal samples Z?
hard
A. Use L = cholesky(C, lower=True) and compute X = Z @ L
B. Use L = cholesky(C, lower=True) and compute X = L @ Z
C. Use L = cholesky(C, upper=True) and compute X = L @ Z
D. Use L = cholesky(C, lower=True) and compute X = L.T @ Z

Solution

  1. Step 1: Decompose covariance matrix

    Compute L = cholesky(C, lower=True) so that C = L x L.T.
  2. Step 2: Generate correlated samples

    Multiply L by independent samples Z: X = L @ Z to introduce correlation matching C.
  3. Final Answer:

    Use L = cholesky(C, lower=True) and compute X = L @ Z -> Option B
  4. Quick Check:

    Correlated samples = L x independent samples [OK]
Hint: Multiply lower-triangular L by independent samples Z [OK]
Common Mistakes:
  • Multiplying in wrong order
  • Using upper matrix incorrectly
  • Multiplying by transpose instead of L
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard