Return the variance of an input (weighted) sample of type complex
or real
of shape (nsam)
or (ndim, nsam)
or (nsam, ndim)
where ndim
is the number of data dimensions (the number of data attributes) and nsam
is the number of data points.
See the documentation of the parent module pm_sampleVar for algorithmic details and variance definitions.
- 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:
-
If
sample is a vector, the output var must be a scalar.
-
If
sample is a matrix, the output var must be a vector of size ndim = size(sample, 3 - dim) .
|
[in] | mean | : The input scalar or contiguous vector of shape (ndim) of the same type and kind as the input sample containing the sample mean.
-
If the input
sample is a 1D array, then mean must be a scalar.
-
If the input
sample is a 2D array, then mean must be a vector of size ndim = size(sample, 3 - dim) (i.e., computed along the specified input dimension dim ).
(optional. default = 0. or getFilled(0., ndim) depending on the rank of the input sample and value of dim .) |
[in] | sample | : The input contiguous array of shape (nsam) , (ndim, nsam) , or (nsam, ndim) of,
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128),
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the sample comprised of nsam observations each with ndim attributes.
If sample is a matrix, then the direction along which variance is computed is dictated by the input argument dim .
|
[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.
-
If
dim = 1 , the input sample of rank 2 is assumed to have the shape (nsam, ndim) .
-
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
-
type
integer of default kind IK, or
-
type
real of the same kind as the input sample ,
containing the corresponding weights of individual nsam observations in sample .
(optional. default = getFilled(1, size(sample, dim)) if dim is present, or getFilled(1, size(sample)) if dim is missing.) |
[in] | weisum | : The input scalar of the same type and kind as the input weight containing sum(weight) .
This quantity is also a byproduct of computing the mean of a sample and is automatically returned by setMean().
(optional. It must be present if and only if the input argument weight is also present.) |
[in] | correction | : The input scalar object that can be the following:
-
The constant fweight or an object of type fweight implying a bias correction based on the assumption of frequency weights for the sample observations, even if the
weight argument is missing.
This is the most popular type of correction, also known as the Bessel correction.
-
The constant rweight or an object of type rweight_type implying a bias correction based on the assumption of reliability weights for the sample observations.
(optional. If missing, no bias-correction will be applied to the output var .) |
Possible calling interfaces ⛓
call setVar(var, sample(
1 : nsam))
call setVar(var, mean, sample(
1 : nsam))
call setVar(var, sample(
1 : nsam), dim)
call setVar(var, mean, sample(
1 : nsam), dim)
call setVar(var, sample(
1 : nsam), weight(
1 : nsam))
call setVar(var, mean, sample(
1 : nsam), weight(
1 : nsam), weisum)
call setVar(var, sample(
1 : nsam), dim, weight(
1 : nsam))
call setVar(var, mean, sample(
1 : nsam), dim, weight(
1 : nsam), weisum)
call setVar(var(
1 :
size(sample,
3 - dim)), sample(:,:), dim)
call setVar(var(
1 :
size(sample,
3 - dim)), mean(
1 :
size(sample,
3 - dim)), sample(:,:), dim)
call setVar(var(
1 :
size(sample,
3 - dim)), sample(:,:), dim, weight(
1 :
size(sample, dim)))
call setVar(var(
1 :
size(sample,
3 - dim)), mean(
1 :
size(sample,
3 - dim)), sample(:,:), dim, weight(
1 :
size(sample, dim)), weisum)
call setVar(var, mean, sample(:,:))
call setVar(var, sample(:,:), weight(
1 :
size(sample)))
call setVar(var, mean, sample(:,:), dim, weight(
1 :
size(sample)), weisum)
Return the variance of an input (weighted) sample of type complex or real of shape (nsam) or (ndim,...
This module contains classes and procedures for computing the properties related to the covariance ma...
- Warning
- The condition
0 < sum(weight)
must hold for the corresponding input arguments.
The condition all(0. <= weight)
must hold for the corresponding input arguments.
The condition 1 < size(sample, dim)
must hold for the corresponding input arguments.
The condition 0 < size(sample, 3 - dim)
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, 3 - dim) == size(var)
must hold for the corresponding input arguments.
The condition size(sample, 3 - dim) == size(mean)
must hold for the corresponding input arguments.
The condition size(sample, dim) == size(weight)
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
- Note the effects of bias-correction in computing the variance become noticeable only for very sample sample sizes (i.e., when
nsam
is small).
-
For a one-dimensional
sample
, one can also use the concise Fortran syntax to achieve the same goal as with the interface var = setVar(sample(:), mean, correction)
with integer weight
:
mean = sum(sample) / size(sample)
var = sum( (sample-mean)**2 ) / (size(sample) - 1)
But the above concise version will be likely slightly slower as it potentially involves more loops.
-
For a two or higher-dimensional
sample
, if the variance is to be computed for the entire sample
(as opposed to computing it along a particular dimension), simply pass reshape(sample, shape = size(sample))
to the appropriate setVar interface.
Alternatively, a 1D pointer of the same size as the multidimensional sample can be passed to the procedure.
- See also
- getVar
setVar
getMean
setMean
setVarMean
getShifted
setShifted
Example usage ⛓
18 type(display_type) :: disp
22 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
23 call disp%show(
"!Compute the variance of a 1-D sample.")
24 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
28 real(TKG) :: var, mean
29 real(TKG),
allocatable :: sample(:)
30 call disp%show(
"sample = getLinSpace(1._TKG, 9._TKG, 5_IK)")
34 call disp%show(
"mean = getMean(sample)")
38 call disp%show(
"call setVar(var, mean, sample)")
39 call setVar(var, mean, sample)
42 call disp%show(
"call setVar(var, sample - mean)")
43 call setVar(var, sample
- mean)
46 call disp%show(
"call setVar(var, mean, sample, dim = 1_IK)")
47 call setVar(var, mean, sample,
dim = 1_IK)
50 call disp%show(
"call setVar(var, sample - mean, dim = 1_IK)")
51 call setVar(var, sample
- mean,
dim = 1_IK)
60 complex(TKG),
allocatable :: sample(:)
61 call disp%show(
"sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)")
65 call disp%show(
"mean = getMean(sample)")
69 call disp%show(
"call setVar(var, mean, sample)")
70 call setVar(var, mean, sample)
73 call disp%show(
"call setVar(var, sample - mean)")
74 call setVar(var, sample
- mean)
77 call disp%show(
"call setVar(var, mean, sample, dim = 1_IK)")
78 call setVar(var, mean, sample,
dim = 1_IK)
81 call disp%show(
"call setVar(var, sample - mean, dim = 1_IK)")
82 call setVar(var, sample
- mean,
dim = 1_IK)
89 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
90 call disp%show(
"!Compute the variance of a 1-D weighted sample.")
91 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
95 real(TKG) :: var, weisum
96 real(TKG),
allocatable :: weight(:)
97 real(TKG),
allocatable :: sample(:)
99 call disp%show(
"sample = getLinSpace(1._TKG, 9._TKG, 5_IK)")
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))
107 call disp%show(
"call setMean(mean, sample, weight, weisum)")
108 call setMean(mean, sample, weight, weisum)
111 call disp%show(
"call setVar(var, mean, sample, weight, weisum)")
112 call setVar(var, mean, sample, weight, weisum)
115 call disp%show(
"call setVar(var, sample - mean, weight, weisum)")
116 call setVar(var, sample
- mean, weight, weisum)
119 call disp%show(
"call setVar(var, mean, sample, 1_IK, weight, weisum)")
120 call setVar(var, mean, sample,
1_IK, weight, weisum)
123 call disp%show(
"call setVar(var, sample - mean, 1_IK, weight, weisum)")
124 call setVar(var, sample
- mean,
1_IK, weight, weisum)
131 real(TKG) :: var, weisum
132 real(TKG),
allocatable :: weight(:)
133 complex(TKG),
allocatable :: sample(:)
135 call disp%show(
"sample = cmplx(getLinSpace(1., 9., 5_IK), -getLinSpace(1., 9., 5_IK), TKG)")
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))
143 call disp%show(
"call setMean(mean, sample, weight, weisum)")
144 call setMean(mean, sample, weight, weisum)
147 call disp%show(
"call setVar(var, mean, sample, weight, weisum)")
148 call setVar(var, mean, sample, weight, weisum)
151 call disp%show(
"call setVar(var, sample - mean, weight, weisum)")
152 call setVar(var, sample
- mean, weight, weisum)
155 call disp%show(
"call setVar(var, mean, sample, 1_IK, weight, weisum)")
156 call setVar(var, mean, sample,
1_IK, weight, weisum)
159 call disp%show(
"call setVar(var, sample - mean, 1_IK, weight, weisum)")
160 call setVar(var, sample
- mean,
1_IK, weight, weisum)
167 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
168 call disp%show(
"!Compute the variance of a 2-D array.")
169 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
173 real(TKG),
allocatable :: var(:), mean(:), sample(:,:)
175 call disp%show(
"sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
179 call disp%show(
"mean = [getMean(sample)]")
183 call disp%show(
"call setResized(var, size(mean, 1, IK))")
185 call disp%show(
"call setVar(var(1), mean(1), sample)")
186 call setVar(var(
1), mean(
1), sample)
189 call disp%show(
"call setVar(var(1), sample - mean(1))")
190 call setVar(var(
1), sample
- mean(
1))
195 call disp%show(
"dim ! The observations axis.")
197 call disp%show(
"sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
201 call disp%show(
"mean = getMean(sample, dim)")
205 call disp%show(
"call setResized(var, size(mean, 1, IK))")
207 call disp%show(
"call setVar(var, mean, sample, dim)")
208 call setVar(var, mean, sample, dim)
211 call disp%show(
"call setVar(var, getShifted(sample, dim, -mean), dim)")
220 real(TKG),
allocatable :: var(:)
221 complex(TKG),
allocatable :: mean(:), sample(:,:)
223 call disp%show(
"sample = cmplx(getUnifRand(1., 9., 4_IK, 5_IK), -getUnifRand(1., 9., 4_IK, 5_IK), TKG)")
227 call disp%show(
"mean = [getMean(sample)]")
231 call disp%show(
"call setResized(var, size(mean, 1, IK))")
233 call disp%show(
"call setVar(var(1), mean(1), sample)")
234 call setVar(var(
1), mean(
1), sample)
237 call disp%show(
"call setVar(var(1), sample - mean(1))")
238 call setVar(var(
1), sample
- mean(
1))
243 call disp%show(
"dim ! The observations axis.")
245 call disp%show(
"sample = getUnifRand(1._TKG, 9._TKG, 4_IK, 5_IK)")
249 call disp%show(
"mean = getMean(sample, dim)")
253 call disp%show(
"call setResized(var, size(mean, 1, IK))")
255 call disp%show(
"call setVar(var, mean, sample, dim)")
256 call setVar(var, mean, sample, dim)
259 call disp%show(
"call setVar(var, getShifted(sample, dim, -mean), dim)")
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.
This is a generic method of the derived type display_type with pass attribute.
Generate and return the (weighted) mean of an input sample of nsam observations with ndim = 1 or 2 at...
Return the (weighted) mean of a pair of time series or of an input sample of nsam observations with n...
Generate a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified i...
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...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
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.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
8+1.00000000,
+3.00000000,
+5.00000000,
+7.00000000,
+9.00000000
12call setVar(var, mean, sample)
15call setVar(var, sample
- mean)
18call setVar(var, mean, sample,
dim = 1_IK)
21call setVar(var, sample
- mean,
dim = 1_IK)
27(
+1.00000000,
-1.00000000), (
+3.00000000,
-3.00000000), (
+5.00000000,
-5.00000000), (
+7.00000000,
-7.00000000), (
+9.00000000,
-9.00000000)
30(
+5.00000000,
-5.00000000)
31call setVar(var, mean, sample)
34call setVar(var, sample
- mean)
37call setVar(var, mean, sample,
dim = 1_IK)
40call setVar(var, sample
- mean,
dim = 1_IK)
51+1.00000000,
+3.00000000,
+5.00000000,
+7.00000000,
+9.00000000
54+1.00000000,
+3.00000000,
+5.00000000,
+7.00000000,
+9.00000000
55call setMean(mean, sample, weight, weisum)
58call setVar(var, mean, sample, weight, weisum)
61call setVar(var, sample
- mean, weight, weisum)
64call setVar(var, mean, sample,
1_IK, weight, weisum)
67call setVar(var, sample
- mean,
1_IK, weight, weisum)
73(
+1.00000000,
-1.00000000), (
+3.00000000,
-3.00000000), (
+5.00000000,
-5.00000000), (
+7.00000000,
-7.00000000), (
+9.00000000,
-9.00000000)
76+1.00000000,
+3.00000000,
+5.00000000,
+7.00000000,
+9.00000000
77call setMean(mean, sample, weight, weisum)
79(
+6.59999990,
-6.59999990)
80call setVar(var, mean, sample, weight, weisum)
83call setVar(var, sample
- mean, weight, weisum)
86call setVar(var, mean, sample,
1_IK, weight, weisum)
89call setVar(var, sample
- mean,
1_IK, weight, weisum)
101+4.99208498,
+2.95732641,
+4.75280142,
+8.25016403,
+2.60768318
102+1.30410624,
+2.56706238,
+8.42079830,
+3.31771803,
+4.79901981
103+3.05362129,
+7.69651318,
+7.68817997,
+3.44250488,
+6.95504951
104+1.95321417,
+5.91499043,
+7.63474894,
+3.45482922,
+4.35354424
109call setVar(var(
1), mean(
1), sample)
112call setVar(var(
1), sample
- mean(
1))
120+5.07040119,
+8.18688107,
+8.07137871,
+2.36299229,
+5.93489456
121+3.51929140,
+6.88988113,
+8.41877842,
+7.37935686,
+1.75933218
122+6.85032940,
+5.97783804,
+3.17212677,
+4.24614668,
+2.63039732
123+2.66183376,
+6.41409445,
+8.04856491,
+5.70079517,
+4.61552954
126+4.52546406,
+6.86717367,
+6.92771244,
+4.92232323,
+3.73503828
128call setVar(var, mean, sample, dim)
130+2.54686403,
+0.684585452,
+4.72299576,
+3.41260529,
+2.68456984
133+2.54686403,
+0.684585452,
+4.72299576,
+3.41260529,
+2.68456984
139+7.12013388,
+5.65459251,
+1.25092983,
+1.68968439,
+7.84949636
140+5.99501181,
+3.63208866,
+1.83200741,
+2.91044521,
+1.03793144
141+6.59227514,
+1.12925911,
+7.63841629,
+6.17782784,
+4.82043600
142+3.04413223,
+4.22631931,
+5.16133022,
+8.76823330,
+2.98810196
145+4.71296787,
+3.08149695,
+5.27164316,
+4.83762360
147call setVar(var, mean, sample, dim)
149+7.52897358,
+2.91167259,
+5.10595751,
+4.51310301
152+7.52897358,
+2.91167259,
+5.10595751,
+4.51310301
157(
+1.23045349,
-3.06762123), (
+3.93084288,
-2.06950092), (
+8.16557503,
-1.45759630), (
+8.82432652,
-1.68302393), (
+3.78081179,
-3.58433437)
158(
+7.25860548,
-5.84598494), (
+7.91704941,
-4.13305187), (
+1.27426434,
-1.79075718), (
+5.33506250,
-8.09331226), (
+2.34009314,
-1.95512199)
159(
+4.86909866,
-4.48561430), (
+4.29004622,
-3.18819809), (
+1.03960514,
-5.02137470), (
+7.14669323,
-5.01868057), (
+8.33708763,
-8.50489902)
160(
+2.94104671,
-2.21028042), (
+7.88658953,
-1.15123463), (
+3.97341585,
-6.72483730), (
+4.07731199,
-5.79815769), (
+6.17104053,
-4.75227404)
163(
+5.03945112,
-4.02679253)
165call setVar(var(
1), mean(
1), sample)
168call setVar(var(
1), sample
- mean(
1))
176(
+7.26133108,
+0.00000000), (
+1.71488905,
+0.00000000), (
+5.41692114,
+0.00000000), (
+4.96555424,
+0.00000000), (
+4.61938810,
+0.00000000)
177(
+3.55054712,
+0.00000000), (
+4.18184471,
+0.00000000), (
+2.47794676,
+0.00000000), (
+2.87746000,
+0.00000000), (
+2.94727612,
+0.00000000)
178(
+4.90886688,
+0.00000000), (
+2.34768295,
+0.00000000), (
+8.17975426,
+0.00000000), (
+7.89459515,
+0.00000000), (
+3.78514671,
+0.00000000)
179(
+1.17962790,
+0.00000000), (
+2.21072626,
+0.00000000), (
+1.87488651,
+0.00000000), (
+4.63587570,
+0.00000000), (
+4.65911531,
+0.00000000)
182(
+4.22509336,
+0.00000000), (
+2.61378574,
+0.00000000), (
+4.48737717,
+0.00000000), (
+5.09337139,
+0.00000000), (
+4.00273132,
+0.00000000)
184call setVar(var, mean, sample, dim)
186+4.85403967,
+0.875023007,
+6.34015465,
+3.24568915,
+0.493108571
189+4.85403967,
+0.875023007,
+6.34015465,
+3.24568915,
+0.493108571
195(
+5.60478735,
+0.00000000), (
+6.39414406,
+0.00000000), (
+3.01272440,
+0.00000000), (
+2.56399250,
+0.00000000), (
+3.28316116,
+0.00000000)
196(
+5.81833553,
+0.00000000), (
+8.63126373,
+0.00000000), (
+8.41351700,
+0.00000000), (
+6.05017996,
+0.00000000), (
+8.69643402,
+0.00000000)
197(
+6.86451054,
+0.00000000), (
+5.15035295,
+0.00000000), (
+1.02694750,
+0.00000000), (
+2.27890921,
+0.00000000), (
+4.09106588,
+0.00000000)
198(
+6.73661900,
+0.00000000), (
+5.54512119,
+0.00000000), (
+3.76807308,
+0.00000000), (
+5.49529791,
+0.00000000), (
+4.76617575,
+0.00000000)
201(
+4.17176199,
+0.00000000), (
+7.52194691,
+0.00000000), (
+3.88235736,
+0.00000000), (
+5.26225758,
+0.00000000)
203call setVar(var, mean, sample, dim)
205+2.34208941,
+1.69465816,
+4.25380421,
+0.957349122
208+2.34208941,
+1.69465816,
+4.25380421,
+0.957349122
- Test:
- test_pm_sampleVar
- Todo:
- Normal Priority: The examples of this generic interface need to be extended to weighted samples.
- Todo:
- Very Low Priority: The functionality of this interface can be expanded in the future to include the computation of the variance of higher dimensional input
sample
and whole sample
input arrays of arbitrary shape.
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.
-
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.
-
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.
- Copyright
- Computational Data Science Lab
- Author:
- Fatemeh Bagheri, Monday 02:15 AM, September 27, 2021, Dallas, TX
Definition at line 2618 of file pm_sampleVar.F90.