σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
SciPy skill›Module 3 · Matrices, signals and structure›Topic 3.2·~45 minIntermediate

Sparse matrices

Skill positionModule 3 of 3 · Topic 2 of 5
3.2 Sparse matrices
—Lab score— / 100Total XP—Streak—

Store only what is not zero

Many large matrices are almost all zeros. A grid for pricing an option has a row for each price level, and each row touches only its two neighbours. A matrix like that is called sparse. Storing every zero wastes memory and time. scipy.sparse keeps only the non-zero entries. The format csr_matrix (compressed sparse row) uses three lists. data holds the non-zero values row by row, indices holds the column of each value, and indptr says where each row starts and ends inside data: row iii is data[indptr[i]:indptr[i+1]]. The last number of indptr is the count of non-zeros, nnz.

Different formats suit different jobs. coo_matrix stores plain (row, column, value) triples and is the easy way to build a matrix. csr_matrix is fast for row slices and for the product AxA\mathbf xAx, which costs about nnz multiplications instead of n2n^2n2. csc_matrix is the same idea by columns, and dia_matrix stores diagonals, ideal for banded matrices. For a system Ax=bA\mathbf x=\mathbf bAx=b, sparse.linalg.spsolve uses the zeros to be much faster than a dense solve. Sparse is not always better: if more than about a third of the entries are non-zero, the bookkeeping costs more than it saves. In finance, sparsity appears in pricing grids, in factor models where most loadings are zero, and when you keep only the strong correlations.

Ax costs ≈nnz multiplications,density=nnzn2,row i=data[indptr[i]:indptr[i+1]]A\mathbf x\ \text{costs}\ \approx \mathrm{nnz}\ \text{multiplications},\qquad \text{density}=\frac{\mathrm{nnz}}{n^{2}},\qquad \text{row } i=\texttt{data}[\texttt{indptr}[i]:\texttt{indptr}[i+1]]Ax costs ≈nnz multiplications,density=n2nnz​,row i=data[indptr[i]:indptr[i+1]](1)
Reading a CSR matrixTake the 4×44\times44×4 matrix with rows (5,0,0,1)(5,0,0,1)(5,0,0,1), (0,0,2,0)(0,0,2,0)(0,0,2,0), (0,0,0,0)(0,0,0,0)(0,0,0,0) and (3,0,0,4)(3,0,0,4)(3,0,0,4). In CSR, data=[5,1,2,3,4], indices=[0,3,2,0,3] and indptr=[0,2,3,3,5]. Row 222 is empty because indptr[2]=indptr[3]=3, and nnz=5. Multiplying by (1,1,1,1)(1,1,1,1)(1,1,1,1) adds up each row, giving (6,2,0,7)(6,2,0,7)(6,2,0,7) with only 555 multiplications. A tridiagonal matrix with n=1000n=1000n=1000, like a finite-difference pricing grid, has 2,9982{,}9982,998 non-zeros out of 1,000,0001{,}000{,}0001,000,000 entries, a density of 0.30%0.30\%0.30%. It needs about 404040 KB in CSR against 888 MB dense. On real data, the correlations of 555555 stocks from the shared price file form a 55×5555\times5555×55 matrix. Keeping only entries with ∣ρ∣>0.5|\rho|>0.5∣ρ∣>0.5 leaves 249249249 of 3,0253{,}0253,025 entries, 8.2%8.2\%8.2%, and the CSR copy takes 3.23.23.2 KB against 24.224.224.2 KB dense. At ∣ρ∣>0.6|\rho|>0.6∣ρ∣>0.6 only 4.3%4.3\%4.3% remain.
  • Three lists: data, indices and indptr describe a CSR matrix, and indptr[-1] is the number of non-zeros.
  • Build in COO, compute in CSR: triples are easy to assemble, row-based storage is fast for products and solves.
  • Sparse pays only when mostly zero: a dense matrix is better when many entries are non-zero.

Lab · what's graded

  • Counting non-zeros10 pts
  • How many values in a row10 pts
  • The density of a grid10 pts
  • A sparse product15 pts
  • Which format for which job15 pts
  • A tridiagonal system10 pts
  • When sparse pays and when it does not30 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

A CSR matrix has indptr=[0,2,3,3,5]. What is nnz, the number of stored non-zero values?

n=n =n=accepted: ± 0.01
2Numeric answer10 ptsNot answered

With indptr=[0,2,3,3,5], how many stored values does row 333 (the last row, counting from 000) have? It is indptr[4]-indptr[3].

k=k =k=accepted: ± 0.01
3Numeric answer10 ptsNot answered

A tridiagonal pricing matrix with n=1000n=1000n=1000 has 2,9982{,}9982,998 non-zeros among 1,000,0001{,}000{,}0001,000,000 entries. What is the density in percent? Give two decimals.

d=d =d=accepted: ± 0.01
4Numeric answer15 ptsNot answered

For the matrix with rows (5,0,0,1)(5,0,0,1)(5,0,0,1), (0,0,2,0)(0,0,2,0)(0,0,2,0), (0,0,0,0)(0,0,0,0)(0,0,0,0) and (3,0,0,4)(3,0,0,4)(3,0,0,4), what is the last entry of AxA\mathbf xAx for x=(1,1,1,1)\mathbf x=(1,1,1,1)x=(1,1,1,1)?

y=y =y=accepted: ± 0.01
5Match15 ptsNot answered

Match each sparse format with the job it suits best.

Pick the partner of each item on the left.

coo_matrix
csr_matrix
csc_matrix
dia_matrix
6Choice10 ptsNot answered

You must solve a tridiagonal system with n=100,000n=100{,}000n=100,000 unknowns at every time step of a pricing grid. What is the sensible choice?

7Explain in words30 ptsNot answered

In one or two sentences, explain why a sparse format saves memory and time for a mostly-zero matrix, and why it can lose to a dense matrix when most entries are non-zero.

Math modeKeyboard or LaTeX inputTask 1 of 7 · Ctrl ⏎ to check

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub