3
<title>Example statistical programs - GNU Scientific Library -- Reference Manual</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="GNU Scientific Library -- Reference Manual">
6
<meta name="generator" content="makeinfo 4.8">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="Statistics.html#Statistics" title="Statistics">
9
<link rel="prev" href="Median-and-Percentiles.html#Median-and-Percentiles" title="Median and Percentiles">
10
<link rel="next" href="Statistics-References-and-Further-Reading.html#Statistics-References-and-Further-Reading" title="Statistics References and Further Reading">
11
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
13
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
15
Permission is granted to copy, distribute and/or modify this document
16
under the terms of the GNU Free Documentation License, Version 1.2 or
17
any later version published by the Free Software Foundation; with the
18
Invariant Sections being ``GNU General Public License'' and ``Free Software
19
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
20
and with the Back-Cover Text being (a) (see below). A copy of the
21
license is included in the section entitled ``GNU Free Documentation
24
(a) The Back-Cover Text is: ``You have freedom to copy and modify this
25
GNU Manual, like GNU software.''-->
26
<meta http-equiv="Content-Style-Type" content="text/css">
27
<style type="text/css"><!--
28
pre.display { font-family:inherit }
29
pre.format { font-family:inherit }
30
pre.smalldisplay { font-family:inherit; font-size:smaller }
31
pre.smallformat { font-family:inherit; font-size:smaller }
32
pre.smallexample { font-size:smaller }
33
pre.smalllisp { font-size:smaller }
34
span.sc { font-variant:small-caps }
35
span.roman { font-family:serif; font-weight:normal; }
36
span.sansserif { font-family:sans-serif; font-weight:normal; }
42
<a name="Example-statistical-programs"></a>
43
Next: <a rel="next" accesskey="n" href="Statistics-References-and-Further-Reading.html#Statistics-References-and-Further-Reading">Statistics References and Further Reading</a>,
44
Previous: <a rel="previous" accesskey="p" href="Median-and-Percentiles.html#Median-and-Percentiles">Median and Percentiles</a>,
45
Up: <a rel="up" accesskey="u" href="Statistics.html#Statistics">Statistics</a>
49
<h3 class="section">20.9 Examples</h3>
51
<p>Here is a basic example of how to use the statistical functions:
53
<pre class="example"><pre class="verbatim"> #include <stdio.h>
54
#include <gsl/gsl_statistics.h>
59
double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};
60
double mean, variance, largest, smallest;
62
mean = gsl_stats_mean(data, 1, 5);
63
variance = gsl_stats_variance(data, 1, 5);
64
largest = gsl_stats_max(data, 1, 5);
65
smallest = gsl_stats_min(data, 1, 5);
67
printf ("The dataset is %g, %g, %g, %g, %g\n",
68
data[0], data[1], data[2], data[3], data[4]);
70
printf ("The sample mean is %g\n", mean);
71
printf ("The estimated variance is %g\n", variance);
72
printf ("The largest value is %g\n", largest);
73
printf ("The smallest value is %g\n", smallest);
77
<p>The program should produce the following output,
79
<pre class="example"><pre class="verbatim"> The dataset is 17.2, 18.1, 16.5, 18.3, 12.6
80
The sample mean is 16.54
81
The estimated variance is 4.2984
82
The largest value is 18.3
83
The smallest value is 12.6
85
<p>Here is an example using sorted data,
87
<pre class="example"><pre class="verbatim"> #include <stdio.h>
88
#include <gsl/gsl_sort.h>
89
#include <gsl/gsl_statistics.h>
94
double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};
95
double median, upperq, lowerq;
97
printf ("Original dataset: %g, %g, %g, %g, %g\n",
98
data[0], data[1], data[2], data[3], data[4]);
100
gsl_sort (data, 1, 5);
102
printf ("Sorted dataset: %g, %g, %g, %g, %g\n",
103
data[0], data[1], data[2], data[3], data[4]);
106
= gsl_stats_median_from_sorted_data (data,
110
= gsl_stats_quantile_from_sorted_data (data,
114
= gsl_stats_quantile_from_sorted_data (data,
118
printf ("The median is %g\n", median);
119
printf ("The upper quartile is %g\n", upperq);
120
printf ("The lower quartile is %g\n", lowerq);
124
<p>This program should produce the following output,
126
<pre class="example"><pre class="verbatim"> Original dataset: 17.2, 18.1, 16.5, 18.3, 12.6
127
Sorted dataset: 12.6, 16.5, 17.2, 18.1, 18.3
129
The upper quartile is 18.1
130
The lower quartile is 16.5