All problems
Runs Test Expectation and Variance
Problem
For a uniformly random binary sequence with n1 ones and n2 zeros, return [expected runs, variance of runs]:
E[R] = 2 * n1 * n2 / (n1 + n2) + 1 Var[R] = 2 * n1 * n2 * (2 * n1 * n2 - n1 - n2) / ((n1 + n2)^2 * (n1 + n2 - 1)).
If either count is 0, return [1.0, 0.0] for a nonempty sequence and [0.0, 0.0] for an empty one.
Hint
Runs are counted by the number of value alternations plus one.
Test cases
case 1
in: [5,5]
expected: [6,2.2222222222222223]
case 2
in: [3,7]
expected: [5.2,1.4933333333333334]
case 3
in: [1,1]
expected: [2,0]
case 4
in: [0,5]
expected: [1,0]
Starter codePython
def runs_test_statistic(n1, n2):
# Your code here
pass4 test cases · run your code against them in the editor