ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_sampleVar::setVarMean Interface Reference

Return the (weighted) sample variance and mean of an input time series of nsam observations, or of an input sample of nsam observations with ndim attributes optionally weighted by the input weight, optionally also sum(weight).
More...

Detailed Description

Return the (weighted) sample variance and mean of an input time series of nsam observations, or of an input sample of nsam observations with ndim attributes optionally weighted by the input weight, optionally also sum(weight).

This generic interface is developed to specifically improve the performance of other procedures where sum(weight) are also needed in addition to the mean of the weighted sample.
It uses a one-pass approach to simultaneously compute the sample mean and variance.

Parameters
[out]var: The output object of rank rank(sample) - 1 of type real of the same kind as the input sample representing its variance:
  1. If sample is a vector, the output var must be a scalar.
  2. If sample is a matrix, the output var must be a vector of size ndim = size(sample, 3 - dim).
[out]mean: The output object of the same type, kind, and rank as the output var containing the sample mean.
[in]sample: The input contiguous array of shape (nsam), (ndim, nsam) or (nsam, ndim) of the same type and kind as the output var, containing the sample whose mean is to be computed.
[in]dim: The input scalar integer of default kind IK representing the dimension (1 or 2) of the input sample along which the mean must be computed.
  1. If dim = 1, the input sample of rank 2 is assumed to have the shape (nsam, ndim).
  2. If dim = 2, the input sample of rank 2 is assumed to have the shape (ndim, nsam).
The input dim must always be 1 or missing for an input sample of rank 1.
(optional. If missing, the variance of the whole input sample is computed.)
[in]weight: The input contiguous vector of length nsam of,
  1. type integer of default kind IK, or
  2. type real of the same kind as the input sample,
containing the corresponding weights of the data points in the input sample.
[out]weisum: The output scalar of the same type and kind as the input weight, containing sum(weight).
(optional. It must be present if and only if the input argument weight is also present.)
[in]meang: The input object of the same type, kind, and rank as the output var containing the best guess for the sample mean.
If no good guess is known a priori, meang can be set to any observation in sample.
See the example usage below.


Possible calling interfaces

! 1D sample
call setVarMean(var, mean, sample(1 : nsam), meang)
call setVarMean(var, mean, sample(1 : nsam), weight(1 : nsam), weisum, meang)
call setVarMean(var, mean, sample(1 : nsam), dim, meang)
call setVarMean(var, mean, sample(1 : nsam), dim, weight(1 : nsam), weisum, meang)
! 2D sample
call setVarMean(var, mean(1 : ndim), sample(:,:), meang(1 : ndim))
call setVarMean(var, mean(1 : ndim), sample(:,:), weight(1 : size(sample)), weisum, meang(1 : ndim))
call setVarMean(var, mean(1 : ndim), sample(:,:), dim, meang(1 : ndim))
call setVarMean(var, mean(1 : ndim), sample(:,:), dim, weight(1 : size(sample, dim)), weisum, meang(1 : ndim))
Return the (weighted) sample variance and mean of an input time series of nsam observations,...
This module contains classes and procedures for computing the properties related to the covariance ma...
Warning
The condition all(0. <= weight) must hold for the corresponding input arguments.
The condition 1 <= dim .and. dim <= rank(sample) must hold for the corresponding input arguments.
The condition size(sample, dim) == size(weight, 1) must hold for the corresponding input arguments.
The condition size(sample, 3 - dim) == size(var, 1) must hold for the corresponding input arguments.
The condition size(sample, 3 - dim) == size(mean, 1) must hold for the corresponding input arguments.
The condition size(sample, 3 - dim) == size(meang, 1) must hold for the corresponding input arguments.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
Note
If the input sample is to be an array of type integer, simply convert the sample to an array of type real of the desired kind for the output real mean of the sample.
There is no point in accepting an input sample of type integer since it will be inevitably converted to an array of type real within the procedure to avoid potential integer overflow.
Furthermore, an sample of type integer creates ambiguity about the kind of the real-valued returned mean by the procedure.
See the notes in the description of the pm_sampleMean.
Note that the mean of any one or two-dimensional sample can be simply computed via the Fortran intrinsic routine sum():
integer(IK) :: i
integer(IK) , parameter :: NDIM = 3_IK
integer(IK) , parameter :: NSAM = 1000_IK
real(TKG) , parameter :: sample(NDIM,NSAM) = reshape([( real(i,RK), i = 1, NSAM )], shape = shape(sample))
real(TKG) , allocatable :: mean(:)
mean = sum(sample, dim = 1) / size(transpose(sample), dim = 1) ! assuming the first dimension represents the observations
mean = sum(sample, dim = 2) / size(sample, dim = 2) ! assuming the second dimension represents the observations
The mean of a whole multidimensional array can be obtained by either,
  1. reshaping the array to a vector form and passing it to this procedure, or
  2. mapping the array to a 1-dimensional pointer array of the same size as the ndim dimensional array.
