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+6.23101759, +1.80139017, +2.85529184, +2.10455704, +4.03061295
114+4.33758450, +6.51584482, +1.30442810, +2.87540388, +6.58009052
115+4.14842224, +5.29646444, +7.34088230, +8.93565845, +2.69979477
116+8.57904148, +2.85514688, +7.13377810, +8.80456448, +3.41684771
117call setResized(var, 1_IK)
118call setResized(mean, 1_IK)
119call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
120mean
121+4.89234114
122getMean(sample) ! for comparison.
123+4.89234114
124var
125+5.74296856
126getVar(sample) ! for comparison.
127+5.74296904
128
129dim ! The observations axis.
130+1
131sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
132sample
133+3.79013968, +8.15100002, +2.66874075, +6.01550865, +3.19957638
134+8.16963673, +6.51185751, +6.24838448, +3.44625854, +3.54126501
135+5.28146791, +8.69198513, +3.21732569, +2.95255518, +8.92523670
136+3.41177845, +7.89192343, +3.99417591, +1.25755692, +7.84615612
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.16325569, +7.81169128, +4.03215694, +3.41796970, +5.87805843
143getMean(sample, dim) ! for comparison.
144+5.16325569, +7.81169176, +4.03215694, +3.41796970, +5.87805843
145var
146+3.50135565, +0.646513224, +1.85899031, +2.90799999, +6.44839525
147getVar(sample, dim) ! for comparison.
148+3.50135541, +0.646513164, +1.85899019, +2.90800071, +6.44839382
149
150dim ! The observations axis.
151+2
152sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
153sample
154+7.85388708, +2.27132416, +6.86721802, +1.85558462, +7.23191881
155+5.52287149, +4.32797050, +2.21704054, +4.57380772, +8.11474705
156+8.06937122, +6.11389065, +8.50014973, +2.98059320, +5.41113091
157+1.92430592, +3.27564240, +6.44183588, +1.29872036, +1.52788019
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+5.21598625, +4.95128727, +6.21502686, +2.89367700
164getMean(sample, dim) ! for comparison.
165+5.21598673, +4.95128727, +6.21502686, +2.89367723
166var
167+6.74247980, +3.66826129, +3.95568323, +3.61685991
168getVar(sample, dim) ! for comparison.
169+6.74248075, +3.66826105, +3.95568347, +3.61685944
170
171
172sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)
173sample
174(+7.78232336, -6.38262939), (+7.63592100, -4.75375175), (+8.89669895, -5.43110228), (+4.19091511, -8.67274570), (+8.93950939, -3.70274353)
175(+1.91753387, -3.11807156), (+5.98391199, -7.60711670), (+5.77987242, -4.52920341), (+8.61166191, -3.52403736), (+2.85432100, -2.22961092)
176(+2.46021032, -1.97570753), (+1.84966040, -6.35449171), (+1.88943672, -7.89821577), (+1.22861147, -3.57067823), (+1.32866573, -7.45364809)
177(+6.60469580, -1.57940865), (+8.87889194, -6.82808161), (+8.97922993, -7.95911503), (+8.79323959, -3.03248167), (+8.04065800, -1.89757729)
178call setResized(var, 1_IK)
179call setResized(mean, 1_IK)
180call setVarMean(var(1), mean(1), sample, meang = sample(1,1))
181mean
182(+5.63229847, -4.92502117)
183getMean(sample) ! for comparison.
184(+5.63229847, -4.92502117)
185var
186+13.9995270
187getVar(sample) ! for comparison.
188+13.9995251
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.92973518, -7.10630751), (+2.06336451, -7.86325741), (+1.94000483, -3.00183058), (+5.54568148, -6.57727957), (+7.44238853, -7.28280354)
195(+6.33854723, -7.94744062), (+7.74714899, -4.25402355), (+7.52029705, -3.73797369), (+7.92195988, -7.30547810), (+6.15820885, -4.61835337)
196(+6.52675772, -2.58185911), (+6.05859995, -8.34707832), (+7.30071497, -7.60633087), (+2.96511412, -6.70989084), (+8.65090370, -4.80329752)
197(+4.87045765, -5.78833961), (+7.97797298, -7.49458599), (+1.54810143, -1.22701502), (+7.48719311, -2.47973680), (+5.79902887, -5.73669624)
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(+4.91637421, -5.85598660), (+5.96177149, -6.98973656), (+4.57727957, -3.89328766), (+5.97998714, -5.76809645), (+7.01263237, -5.61028767)
204getMean(sample, dim) ! for comparison.
205(+4.91637421, -5.85598660), (+5.96177197, -6.98973656), (+4.57727957, -3.89328766), (+5.97998714, -5.76809597), (+7.01263237, -5.61028767)
206var
207+7.54999924, +8.20100021, +13.4810286, +7.50990295, +2.37998748
208getVar(sample, dim) ! for comparison.
209+7.55000019, +8.20100212, +13.4810276, +7.50990343, +2.37998796
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(+1.25199795, -6.27884865), (+5.68475628, -6.30847120), (+3.76309681, -6.11957026), (+8.16487122, -5.28562069), (+7.69224882, -7.48434973)
216(+7.33500957, -5.06785727), (+3.34104443, -3.85906649), (+1.62672853, -6.53159666), (+4.29207230, -1.78148365), (+8.96261024, -1.53834772)
217(+6.75989008, -6.73386955), (+1.08569288, -1.45365524), (+6.94705677, -1.12162447), (+6.62572098, -2.23531008), (+8.08164883, -2.25543356)
218(+8.65246201, -4.72744513), (+6.72593355, -6.40350914), (+5.56740379, -3.28813028), (+8.41146660, -8.31492710), (+2.95394373, -1.24187660)
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(+5.31139421, -6.29537201), (+5.11149311, -3.75567031), (+5.90000200, -2.75997853), (+6.46224213, -4.79517746)
225getMean(sample, dim) ! for comparison.
226(+5.31139469, -6.29537249), (+5.11149311, -3.75567055), (+5.90000248, -2.75997853), (+6.46224213, -4.79517794)
227var
228+7.05814695, +10.7953768, +10.2023792, +10.3304119
229getVar(sample, dim) ! for comparison.
230+7.05814838, +10.7953749, +10.2023783, +10.3304129
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+8.89806938, +1.07575369, +2.16644669, +4.66418076, +7.22673368
241+5.12503719, +7.14868784, +3.54351807, +3.81741142, +2.47923470
242+8.79406166, +6.82515669, +1.09592295, +4.51031494, +7.87915277
243+4.29261446, +8.10224724, +5.29211760, +7.02474022, +7.13483000
244iweight = getUnifRand(1, 9, size(sample, kind = IK))
245iweight
246+2, +9, +1, +7, +7, +3, +2, +5, +1, +4, +6, +2, +3, +7, +3, +1, +8, +8, +8, +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+5.05798531
253getMean(sample, iweight) ! for comparison.
254+5.05798531
255var
256+5.68180990
257getVar(sample, iweight) ! for comparison.
258+5.68180656
259[iweisum, sum(iweight)] ! for comparison.
260+96, +96
261call setVarMean(var(1), mean(1), sample, rweight, rweisum, meang = sample(1,1))
262mean
263+5.05798531
264getMean(sample, rweight) ! for comparison.
265+5.05798531
266var
267+5.68180990
268getVar(sample, rweight) ! for comparison.
269+5.68180656
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+2.62256050, +8.51588058, +6.90235949, +1.76822758, +3.10969782
278+1.45672417, +3.21343994, +7.69671488, +7.87379646, +1.73079443
279+7.17184067, +3.26621532, +4.33842659, +8.00684738, +7.03410196
280+8.92055321, +2.59056044, +1.40899706, +8.33997631, +2.43560934
281iweight = getUnifRand(1, 9, size(sample, dim, IK))
282iweight
283+6, +4, +9, +1
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+4.75146866, +4.79677677, +5.63279247, +6.12530756, +4.56619453
290getMean(sample, dim, iweight) ! for comparison.
291+4.75146914, +4.79677725, +5.63279295, +6.12530804, +4.56619453
292var
293+7.03599548, +5.94845724, +2.98144007, +8.14501095, +5.21203852
294getVar(sample, dim, iweight) ! for comparison.
295+7.03599691, +5.94845915, +2.98144007, +8.14501095, +5.21203804
296[iweisum, sum(iweight)] ! for comparison.
297+20, +20
298
299dim ! The observations axis.
300+2
301sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)
302sample
303+5.98273134, +8.06611633, +4.58259869, +6.16902781, +8.13586140
304+8.51793289, +6.72156143, +5.95537281, +6.54280615, +2.61302185
305+3.50624990, +2.01927042, +1.12704802, +5.71999645, +3.34952784
306+1.84521151, +2.45969009, +6.81009054, +5.38087606, +7.71486521
307iweight = getUnifRand(1, 9, size(sample, dim, IK))
308iweight
309+4, +3, +7, +5, +5
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+6.32216311, +5.90429878, +3.05499029, +5.32955265
316getMean(sample, dim, iweight) ! for comparison.
317+6.32216263, +5.90429878, +3.05499029, +5.32955265
318var
319+1.97218025, +3.56447172, +2.74985290, +4.87818813
320getVar(sample, dim, iweight) ! for comparison.
321+1.97218025, +3.56447196, +2.74985290, +4.87819004
322[iweisum, sum(iweight)] ! for comparison.
323+24, +24
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 at Austin

Definition at line 6304 of file pm_sampleVar.F90.


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