{"record":{"id":"eb900fc2e8fd2030","repo":"stanfordnlp/CoreNLP","slug":"number-of-iterations-must-be-positive","errorCode":null,"errorMessage":"Number of iterations must be positive!","messagePattern":"Number of iterations must be positive!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1729,"sourceCode":"   * difference between the means of the two arrays.  A randomized test\n   * statistic is computed the same way after initially randomizing the\n   * arrays by swapping each pair of elements with 50% probability.  For\n   * the given number of iterations, we generate a randomized test\n   * statistic and compare it to the actual test statistic.  The return\n   * value is the proportion of iterations in which a randomized test\n   * statistic was found to exceed the actual test statistic.\n   *\n   * @param A Outcome of one r.v.\n   * @param B Outcome of another r.v.\n   * @return Significance level by randomization\n   */\n  public static double sigLevelByApproxRand(double[] A, double[] B, int iterations) {\n    if (A.length == 0)\n      throw new IllegalArgumentException(\"Input arrays must not be empty!\");\n    if (A.length != B.length)\n      throw new IllegalArgumentException(\"Input arrays must have equal length!\");\n    if (iterations <= 0)\n      throw new IllegalArgumentException(\"Number of iterations must be positive!\");\n    double testStatistic = absDiffOfMeans(A, B, false); // not randomized\n    int successes = 0;\n    for (int i = 0; i < iterations; i++) {\n      double t =  absDiffOfMeans(A, B, true); // randomized\n      if (t >= testStatistic) successes++;\n    }\n    return (double) (successes + 1) / (double) (iterations + 1);\n  }\n\n  public static double sigLevelByApproxRand(int[] A, int[] B) {\n    return sigLevelByApproxRand(A, B, 1000);\n  }\n\n  public static double sigLevelByApproxRand(int[] A, int[] B, int iterations) {\n    if (A.length == 0)\n      throw new IllegalArgumentException(\"Input arrays must not be empty!\");\n    if (A.length != B.length)\n      throw new IllegalArgumentException(\"Input arrays must have equal length!\");","sourceCodeStart":1711,"sourceCodeEnd":1747,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1711-L1747","documentation":"The approximate randomization test in sigLevelByApproxRand needs a positive number of permutation iterations to estimate significance. Passing iterations <= 0 means zero samples of the null distribution, so an IllegalArgumentException(\"Number of iterations must be positive!\") is thrown.","triggerScenarios":"Calling sigLevelByApproxRand(double[] A, double[] B, 0) or with a negative iteration count — typically when the count comes from config parsed as 0/absent or a loop variable that never incremented.","commonSituations":"Evaluation configs where the iteration count field was left unset (defaulting to 0) or a user passed 0 to 'disable' the test instead of skipping the call.","solutions":["Pass a sensible iteration count (e.g. 1000, as the int[] convenience overload defaults to) before calling.","Read the iterations value from config with a validated default > 0.","Skip the significance test entirely in caller code when the user requested zero iterations, instead of passing 0.","Catch IllegalArgumentException and treat it as a configuration error with a clear message."],"exampleFix":"// before\ndouble p = ArrayMath.sigLevelByApproxRand(a, b, config.iterations);\n// after\nint iters = Math.max(1000, config.iterations);\ndouble p = ArrayMath.sigLevelByApproxRand(a, b, iters);","handlingStrategy":"validation","validationCode":"int iters = iterations > 0 ? iterations : 1000;\ndouble p = ArrayMath.sigLevelByApproxRand(a, b, iters);","typeGuard":null,"tryCatchPattern":"try {\n  p = ArrayMath.sigLevelByApproxRand(a, b, iterations);\n} catch (IllegalArgumentException e) {\n  // invalid configuration: iterations <= 0\n}","preventionTips":["Validate config values for iteration counts at load time","Use documented defaults (1000) instead of raw unset values","Skip the test explicitly rather than passing 0 iterations","Clamp or reject non-positive iteration settings in evaluation tooling"],"tags":["statistics","argument-validation","configuration","hypothesis-testing"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}