## File: Example-statistical-programs.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (171 lines) | stat: -rw-r--r-- 6,802 bytes parent folder | download
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171` `````` GNU Scientific Library – Reference Manual: Example statistical programs

21.10 Examples

Here is a basic example of how to use the statistical functions:

#include <stdio.h> #include <gsl/gsl_statistics.h>  int main(void) {   double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};   double mean, variance, largest, smallest;    mean     = gsl_stats_mean(data, 1, 5);   variance = gsl_stats_variance(data, 1, 5);   largest  = gsl_stats_max(data, 1, 5);   smallest = gsl_stats_min(data, 1, 5);    printf ("The dataset is %g, %g, %g, %g, %g\n",          data[0], data[1], data[2], data[3], data[4]);    printf ("The sample mean is %g\n", mean);   printf ("The estimated variance is %g\n", variance);   printf ("The largest value is %g\n", largest);   printf ("The smallest value is %g\n", smallest);   return 0; }

The program should produce the following output,

The dataset is 17.2, 18.1, 16.5, 18.3, 12.6 The sample mean is 16.54 The estimated variance is 5.373 The largest value is 18.3 The smallest value is 12.6

Here is an example using sorted data,

#include <stdio.h> #include <gsl/gsl_sort.h> #include <gsl/gsl_statistics.h>  int main(void) {   double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};   double median, upperq, lowerq;    printf ("Original dataset:  %g, %g, %g, %g, %g\n",          data[0], data[1], data[2], data[3], data[4]);    gsl_sort (data, 1, 5);    printf ("Sorted dataset: %g, %g, %g, %g, %g\n",          data[0], data[1], data[2], data[3], data[4]);    median      = gsl_stats_median_from_sorted_data (data,                                           1, 5);    upperq      = gsl_stats_quantile_from_sorted_data (data,                                             1, 5,                                            0.75);   lowerq      = gsl_stats_quantile_from_sorted_data (data,                                             1, 5,                                            0.25);    printf ("The median is %g\n", median);   printf ("The upper quartile is %g\n", upperq);   printf ("The lower quartile is %g\n", lowerq);   return 0; }

This program should produce the following output,

Original dataset:  17.2, 18.1, 16.5, 18.3, 12.6 Sorted dataset: 12.6, 16.5, 17.2, 18.1, 18.3 The median is 17.2 The upper quartile is 18.1 The lower quartile is 16.5

``````