σμ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
  • 日本語
  • 中文
Linear Algebra skill›Module 1 · Vectors and matrices›Topic 1.3·~40 minBeginner

Matrix multiplication and its cost

Skill positionModule 1 of 4 · Topic 3 of 3
1.3 Matrix multiplication and its cost
—Lab score— / 100Total XP—Streak—

Composing maps

The product ABABAB is the map you get by applying BBB first and then AAA: (AB)x=A(Bx)(AB)\mathbf{x}=A(B\mathbf{x})(AB)x=A(Bx). Entry (i,j)(i,j)(i,j) of the product is row iii of AAA dotted with column jjj of BBB, so the shapes must fit: an m×nm\times nm×n matrix times an n×pn\times pn×p matrix gives an m×pm\times pm×p matrix. The product is associative, (AB)C=A(BC)(AB)C=A(BC)(AB)C=A(BC), and it distributes over sums, but in general it is not commutative: AB≠BAAB\neq BAAB=BA because the order of two actions matters.

(AB)ij=∑k=1nAikBkj,(AB)T=BTAT(AB)_{ij}=\sum_{k=1}^{n}A_{ik}B_{kj},\qquad (AB)^{T}=B^{T}A^{T}(AB)ij​=k=1∑n​Aik​Bkj​,(AB)T=BTAT(1)

Computing ABABAB for an m×nm\times nm×n and an n×pn\times pn×p matrix takes mnpmnpmnp multiply-adds (about 2mnp2mnp2mnp floating-point operations). For two n×nn\times nn×n matrices that is n3n^3n3, so doubling nnn makes the work eight times larger. A matrix times a vector costs only n2n^2n2. Because the product is associative, the order of the brackets does not change the answer but can change the cost enormously.

Order matters, twiceWith A=(1201)A=\begin{pmatrix}1&2\\0&1\end{pmatrix}A=(10​21​) and B=(0110)B=\begin{pmatrix}0&1\\1&0\end{pmatrix}B=(01​10​): AB=(2110)AB=\begin{pmatrix}2&1\\1&0\end{pmatrix}AB=(21​10​) but BA=(0112)BA=\begin{pmatrix}0&1\\1&2\end{pmatrix}BA=(01​12​), so the order changes the answer. Brackets change the cost: for n×nn\times nn×n matrices A,BA,BA,B and a vector v\mathbf{v}v, (AB)v(AB)\mathbf{v}(AB)v costs n3+n2n^3+n^2n3+n2 multiply-adds while A(Bv)A(B\mathbf{v})A(Bv) costs 2n22n^22n2. For n=1000n=1000n=1000 that is a thousand million against two million.
  • Shape rule: (m×n)(n×p)=(m×p)(m\times n)(n\times p)=(m\times p)(m×n)(n×p)=(m×p); the inner sizes must match.
  • Not commutative: AB≠BAAB\neq BAAB=BA in general, so never swap factors.
  • Cost: mnpmnpmnp multiply-adds; matrix times vector is n2n^2n2, matrix times matrix is n3n^3n3.
Bracket your products from the vector sideWhen a chain of matrices ends in a vector, multiply from the vector side: A(B(Cv))A(B(C\mathbf{v}))A(B(Cv)) uses only matrix-vector products. Portfolio risk wTΣw\mathbf{w}^{T}\Sigma\mathbf{w}wTΣw is computed as w⋅(Σw)\mathbf{w}\cdot(\Sigma\mathbf{w})w⋅(Σw), never by first forming a matrix from w\mathbf{w}w.

Lab · what's graded

  • The product AB20 pts
  • The product BA15 pts
  • The shape of a product10 pts
  • Counting multiply-adds15 pts
  • Two ways to bracket20 pts
  • Why the bracketing matters20 pts
0/ 6 answered
Tasks
1Matrix · vector20 ptsNot answered

Let A=(1201)A=\begin{pmatrix}1&2\\0&1\end{pmatrix}A=(10​21​) and B=(0110)B=\begin{pmatrix}0&1\\1&0\end{pmatrix}B=(01​10​). Fill in ABABAB.

AB=AB =AB=
2Matrix · vector15 ptsNot answered

Let A=(1201)A=\begin{pmatrix}1&2\\0&1\end{pmatrix}A=(10​21​) and B=(0110)B=\begin{pmatrix}0&1\\1&0\end{pmatrix}B=(01​10​). Fill in BABABA and notice that it differs from ABABAB.

BA=BA =BA=
3Choice10 ptsNot answered

PPP is 3×43\times43×4 and QQQ is 4×24\times24×2. What is the shape of PQPQPQ?

4Numeric answer15 ptsNot answered

How many multiply-adds does it take to multiply a 200×300200\times300200×300 matrix by a 300×100300\times100300×100 matrix?

[objectObject]=[object Object] =[objectObject]=accepted: ± 0.1%
5Numeric answer20 ptsNot answered

AAA and BBB are 1000×10001000\times10001000×1000 and v\mathbf{v}v has 1000 entries. How many times more multiply-adds does (AB)v(AB)\mathbf{v}(AB)v need than A(Bv)A(B\mathbf{v})A(Bv)? Use n3+n2n^3+n^2n3+n2 against 2n22n^22n2.

[objectObject]=[object Object] =[objectObject]=accepted: ± 1
6Explain in words20 ptsNot answered

In two sentences, say why you should compute A(Bv)A(B\mathbf{v})A(Bv) rather than (AB)v(AB)\mathbf{v}(AB)v, and whether the answers differ.

Math modeKeyboard or LaTeX inputTask 1 of 6 · 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