exportHistogram method

String exportHistogram(
  1. {bool normalize = true,
  2. int intervals = 0,
  3. ProbabilityDensity? pdf,
  4. dynamic verbose = false,
  5. String commentCharacter = '#',
  6. int precision = 20}
)

Builds a histogram from the entries of this and writes the data records to a String.

  • If normalize == true the histogram count will be normalized such that the total histogram area is equal to 1 .0.
  • intervals: The number of same-size intervals used to construct the histogram.
  • pdf: The probability density function used to calculate the third column.
  • verbose: Logical flag indicating if sample stats should be printed as comments above the histogram data.
  • commentCharacter: The String character used to prefix comments. It defaults to # the comment character used by Gnuplot.
  • precision is used when converting numbers to a String.

Implementation

String exportHistogram({
  bool normalize = true,
  int intervals = 0,
  ProbabilityDensity? pdf,
  verbose = false,
  String commentCharacter = '#',
  int precision = 20,
}) {
  final b = StringBuffer();
  if (length < 2) {
    b.writeln('$commentCharacter Could not generate histogram. '
        '$this is too short.');
    return b.toString();
  }
  final stats = Stats(this);

  final hist = stats.histogram(
    intervals: intervals,
    normalize: normalize,
    probabilityDensity: pdf,
  );

  final bool hasPdf = pdf != null;

  // hist[0] = [min, min + intervalSize, ..., max].
  final intervalSize = hist[0][1] - hist[0][0];
  // Actual intervals used to build the histogram.
  final gridPoints = hist[0].length;
  final actualIntervals = gridPoints - 1;

  if (verbose) {
    b.writeln('$commentCharacter Intervals: $actualIntervals');
    b.writeln('$commentCharacter Min: '
        '${stats.min.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Max: '
        '${stats.max.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Mean: '
        '${stats.mean.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter StdDev: '
        '${stats.stdDev.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Median: '
        '${stats.median.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter First Quartile: '
        '${stats.quartile1.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Third Quartile: '
        '${stats.quartile3.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Interval size: '
        '${intervalSize.toStringAsPrecision(precision)}');
    b.writeln('$commentCharacter Integrated histogram: '
        '${hist[1].sum() * intervalSize}');
    b.writeln(commentCharacter);
    b.writeln('$commentCharacter ------------------------------------'
        '-------------------------');
  }
  if (hasPdf) {
    b.write('$commentCharacter     Range      Prob. Density     '
        'Prob. Density Function\n');
  } else {
    b.write(
        '$commentCharacter     Range     Count    Prob. Density Func. \n');
  }
  for (var i = 0; i < gridPoints; ++i) {
    b.writeln('${hist[0][i].toStringAsPrecision(precision)}     '
        '${hist[1][i].toStringAsPrecision(precision)}     '
        '${hist[2][i].toStringAsPrecision(precision)}');
  }
  return b.toString();
}