Skip to main content

sample_statistics 1.0.0 copy "sample_statistics: ^1.0.0" to clipboard
sample_statistics: ^1.0.0 copied to clipboard

Sample statistics, histograms, probability distributions, and random sample generators.

Sample Statistics #

Dart

Introduction #

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.

Usage #

To use this package include sample_statistics as a dependency in your pubspec.yaml file.

1. Sample Statistics #

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]

2. Histograms #

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));
}

The console output is show below:

$ dart example/bin/histogram_example.dart
# Intervals: 9
# Min: -10.00000000
# Max: 14.00000000
# Interval size: 3.000000000
# Mean:   0.7500000000
# StdDev: 4.888573222
# Median: 0.000000000
# First Quartile: -1.000000000
# Third Quartile: 3.000000000
# Histogram integral: 84.0
#
# -------------------------------------------------------------
#     Interval Mid-Point                  Count
        -10.00000000                 1.000000000
        -7.000000000                 1.000000000
        -4.000000000                 4.000000000
        -1.000000000                 9.000000000
        2.000000000                  7.000000000
        5.000000000                  3.000000000
        8.000000000                  1.000000000
        11.00000000                  1.000000000
        14.00000000                  1.000000000

A histogram constructed from Unicode block character may be printed using the command:

// Ansi.status = AnsiOutput.disabled; // Uncomment if the terminal is not
//                                       Ansi compatible.
print(stats.blockHistogram);

On a monochrome terminal the block histogram is rendered as shown below:

   x̃x̄
▁▁▄█▇▃▁▁▁

On a terminal with Ansi support, the block histogram is rendered as: Block Histogram

If a block contains the mean , it is colored green. If it contains the median of the sample it is colored blue. In this case above, the single block containing the mean and median is printed in a cyan hue.

The image below shows the histogram data shown above plotted using gnuplot. Histogram

3. Random Sample Generators #

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),
     ),
   );

 }

Examples #

For further examples on how to generate random samples, export histograms, and access sample statistics see folder example.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

4
likes
0
points
803
downloads

Publisher

verified publishersimphotonics.com

Weekly Downloads

Sample statistics, histograms, probability distributions, and random sample generators.

Repository (GitHub)
View/report issues

Topics

#statistics #histogram #probability-distribution #random-sample-generator

License

unknown (license)

Dependencies

ansi_modifier, exception_templates, lazy_memo, list_operators

More

Packages that depend on sample_statistics