<html lang="en"> <head> <title>Descriptive Statistics - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Statistics.html#Statistics" title="Statistics"> <link rel="next" href="Basic-Statistical-Functions.html#Basic-Statistical-Functions" title="Basic Statistical Functions"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Descriptive-Statistics"></a> <p> Next: <a rel="next" accesskey="n" href="Basic-Statistical-Functions.html#Basic-Statistical-Functions">Basic Statistical Functions</a>, Up: <a rel="up" accesskey="u" href="Statistics.html#Statistics">Statistics</a> <hr> </div> <h3 class="section">26.1 Descriptive Statistics</h3> <p>One principal goal of descriptive statistics is to represent the essence of a large data set concisely. Octave provides the mean, median, and mode functions which all summarize a data set with just a single number corresponding to the central tendency of the data. <!-- mean scripts/statistics/base/mean.m --> <p><a name="doc_002dmean"></a> <div class="defun"> — Function File: <b>mean</b> (<var>x</var>)<var><a name="index-mean-2407"></a></var><br> — Function File: <b>mean</b> (<var>x, dim</var>)<var><a name="index-mean-2408"></a></var><br> — Function File: <b>mean</b> (<var>x, opt</var>)<var><a name="index-mean-2409"></a></var><br> — Function File: <b>mean</b> (<var>x, dim, opt</var>)<var><a name="index-mean-2410"></a></var><br> <blockquote><p>Compute the mean of the elements of the vector <var>x</var>. <pre class="example"> mean (x) = SUM_i x(i) / N </pre> <p>If <var>x</var> is a matrix, compute the mean for each column and return them in a row vector. <p>The optional argument <var>opt</var> selects the type of mean to compute. The following options are recognized: <dl> <dt>"a"<dd>Compute the (ordinary) arithmetic mean. [default] <br><dt>"g"<dd>Compute the geometric mean. <br><dt>"h"<dd>Compute the harmonic mean. </dl> <p>If the optional argument <var>dim</var> is given, operate along this dimension. <p>Both <var>dim</var> and <var>opt</var> are optional. If both are supplied, either may appear first. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmedian.html#doc_002dmedian">median</a>, <a href="doc_002dmode.html#doc_002dmode">mode</a>. </p></blockquote></div> <!-- median scripts/statistics/base/median.m --> <p><a name="doc_002dmedian"></a> <div class="defun"> — Function File: <b>median</b> (<var>x</var>)<var><a name="index-median-2411"></a></var><br> — Function File: <b>median</b> (<var>x, dim</var>)<var><a name="index-median-2412"></a></var><br> <blockquote><p>Compute the median value of the elements of the vector <var>x</var>. If the elements of <var>x</var> are sorted, the median is defined as <pre class="example"> x(ceil(N/2)) N odd median (x) = (x(N/2) + x((N/2)+1))/2 N even </pre> <p>If <var>x</var> is a matrix, compute the median value for each column and return them in a row vector. If the optional <var>dim</var> argument is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmean.html#doc_002dmean">mean</a>, <a href="doc_002dmode.html#doc_002dmode">mode</a>. </p></blockquote></div> <!-- mode scripts/statistics/base/mode.m --> <p><a name="doc_002dmode"></a> <div class="defun"> — Function File: <b>mode</b> (<var>x</var>)<var><a name="index-mode-2413"></a></var><br> — Function File: <b>mode</b> (<var>x, dim</var>)<var><a name="index-mode-2414"></a></var><br> — Function File: [<var>m</var>, <var>f</var>, <var>c</var>] = <b>mode</b> (<var><small class="dots">...</small></var>)<var><a name="index-mode-2415"></a></var><br> <blockquote><p>Compute the most frequently occurring value in a dataset (mode). <code>mode</code> determines the frequency of values along the first non-singleton dimension and returns the value with the highest frequency. If two, or more, values have the same frequency <code>mode</code> returns the smallest. <p>If the optional argument <var>dim</var> is given, operate along this dimension. <p>The return variable <var>f</var> is the number of occurrences of the mode in in the dataset. The cell array <var>c</var> contains all of the elements with the maximum frequency. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmean.html#doc_002dmean">mean</a>, <a href="doc_002dmedian.html#doc_002dmedian">median</a>. </p></blockquote></div> <p>Using just one number, such as the mean, to represent an entire data set may not give an accurate picture of the data. One way to characterize the fit is to measure the dispersion of the data. Octave provides several functions for measuring dispersion. <!-- range scripts/statistics/base/range.m --> <p><a name="doc_002drange"></a> <div class="defun"> — Function File: <b>range</b> (<var>x</var>)<var><a name="index-range-2416"></a></var><br> — Function File: <b>range</b> (<var>x, dim</var>)<var><a name="index-range-2417"></a></var><br> <blockquote><p>Return the range, i.e., the difference between the maximum and the minimum of the input data. If <var>x</var> is a vector, the range is calculated over the elements of <var>x</var>. If <var>x</var> is a matrix, the range is calculated over each column of <var>x</var>. <p>If the optional argument <var>dim</var> is given, operate along this dimension. <p>The range is a quickly computed measure of the dispersion of a data set, but is less accurate than <code>iqr</code> if there are outlying data points. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002diqr.html#doc_002diqr">iqr</a>, <a href="doc_002dstd.html#doc_002dstd">std</a>. </p></blockquote></div> <!-- iqr scripts/statistics/base/iqr.m --> <p><a name="doc_002diqr"></a> <div class="defun"> — Function File: <b>iqr</b> (<var>x</var>)<var><a name="index-iqr-2418"></a></var><br> — Function File: <b>iqr</b> (<var>x, dim</var>)<var><a name="index-iqr-2419"></a></var><br> <blockquote><p>Return the interquartile range, i.e., the difference between the upper and lower quartile of the input data. If <var>x</var> is a matrix, do the above for first non-singleton dimension of <var>x</var>. <p>If the optional argument <var>dim</var> is given, operate along this dimension. <p>As a measure of dispersion, the interquartile range is less affected by outliers than either <code>range</code> or <code>std</code>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002drange.html#doc_002drange">range</a>, <a href="doc_002dstd.html#doc_002dstd">std</a>. </p></blockquote></div> <!-- meansq scripts/statistics/base/meansq.m --> <p><a name="doc_002dmeansq"></a> <div class="defun"> — Function File: <b>meansq</b> (<var>x</var>)<var><a name="index-meansq-2420"></a></var><br> — Function File: <b>meansq</b> (<var>x, dim</var>)<var><a name="index-meansq-2421"></a></var><br> <blockquote><p>Compute the mean square of the elements of the vector <var>x</var>. <pre class="example"> std (x) = 1/N SUM_i x(i)^2 </pre> <p>For matrix arguments, return a row vector containing the mean square of each column. <p>If the optional argument <var>dim</var> is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dvar.html#doc_002dvar">var</a>, <a href="doc_002dstd.html#doc_002dstd">std</a>, <a href="doc_002dmoment.html#doc_002dmoment">moment</a>. </p></blockquote></div> <!-- std scripts/statistics/base/std.m --> <p><a name="doc_002dstd"></a> <div class="defun"> — Function File: <b>std</b> (<var>x</var>)<var><a name="index-std-2422"></a></var><br> — Function File: <b>std</b> (<var>x, opt</var>)<var><a name="index-std-2423"></a></var><br> — Function File: <b>std</b> (<var>x, opt, dim</var>)<var><a name="index-std-2424"></a></var><br> <blockquote><p>Compute the standard deviation of the elements of the vector <var>x</var>. <pre class="example"> std (x) = sqrt ( 1/(N-1) SUM_i (x(i) - mean(x))^2 ) </pre> <p class="noindent">where N is the number of elements. <p>If <var>x</var> is a matrix, compute the standard deviation for each column and return them in a row vector. <p>The argument <var>opt</var> determines the type of normalization to use. Valid values are <dl> <dt>0:<dd> normalize with N-1, provides the square root of the best unbiased estimator of the variance [default] <br><dt>1:<dd> normalize with N, this provides the square root of the second moment around the mean </dl> <p>If the optional argument <var>dim</var> is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dvar.html#doc_002dvar">var</a>, <a href="doc_002drange.html#doc_002drange">range</a>, <a href="doc_002diqr.html#doc_002diqr">iqr</a>, <a href="doc_002dmean.html#doc_002dmean">mean</a>, <a href="doc_002dmedian.html#doc_002dmedian">median</a>. </p></blockquote></div> <p>In addition to knowing the size of a dispersion it is useful to know the shape of the data set. For example, are data points massed to the left or right of the mean? Octave provides several common measures to describe the shape of the data set. Octave can also calculate moments allowing arbitrary shape measures to be developed. <!-- var scripts/statistics/base/var.m --> <p><a name="doc_002dvar"></a> <div class="defun"> — Function File: <b>var</b> (<var>x</var>)<var><a name="index-var-2425"></a></var><br> — Function File: <b>var</b> (<var>x, opt</var>)<var><a name="index-var-2426"></a></var><br> — Function File: <b>var</b> (<var>x, opt, dim</var>)<var><a name="index-var-2427"></a></var><br> <blockquote><p>Compute the variance of the elements of the vector <var>x</var>. <pre class="example"> var (x) = 1/(N-1) SUM_i (x(i) - mean(x))^2 </pre> <p>If <var>x</var> is a matrix, compute the variance for each column and return them in a row vector. <p>The argument <var>opt</var> determines the type of normalization to use. Valid values are <dl> <dt>0:<dd> normalize with N-1, provides the best unbiased estimator of the variance [default] <br><dt>1:<dd> normalizes with N, this provides the second moment around the mean </dl> <p>If the optional argument <var>dim</var> is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dcov.html#doc_002dcov">cov</a>, <a href="doc_002dstd.html#doc_002dstd">std</a>, <a href="doc_002dskewness.html#doc_002dskewness">skewness</a>, <a href="doc_002dkurtosis.html#doc_002dkurtosis">kurtosis</a>, <a href="doc_002dmoment.html#doc_002dmoment">moment</a>. </p></blockquote></div> <!-- skewness scripts/statistics/base/skewness.m --> <p><a name="doc_002dskewness"></a> <div class="defun"> — Function File: <b>skewness</b> (<var>x</var>)<var><a name="index-skewness-2428"></a></var><br> — Function File: <b>skewness</b> (<var>x, dim</var>)<var><a name="index-skewness-2429"></a></var><br> <blockquote><p>Compute the skewness of the elements of the vector <var>x</var>. <pre class="example"> skewness (x) = 1/N std(x)^(-3) sum ((x - mean(x)).^3) </pre> <p class="noindent">If <var>x</var> is a matrix, return the skewness along the first non-singleton dimension of the matrix. If the optional <var>dim</var> argument is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dvar.html#doc_002dvar">var</a>, <a href="doc_002dkurtosis.html#doc_002dkurtosis">kurtosis</a>, <a href="doc_002dmoment.html#doc_002dmoment">moment</a>. </p></blockquote></div> <!-- kurtosis scripts/statistics/base/kurtosis.m --> <p><a name="doc_002dkurtosis"></a> <div class="defun"> — Function File: <b>kurtosis</b> (<var>x</var>)<var><a name="index-kurtosis-2430"></a></var><br> — Function File: <b>kurtosis</b> (<var>x, dim</var>)<var><a name="index-kurtosis-2431"></a></var><br> <blockquote><p>Compute the kurtosis of the elements of the vector <var>x</var>. <pre class="example"> kurtosis (x) = 1/N std(x)^(-4) sum ((x - mean(x)).^4) - 3 </pre> <p>If <var>x</var> is a matrix, return the kurtosis over the first non-singleton dimension of the matrix. If the optional <var>dim</var> argument is given, operate along this dimension. <p>Note: The definition of kurtosis above yields a kurtosis of zero for the stdnormal distribution and is sometimes referred to as "excess kurtosis". To calculate kurtosis without the normalization factor of -3 use <code>moment (</code><var>x</var><code>, 4, 'c') / std (</code><var>x</var><code>)^4</code>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dvar.html#doc_002dvar">var</a>, <a href="doc_002dskewness.html#doc_002dskewness">skewness</a>, <a href="doc_002dmoment.html#doc_002dmoment">moment</a>. </p></blockquote></div> <!-- moment scripts/statistics/base/moment.m --> <p><a name="doc_002dmoment"></a> <div class="defun"> — Function File: <b>moment</b> (<var>x, p</var>)<var><a name="index-moment-2432"></a></var><br> — Function File: <b>moment</b> (<var>x, p, type</var>)<var><a name="index-moment-2433"></a></var><br> — Function File: <b>moment</b> (<var>x, p, dim</var>)<var><a name="index-moment-2434"></a></var><br> — Function File: <b>moment</b> (<var>x, p, type, dim</var>)<var><a name="index-moment-2435"></a></var><br> — Function File: <b>moment</b> (<var>x, p, dim, type</var>)<var><a name="index-moment-2436"></a></var><br> <blockquote><p>Compute the <var>p</var>-th moment of the vector <var>x</var> about zero. <pre class="example"> moment (x) = 1/N SUM_i x(i)^p </pre> <p>If <var>x</var> is a matrix, return the row vector containing the <var>p</var>-th moment of each column. <p>The optional string <var>type</var> specifies the type of moment to be computed. Valid options are: <dl> <dt>"c"<dd> Central Moment. The moment about the mean defined as <pre class="example"> 1/N SUM_i (x(i) - mean(x))^p </pre> <br><dt>"a"<dd> Absolute Moment. The moment about zero ignoring sign defined as <pre class="example"> 1/N SUM_i ( abs (x(i)) )^p </pre> <br><dt>"ac"<dd> Absolute Central Moment. Defined as <pre class="example"> 1/N SUM_i ( abs (x(i) - mean(x)) )^p </pre> </dl> <p>If the optional argument <var>dim</var> is given, operate along this dimension. <p>If both <var>type</var> and <var>dim</var> are given they may appear in any order. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dvar.html#doc_002dvar">var</a>, <a href="doc_002dskewness.html#doc_002dskewness">skewness</a>, <a href="doc_002dkurtosis.html#doc_002dkurtosis">kurtosis</a>. </p></blockquote></div> <!-- quantile scripts/statistics/base/quantile.m --> <p><a name="doc_002dquantile"></a> <div class="defun"> — Function File: <var>q</var> = <b>quantile</b> (<var>x, p</var>)<var><a name="index-quantile-2437"></a></var><br> — Function File: <var>q</var> = <b>quantile</b> (<var>x, p, dim</var>)<var><a name="index-quantile-2438"></a></var><br> — Function File: <var>q</var> = <b>quantile</b> (<var>x, p, dim, method</var>)<var><a name="index-quantile-2439"></a></var><br> <blockquote><p>For a sample, <var>x</var>, calculate the quantiles, <var>q</var>, corresponding to the cumulative probability values in <var>p</var>. All non-numeric values (NaNs) of <var>x</var> are ignored. <p>If <var>x</var> is a matrix, compute the quantiles for each column and return them in a matrix, such that the i-th row of <var>q</var> contains the <var>p</var>(i)th quantiles of each column of <var>x</var>. <p>The optional argument <var>dim</var> determines the dimension along which the quantiles are calculated. If <var>dim</var> is omitted, and <var>x</var> is a vector or matrix, it defaults to 1 (column-wise quantiles). If <var>x</var> is an N-D array, <var>dim</var> defaults to the first non-singleton dimension. <p>The methods available to calculate sample quantiles are the nine methods used by R (http://www.r-project.org/). The default value is METHOD = 5. <p>Discontinuous sample quantile methods 1, 2, and 3 <ol type=1 start=1> <li>Method 1: Inverse of empirical distribution function. <li>Method 2: Similar to method 1 but with averaging at discontinuities. <li>Method 3: SAS definition: nearest even order statistic. </ol> <p>Continuous sample quantile methods 4 through 9, where p(k) is the linear interpolation function respecting each methods' representative cdf. <ol type=1 start=4> <li>Method 4: p(k) = k / n. That is, linear interpolation of the empirical cdf. <li>Method 5: p(k) = (k - 0.5) / n. That is a piecewise linear function where the knots are the values midway through the steps of the empirical cdf. <li>Method 6: p(k) = k / (n + 1). <li>Method 7: p(k) = (k - 1) / (n - 1). <li>Method 8: p(k) = (k - 1/3) / (n + 1/3). The resulting quantile estimates are approximately median-unbiased regardless of the distribution of <var>x</var>. <li>Method 9: p(k) = (k - 3/8) / (n + 1/4). The resulting quantile estimates are approximately unbiased for the expected order statistics if <var>x</var> is normally distributed. </ol> <p>Hyndman and Fan (1996) recommend method 8. Maxima, S, and R (versions prior to 2.0.0) use 7 as their default. Minitab and SPSS use method 6. <span class="sc">matlab</span> uses method 5. <p>References: <ul> <li>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole. <li>Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, American Statistician, 50, 361–365. <li>R: A Language and Environment for Statistical Computing; <a href="http://cran.r-project.org/doc/manuals/fullrefman.pdf">http://cran.r-project.org/doc/manuals/fullrefman.pdf</a>. </ul> <p>Examples: <!-- Set example in small font to prevent overfull line --> <pre class="smallexample"> x = randi (1000, [10, 1]); # Create empirical data in range 1-1000 q = quantile (x, [0, 1]); # Return minimum, maximum of distribution q = quantile (x, [0.25 0.5 0.75]); # Return quartiles of distribution </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprctile.html#doc_002dprctile">prctile</a>. </p></blockquote></div> <!-- prctile scripts/statistics/base/prctile.m --> <p><a name="doc_002dprctile"></a> <div class="defun"> — Function File: <var>q</var> = <b>prctile</b> (<var>x</var>)<var><a name="index-prctile-2440"></a></var><br> — Function File: <var>q</var> = <b>prctile</b> (<var>x, p</var>)<var><a name="index-prctile-2441"></a></var><br> — Function File: <var>q</var> = <b>prctile</b> (<var>x, p, dim</var>)<var><a name="index-prctile-2442"></a></var><br> <blockquote><p>For a sample <var>x</var>, compute the quantiles, <var>q</var>, corresponding to the cumulative probability values, <var>p</var>, in percent. All non-numeric values (NaNs) of <var>x</var> are ignored. <p>If <var>x</var> is a matrix, compute the percentiles for each column and return them in a matrix, such that the i-th row of <var>y</var> contains the <var>p</var>(i)th percentiles of each column of <var>x</var>. <p>If <var>p</var> is unspecified, return the quantiles for <code>[0 25 50 75 100]</code>. The optional argument <var>dim</var> determines the dimension along which the percentiles are calculated. If <var>dim</var> is omitted, and <var>x</var> is a vector or matrix, it defaults to 1 (column-wise quantiles). When <var>x</var> is an N-D array, <var>dim</var> defaults to the first non-singleton dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dquantile.html#doc_002dquantile">quantile</a>. </p></blockquote></div> <p>A summary view of a data set can be generated quickly with the <code>statistics</code> function. <!-- statistics scripts/statistics/base/statistics.m --> <p><a name="doc_002dstatistics"></a> <div class="defun"> — Function File: <b>statistics</b> (<var>x</var>)<var><a name="index-statistics-2443"></a></var><br> — Function File: <b>statistics</b> (<var>x, dim</var>)<var><a name="index-statistics-2444"></a></var><br> <blockquote><p>Return a vector with the minimum, first quartile, median, third quartile, maximum, mean, standard deviation, skewness, and kurtosis of the elements of the vector <var>x</var>. <p>If <var>x</var> is a matrix, calculate statistics over the first non-singleton dimension. If the optional argument <var>dim</var> is given, operate along this dimension. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmin.html#doc_002dmin">min</a>, <a href="doc_002dmax.html#doc_002dmax">max</a>, <a href="doc_002dmedian.html#doc_002dmedian">median</a>, <a href="doc_002dmean.html#doc_002dmean">mean</a>, <a href="doc_002dstd.html#doc_002dstd">std</a>, <a href="doc_002dskewness.html#doc_002dskewness">skewness</a>, <a href="doc_002dkurtosis.html#doc_002dkurtosis">kurtosis</a>. </p></blockquote></div> </body></html>