Statistical Functions

Table of Contents

  1. Introduction
  2. Simple Statistics
  3. Moving average: moving_average

Introduction

These are defined in stat.tcl. Note that there are also built-in statistical functions called correlation and moving_correlation. The Tally Unary Operator "#" also has statistical applications.

Simple Statistics

Three examples are provided for each function. The first uses the vector v which is defined as follows (note the missing values):

% nap "v = {12 6 _ 7 3 15 _ 10 18 6}"

The second example produces statistics of each column of a matrix. The third example produces statistics of each row of the same matrix. This matrix is called m and is defined as follows:

% nap "m = {
    {1.5 2.1 1.5 0.2}
    {6.2 4.9   _ 0.2}
}"

Arithmetic mean: am(x[,verb_rank])

% [nap "am(v)"]
9.625
% [nap "am(m)"]
3.85 3.5 1.5 0.2
% [nap "am(m,1)"]
1.325 3.76667

Geometric mean: gm(x[,verb_rank])

% [nap "gm(v)"]
8.38752
% [nap "gm(m)"]
3.04959 3.2078 1.5 0.2
% [nap "gm(m,1)"]
0.985957 1.82476

Median: median(x[,verb_rank])

% [nap "median(v)"]
8.5
% [nap "median(m)"]
3.85 3.5 1.5 0.2
% [nap "median(m,1)"]
1.5 4.9

Mode: mode(x[,verb_rank])

% [nap "mode(v)"]
6
% [nap "mode(m)"]
3.85 3.5 1.5 0.2
% [nap "mode(m,1)"]
1.50625 3.7625

Percentiles: percentile(x,pc [,verb_rank [,nc]])

pc = vector of required percentiles
nc = number of class intervals (default: 256)

The following examples calculate the following percentiles:

% [nap "percentile(v, {0 50 100})"]
3 8.50781 18
% [nap "percentile(m, {0 50 100})"]
1.50000 2.10000 1.50000 0.20000
3.85918 3.50547 1.50000 0.20000
6.20000 4.90000 1.50000 0.20000
% [nap "percentile(m, {0 50 100}, 1)"]
0.20000 0.20000
1.50254 4.92266
2.10000 6.20000

Root mean square: rms(x[,verb_rank])

% [nap "rms(v)"]
10.7413
% [nap "rms(m)"]
4.51054 3.76962 1.5 0.2
% [nap "rms(m,1)"]
1.49583 4.56399

Standard-deviation (with division by n): sd(x[,verb_rank])

% [nap "sd(v)"]
4.76806
% [nap "sd(m)"]
2.35 1.4 0 0
% [nap "sd(m,1)"]
0.694172 2.57725

Standard-deviation (with division by n-1): sd1(x[,verb_rank])

% [nap "sd1(v)"]
5.09727
% [nap "sd1(m)"]
3.3234 1.9799 _ 0
% [nap "sd1(m,1)"]
0.801561 3.15647

Variance (with division by n): var(x[,verb_rank])

% [nap "var(v)"]
22.7344
% [nap "var(m)"]
5.5225 1.96 0 0
% [nap "var(m,1)"]
0.481875 6.64222

Variance (with division by n-1): var1(x[,verb_rank])

% [nap "var1(v)"]
25.9821
% [nap "var1(m)"]
11.045 3.92 _ 0
% [nap "var1(m,1)"]
0.6425 9.96333

Moving Average: moving_average(x,shape_window[,step])

Move window of specified shape by specified step (can vary for each dimension). Result is arithmetic mean of x values in each window. x can have any rank > 0.

shape_window is either a scalar or a vector with an element for each dimension. If it is a scalar then it is treated as a vector with rank(x) identical elements.

Similarly, step is either a scalar or a vector with an element for each dimension. If it is a scalar then it is treated as a vector with rank(x) identical elements. The value -1 is treated like 1, except that missing values are prepended & appended (along this dimension of x) to produce a result with the same dimension size as x.

The following examples illustrate the application of moving_average to a vector.

% nap "v = {12 6 _ 7 3 15 _ 10 18 5}"
% [nap "moving_average(v, 3)"] value
9 6.5 5 8.33333 9 12.5 14 11
% [nap "moving_average(v, 3, 3)"] value
9 8.33333 14
% [nap "moving_average(v, 3, -1)"] value
9 9 6.5 5 8.33333 9 12.5 14 11 11.5

The following examples illustrate the application of moving_average to a matrix.

% nap "m = {
    {1 2 1 4 0 0}
    {5 4 2 9 2 7}
    {0 1 1 3 1 4}
}"
% [nap "moving_average(m, {3 3})"] value
1.88889 3.00000 2.55556 3.33333
% [nap "moving_average(m, {3 3}, {3 3})"] value
1.88889 3.33333
% [nap "moving_average(m, 3, 3)"] value
1.88889 3.33333
% [nap "moving_average(m, 3, -1)"] value
3.00000 2.50000 3.66667 3.00000 3.66667 2.25000
2.16667 1.88889 3.00000 2.55556 3.33333 2.33333
2.50000 2.16667 3.33333 3.00000 4.33333 3.50000
% [nap "moving_average(m, {1 3})"] value
1.333333 2.333333 1.666667 1.333333
3.666667 5.000000 4.333333 6.000000
0.666667 1.666667 1.666667 2.666667

Author: Harvey Davies       © 2002, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: stat.html,v 1.6 2005/03/09 23:04:30 dav480 Exp $