LIVE·WAITING…
connecting to the live quote stream…
--:--:--UTC
#18
Pairs spread & cointegration
hardQuantacceptance —~45 min
Implement pairs_spread(x, y) — the first step of a pairs trade: regress y on x with an intercept by ordinary least squares to get the hedge ratio beta, then measure the residual spread y - a - beta·x. Return [beta, spread_std] where spread_std is the population standard deviation of the residuals.
The hedge ratio sizes the two legs; the residual's spread is what you trade around. Return both rounded to 4 decimal places.
example 1
input
x=[-2.0, -1.0, 0.0, 1.0, 2.0], y=[1.0, 2.5, 4.0, 5.5, 7.5]
output
[1.6, 0.1414]
example 2
input
x=[10.0, 11.0, 12.0, 13.0, 14.0], y=[21.0, 22.5, 24.0, 25.5, 27.5]
output
[1.6, 0.1414]
constraints
- 3 ≤ len(x) == len(y) ≤ 5_000
- x is not constant
teacher · 2 hints · the first is free
↗ read the theory
the chapter this problem is cut from
▶ open in the workbench
run the same math on a real tape
●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 pairs_spread()
x=[-2.0, -1.0, 0.0, 1.0, 2.0], y=[1.0, 2.5, 4.0, 5.5, 7.5]
expected
[1.6, 0.1414]
the judge calls pairs_spread() 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