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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
function y = var(x,w,dim)
// This function var estimate the variance of the values in X.
// Calling Sequence
// y=var(x)
// y=var(x,w)
// y=var(x,w,dim)
// Parameters
// x: a vector or matrix.
// w: weight vector W of length X, or may take the value of 0 and 1. The default value is 0. Consider only non-negative values.
// dim: consider the variance along the dimension of X. 1 for clumun wise variamce and 2 for row wise variance.
// y: returns the variance of the values in X.
// Examples
// x=[1.2, 5, 10, -20, 12,10,5,20,32,20];
// w=1:10;
// dim=2;
// y=var(x, w, dim) ;
// See also
// Authors
// Jitendra Singh
if argn(2)==1 then
if type(x)==10 then
x=ascii(x)
end
if size(x,1)==1 |size(x,2)==1 then
sd=stdev(x)
else
sd=stdev(x,1)
end
y=sd.^2;
end
if argn(2)<=3 & argn(2)>1 then
if size(x,1)==1 |size(x,2)==1 then
sd=stdev(x);
n=length(x);
y1= sd.^2;
if length(w)==1 & w==0 then
y=y1;
elseif length(w)==1 & w==1 then
y=y1*((n-1)/n);
elseif length(w)==n & and(w>=0) then
if size(x,1)~=size(w,1) then
w=w';
end
wmean=sum(x.*w)/sum(w);
x1=(x-wmean).^2;
l=length(find(w>0));
sd=((sum(w.*x1))/(((l-1)/l)*sum(w)));
y=(sd)*((n-1)/n);
elseif length(w)~=size(x)& length(w)>1 then
error ('The length of W must be compatible with X.')
else
error('W must be a vector of nonnegative weights, or a scalar 0 or 1.')
end
else
n=size(x,1)
sd=stdev(x,1)
y1=sd.^2;
if length(w)==1 & w==0 then
y=y1;
elseif length(w)==1 & w==1 then
y=y1*((n-1)/n);
elseif length(w)==n & and(w>=0) then
for i=1:size(x,2)
xx=(x(:,i))
if size(xx,1)~=size(w,1) then
w=w';
end
wmean=sum(xx.*w)/sum(w);
x1=(x(:,i)-wmean).^2;
l=length(find(w>0));
sd(i)=sum((w.*x1)/(((l-1)/l)*sum(w)));
end
y=sd*((l-1)/l);
elseif length(w)~=size(x,1) & length(w)>1 then
error ('The length of W must be compatible with X.')
else
error('W must be a vector of nonnegative weights, or a scalar 0 or 1.')
end
end
end
if argn(2)==3 then
if dim==2 then
if size(x,1)==1 | size(x,2)==1 then
if size(x,1)==1 then
y=y;
elseif size(x,2)==1 & length(w)== length(x) then
error ('The length of W must be compatible with X.')
else
y=zeros(size(x,1),size(x,2))
end
else
n=size(x,2)
sd=stdev(x,2)
y1=sd.^2;
if length(w)==1 & w==0 then
y=y1;
elseif length(w)==1 & w==1 then
y=y1*((n-1)/n);
elseif length(w)==n & and(w>=0) then
for i=1:size(x,1)
xx=(x(:,i))
if size(xx,1)~=size(w,1) then
w=w';
end
wmean=sum(xx.*w)/sum(w);
x1=(x(:,i)-wmean).^2;
l=length(find(w>0));
sd(i)=sum((w.*x1)/(((l-1)/l)*sum(w)));
end
y=sd*((l-1)/l);
else
error('The length of W must be compatible with X.')
end
end
end
if dim==1 then
if size(x,1)==1 | size(x,2)==1 then
if size(x,2)==1 then
y=y;
elseif size(x,1)==1 & length(w)== length(x) then
error ('The length of W must be compatible with X.')
else
y=zeros(size(x,1),size(x,2))
end
end
end
if dim>2 then
if length(w)==1 & w==1 | w==0 then
y=zeros(size(x,1), size(x,2))
else
error ('The length of W must be compatible with X.')
end
end
if dim<=0 then
error('Dimension argument must be a positive integer scalar within indexing range.')
end
end
endfunction
|