stanfordnlp/CoreNLP · error · IllegalArgumentException

Number of iterations must be positive!

Error message

Number of iterations must be positive!

What it means

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.

Solutions

  1. Pass a sensible iteration count (e.g. 1000, as the int[] convenience overload defaults to) before calling.
  2. Read the iterations value from config with a validated default > 0.
  3. Skip the significance test entirely in caller code when the user requested zero iterations, instead of passing 0.
  4. Catch IllegalArgumentException and treat it as a configuration error with a clear message.

Example fix

// before
double p = ArrayMath.sigLevelByApproxRand(a, b, config.iterations);
// after
int iters = Math.max(1000, config.iterations);
double p = ArrayMath.sigLevelByApproxRand(a, b, iters);
Defensive patterns

Strategy: validation

Validate before calling

int iters = iterations > 0 ? iterations : 1000;
double p = ArrayMath.sigLevelByApproxRand(a, b, iters);

Try / catch

try {
  p = ArrayMath.sigLevelByApproxRand(a, b, iterations);
} catch (IllegalArgumentException e) {
  // invalid configuration: iterations <= 0
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/eb900fc2e8fd2030. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/math/ArrayMath.java:1729

   * difference between the means of the two arrays.  A randomized test
   * statistic is computed the same way after initially randomizing the
   * arrays by swapping each pair of elements with 50% probability.  For
   * the given number of iterations, we generate a randomized test
   * statistic and compare it to the actual test statistic.  The return
   * value is the proportion of iterations in which a randomized test
   * statistic was found to exceed the actual test statistic.
   *
   * @param A Outcome of one r.v.
   * @param B Outcome of another r.v.
   * @return Significance level by randomization
   */
  public static double sigLevelByApproxRand(double[] A, double[] B, int iterations) {
    if (A.length == 0)
      throw new IllegalArgumentException("Input arrays must not be empty!");
    if (A.length != B.length)
      throw new IllegalArgumentException("Input arrays must have equal length!");
    if (iterations <= 0)
      throw new IllegalArgumentException("Number of iterations must be positive!");
    double testStatistic = absDiffOfMeans(A, B, false); // not randomized
    int successes = 0;
    for (int i = 0; i < iterations; i++) {
      double t =  absDiffOfMeans(A, B, true); // randomized
      if (t >= testStatistic) successes++;
    }
    return (double) (successes + 1) / (double) (iterations + 1);
  }

  public static double sigLevelByApproxRand(int[] A, int[] B) {
    return sigLevelByApproxRand(A, B, 1000);
  }

  public static double sigLevelByApproxRand(int[] A, int[] B, int iterations) {
    if (A.length == 0)
      throw new IllegalArgumentException("Input arrays must not be empty!");
    if (A.length != B.length)
      throw new IllegalArgumentException("Input arrays must have equal length!");

View on GitHub (pinned to 1b7edd19c4)