All problems
Ljung-Box Statistic Lite
Problem
Compute the Ljung-Box portmanteau statistic from a list of sample autocorrelations: Q = n * (n + 2) * sum over k = 1..h of rho_k^2 / (n - k), where rho_k is acf_values[k-1].
Return 0.0 if h is not positive or n <= h. Larger Q indicates stronger serial correlation.
Hint
Each lag is weighted by 1 / (n - k), which grows with k.
Test cases
case 1
in: [[0.2,-0.1,0.05],100,3]
expected: 5.424915045680878
case 2
in: [[0.5,-0.3,0.2,0.1],50,4]
expected: 20.918289471200133
case 3
in: [[0.1],20,1]
expected: 0.2315789473684211
case 4
in: [[0.9],10,1]
expected: 10.8
Starter codePython
def ljung_box(acf_values, n, h):
# Your code here
pass4 test cases · run your code against them in the editor