code: private static float patternMatchVariance(int[] counters, int[] pattern, float maxIndividualVariance) {
      int numCounters = counters.length;
      int total = 0;
      int patternLength = 0;
      for (int i = 0; i < numCounters; i++) {
        total += counters[i];
        patternLength += pattern[i];
      }
      if (total < patternLength) {
        // If we don't even have one pixel per unit of bar width, assume this
        // is too small to reliably match, so fail:
        return Float.POSITIVE_INFINITY;
      }
      // We're going to fake floating-point math in integers. We just need to use more bits.
      // Scale up patternLength so that intermediate values below like scaledCounter will have
      // more "significant digits".
      float unitBarWidth = (float) total / patternLength;
      maxIndividualVariance *= unitBarWidth;
  
      float totalVariance = 0.0f;
      for (int x = 0; x < numCounters; x++) {
        int counter = counters[x];
        float scaledPattern = pattern[x] * unitBarWidth;
        float variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
        if (variance > maxIndividualVariance) {
          return Float.POSITIVE_INFINITY;
        }
        totalVariance += variance;
      }
      return totalVariance / total;
    }
docs: Determines how closely a set of observed counts of runs of black/white
  values matches a given target pattern. This is reported as the ratio of
  the total variance from the expected pattern proportions across all
  pattern elements, to the length of the pattern.
  
  @param counters observed counters
  @param pattern expected pattern
  @param maxIndividualVariance The most any counter can differ before we give up
  @return ratio of total variance between counters and pattern compared to total pattern size