summaryrefslogtreecommitdiff
path: root/modules/statistics/macros/geomean.sci
blob: a51d37fa318036b64383ff19637326d4231fc862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 1999 - INRIA - Carlos Klimann
// Copyright (C) 2010 - Samuel GOUGEON : bug 7204, code style
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
//

function gm = geomean(x,orien)
    //
    //This function computes the geometric mean of a vector or matrix x.
    //
    //For a vector  or  matrix  x,  gm=geomean(x) returns in  scalar  gm the
    //geometric mean of all the entries of x.
    //
    //gm=geomean(x,'r')(or,  equivalently,   gm=gmean(x,1)) returns  in each
    //entry of the row vector gm the geometric mean of each column of x.
    //
    //gm=geomean(x,'c')(or,   equivalently,  gm=gmean(x,2)) returns in  each
    //entry of the column vector gm the geometric mean of each row of x.
    //
    //References:  Wonacott, T.H. & Wonacott, R.J.; Introductory
    //Statistics, J.Wiley & Sons, 1990.
    //
    //
    [lhs, rhs] = argn(0)
    if rhs == 0 then
        msg = gettext("%s: Wrong number of input arguments: %d to %d expected.\n")
        error(msprintf(msg, "geomean",1,2))
    end
    if x == [] then
        gm = %nan
        return
    end
    if rhs == 1 then
        gm = prod(x)^(1/size(x,"*"))
    elseif rhs==2
        gm = prod(x,orien).^(1/size(x,orien))
    else
        msg = gettext("%s: Wrong number of input arguments: %d to %d expected.\n")
        error(msprintf(msg, "geomean",1,2))
    end
endfunction