The package sample_statistics provides helpers for
calculating statistics of numerical samples and generating/exporting
histograms. It includes common probability
distribution functions, an approximation of the error function,
and random sample generators.
Throughout the library the acronym pdf stands for Probability Distribution Function, while cdf stands for Cummulative Distribution Function.
To use this package include sample_statistics
as a dependency in your pubspec.yaml file.
To access sample statistics use the class Stats.
It calculates sample statistics in a lazy fashion and caches results
to avoid lengthy calculations when the same quantity is accessed repeatedly.
import 'package:sample_statistics/sample_statistics.dart'
void main() {
final sample = <num>[-10, 0, 1, 2, 3, 4, 5, 6, 20];
final stats = Stats(sample);
print('\nRunning statistic_example.dart ...')
print('Sample: $sample');
print('min: ${stats.min}');
print('max: ${stats.max}');
print('mean: ${stats.mean}');
print('median: ${stats.median}');
print('first quartile: ${stats.quartile1}');
print('third quartile: ${stats.quartile3}');
print('interquartile range: ${stats.iqr}');
print('standard deviation: ${stats.stdDev}');
final outliers = sample.removeOutliers();
print('outliers: $outliers');
print('sample with outliers removed: $sample');
stats.addDataPoints([-2, 7]);
print('Sample with additional data points: ${stats.sample}');
print('Sorted sample: ${stats.sortedSample}');
}Click to show console output.
$ dart sample_statistics_example.dart
Running sample_statistics_example.dart ...
Sample: [-10, 0, 1, 2, 3, 4, 5, 6, 20]
min: -10
max: 20
mean: 3.4444444444444446
median: 3
first quartile: 1
third quartile: 5
inter-quartile-range:4
standard deviation: 7.779960011322538
outliers:[-10, 20]
Sample without outliers: [0, 1, 2, 3, 4, 5, 6]
Sample with additional data points: [0, 1, 2, 3, 4, 5, 6, -2, 7]
Sorted sample: [-2, 0, 1, 2, 3, 4, 5, 6, 7]
To package includes extension methods on Stats
provides methods for generating and exporting histograms:
import 'package:sample_statistics/sample_statistics.dart';
void main(List<String> args) {
final sample = [
-10, -8, -5,-4, -3, -3,-1, -1, -1, 0, 0, 0,
0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 7, 10, 14,
];
final stats = Stats(sample);
print(stats.exportHistogram(verbose: true, normalize: false));
print(stats.blockHistogram());
}The console output is show below:
$ dart example/bin/histogram_example.dart
# Intervals: 8
# Min: -10.00000000
# Max: 14.00000000
# Interval size: 3.428571429
# Mean: 0.8461538462
# StdDev: 5.065114472
# Median: 0.5000000000
# First Quartile: -1.000000000
# Third Quartile: 3.000000000
# Histogram integral: 89.14285714285715
#
# -------------------------------------------------------------
# Interval Mid-Point Count
-10.00000000 1.000000000
-6.571428571 2.000000000
-3.142857143 3.000000000
0.2857142857 9.000000000
3.714285714 8.000000000
7.142857143 1.000000000
10.57142857 1.000000000
14.00000000 1.000000000
▁▂▃▉█▁▁▁
On a monochrome terminal the block histogram is rendered as shown above. On a terminal with Ansi support, the block histogram is rendered as:
If a block contains the
The image below shows the histogram data shown above
plotted using gnuplot.
The library sample_generators includes functions for generating random samples
that follow the probability distribution functions listed below:
- normal distribution,
- truncated normal distribution,
- exponential distribution,
- uniform distribution,
- triangular distribution.
Additionally, the library includes the function randomSample
which is based on the rejection sampling method.
It expects a callback of type ProbabilityDensity
and can be used to generate random samples that follow
an arbitrary probability distribution function.
The program listed below demonstrates how to generated a random sample and write a histogram to a file.
import 'dart:io';
import 'package:sample_statistics/sample_statistics.dart';
void main(List<String> args) async{
final xMmin = 1.0;
final xMmax = 9.0;
final meanOfParent = 5.0;
final stdDevOfParent = 2.0;
final sampleSize = 1000;
// Generating the random sample with 1000 entries.
final sample = truncatedNormalSample(
sampleSize,
xMmin,
xMmax,
meanOfParent,
stdDevOfParent,
);
final stats = Stats(sample);
print(stats.mean);
print(stats.stdDev);
print(stats.min);
// Exporting a histogram.
// Export histogram
await File('example/data/truncated_normal$sampleSize.hist').writeAsString(
sample.exportHistogram(
pdf: (x) =>
truncatedNormalPdf(x, xMin, xMax, meanOfParent, stdDevOfParent),
),
);
}For further examples on how to generate random samples, export histograms, and access sample statistics see folder example.
Please file feature requests and bugs at the issue tracker.