See the examples below.
See also
getVar
setVar
getCov
setCov
getMean
setMean
setCovMean


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK, RK
4 use pm_kind, only: TKG => RKS ! All other real types are also supported.
5 use pm_io, only: display_type
6 use pm_sampleVar, only: getVar
7 use pm_sampleMean, only: getMean
8 use pm_sampleVar, only: setVarMean
10 use pm_sampleCov, only: fweight, rweight
11 use pm_sampleShift, only: getShifted
12 use pm_arrayResize, only: setResized
13 use pm_distUnif, only: getUnifRand
14
15 implicit none
16
17 integer(IK) :: dim
18 type(display_type) :: disp
19 disp = display_type(file = "main.out.F90")
20
21 call disp%skip()
22 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
23 call disp%show("!Compute the variance of a 1-D sample.")
24 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
25 call disp%skip()
26
27 block
28 real(TKG) :: var, mean
29 real(TKG), allocatable :: sample(:)
30 call disp%show("sample = getLinSpace(1._TKG, 9._TKG, 5_IK)")
31 sample = getLinSpace(1._TKG, 9._TKG, 5_IK)
32 call disp%show("sample")
33 call disp%show( sample )
34 call disp%show("call setVarMean(var, mean, sample, meang = sample(1))")
35 call setVarMean(var, mean, sample, meang = sample(1))
36 call disp%show("mean")
37 call disp%show( mean )
38 call disp%show("getMean(sample) ! for comparison.")
39 call disp%show( getMean(sample) )
40 call disp%show("var")
41 call disp%show( var )
42 call disp%show("getVar(sample) ! for comparison.")
43 call disp%show( getVar(sample) )
44 call disp%show("call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))")
45 call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))
46 call disp%show("mean")
47 call disp%show( mean )
48 call disp%show("getMean(sample, dim = 1_IK) ! for comparison.")
49 call disp%show( getMean(sample, dim = 1_IK) )
50 call disp%show("var")
51 call disp%show( var )
52 call disp%show("getVar(sample, dim = 1_IK) ! for comparison.")
53 call disp%show( getVar(sample, dim = 1_IK) )
54 call disp%skip()
55 end block
56
57 block
58 real(TKG) :: var
59 complex(TKG) :: mean
60 complex(TKG), allocatable :: sample(:)
61 call disp%show("sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)")
62 sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)
63 call disp%show("sample")
64 call disp%show( sample )
65 call disp%show("call setVarMean(var, mean, sample, meang = sample(1))")
66 call setVarMean(var, mean, sample, meang = sample(1))
67 call disp%show("mean")
68 call disp%show( mean )
69 call disp%show("getMean(sample) ! for comparison.")
70 call disp%show( getMean(sample) )
71 call disp%show("var")
72 call disp%show( var )
73 call disp%show("getVar(sample) ! for comparison.")
74 call disp%show( getVar(sample) )
75 call disp%show("call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))")
76 call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))
77 call disp%show("mean")
78 call disp%show( mean )
79 call disp%show("getMean(sample, dim = 1_IK) ! for comparison.")
80 call disp%show( getMean(sample, dim = 1_IK) )
81 call disp%show("var")
82 call disp%show( var )
83 call disp%show("getVar(sample, dim = 1_IK) ! for comparison.")
84 call disp%show( getVar(sample, dim = 1_IK) )
85 call disp%skip()
86 end block
87
88 call disp%skip()
89 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
90 call disp%show("!Compute the variance of a 1-D weighted sample.")
91 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
92 call disp%skip()
93
94 block
95 real(TKG) :: var, weisum
96 real(TKG), allocatable :: weight(:)
97 real(TKG), allocatable :: sample(:)
98 real(TKG) :: mean
99 call disp%show("sample = getLinSpace(1._TKG, 9._TKG, 5_IK)")
100 sample = getLinSpace(1._TKG, 9._TKG, 5_IK)
101 call disp%show("sample")
102 call disp%show( sample )
103 call disp%show("weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))")
104 weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))
105 call disp%show("weight")
106 call disp%show( weight )
107 call disp%show("call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))")
108 call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))
109 call disp%show("mean")
110 call disp%show( mean )
111 call disp%show("getMean(sample, weight) ! for comparison.")
112 call disp%show( getMean(sample, weight) )
113 call disp%show("var")
114 call disp%show( var )
115 call disp%show("getVar(sample, weight) ! for comparison.")
116 call disp%show( getVar(sample, weight) )
117 call disp%show("call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))")
118 call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))
119 call disp%show("mean")
120 call disp%show( mean )
121 call disp%show("getMean(sample, 1_IK, weight) ! for comparison.")
122 call disp%show( getMean(sample, 1_IK, weight) )
123 call disp%show("var")
124 call disp%show( var )
125 call disp%show("getVar(sample, 1_IK, weight) ! for comparison.")
126 call disp%show( getVar(sample, 1_IK, weight) )
127 call disp%skip()
128 end block
129
130 block
131 real(TKG) :: var, weisum
132 real(TKG), allocatable :: weight(:)
133 complex(TKG), allocatable :: sample(:)
134 complex(TKG) :: mean
135 call disp%show("sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)")
136 sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)
137 call disp%show("sample")
138 call disp%show( sample )
139 call disp%show("weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))")
140 weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))
141 call disp%show("weight")
142 call disp%show( weight )
143 call disp%show("call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))")
144 call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))
145 call disp%show("mean")
146 call disp%show( mean )
147 call disp%show("getMean(sample, weight) ! for comparison.")
148 call disp%show( getMean(sample, weight) )
149 call disp%show("var")
150 call disp%show( var )
151 call disp%show("getVar(sample, weight) ! for comparison.")
152 call disp%show( getVar(sample, weight) )
153 call disp%show("call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))")
154 call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))
155 call disp%show("mean")
156 call disp%show( mean )
157 call disp%show("getMean(sample, 1_IK, weight) ! for comparison.")
158 call disp%show( getMean(sample, 1_IK, weight) )
159 call disp%show("var")
160 call disp%show( var )
161 call disp%show("getVar(sample, 1_IK, weight) ! for comparison.")
162 call disp%show( getVar(sample, 1_IK, weight) )
163 call disp%skip()
164 end block
165
166 call disp%skip()
167 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
168 call disp%show("!Compute the variance of a 2-D array.")
169 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
170 call disp%skip()
171
172 block
173 real(TKG), allocatable :: var(:)
174 real(TKG), allocatable :: meang(:), mean(:), sample(:,:)
175 call disp%skip()
176 call disp%show("sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
177 sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
178 call disp%show("sample")
179 call disp%show( sample )
180 call disp%show("call setResized(var, 1_IK)")
181 call setResized(var, 1_IK)
182 call disp%show("call setResized(mean, 1_IK)")
183 call setResized(mean, 1_IK)
184 call disp%show("call setVarMean(var(1), mean(1), sample, meang = sample(1,1))")
185 call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
186 call disp%show("mean")
187 call disp%show( mean )
188 call disp%show("getMean(sample) ! for comparison.")
189 call disp%show( getMean(sample) )
190 call disp%show("var")
191 call disp%show( var )
192 call disp%show("getVar(sample) ! for comparison.")
193 call disp%show( getVar(sample) )
194 call disp%skip()
195 do dim = 1, 2
196 call disp%show("dim ! The observations axis.")
197 call disp%show( dim )
198 call disp%show("sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
199 sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
200 call disp%show("sample")
201 call disp%show( sample )
202 call disp%show("call setResized(var, size(sample, 3 - dim, IK))")
203 call setResized(var, size(sample, 3 - dim, IK))
204 call disp%show("call setResized(mean, size(sample, 3 - dim, IK))")
205 call setResized(mean, size(sample, 3 - dim, IK))
206 call disp%show("if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if")
207 if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
208 call disp%show("call setVarMean(var, mean, sample, dim, meang)")
209 call setVarMean(var, mean, sample, dim, meang)
210 call disp%show("mean")
211 call disp%show( mean )
212 call disp%show("getMean(sample, dim) ! for comparison.")
213 call disp%show( getMean(sample, dim) )
214 call disp%show("var")
215 call disp%show( var )
216 call disp%show("getVar(sample, dim) ! for comparison.")
217 call disp%show( getVar(sample, dim) )
218 call disp%skip()
219 end do
220 end block
221
222 block
223 real(TKG), allocatable :: var(:)
224 complex(TKG), allocatable :: meang(:), mean(:), sample(:,:)
225 call disp%skip()
226 call disp%show("sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)")
227 sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
228 call disp%show("sample")
229 call disp%show( sample )
230 call disp%show("call setResized(var, 1_IK)")
231 call setResized(var, 1_IK)
232 call disp%show("call setResized(mean, 1_IK)")
233 call setResized(mean, 1_IK)
234 call disp%show("call setVarMean(var(1), mean(1), sample, meang = sample(1,1))")
235 call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
236 call disp%show("mean")
237 call disp%show( mean )
238 call disp%show("getMean(sample) ! for comparison.")
239 call disp%show( getMean(sample) )
240 call disp%show("var")
241 call disp%show( var )
242 call disp%show("getVar(sample) ! for comparison.")
243 call disp%show( getVar(sample) )
244 call disp%skip()
245 do dim = 1, 2
246 call disp%show("dim ! The observations axis.")
247 call disp%show( dim )
248 call disp%show("sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)")
249 sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
250 call disp%show("sample")
251 call disp%show( sample )
252 call disp%show("call setResized(var, size(sample, 3 - dim, IK))")
253 call setResized(var, size(sample, 3 - dim, IK))
254 call disp%show("call setResized(mean, size(sample, 3 - dim, IK))")
255 call setResized(mean, size(sample, 3 - dim, IK))
256 call disp%show("if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if")
257 if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
258 call disp%show("call setVarMean(var, mean, sample, dim, meang)")
259 call setVarMean(var, mean, sample, dim, meang)
260 call disp%show("mean")
261 call disp%show( mean )
262 call disp%show("getMean(sample, dim) ! for comparison.")
263 call disp%show( getMean(sample, dim) )
264 call disp%show("var")
265 call disp%show( var )
266 call disp%show("getVar(sample, dim) ! for comparison.")
267 call disp%show( getVar(sample, dim) )
268 call disp%skip()
269 end do
270 end block
271
272 call disp%skip()
273 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
274 call disp%show("!Compute the variance of a 2-D weighted sample.")
275 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
276 call disp%skip()
277
278 block
279 real(TKG):: rweisum
280 real(TKG), allocatable :: var(:)
281 real(TKG), allocatable :: rweight(:)
282 real(TKG), allocatable :: meang(:), mean(:), sample(:,:)
283 integer(IK), allocatable :: iweight(:)
284 integer(IK) :: iweisum
285 call disp%skip()
286 call disp%show("sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
287 sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
288 call disp%show("sample")
289 call disp%show( sample )
290 call disp%show("iweight = getUnifRand(1, 9, size(sample, kind = IK))")
291 iweight = getUnifRand(1, 9, size(sample, kind = IK))
292 call disp%show("iweight")
293 call disp%show( iweight )
294 call disp%show("rweight = iweight")
295 rweight = iweight
296 call disp%show("call setResized(var, 1_IK)")
297 call setResized(var, 1_IK)
298 call disp%show("call setResized(mean, 1_IK)")
299 call setResized(mean, 1_IK)
300 call disp%show("call setVarMean(var(1), mean(1), sample, iweight, iweisum, meang = sample(1,1))")
301 call setVarMean(var(1), mean(1), sample, iweight, iweisum, meang = sample(1,1))
302 call disp%show("mean")
303 call disp%show( mean )
304 call disp%show("getMean(sample, iweight) ! for comparison.")
305 call disp%show( getMean(sample, iweight) )
306 call disp%show("var")
307 call disp%show( var )
308 call disp%show("getVar(sample, iweight) ! for comparison.")
309 call disp%show( getVar(sample, iweight) )
310 call disp%show("[iweisum, sum(iweight)] ! for comparison.")
311 call disp%show( [iweisum, sum(iweight)] )
312 call disp%show("call setVarMean(var(1), mean(1), sample, rweight, rweisum, meang = sample(1,1))")
313 call setVarMean(var(1), mean(1), sample, rweight, rweisum, meang = sample(1,1))
314 call disp%show("mean")
315 call disp%show( mean )
316 call disp%show("getMean(sample, rweight) ! for comparison.")
317 call disp%show( getMean(sample, rweight) )
318 call disp%show("var")
319 call disp%show( var )
320 call disp%show("getVar(sample, rweight) ! for comparison.")
321 call disp%show( getVar(sample, rweight) )
322 call disp%show("[rweisum, sum(rweight)] ! for comparison.")
323 call disp%show( [rweisum, sum(rweight)] )
324 call disp%skip()
325 do dim = 1, 2
326 call disp%show("dim ! The observations axis.")
327 call disp%show( dim )
328 call disp%show("sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
329 sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
330 call disp%show("sample")
331 call disp%show( sample )
332 call disp%show("iweight = getUnifRand(1, 9, size(sample, dim, IK))")
333 iweight = getUnifRand(1, 9, size(sample, dim, IK))
334 call disp%show("iweight")
335 call disp%show( iweight )
336 call disp%show("call setResized(var, size(sample, 3 - dim, IK))")
337 call setResized(var, size(sample, 3 - dim, IK))
338 call disp%show("call setResized(mean, size(sample, 3 - dim, IK))")
339 call setResized(mean, size(sample, 3 - dim, IK))
340 call disp%show("if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if")
341 if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
342 call disp%show("call setVarMean(var, mean, sample, dim, iweight, iweisum, meang)")
343 call setVarMean(var, mean, sample, dim, iweight, iweisum, meang)
344 call disp%show("mean")
345 call disp%show( mean )
346 call disp%show("getMean(sample, dim, iweight) ! for comparison.")
347 call disp%show( getMean(sample, dim, iweight) )
348 call disp%show("var")
349 call disp%show( var )
350 call disp%show("getVar(sample, dim, iweight) ! for comparison.")
351 call disp%show( getVar(sample, dim, iweight) )
352 call disp%show("[iweisum, sum(iweight)] ! for comparison.")
353 call disp%show( [iweisum, sum(iweight)] )
354 call disp%skip()
355 end do
356 end block
357
358end program example
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate count evenly spaced points over the interval [x1, x2] if x1 < x2, or [x2,...
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return the (weighted) mean of an input sample of nsam observations with ndim = 1 or 2 at...
Generate a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified i...
Generate and return the variance of the input sample of type complex or real of shape (nsam) or (ndim...
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
This module contains classes and procedures for computing the properties related to the covariance ma...
This module contains classes and procedures for computing the first moment (i.e., the statistical mea...
This module contains classes and procedures for shifting univariate or multivariate samples by arbitr...
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Compute the variance of a 1-D sample.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6sample = getLinSpace(1._TKG, 9._TKG, 5_IK)
7sample
8+1.00000000, +3.00000000, +5.00000000, +7.00000000, +9.00000000
9call setVarMean(var, mean, sample, meang = sample(1))
10mean
11+5.00000000
12getMean(sample) ! for comparison.
13+5.00000000
14var
15+8.00000000
16getVar(sample) ! for comparison.
17+8.00000000
18call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))
19mean
20+5.00000000
21getMean(sample, dim = 1_IK) ! for comparison.
22+5.00000000
23var
24+8.00000000
25getVar(sample, dim = 1_IK) ! for comparison.
26+8.00000000
27
28sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)
29sample
30(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+5.00000000, -5.00000000), (+7.00000000, -7.00000000), (+9.00000000, -9.00000000)
31call setVarMean(var, mean, sample, meang = sample(1))
32mean
33(+5.00000000, -5.00000000)
34getMean(sample) ! for comparison.
35(+5.00000000, -5.00000000)
36var
37+16.0000000
38getVar(sample) ! for comparison.
39+16.0000000
40call setVarMean(var, mean, sample, dim = 1_IK, meang = sample(1))
41mean
42(+5.00000000, -5.00000000)
43getMean(sample, dim = 1_IK) ! for comparison.
44(+5.00000000, -5.00000000)
45var
46+16.0000000
47getVar(sample, dim = 1_IK) ! for comparison.
48+16.0000000
49
50
51!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52!Compute the variance of a 1-D weighted sample.
53!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54
55sample = getLinSpace(1._TKG, 9._TKG, 5_IK)
56sample
57+1.00000000, +3.00000000, +5.00000000, +7.00000000, +9.00000000
58weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))
59weight
60+1.00000000, +3.00000000, +5.00000000, +7.00000000, +9.00000000
61call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))
62mean
63+6.59999990
64getMean(sample, weight) ! for comparison.
65+6.59999990
66var
67+5.44000244
68getVar(sample, weight) ! for comparison.
69+5.44000006
70call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))
71mean
72+6.59999990
73getMean(sample, 1_IK, weight) ! for comparison.
74+6.59999990
75var
76+5.44000244
77getVar(sample, 1_IK, weight) ! for comparison.
78+5.44000006
79
80sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)
81sample
82(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+5.00000000, -5.00000000), (+7.00000000, -7.00000000), (+9.00000000, -9.00000000)
83weight = getLinSpace(1._TKG, 9._TKG, size(sample, kind = IK))
84weight
85+1.00000000, +3.00000000, +5.00000000, +7.00000000, +9.00000000
86call setVarMean(var, mean, sample, weight, weisum, meang = sample(1))
87mean
88(+6.59999990, -6.59999990)
89getMean(sample, weight) ! for comparison.
90(+6.59999990, -6.59999990)
91var
92+10.8800049
93getVar(sample, weight) ! for comparison.
94+10.8800001
95call setVarMean(var, mean, sample, 1_IK, weight, weisum, meang = sample(1))
96mean
97(+6.59999990, -6.59999990)
98getMean(sample, 1_IK, weight) ! for comparison.
99(+6.59999990, -6.59999990)
100var
101+10.8800049
102getVar(sample, 1_IK, weight) ! for comparison.
103+10.8800001
104
105
106!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107!Compute the variance of a 2-D array.
108!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110
111sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
112sample
113+7.94998980, +4.45161057, +3.87645626, +3.24502325, +3.21608257
114+2.39749146, +4.83929014, +1.17462206, +6.79872942, +3.67211962
115+2.75150585, +5.30928135, +3.19681406, +7.65632486, +7.21404600
116+2.42819500, +1.88159609, +6.91840076, +3.02238083, +6.87811089
117call setResized(var, 1_IK)
118call setResized(mean, 1_IK)
119call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
120mean
121+4.44390392
122getMean(sample) ! for comparison.
123+4.44390297
124var
125+4.22961140
126getVar(sample) ! for comparison.
127+4.22960663
128
129dim ! The observations axis.
130+1
131sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
132sample
133+6.81599092, +7.56448460, +6.41020107, +1.14715481, +1.09053707
134+4.99234056, +3.55882883, +2.53699541, +6.23901415, +6.93865728
135+1.25797367, +3.77786493, +5.39258099, +1.30912256, +8.22147560
136+7.62193394, +8.02175522, +8.21084499, +2.37028265, +2.29326057
137call setResized(var, size(sample, 3 - dim, IK))
138call setResized(mean, size(sample, 3 - dim, IK))
139if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
140call setVarMean(var, mean, sample, dim, meang)
141mean
142+5.17206001, +5.73073339, +5.63765574, +2.76639366, +4.63598251
143getMean(sample, dim) ! for comparison.
144+5.17206001, +5.73073339, +5.63765574, +2.76639366, +4.63598251
145var
146+6.01418972, +4.28557205, +4.22307158, +4.24039268, +9.05414963
147getVar(sample, dim) ! for comparison.
148+6.01419067, +4.28557205, +4.22307158, +4.24039316, +9.05414963
149
150dim ! The observations axis.
151+2
152sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
153sample
154+2.69432735, +1.31120729, +4.90211630, +5.18859053, +8.56194115
155+7.34854364, +3.49841166, +1.05653334, +1.38620520, +2.35181856
156+4.36591196, +3.29729652, +3.78562927, +8.64351273, +2.50805187
157+3.27256918, +4.96659422, +1.90125799, +5.40965128, +1.93678522
158call setResized(var, size(sample, 3 - dim, IK))
159call setResized(mean, size(sample, 3 - dim, IK))
160if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
161call setVarMean(var, mean, sample, dim, meang)
162mean
163+4.53163671, +3.12830257, +4.52008057, +3.49737167
164getMean(sample, dim) ! for comparison.
165+4.53163671, +3.12830234, +4.52008057, +3.49737144
166var
167+6.11181307, +5.17549610, +4.62186813, +2.16979480
168getVar(sample, dim) ! for comparison.
169+6.11181355, +5.17549467, +4.62186813, +2.16979480
170
171
172sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
173sample
174(+2.08800459, -8.98819828), (+5.08825827, -2.36627483), (+4.15972090, -2.91407824), (+7.07735348, -6.14716530), (+7.84971905, -6.87458897)
175(+3.54088163, -5.38680601), (+1.95898199, -6.61446714), (+1.83908987, -7.53111124), (+6.69927216, -2.99935532), (+4.82910442, -2.73835850)
176(+1.04224539, -1.37500620), (+8.59548378, -8.61305332), (+6.56422853, -2.42932081), (+6.96554708, -5.31055403), (+3.40410900, -7.65198421)
177(+5.46654701, -6.09119558), (+4.82176495, -5.67076683), (+2.29676819, -7.51938629), (+1.65075016, -3.27941990), (+5.46735811, -4.31308889)
178call setResized(var, 1_IK)
179call setResized(mean, 1_IK)
180call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
181mean
182(+4.57025909, -5.24070930)
183getMean(sample) ! for comparison.
184(+4.57025909, -5.24070835)
185var
186+9.95799541
187getVar(sample) ! for comparison.
188+9.95800114
189
190dim ! The observations axis.
191+1
192sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
193sample
194(+1.01534176, -6.94132137), (+5.19471741, -2.58132601), (+5.62161636, -7.30456686), (+1.44825840, -6.02162027), (+1.82166719, -3.74837732)
195(+2.49023151, -3.39636421), (+8.17473507, -7.97577333), (+4.23489189, -8.41050053), (+7.83253574, -7.64903450), (+1.80643177, -1.11736822)
196(+1.21414280, -2.68762541), (+7.59500837, -2.08668661), (+1.63792324, -3.19332600), (+5.17135048, -1.00223970), (+5.40919828, -4.60305119)
197(+2.16135454, -4.93095589), (+6.29819345, -1.42012405), (+5.72278070, -3.33578634), (+3.66600657, -8.54844570), (+1.29871130, -1.76002932)
198call setResized(var, size(sample, 3 - dim, IK))
199call setResized(mean, size(sample, 3 - dim, IK))
200if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
201call setVarMean(var, mean, sample, dim, meang)
202mean
203(+1.72026765, -4.48906660), (+6.81566334, -3.51597738), (+4.30430317, -5.56104469), (+4.52953768, -5.80533504), (+2.58400202, -2.80720663)
204getMean(sample, dim) ! for comparison.
205(+1.72026765, -4.48906660), (+6.81566334, -3.51597738), (+4.30430317, -5.56104517), (+4.52953768, -5.80533504), (+2.58400202, -2.80720663)
206var
207+3.04712296, +8.13713074, +8.14473152, +13.9005203, +4.72061777
208getVar(sample, dim) ! for comparison.
209+3.04712343, +8.13713074, +8.14473152, +13.9005184, +4.72061729
210
211dim ! The observations axis.
212+2
213sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
214sample
215(+3.88544273, -5.26088095), (+4.00470352, -2.12165880), (+3.33925104, -5.24632263), (+2.15385342, -2.76061201), (+4.77310371, -3.30723619)
216(+1.44570684, -5.38315821), (+1.07031631, -7.63331032), (+5.37028265, -4.99051952), (+8.03854084, -3.03792763), (+2.27835321, -6.14734316)
217(+7.17909908, -3.83046293), (+5.53971720, -7.50785971), (+4.86186314, -6.42338419), (+4.62255764, -6.36426926), (+6.81819582, -3.85481453)
218(+1.56008577, -6.56934547), (+6.55750275, -5.37784243), (+8.22171688, -4.53394413), (+3.09237242, -7.15203190), (+8.25220680, -1.49130678)
219call setResized(var, size(sample, 3 - dim, IK))
220call setResized(mean, size(sample, 3 - dim, IK))
221if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
222call setVarMean(var, mean, sample, dim, meang)
223mean
224(+3.63127089, -3.73934221), (+3.64064002, -5.43845177), (+5.80428648, -5.59615803), (+5.53677654, -5.02489424)
225getMean(sample, dim) ! for comparison.
226(+3.63127065, -3.73934221), (+3.64064002, -5.43845177), (+5.80428696, -5.59615850), (+5.53677702, -5.02489424)
227var
228+2.42469454, +9.37989330, +3.27032351, +11.4350834
229getVar(sample, dim) ! for comparison.
230+2.42469478, +9.37989330, +3.27032399, +11.4350843
231
232
233!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234!Compute the variance of a 2-D weighted sample.
235!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236
237
238sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
239sample
240+1.65361500, +5.37214136, +7.00109816, +2.28111219, +2.34323931
241+8.94827461, +6.73563385, +5.03175306, +3.27840185, +2.02162170
242+1.11423874, +1.42326212, +1.91157293, +3.53081703, +4.71305990
243+1.27170420, +1.43061256, +4.31328773, +5.25232840, +7.57500553
244iweight = getUnifRand(1, 9, size(sample, kind = IK))
245iweight
246+2, +2, +9, +8, +3, +1, +9, +1, +3, +2, +2, +3, +7, +6, +6, +2, +3, +9, +9, +9
247rweight = iweight
248call setResized(var, 1_IK)
249call setResized(mean, 1_IK)
250call setVarMean(var(1), mean(1), sample, iweight, iweisum, meang = sample(1,1))
251mean
252+3.43200064
253getMean(sample, iweight) ! for comparison.
254+3.43200040
255var
256+4.92176962
257getVar(sample, iweight) ! for comparison.
258+4.92176962
259[iweisum, sum(iweight)] ! for comparison.
260+96, +96
261call setVarMean(var(1), mean(1), sample, rweight, rweisum, meang = sample(1,1))
262mean
263+3.43200064
264getMean(sample, rweight) ! for comparison.
265+3.43200040
266var
267+4.92176962
268getVar(sample, rweight) ! for comparison.
269+4.92176962
270[rweisum, sum(rweight)] ! for comparison.
271+96.0000000, +96.0000000
272
273dim ! The observations axis.
274+1
275sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
276sample
277+4.03435707, +7.72161913, +6.33432531, +6.31476259, +4.13681412
278+8.65785599, +3.40431786, +1.29530859, +2.84341335, +2.69444132
279+8.94824600, +2.30203676, +2.34408951, +4.85817719, +5.71486139
280+6.90595722, +6.52811050, +6.25133896, +5.65091419, +5.36067152
281iweight = getUnifRand(1, 9, size(sample, dim, IK))
282iweight
283+8, +4, +2, +7
284call setResized(var, size(sample, 3 - dim, IK))
285call setResized(mean, size(sample, 3 - dim, IK))
286if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
287call setVarMean(var, mean, sample, dim, iweight, iweisum, meang)
288mean
289+6.34021282, +5.98528910, +4.96682787, +5.29354811, +4.42031908
290getMean(sample, dim, iweight) ! for comparison.
291+6.34021282, +5.98528910, +4.96682835, +5.29354811, +4.42031908
292var
293+3.80313230, +3.80760312, +4.48513794, +1.60136867, +1.05233967
294getVar(sample, dim, iweight) ! for comparison.
295+3.80313253, +3.80760336, +4.48513842, +1.60136890, +1.05233979
296[iweisum, sum(iweight)] ! for comparison.
297+21, +21
298
299dim ! The observations axis.
300+2
301sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
302sample
303+3.96113110, +1.16317797, +8.30420208, +2.87538433, +8.48839188
304+8.25876904, +5.44241714, +8.83090401, +1.35686874, +3.97847795
305+8.46551609, +5.08023834, +4.92049360, +8.74204159, +5.25961733
306+6.94744015, +3.12607765, +8.86942577, +2.65129375, +3.27874422
307iweight = getUnifRand(1, 9, size(sample, dim, IK))
308iweight
309+7, +9, +7, +8, +8
310call setResized(var, size(sample, 3 - dim, IK))
311call setResized(mean, size(sample, 3 - dim, IK))
312if (dim == 1) then; meang = sample(1,:); else; meang = sample(:,1); end if
313call setVarMean(var, mean, sample, dim, iweight, iweisum, meang)
314mean
315+4.80092669, +5.41774940, +6.44711494, +4.77674484
316getMean(sample, dim, iweight) ! for comparison.
317+4.80092669, +5.41774988, +6.44711447, +4.77674532
318var
319+8.93300533, +7.34745121, +2.95029521, +5.86790609
320getVar(sample, dim, iweight) ! for comparison.
321+8.93300533, +7.34745121, +2.95029449, +5.86790752
322[iweisum, sum(iweight)] ! for comparison.
323+39, +39
324
325
Test:
test_pm_sampleMean


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, April 21, 2017, 1:54 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 6304 of file pm_sampleVar.F90.


The documentation for this interface was generated from the following file: