LIVE
connecting to the live quote stream…
--:--:--UTC
easyMaths○
#35

Portfolio variance

easyMathsacceptance —~10 min

Implement portfolio_variance(w, cov) — wᵀ·Σ·w for a weight vector w and a covariance matrix cov given as a list of rows: the double sum over i, j of w[i]·w[j]·cov[i][j].

Diversification lives entirely in the off-diagonal terms; a risk model that only adds up variances has no idea two positions can hedge each other. Return the variance rounded to 6 decimal places.

example 1
input
w=[0.6, 0.4], cov=[[0.04, 0.0], [0.0, 0.09]]
output
0.0288
example 2
input
w=[0.6, 0.4], cov=[[0.04, 0.012], [0.012, 0.09]]
output
0.03456
note

Same weights and variances, now with a positive covariance — the answer must rise.

constraints
  • 1 ≤ n ≤ 20
  • cov is symmetric
teacher · 2 hints · the first is free
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to portfolio_variance()
w=[0.6, 0.4], cov=[[0.04, 0.0], [0.0, 0.09]]
expected
0.0288

the judge calls portfolio_variance() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice