mimo-openenv-software / tasks /format-code-task-002736.json
akseljoonas's picture
Publish validated twelve-task MiMo curriculum and immutable image request
2a3480c verified
Raw History Blame Contribute Delete
5.61 kB
{"cwd": "/testbed", "dataset_type": "opensource-code", "docker_image": "format-code-task-002736:latest", "instance_id": "format-code-task-002736", "problem_statement": "ENH: Allow recursive estimation for samp size less than window size in RollingOLS\n#### Is your feature request related to a problem? Please describe\r\nSometimes you want estimates for sample sizes smaller than the full window size. \r\n\r\n\r\n#### Describe the solution you'd like\r\nAllow recursive estimation before the full sample is reached in RollingOLS so that the window would be\r\n```\r\n[1,n1]\r\n[1,n1+2]\r\n...\r\n[1,window]\r\n[2,window+1]\r\n....\r\n```\r\n\r\nThis is expanding window until the full window length is reached.", "test_command": "bash /testbed/mimo_test_command.sh", "test_patch": "diff --git a/statsmodels/regression/tests/test_rolling.py b/statsmodels/regression/tests/test_rolling.py\nindex 4ffcda3b7..da03fa281 100644\n--- a/statsmodels/regression/tests/test_rolling.py\n+++ b/statsmodels/regression/tests/test_rolling.py\n@@ -283,3 +283,125 @@ def test_min_nobs(basic_data):\n mod = RollingOLS(y, x, 150, min_nobs=min_nobs)\n res = mod.fit()\n assert np.all(res.nobs[res.nobs != 0] >= min_nobs)\n+\n+\n+def _gen_simple(nobs=120, nvar=3, seed=12345):\n+ rs = np.random.RandomState(seed)\n+ x = rs.standard_normal((nobs, nvar))\n+ x = tools.add_constant(x)\n+ beta = np.array([1.0, -0.5, 0.7, 0.3])\n+ y = x @ beta + rs.standard_normal(nobs) * 0.5\n+ return y, x\n+\n+\n+def test_expanding_default_is_false():\n+\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 60\n+ min_nobs = 12\n+ mod = RollingOLS(y, x, window=window, min_nobs=min_nobs)\n+ res = mod.fit()\n+ params = np.asarray(res.params)\n+\n+\n+ assert np.all(np.isnan(params[:window - 1]))\n+ assert not np.any(np.isnan(params[window - 1:]))\n+\n+\n+def test_expanding_true_fills_pre_window_indices():\n+\n+\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 60\n+ min_nobs = 12\n+ mod = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True)\n+ res = mod.fit()\n+ params = np.asarray(res.params)\n+\n+ assert np.all(np.isnan(params[:min_nobs - 1]))\n+\n+\n+ assert not np.any(np.isnan(params[min_nobs - 1:window - 1]))\n+\n+ assert not np.any(np.isnan(params[window - 1:]))\n+\n+\n+def test_expanding_matches_ols_at_min_nobs():\n+\n+\n+ from statsmodels.regression.linear_model import OLS\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 60\n+ min_nobs = 10\n+ mod = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True)\n+ res = mod.fit()\n+ params = np.asarray(res.params)\n+ expected = OLS(y[:min_nobs], x[:min_nobs]).fit().params\n+ assert_allclose(params[min_nobs - 1], expected)\n+\n+\n+def test_expanding_matches_ols_within_expanding_phase():\n+\n+\n+ from statsmodels.regression.linear_model import OLS\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 50\n+ min_nobs = 8\n+ mod = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True)\n+ res = mod.fit()\n+ params = np.asarray(res.params)\n+\n+ for i in (min_nobs - 1, min_nobs + 5, window // 2, window - 2):\n+ expected = OLS(y[:i + 1], x[:i + 1]).fit().params\n+ assert_allclose(params[i], expected,\n+ err_msg='mismatch at i={}'.format(i))\n+\n+\n+def test_expanding_post_window_matches_rolling():\n+\n+\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 40\n+ min_nobs = 8\n+ res_roll = RollingOLS(y, x, window=window, min_nobs=min_nobs).fit()\n+ res_exp = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True).fit()\n+ params_roll = np.asarray(res_roll.params)\n+ params_exp = np.asarray(res_exp.params)\n+ assert_allclose(params_exp[window - 1:], params_roll[window - 1:])\n+\n+\n+def test_expanding_at_window_boundary_matches_rolling_first():\n+\n+\n+ from statsmodels.regression.linear_model import OLS\n+ y, x = _gen_simple(nobs=120, nvar=3)\n+ window = 30\n+ min_nobs = 5\n+ res_exp = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True).fit()\n+ params_exp = np.asarray(res_exp.params)\n+ expected = OLS(y[:window], x[:window]).fit().params\n+ assert_allclose(params_exp[window - 1], expected)\n+\n+\n+def test_expanding_nobs_grows():\n+\n+\n+ y, x = _gen_simple(nobs=100, nvar=3)\n+ window = 30\n+ min_nobs = 6\n+ mod = RollingOLS(y, x, window=window, min_nobs=min_nobs,\n+ expanding=True)\n+ res = mod.fit()\n+ nobs_arr = np.asarray(res.nobs)\n+\n+ for i in range(min_nobs - 1, window - 1):\n+ assert nobs_arr[i] == i + 1, \\\n+ 'nobs at i={} expected {} got {}'.format(i, i + 1, nobs_arr[i])\n+\n+ for i in range(window - 1, len(y)):\n+ assert nobs_arr[i] == window\ndiff --git a/test_commands.json b/test_commands.json\nnew file mode 100644\nindex 000000000..4706cf86e\n--- /dev/null\n+++ b/test_commands.json\n@@ -0,0 +1,3 @@\n+{\n+ \"test_commands\": [\"python -m pytest statsmodels/regression/tests/test_rolling.py -q -k expanding\"]\n+}\ndiff --git a/mimo_test_command.sh b/mimo_test_command.sh\nnew file mode 100755\n--- /dev/null\n+++ b/mimo_test_command.sh\n@@ -0,0 +1,8 @@\n+#!/usr/bin/env bash\n+\n+set -uo pipefail\n+cd /testbed\n+rc=0\n+python -m pytest statsmodels/regression/tests/test_rolling.py -q -k expanding\n+rc=$(( rc | $? ))\n+exit $rc\n", "verifier_timeout_sec": 1800}