项目作者: simphotonics

项目描述 :
Sample statistics, histograms, probability distributions, and random sample generators for Dart. The package uses null-safety features.
高级语言: Dart
项目地址: git://github.com/simphotonics/sample_statistics.git
创建时间: 2020-10-27T23:15:40Z
项目社区:https://github.com/simphotonics/sample_statistics

开源协议:BSD 3-Clause "New" or "Revised" License

下载


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 expensive calculations if the
same quantity is accessed repeatedly. If the random sample changes
use the method updateCache() to recalculate the sample statistics.

  1. import 'package:sample_statistics/sample_statistics.dart'
  2. void main() {
  3. final sample = <num>[-10, 0, 1, 2, 3, 4, 5, 6, 20]
  4. final stats = Stats(sample)
  5. print('\nRunning statistic_example.dart ...')
  6. print('Sample: $sample')
  7. print('min: ${stats.min}')
  8. print('max: ${stats.max}')
  9. print('mean: ${stats.mean}')
  10. print('median: ${stats.median}')
  11. print('first quartile: ${stats.quartile1}')
  12. print('third quartile: ${stats.quartile3}')
  13. print('standard deviation: ${stats.stdDev}')
  14. final outliers = sample.removeOutliers();
  15. print('outliers: $outliers')
  16. print('sample with outliers removed: $sample');
  17. // Update statistics after sample has changed:
  18. stats.updateCache();
  19. }
Click to show console output.

Console $ dart sample_statistics_example.dart Running sample_statistic_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 standard deviation: 7.779960011322538 outliers:[-10, 20] sample with outliers removed: [0, 1, 2, 3, 4, 5, 6]

2. 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.

  1. import 'dart:io';
  2. import 'package:sample_statistics/sample_statistics.dart';
  3. void main(List<String> args) async{
  4. final xMmin = 1.0;
  5. final xMmax = 9.0;
  6. final meanOfParent = 5.0;
  7. final stdDevOfParent = 2.0;
  8. final sampleSize = 1000;
  9. // Generating the random sample with 1000 entries.
  10. final sample = truncatedNormalSample(
  11. sampleSize,
  12. xMmin,
  13. xMmax,
  14. meanOfParent,
  15. stdDevOfParent,
  16. );
  17. final stats = Stats(sample);
  18. print(stats.mean);
  19. print(stats.stdDev);
  20. print(stats.min);
  21. // Exporting a histogram.
  22. // Export histogram
  23. await File('example/data/truncated_normal$sampleSize.hist').writeAsString(
  24. sample.exportHistogram(
  25. pdf: (x) =>
  26. truncatedNormalPdf(x, xMin, xMax, meanOfParent, stdDevOfParent),
  27. ),
  28. );
  29. }

3. Generating Histograms

To generate a histogram, the first step consists in dividing the random
sample range into a suitable number of intervals.
The second step consists in counting how many sample entries fall into each
interval.

The figures below show the histograms obtained from two random samples that
follow a truncated normal distribution with
xMin = 2.0, xMax = 6.0 and normal parent distribution
with meanOfParent = 3.0, and stdDevOfParent = 1.0.
The random samples were generated using the function
truncatedNormalSample.
The histograms were generated using the extension method
exportHistogram, see source code above.

Histogram

The figure on the left shows the histogram of a sample with size 150.
The figure on the right shows the histogram of a sample with size 600.
Increasing the random sample size leads to an increasingly
closer match between the shape of the histogram and
the underlying probability distribution.

Using the distribution parameters mentioned above with the function
meanTruncatedNormal, one can determine
a theoretical mean of 3.2828. It can be seen that in the limit of a
large sample size the sample mean approaches
the mean of the underlying probability distribution.

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.