summaryrefslogtreecommitdiff
path: root/modules/statistics/macros/variancef.sci
blob: e593de8eacb4e88b3b39291b7e3a7efab20df244 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2013 - Scilab Enterprises - Paul BIGNIER : m parameter added
// Copyright (C) 2013 - Samuel GOUGEON : http://bugzilla.scilab.org/11209 fixed
// Copyright (C) 2000 - INRIA - Carlos Klimann
//
// 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 [s, m] = variancef(x, fre, orien, m)
    //
    //This function  computes  the variance  of the values  of   a vector or
    //matrix x, each  of  them  counted with  a  frequency signaled   by the
    //corresponding values of the integer vector or matrix fre with the same
    //type of x.
    //
    //For a vector or matrix  x, s=variancef(x,fre) (or s=variancef(x,fre,'*') returns
    //in scalar s the variance  of all the  entries of x, each value counted
    //with the multiplicity indicated by the corresponding value of fre.
    //
    //s=variancef(x,fre,'r')(or,   equivalently, s=variancef(x,fre,1)) returns in each
    //entry of the row vector s  of type 1xsize(x,'c')  the variance of each
    //column of x, each value counted with the multiplicity indicated by the
    //corresponding value of fre.
    //
    //s=variancef(x,fre,'c')(or, equivalently,   s=variancef(x,fre,2)) returns in each
    //entry of  the column vector  s of type   size(x,'c')x1 the variance of
    //each row of  x, each value counted with  the multiplicity indicated by
    //the corresponding value of fre.
    //
    //The input argument m represents the a priori mean. If it is present, then the sum is
    //divided by n. Otherwise ("sample variance"), it is divided by n-1.
    //
    //

    [lhs,rhs] = argn(0)
    if rhs<2 | rhs>4 then
        msg = gettext("%s: Wrong number of input arguments: %d to %d expected.\n")
        error(msprintf(msg, "variancef", 2, 4))
    end
    if x==[] | fre==[] | fre==0
        s = %nan
        return
    end
    if rhs==2 then
        sumfre = sum(fre)
        if sumfre <= 1 then
            msg = gettext("%s: Wrong value for input argument #%d: Must be > %d.\n")
        error(msprintf(msg, "variancef", 2, 1)), end
        m = meanf(x,fre)
        s = sum((abs(x-m).^2).*fre) / (sumfre-1)
        return
    end
    biased = %f
    if rhs==4 then
        if typeof(m)~="constant" then
            tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
            error(msprintf(tmp, "variancef", ))
        elseif orien=="*" then
            if ~isscalar(m) then
                tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
                error(msprintf(tmp, "variancef", ))
            end
        elseif orien=="r" | orien==1 then
            if size(m)~=[1 size(x,"c")] & ~isscalar(m) then
                tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
                error(msprintf(tmp, "variancef", ))
            end
        elseif orien=="c" | orien==2 then
            if size(m)~=[size(x,"r") 1] & ~isscalar(m) then
                tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
                error(msprintf(tmp, "variancef", ))
            end
        end
        if isnan(m) then
            biased = %t; // Compute the biased variance
        end
    end
    if orien=="*",
        sumfre = sum(fre)
        if sumfre <= 1 then
            msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
        error(msprintf(msg, "variancef", 2, 1)),end
        if rhs<4 then
            m = meanf(x,fre)
            s = sum((abs(x-m).^2).*fre) / (sumfre-1)
        elseif biased == %t
            m = meanf(x,fre)
            s = sum((abs(x-m).^2).*fre) / sumfre
        else
            s = sum((abs(x-m).^2).*fre) / sumfre
        end
    elseif orien=="r" | orien==1,
        sumfre = sum(fre, "r")
        if or(sumfre==0) then
            msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
            error(msprintf(msg, "variancef", 2, 1))
        end
        if rhs<4 | biased == %t then
            m = meanf(x,fre,"r")
        elseif isscalar(m) then
            m = m*ones(1, size(x,"c"));
        end
        m2 = ones(size(x,"r"),1)*m
        if rhs<4 then
            s = sum((abs(x-m2).^2).*fre, "r") ./ (sumfre-1)
        else
            s = sum((abs(x-m2).^2).*fre, "r") ./ sumfre
        end
    elseif orien=="c" | orien==2,
        sumfre = sum(fre, "c")
        if or(sumfre==0) then
            msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
            error(msprintf(msg, "variancef", 2, 1))
        end
        if rhs<4 | biased == %t then
            m = meanf(x,fre,"c")
        elseif isscalar(m) then
            m = m*ones(size(x,"r"), 1);
        end
        m2 = m*ones(1,size(x,"c"))
        if rhs<4 then
            s = sum((abs(x-m2).^2).*fre, "c") ./ (sumfre-1)
        else
            s = sum((abs(x-m2).^2).*fre, "c") ./ sumfre
        end
    else
        msg = _("%s: Wrong value for input argument #%d: ''%s'', ''%s'', ''%s'', %d or %d expected.\n")
        error(msprintf(msg, "variancef", 3, "*", "c", "r", 1, 2))
    end

endfunction