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

Return the covariance matrix corresponding to the input (potentially weighted) correlation matrix or return the biased sample covariance matrix of the input array of shape (ndim, nsam) or (nsam, ndim) or a pair of (potentially weighted) time series x(1:nsam) and y(1:nsam) where ndim is the number of data dimensions (the number of data attributes) and nsam is the number of data points.
More...

Detailed Description

Return the covariance matrix corresponding to the input (potentially weighted) correlation matrix or return the biased sample covariance matrix of the input array of shape (ndim, nsam) or (nsam, ndim) or a pair of (potentially weighted) time series x(1:nsam) and y(1:nsam) where ndim is the number of data dimensions (the number of data attributes) and nsam is the number of data points.

This generic interface performs either of the following two computational tasks:

  1. compute the covariance matrix corresponding to an input correlation matrix and vector of standard deviations in arbitrary ndim dimensions.
  2. compute the sample covariance matrix of a random sample of nsam observations in arbitrary ndim dimensional space.

See the documentation of the parent module pm_sampleCov for algorithmic details and sample covariance matrix definition.

Parameters
[in,out]cov: The output positive semi-definite square matrix of shape (1:ndim, 1:ndim) of,
  1. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128),
  2. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
representing the covariance matrix corresponding to the input sample or cor, whichever is present.
On output,
  1. If the input arguments x and y are present, then ndim == 2 holds by definition and the output covariance is of the form

    \begin{equation} \ms{cov} = \begin{bmatrix} \sigma_{xx} && \sigma_{yx} \\ \sigma_{xy} && \sigma_{yy} \end{bmatrix} \end{equation}

  2. Otherwise, is subset is present, then only the specified input subset will be overwritten with the covariance matrix.
    Any elements not in the specified input subset remain intact.
[in]subset: The input scalar constant argument that can be any of the following:
  1. The constant lowDia, implying that only the lower-diagonal subset of the output covariance matrix must be computed.
  2. The constant uppDia, implying that only the upper-diagonal subset of the output covariance matrix must be computed.
This input argument is merely serves to resolve the different procedures of this generic interface from each other at compile-time.
(optional. It must be present if and only if the input arguments x and y are both missing.)
[in]cor: The input positive semi-definite square matrix of shape (1:ndim, 1:ndim) of the same type and kind as the input cov, representing the correlation matrix based upon which the output covariance matrix cov is to be computed.
(optional. It must be present if and only if the input arguments std and subsetr are present and the rest are missing.)
[in]subsetr: The input scalar constant argument that can be any of the following:
  1. The constant lowDia, implying that only the lower-diagonal subset of the input correlation matrix must be used.
  2. The constant uppDia, implying that only the upper-diagonal subset of the input correlation matrix must be used.
This input argument is merely serves to resolve the different procedures of this generic interface from each other at compile-time.
Although the allowed subset constants imply the use of the diagonal elements, they are by definition assumed to be 1 and therefore are not referenced in the algorithm.
(optional. It must be present if and only if the input arguments subset, cor, and std are present and the rest are missing.)
[in]std: The input positive vector of shape (1:ndim) of type real of the same kind as the input cov, containing the standard deviations of the ndim data attributes based upon which the output covariance matrix cov is to be computed.
(optional. It must be present if and only if the input arguments cor and subsetr are present and the rest are missing.)
[in]mean: The input contiguous vector of shape (ndim) of the same type and kind as the output cov containing the sample mean.
  1. If the input sample is missing and x and y are present, then mean must be a vector of size ndim = 2.
  2. If the input sample is present and 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).
The mean vector can be readily computed via getMean or setMean.
(optional. default = getFilled(0., ndim) or [0., 0.] depending on the rank of the input sample and dim or the presence of x and y.)
[in]x: The input contiguous vector of shape (nsam) of the same type and kind as the output cov, containing the first attribute x of the observational sample, where nsam is the number of observations in the sample.
(optional. It must be present if and only if the input argument y is present and cor and sample are missing.)
[in]y: The input contiguous vector of shape (nsam) of the same type and kind as the output cov, containing the second attribute x of the observational sample, where nsam is the number of observations in the sample.
(optional. It must be present if and only if the input argument x is present and cor and sample are missing.)
[in]sample: The input contiguous array of shape (ndim, nsam) or (nsam, ndim) of the same type and kind as the output cov, containing the sample comprised of nsam observations each with ndim attributes.
If sample is a matrix, then the direction along which the covariance matrix is computed is dictated by the input argument dim.
(optional. It must be present if and only if the input argument dim is present and x and y are missing.)
[in]dim: The input scalar integer of default kind IK indicating the dimension of sample along which the covariance matrix must be computed.
  1. If dim = 1, the input sample is assumed to have the shape (nsam, ndim).
  2. If dim = 2, the input sample is assumed to have the shape (ndim, nsam).
(optional. It must be present if and only if the input argument sample is present.)
[in]weight: The contiguous vector of length nsam of,
  1. type integer of default kind IK, or
  2. type real of the same kind as the kind of the output cov,
containing the corresponding weights of individual nsam observations in sample.
(optional. default = getFilled(1, nsam). It can be present if and only if the input arguments sample or x and y are present.)
[in]weisum: The input scalar of the same type and kind as the input weight containing sum(weight).
This quantity is 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 weight argument is present and the input arguments cor, subsetr, and std are missing.)


Possible calling interfaces

use pm_sampleCov, only: setCov, lowDia, uppDia
! correlation matrix to covariance matrix.
call setCov(cov(1:ndim, 1:ndim), subset, cor(1:ndim, 1:ndim), subsetr, std(1:ndim))
! XY time series covariance matrix.
call setCov(cov(1:2, 1:2) , x(1:nsam), y(1:nsam))
call setCov(cov(1:2, 1:2), mean(1:2), x(1:nsam), y(1:nsam))
call setCov(cov(1:2, 1:2) , x(1:nsam), y(1:nsam), weight(1:nsam))
call setCov(cov(1:2, 1:2), mean(1:2), x(1:nsam), y(1:nsam), weight(1:nsam), weisum)
! sample covariance matrix.
call setCov(cov(1:ndim, 1:ndim), subset , sample(:,:), dim)
call setCov(cov(1:ndim, 1:ndim), subset, mean(1:ndim), sample(:,:), dim)
call setCov(cov(1:ndim, 1:ndim), subset, , sample(:,:), dim, weight(1:nsam))
call setCov(cov(1:ndim, 1:ndim), subset, mean(1:ndim), sample(:,:), dim, weight(1:nsam), weisum)
Return the covariance matrix corresponding to the input (potentially weighted) correlation matrix or ...
This module contains classes and procedures for computing the properties related to the covariance ma...
Warning
The condition any(x(1) /= x) must hold for the corresponding input arguments.
The condition any(y(1) /= x) must hold for the corresponding input arguments.
The input sample must contain at least two unique values per sample attribute.
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 all(shape(cov) == shape(cor)) 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(variance) 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.
The condition size(x) == size(weight) must hold for the corresponding input arguments.
The condition size(x) == size(y) 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 covariance matrix become noticeable only for very sample sample sizes (i.e., when nsam is small).
See also
getCor
setCor
getCov
setCov
getVar
setVar
getMean
setMean
getCovMerged
setCovMerged
setCovMeanMerged
getVarCorrection
getMeanMerged
setMeanMerged
getVarMerged
setVarMerged


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_sampleMean, only: getMean
5 use pm_sampleMean, only: setMean
9 use pm_distUnif, only: getUnifRand
10 use pm_arrayFill, only: getFilled
11 use pm_sampleCov, only: uppDia
12 use pm_sampleCov, only: lowDia
13 use pm_sampleCov, only: setCov
14 use pm_io, only: display_type
15 use pm_io, only: getFormat
16
17 implicit none
18
19 integer(IK) :: itry, ntry = 10
20 type(display_type) :: disp
21 character(:), allocatable :: format
22 disp = display_type(file = "main.out.F90")
23
24 call disp%skip()
25 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
26 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%show("!Convert correlation matrix and standard deviation to covariance matrix.")
28 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
29 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
30 call disp%skip()
31
32 block
33 use pm_kind, only: TKG => RKS ! All other real types are also supported.
34 use pm_matrixCopy, only: setMatCopy, rdpack
35 use pm_distCov, only: getCovRand
36 integer(IK) :: ndim
37 real(TKG), allocatable :: cov(:,:), cor(:,:), std(:)
38 format = getFormat(mold = [0._TKG], ed = SK_"es", signed = .true._LK)
39 do itry = 1, ntry
40 call disp%skip()
41 call disp%show("ndim = getUnifRand(1, 7)")
42 ndim = getUnifRand(1, 7)
43 call disp%show("ndim")
44 call disp%show( ndim )
45 call disp%show("std = getUnifRand(1, 10, ndim)")
46 std = getUnifRand(1, 10, ndim)
47 call disp%show("std")
48 call disp%show( std , format = format )
49 call disp%show("cor = getCovRand(1., ndim)")
50 cor = getCovRand(1., ndim)
51 call disp%show("cor")
52 call disp%show( cor , format = format )
53 call disp%show("cov = getFilled(0._TKG, ndim, ndim + 1)")
54 cov = getFilled(0._TKG, ndim, ndim + 1)
55 call disp%show("call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)")
56 call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
57 call disp%show("cov")
58 call disp%show( cov , format = format )
59 call disp%show("call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.")
60 call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std)
61 call disp%show("cov")
62 call disp%show( cov , format = format )
63 call disp%skip()
64 call disp%show("cov = getFilled(0._TKG, ndim, ndim + 1)")
65 cov = getFilled(0._TKG, ndim, ndim + 1)
66 call disp%show("call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)")
67 call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
68 call disp%show("cov")
69 call disp%show( cov , format = format )
70 call disp%show("call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.")
71 call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std)
72 call disp%show("cov")
73 call disp%show( cov , format = format )
74 call disp%skip()
75 end do
76 end block
77
78 call disp%skip()
79 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
80 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
81 call disp%show("!Compute the covariance matrix of a 2-D sample.")
82 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
83 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
84 call disp%skip()
85
86 block
87 use pm_kind, only: TKG => RKS ! All other real types are also supported.
88 real(TKG), allocatable :: sample(:,:), samShifted(:,:), cov(:,:), mean(:)
89 integer(IK) :: ndim, nsam, dim
90 call disp%show("ndim = 2; nsam = 10; dim = 2")
91 ndim = 2; nsam = 10; dim = 2
92 call disp%show("sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])")
93 sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])
94 call disp%show("sample")
95 call disp%show( sample )
96 call disp%show("mean = getMean(sample, dim)")
97 mean = getMean(sample, dim)
98 call disp%show("mean")
99 call disp%show( mean )
100 call disp%show("samShifted = getShifted(sample, dim, -mean)")
101 samShifted = getShifted(sample, dim, -mean)
102 call disp%show("samShifted")
103 call disp%show( samShifted )
104 call disp%show("cov = getFilled(0., ndim, ndim)")
105 cov = getFilled(0., ndim, ndim)
106 call disp%show("call setCov(cov, uppDia, mean, sample, dim)")
107 call setCov(cov, uppDia, mean, sample, dim)
108 call disp%show("cov")
109 call disp%show( cov )
110 call disp%show("cov = getFilled(0., ndim, ndim)")
111 cov = getFilled(0., ndim, ndim)
112 call disp%show("call setCov(cov, uppDia, samShifted, dim) ! same result as above.")
113 call setCov(cov, uppDia, samShifted, dim)
114 call disp%show("cov")
115 call disp%show( cov )
116 call disp%skip()
117 call disp%show("cov = getFilled(0., ndim, ndim)")
118 cov = getFilled(0., ndim, ndim)
119 call disp%show("call setCov(cov, lowDia, mean, sample, dim)")
120 call setCov(cov, lowDia, mean, sample, dim)
121 call disp%show("cov")
122 call disp%show( cov )
123 call disp%show("cov = getFilled(0., ndim, ndim)")
124 cov = getFilled(0., ndim, ndim)
125 call disp%show("call setCov(cov, lowDia, samShifted, dim) ! same result as above.")
126 call setCov(cov, lowDia, samShifted, dim)
127 call disp%show("cov")
128 call disp%show( cov )
129 call disp%skip()
130 call disp%show("Compute the sample covariance along the first dimension.", deliml = SK_'''')
131 call disp%skip()
132 call disp%show("dim = 1")
133 dim = 1
134 call disp%show("cov = getFilled(0., ndim, ndim)")
135 cov = getFilled(0., ndim, ndim)
136 call disp%show("call setCov(cov, uppDia, mean, transpose(sample), dim)")
137 call setCov(cov, uppDia, mean, transpose(sample), dim)
138 call disp%show("cov")
139 call disp%show( cov )
140 call disp%show("cov = getFilled(0., ndim, ndim)")
141 cov = getFilled(0., ndim, ndim)
142 call disp%show("call setCov(cov, uppDia, transpose(samShifted), dim) ! same result as above.")
143 call setCov(cov, uppDia, transpose(samShifted), dim)
144 call disp%show("cov")
145 call disp%show( cov )
146 call disp%skip()
147 call disp%show("cov = getFilled(0., ndim, ndim)")
148 cov = getFilled(0., ndim, ndim)
149 call disp%show("call setCov(cov, lowDia, mean, transpose(sample), dim)")
150 call setCov(cov, lowDia, mean, transpose(sample), dim)
151 call disp%show("cov")
152 call disp%show( cov )
153 call disp%show("cov = getFilled(0., ndim, ndim)")
154 cov = getFilled(0., ndim, ndim)
155 call disp%show("call setCov(cov, lowDia, transpose(samShifted), dim) ! same result as above.")
156 call setCov(cov, lowDia, transpose(samShifted), dim)
157 call disp%show("cov")
158 call disp%show( cov )
159 call disp%skip()
160 call disp%show("Compute the full sample covariance for a pair of time series.", deliml = SK_'''')
161 call disp%skip()
162 call disp%show("cov = getFilled(0., ndim, ndim)")
163 cov = getFilled(0., ndim, ndim)
164 call disp%show("call setCov(cov, mean, sample(1,:), sample(2,:))")
165 call setCov(cov, mean, sample(1,:), sample(2,:))
166 call disp%show("cov")
167 call disp%show( cov )
168 call disp%show("cov = getFilled(0., ndim, ndim)")
169 cov = getFilled(0., ndim, ndim)
170 call disp%show("call setCov(cov, samShifted(1,:), samShifted(2,:)) ! same result as above.")
171 call setCov(cov, samShifted(1,:), samShifted(2,:))
172 call disp%show("cov")
173 call disp%show( cov )
174 call disp%skip()
175 end block
176
177 call disp%skip()
178 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
179 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
180 call disp%show("!Compute the biased covariance matrix of a weighted 2-D sample.")
181 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
182 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
183 call disp%skip()
184
185 block
186 use pm_kind, only: TKG => RKS ! All other real types are also supported.
187 real(TKG) :: rweisum
188 integer(IK) :: iweisum
189 real(TKG), allocatable :: rweight(:)
190 integer(IK), allocatable :: iweight(:)
191 real(TKG), allocatable :: sample(:,:), samShifted(:,:), cov(:,:), mean(:)
192 integer(IK) :: ndim, nsam, dim
193 call disp%show("ndim = 2; nsam = 10; dim = 2")
194 ndim = 2; nsam = 10; dim = 2
195 call disp%show("sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])")
196 sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])
197 call disp%show("sample")
198 call disp%show( sample )
199 call disp%show("call setResized(mean, ndim)")
200 call setResized(mean, ndim)
201 call disp%show("iweight = getUnifRand(1, 10, nsam) ! integer-valued weights.")
202 iweight = getUnifRand(1, 10, nsam) ! integer-valued weights.
203 call disp%show("iweight")
204 call disp%show( iweight )
205 call disp%show("call setMean(mean, sample, dim, iweight, iweisum)")
206 call setMean(mean, sample, dim, iweight, iweisum)
207 call disp%show("mean")
208 call disp%show( mean )
209 call disp%show("iweisum")
210 call disp%show( iweisum )
211 call disp%show("rweight = iweight ! or real-valued weights.")
212 rweight = iweight ! or real-valued weights.
213 call disp%show("iweight")
214 call disp%show( iweight )
215 call disp%show("call setMean(mean, sample, dim, rweight, rweisum)")
216 call setMean(mean, sample, dim, rweight, rweisum)
217 call disp%show("mean")
218 call disp%show( mean )
219 call disp%show("rweisum")
220 call disp%show( rweisum )
221
222 call disp%skip()
223 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
224 call disp%show("!Compute the covariance matrix integer weights.")
225 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
226 call disp%skip()
227
228 call disp%show("samShifted = getShifted(sample, dim, -mean)")
229 samShifted = getShifted(sample, dim, -mean)
230 call disp%show("samShifted")
231 call disp%show( samShifted )
232 call disp%show("cov = getFilled(0., ndim, ndim)")
233 cov = getFilled(0., ndim, ndim)
234 call disp%show("call setCov(cov, uppDia, mean, sample, dim, iweight, iweisum)")
235 call setCov(cov, uppDia, mean, sample, dim, iweight, iweisum)
236 call disp%show("cov")
237 call disp%show( cov )
238 call disp%show("cov = getFilled(0., ndim, ndim)")
239 cov = getFilled(0., ndim, ndim)
240 call disp%show("call setCov(cov, uppDia, samShifted, dim, iweight, sum(iweight)) ! same result as above.")
241 call setCov(cov, uppDia, samShifted, dim, iweight, sum(iweight))
242 call disp%show("cov")
243 call disp%show( cov )
244 call disp%skip()
245 call disp%show("cov = getFilled(0., ndim, ndim)")
246 cov = getFilled(0., ndim, ndim)
247 call disp%show("call setCov(cov, lowDia, mean, sample, dim, iweight, iweisum)")
248 call setCov(cov, lowDia, mean, sample, dim, iweight, iweisum)
249 call disp%show("cov")
250 call disp%show( cov )
251 call disp%show("cov = getFilled(0., ndim, ndim)")
252 cov = getFilled(0., ndim, ndim)
253 call disp%show("call setCov(cov, lowDia, samShifted, dim, iweight, sum(iweight)) ! same result as above.")
254 call setCov(cov, lowDia, samShifted, dim, iweight, sum(iweight))
255 call disp%show("cov")
256 call disp%show( cov )
257 call disp%skip()
258 call disp%show("Compute the sample covariance along the first dimension.", deliml = SK_'''')
259 call disp%skip()
260 call disp%show("dim = 1")
261 dim = 1
262 call disp%show("cov = getFilled(0., ndim, ndim)")
263 cov = getFilled(0., ndim, ndim)
264 call disp%show("call setCov(cov, uppDia, mean, transpose(sample), dim, iweight, iweisum)")
265 call setCov(cov, uppDia, mean, transpose(sample), dim, iweight, iweisum)
266 call disp%show("cov")
267 call disp%show( cov )
268 call disp%show("cov = getFilled(0., ndim, ndim)")
269 cov = getFilled(0., ndim, ndim)
270 call disp%show("call setCov(cov, uppDia, transpose(samShifted), dim, iweight, sum(iweight)) ! same result as above.")
271 call setCov(cov, uppDia, transpose(samShifted), dim, iweight, sum(iweight))
272 call disp%show("cov")
273 call disp%show( cov )
274 call disp%skip()
275 call disp%show("cov = getFilled(0., ndim, ndim)")
276 cov = getFilled(0., ndim, ndim)
277 call disp%show("call setCov(cov, lowDia, mean, transpose(sample), dim, iweight, iweisum)")
278 call setCov(cov, lowDia, mean, transpose(sample), dim, iweight, iweisum)
279 call disp%show("cov")
280 call disp%show( cov )
281 call disp%show("cov = getFilled(0., ndim, ndim)")
282 cov = getFilled(0., ndim, ndim)
283 call disp%show("call setCov(cov, lowDia, transpose(samShifted), dim, iweight, sum(iweight)) ! same result as above.")
284 call setCov(cov, lowDia, transpose(samShifted), dim, iweight, sum(iweight))
285 call disp%show("cov")
286 call disp%show( cov )
287 call disp%skip()
288 call disp%show("Compute the full sample covariance for a pair of time series.", deliml = SK_'''')
289 call disp%skip()
290 call disp%show("cov = getFilled(0., ndim, ndim)")
291 cov = getFilled(0., ndim, ndim)
292 call disp%show("call setCov(cov, mean, sample(1,:), sample(2,:), iweight, iweisum)")
293 call setCov(cov, mean, sample(1,:), sample(2,:), iweight, iweisum)
294 call disp%show("cov")
295 call disp%show( cov )
296 call disp%show("cov = getFilled(0., ndim, ndim)")
297 cov = getFilled(0., ndim, ndim)
298 call disp%show("call setCov(cov, samShifted(1,:), samShifted(2,:), iweight, sum(iweight)) ! same result as above.")
299 call setCov(cov, samShifted(1,:), samShifted(2,:), iweight, sum(iweight))
300 call disp%show("cov")
301 call disp%show( cov )
302 call disp%skip()
303
304 call disp%skip()
305 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
306 call disp%show("!Compute the covariance matrix real weights.")
307 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
308 call disp%skip()
309
310 call disp%show("dim = 2_IK")
311 dim = 2_IK
312 call disp%show("samShifted = getShifted(sample, dim, -mean)")
313 samShifted = getShifted(sample, dim, -mean)
314 call disp%show("samShifted")
315 call disp%show( samShifted )
316 call disp%show("cov = getFilled(0., ndim, ndim)")
317 cov = getFilled(0., ndim, ndim)
318 call disp%show("call setCov(cov, uppDia, mean, sample, dim, rweight, rweisum)")
319 call setCov(cov, uppDia, mean, sample, dim, rweight, rweisum)
320 call disp%show("cov")
321 call disp%show( cov )
322 call disp%show("cov = getFilled(0., ndim, ndim)")
323 cov = getFilled(0., ndim, ndim)
324 call disp%show("call setCov(cov, uppDia, samShifted, dim, rweight, sum(rweight)) ! same result as above.")
325 call setCov(cov, uppDia, samShifted, dim, rweight, sum(rweight))
326 call disp%show("cov")
327 call disp%show( cov )
328 call disp%skip()
329 call disp%show("cov = getFilled(0., ndim, ndim)")
330 cov = getFilled(0., ndim, ndim)
331 call disp%show("call setCov(cov, lowDia, mean, sample, dim, rweight, rweisum)")
332 call setCov(cov, lowDia, mean, sample, dim, rweight, rweisum)
333 call disp%show("cov")
334 call disp%show( cov )
335 call disp%show("cov = getFilled(0., ndim, ndim)")
336 cov = getFilled(0., ndim, ndim)
337 call disp%show("call setCov(cov, lowDia, samShifted, dim, rweight, sum(rweight)) ! same result as above.")
338 call setCov(cov, lowDia, samShifted, dim, rweight, sum(rweight))
339 call disp%show("cov")
340 call disp%show( cov )
341 call disp%skip()
342 call disp%show("Compute the sample covariance along the first dimension.", deliml = SK_'''')
343 call disp%skip()
344 call disp%show("dim = 1")
345 dim = 1
346 call disp%show("cov = getFilled(0., ndim, ndim)")
347 cov = getFilled(0., ndim, ndim)
348 call disp%show("call setCov(cov, uppDia, mean, transpose(sample), dim, rweight, rweisum)")
349 call setCov(cov, uppDia, mean, transpose(sample), dim, rweight, rweisum)
350 call disp%show("cov")
351 call disp%show( cov )
352 call disp%show("cov = getFilled(0., ndim, ndim)")
353 cov = getFilled(0., ndim, ndim)
354 call disp%show("call setCov(cov, uppDia, transpose(samShifted), dim, rweight, sum(rweight)) ! same result as above.")
355 call setCov(cov, uppDia, transpose(samShifted), dim, rweight, sum(rweight))
356 call disp%show("cov")
357 call disp%show( cov )
358 call disp%skip()
359 call disp%show("cov = getFilled(0., ndim, ndim)")
360 cov = getFilled(0., ndim, ndim)
361 call disp%show("call setCov(cov, lowDia, mean, transpose(sample), dim, rweight, rweisum)")
362 call setCov(cov, lowDia, mean, transpose(sample), dim, rweight, rweisum)
363 call disp%show("cov")
364 call disp%show( cov )
365 call disp%show("cov = getFilled(0., ndim, ndim)")
366 cov = getFilled(0., ndim, ndim)
367 call disp%show("call setCov(cov, lowDia, transpose(samShifted), dim, rweight, sum(rweight)) ! same result as above.")
368 call setCov(cov, lowDia, transpose(samShifted), dim, rweight, sum(rweight))
369 call disp%show("cov")
370 call disp%show( cov )
371 call disp%skip()
372 call disp%show("Compute the full sample covariance for a pair of time series.", deliml = SK_'''')
373 call disp%skip()
374 call disp%show("cov = getFilled(0., ndim, ndim)")
375 cov = getFilled(0., ndim, ndim)
376 call disp%show("call setCov(cov, mean, sample(1,:), sample(2,:), rweight, rweisum)")
377 call setCov(cov, mean, sample(1,:), sample(2,:), rweight, rweisum)
378 call disp%show("cov")
379 call disp%show( cov )
380 call disp%show("cov = getFilled(0., ndim, ndim)")
381 cov = getFilled(0., ndim, ndim)
382 call disp%show("call setCov(cov, samShifted(1,:), samShifted(2,:), rweight, sum(rweight)) ! same result as above.")
383 call setCov(cov, samShifted(1,:), samShifted(2,:), rweight, sum(rweight))
384 call disp%show("cov")
385 call disp%show( cov )
386 call disp%skip()
387 end block
388
389end program example
Generate and return an array of the specified rank and shape of arbitrary intrinsic type and kind wit...
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 random positive-definite (correlation or covariance) matrix using the Gram meth...
Definition: pm_distCov.F90:394
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
Generate and return a generic or type/kind-specific IO format with the requested specifications that ...
Definition: pm_io.F90:18485
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
Copy a desired subset of the input source matrix of arbitrary shape (:) or (:,:) to the target subset...
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 convenient allocation and filling of array...
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 generating random matrices distributed on the space o...
Definition: pm_distCov.F90:72
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 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 procedures and generic interfaces relevant to copying (diagonal or upper/lower t...
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!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4!Convert correlation matrix and standard deviation to covariance matrix.
5!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
8
9ndim = getUnifRand(1, 7)
10ndim
11+6
12std = getUnifRand(1, 10, ndim)
13std
14+8.000000E+00, +1.000000E+01, +6.000000E+00, +7.000000E+00, +7.000000E+00, +2.000000E+00
15cor = getCovRand(1., ndim)
16cor
17+1.000000E+00, -6.146030E-01, +6.040904E-01, +8.290617E-03, -3.783300E-01, +1.096828E-01
18-6.146030E-01, +1.000000E+00, -9.506434E-01, -4.666466E-01, -1.907399E-01, -5.286536E-01
19+6.040904E-01, -9.506434E-01, +1.000000E+00, +6.326929E-01, +1.303558E-01, +4.052165E-01
20+8.290617E-03, -4.666466E-01, +6.326929E-01, +1.000000E+00, +4.495044E-02, -3.255494E-02
21-3.783300E-01, -1.907399E-01, +1.303558E-01, +4.495044E-02, +1.000000E+00, +2.005435E-01
22+1.096828E-01, -5.286536E-01, +4.052165E-01, -3.255494E-02, +2.005435E-01, +1.000000E+00
23cov = getFilled(0._TKG, ndim, ndim + 1)
24call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
25cov
26+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
27-6.146030E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
28+6.040904E-01, -9.506434E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
29+8.290617E-03, -4.666466E-01, +6.326929E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
30-3.783300E-01, -1.907399E-01, +1.303558E-01, +4.495044E-02, +1.000000E+00, +0.000000E+00, +0.000000E+00
31+1.096828E-01, -5.286536E-01, +4.052165E-01, -3.255494E-02, +2.005435E-01, +1.000000E+00, +0.000000E+00
32call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
33cov
34+1.000000E+00, +6.400000E+01, -4.916824E+01, +2.899634E+01, +4.642745E-01, -2.118648E+01, +1.754925E+00
35-6.146030E-01, +1.000000E+00, +1.000000E+02, -5.703860E+01, -3.266526E+01, -1.335180E+01, -1.057307E+01
36+6.040904E-01, -9.506434E-01, +1.000000E+00, +3.600000E+01, +2.657310E+01, +5.474944E+00, +4.862597E+00
37+8.290617E-03, -4.666466E-01, +6.326929E-01, +1.000000E+00, +4.900000E+01, +2.202572E+00, -4.557692E-01
38-3.783300E-01, -1.907399E-01, +1.303558E-01, +4.495044E-02, +1.000000E+00, +4.900000E+01, +2.807609E+00
39+1.096828E-01, -5.286536E-01, +4.052165E-01, -3.255494E-02, +2.005435E-01, +1.000000E+00, +4.000000E+00
40
41cov = getFilled(0._TKG, ndim, ndim + 1)
42call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
43cov
44+0.000000E+00, +1.000000E+00, -6.146030E-01, +6.040904E-01, +8.290617E-03, -3.783300E-01, +1.096828E-01
45+0.000000E+00, +0.000000E+00, +1.000000E+00, -9.506434E-01, -4.666466E-01, -1.907399E-01, -5.286536E-01
46+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +6.326929E-01, +1.303558E-01, +4.052165E-01
47+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +4.495044E-02, -3.255494E-02
48+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +2.005435E-01
49+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
50call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
51cov
52+6.400000E+01, +1.000000E+00, -6.146030E-01, +6.040904E-01, +8.290617E-03, -3.783300E-01, +1.096828E-01
53-4.916824E+01, +1.000000E+02, +1.000000E+00, -9.506434E-01, -4.666466E-01, -1.907399E-01, -5.286536E-01
54+2.899634E+01, -5.703860E+01, +3.600000E+01, +1.000000E+00, +6.326929E-01, +1.303558E-01, +4.052165E-01
55+4.642745E-01, -3.266526E+01, +2.657310E+01, +4.900000E+01, +1.000000E+00, +4.495044E-02, -3.255494E-02
56-2.118648E+01, -1.335180E+01, +5.474944E+00, +2.202572E+00, +4.900000E+01, +1.000000E+00, +2.005435E-01
57+1.754925E+00, -1.057307E+01, +4.862597E+00, -4.557692E-01, +2.807609E+00, +4.000000E+00, +1.000000E+00
58
59
60ndim = getUnifRand(1, 7)
61ndim
62+5
63std = getUnifRand(1, 10, ndim)
64std
65+2.000000E+00, +1.000000E+00, +1.000000E+01, +5.000000E+00, +8.000000E+00
66cor = getCovRand(1., ndim)
67cor
68+1.000000E+00, +8.009845E-01, -2.793278E-01, +3.857585E-01, -5.876297E-01
69+8.009845E-01, +1.000000E+00, +2.067818E-02, +4.578368E-01, -6.384791E-01
70-2.793278E-01, +2.067818E-02, +1.000000E+00, +4.020308E-01, +4.063008E-01
71+3.857585E-01, +4.578368E-01, +4.020308E-01, +1.000000E+00, -4.943134E-01
72-5.876297E-01, -6.384791E-01, +4.063008E-01, -4.943134E-01, +1.000000E+00
73cov = getFilled(0._TKG, ndim, ndim + 1)
74call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
75cov
76+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
77+8.009845E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
78-2.793278E-01, +2.067818E-02, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
79+3.857585E-01, +4.578368E-01, +4.020308E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00
80-5.876297E-01, -6.384791E-01, +4.063008E-01, -4.943134E-01, +1.000000E+00, +0.000000E+00
81call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
82cov
83+1.000000E+00, +4.000000E+00, +1.601969E+00, -5.586555E+00, +3.857584E+00, -9.402076E+00
84+8.009845E-01, +1.000000E+00, +1.000000E+00, +2.067818E-01, +2.289184E+00, -5.107833E+00
85-2.793278E-01, +2.067818E-02, +1.000000E+00, +1.000000E+02, +2.010154E+01, +3.250406E+01
86+3.857585E-01, +4.578368E-01, +4.020308E-01, +1.000000E+00, +2.500000E+01, -1.977254E+01
87-5.876297E-01, -6.384791E-01, +4.063008E-01, -4.943134E-01, +1.000000E+00, +6.400000E+01
88
89cov = getFilled(0._TKG, ndim, ndim + 1)
90call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
91cov
92+0.000000E+00, +1.000000E+00, +8.009845E-01, -2.793278E-01, +3.857585E-01, -5.876297E-01
93+0.000000E+00, +0.000000E+00, +1.000000E+00, +2.067818E-02, +4.578368E-01, -6.384791E-01
94+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +4.020308E-01, +4.063008E-01
95+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -4.943134E-01
96+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
97call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
98cov
99+4.000000E+00, +1.000000E+00, +8.009845E-01, -2.793278E-01, +3.857585E-01, -5.876297E-01
100+1.601969E+00, +1.000000E+00, +1.000000E+00, +2.067818E-02, +4.578368E-01, -6.384791E-01
101-5.586555E+00, +2.067818E-01, +1.000000E+02, +1.000000E+00, +4.020308E-01, +4.063008E-01
102+3.857584E+00, +2.289184E+00, +2.010154E+01, +2.500000E+01, +1.000000E+00, -4.943134E-01
103-9.402076E+00, -5.107833E+00, +3.250406E+01, -1.977254E+01, +6.400000E+01, +1.000000E+00
104
105
106ndim = getUnifRand(1, 7)
107ndim
108+1
109std = getUnifRand(1, 10, ndim)
110std
111+7.000000E+00
112cor = getCovRand(1., ndim)
113cor
114+1.000000E+00
115cov = getFilled(0._TKG, ndim, ndim + 1)
116call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
117cov
118+1.000000E+00, +0.000000E+00
119call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
120cov
121+1.000000E+00, +4.900000E+01
122
123cov = getFilled(0._TKG, ndim, ndim + 1)
124call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
125cov
126+0.000000E+00, +1.000000E+00
127call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
128cov
129+4.900000E+01, +1.000000E+00
130
131
132ndim = getUnifRand(1, 7)
133ndim
134+6
135std = getUnifRand(1, 10, ndim)
136std
137+3.000000E+00, +9.000000E+00, +2.000000E+00, +9.000000E+00, +5.000000E+00, +8.000000E+00
138cor = getCovRand(1., ndim)
139cor
140+1.000000E+00, +2.053825E-01, +3.161243E-01, -1.263403E-03, +2.475277E-01, -3.331965E-01
141+2.053825E-01, +1.000000E+00, +7.257905E-01, -1.875798E-02, +4.492319E-01, +4.462033E-01
142+3.161243E-01, +7.257905E-01, +1.000000E+00, -4.131553E-01, -3.410673E-02, +5.687057E-01
143-1.263403E-03, -1.875798E-02, -4.131553E-01, +1.000000E+00, -1.294024E-01, -1.136455E-01
144+2.475277E-01, +4.492319E-01, -3.410673E-02, -1.294024E-01, +1.000000E+00, -1.098759E-01
145-3.331965E-01, +4.462033E-01, +5.687057E-01, -1.136455E-01, -1.098759E-01, +1.000000E+00
146cov = getFilled(0._TKG, ndim, ndim + 1)
147call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
148cov
149+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
150+2.053825E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
151+3.161243E-01, +7.257905E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
152-1.263403E-03, -1.875798E-02, -4.131553E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
153+2.475277E-01, +4.492319E-01, -3.410673E-02, -1.294024E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00
154-3.331965E-01, +4.462033E-01, +5.687057E-01, -1.136455E-01, -1.098759E-01, +1.000000E+00, +0.000000E+00
155call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
156cov
157+1.000000E+00, +9.000000E+00, +5.545328E+00, +1.896746E+00, -3.411189E-02, +3.712915E+00, -7.996715E+00
158+2.053825E-01, +1.000000E+00, +8.100000E+01, +1.306423E+01, -1.519396E+00, +2.021544E+01, +3.212664E+01
159+3.161243E-01, +7.257905E-01, +1.000000E+00, +4.000000E+00, -7.436795E+00, -3.410673E-01, +9.099292E+00
160-1.263403E-03, -1.875798E-02, -4.131553E-01, +1.000000E+00, +8.100000E+01, -5.823108E+00, -8.182474E+00
161+2.475277E-01, +4.492319E-01, -3.410673E-02, -1.294024E-01, +1.000000E+00, +2.500000E+01, -4.395036E+00
162-3.331965E-01, +4.462033E-01, +5.687057E-01, -1.136455E-01, -1.098759E-01, +1.000000E+00, +6.400000E+01
163
164cov = getFilled(0._TKG, ndim, ndim + 1)
165call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
166cov
167+0.000000E+00, +1.000000E+00, +2.053825E-01, +3.161243E-01, -1.263403E-03, +2.475277E-01, -3.331965E-01
168+0.000000E+00, +0.000000E+00, +1.000000E+00, +7.257905E-01, -1.875798E-02, +4.492319E-01, +4.462033E-01
169+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -4.131553E-01, -3.410673E-02, +5.687057E-01
170+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -1.294024E-01, -1.136455E-01
171+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -1.098759E-01
172+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
173call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
174cov
175+9.000000E+00, +1.000000E+00, +2.053825E-01, +3.161243E-01, -1.263403E-03, +2.475277E-01, -3.331965E-01
176+5.545328E+00, +8.100000E+01, +1.000000E+00, +7.257905E-01, -1.875798E-02, +4.492319E-01, +4.462033E-01
177+1.896746E+00, +1.306423E+01, +4.000000E+00, +1.000000E+00, -4.131553E-01, -3.410673E-02, +5.687057E-01
178-3.411189E-02, -1.519396E+00, -7.436795E+00, +8.100000E+01, +1.000000E+00, -1.294024E-01, -1.136455E-01
179+3.712915E+00, +2.021544E+01, -3.410673E-01, -5.823108E+00, +2.500000E+01, +1.000000E+00, -1.098759E-01
180-7.996715E+00, +3.212664E+01, +9.099292E+00, -8.182474E+00, -4.395036E+00, +6.400000E+01, +1.000000E+00
181
182
183ndim = getUnifRand(1, 7)
184ndim
185+6
186std = getUnifRand(1, 10, ndim)
187std
188+9.000000E+00, +4.000000E+00, +9.000000E+00, +6.000000E+00, +1.000000E+01, +7.000000E+00
189cor = getCovRand(1., ndim)
190cor
191+1.000000E+00, +8.902969E-01, +7.304340E-01, +5.993081E-01, -7.672507E-01, -6.349715E-01
192+8.902969E-01, +1.000000E+00, +3.396693E-01, +3.598257E-01, -6.172136E-01, -5.159491E-01
193+7.304340E-01, +3.396693E-01, +1.000000E+00, +6.754510E-01, -6.397194E-01, -5.528387E-01
194+5.993081E-01, +3.598257E-01, +6.754510E-01, +1.000000E+00, -9.078873E-01, -1.553995E-01
195-7.672507E-01, -6.172136E-01, -6.397194E-01, -9.078873E-01, +1.000000E+00, +1.096865E-01
196-6.349715E-01, -5.159491E-01, -5.528387E-01, -1.553995E-01, +1.096865E-01, +1.000000E+00
197cov = getFilled(0._TKG, ndim, ndim + 1)
198call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
199cov
200+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
201+8.902969E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
202+7.304340E-01, +3.396693E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
203+5.993081E-01, +3.598257E-01, +6.754510E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
204-7.672507E-01, -6.172136E-01, -6.397194E-01, -9.078873E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00
205-6.349715E-01, -5.159491E-01, -5.528387E-01, -1.553995E-01, +1.096865E-01, +1.000000E+00, +0.000000E+00
206call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
207cov
208+1.000000E+00, +8.100000E+01, +3.205069E+01, +5.916515E+01, +3.236264E+01, -6.905257E+01, -4.000320E+01
209+8.902969E-01, +1.000000E+00, +1.600000E+01, +1.222809E+01, +8.635817E+00, -2.468855E+01, -1.444658E+01
210+7.304340E-01, +3.396693E-01, +1.000000E+00, +8.100000E+01, +3.647436E+01, -5.757475E+01, -3.482884E+01
211+5.993081E-01, +3.598257E-01, +6.754510E-01, +1.000000E+00, +3.600000E+01, -5.447324E+01, -6.526778E+00
212-7.672507E-01, -6.172136E-01, -6.397194E-01, -9.078873E-01, +1.000000E+00, +1.000000E+02, +7.678058E+00
213-6.349715E-01, -5.159491E-01, -5.528387E-01, -1.553995E-01, +1.096865E-01, +1.000000E+00, +4.900000E+01
214
215cov = getFilled(0._TKG, ndim, ndim + 1)
216call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
217cov
218+0.000000E+00, +1.000000E+00, +8.902969E-01, +7.304340E-01, +5.993081E-01, -7.672507E-01, -6.349715E-01
219+0.000000E+00, +0.000000E+00, +1.000000E+00, +3.396693E-01, +3.598257E-01, -6.172136E-01, -5.159491E-01
220+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +6.754510E-01, -6.397194E-01, -5.528387E-01
221+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -9.078873E-01, -1.553995E-01
222+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +1.096865E-01
223+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
224call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
225cov
226+8.100000E+01, +1.000000E+00, +8.902969E-01, +7.304340E-01, +5.993081E-01, -7.672507E-01, -6.349715E-01
227+3.205069E+01, +1.600000E+01, +1.000000E+00, +3.396693E-01, +3.598257E-01, -6.172136E-01, -5.159491E-01
228+5.916515E+01, +1.222809E+01, +8.100000E+01, +1.000000E+00, +6.754510E-01, -6.397194E-01, -5.528387E-01
229+3.236264E+01, +8.635817E+00, +3.647436E+01, +3.600000E+01, +1.000000E+00, -9.078873E-01, -1.553995E-01
230-6.905257E+01, -2.468855E+01, -5.757475E+01, -5.447324E+01, +1.000000E+02, +1.000000E+00, +1.096865E-01
231-4.000320E+01, -1.444658E+01, -3.482884E+01, -6.526778E+00, +7.678058E+00, +4.900000E+01, +1.000000E+00
232
233
234ndim = getUnifRand(1, 7)
235ndim
236+2
237std = getUnifRand(1, 10, ndim)
238std
239+2.000000E+00, +1.000000E+01
240cor = getCovRand(1., ndim)
241cor
242+1.000000E+00, +6.533102E-01
243+6.533102E-01, +1.000000E+00
244cov = getFilled(0._TKG, ndim, ndim + 1)
245call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
246cov
247+1.000000E+00, +0.000000E+00, +0.000000E+00
248+6.533102E-01, +1.000000E+00, +0.000000E+00
249call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
250cov
251+1.000000E+00, +4.000000E+00, +1.306620E+01
252+6.533102E-01, +1.000000E+00, +1.000000E+02
253
254cov = getFilled(0._TKG, ndim, ndim + 1)
255call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
256cov
257+0.000000E+00, +1.000000E+00, +6.533102E-01
258+0.000000E+00, +0.000000E+00, +1.000000E+00
259call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
260cov
261+4.000000E+00, +1.000000E+00, +6.533102E-01
262+1.306620E+01, +1.000000E+02, +1.000000E+00
263
264
265ndim = getUnifRand(1, 7)
266ndim
267+3
268std = getUnifRand(1, 10, ndim)
269std
270+4.000000E+00, +2.000000E+00, +4.000000E+00
271cor = getCovRand(1., ndim)
272cor
273+1.000000E+00, +2.043380E-01, +4.009339E-01
274+2.043380E-01, +1.000000E+00, +9.075730E-01
275+4.009339E-01, +9.075730E-01, +1.000000E+00
276cov = getFilled(0._TKG, ndim, ndim + 1)
277call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
278cov
279+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
280+2.043380E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00
281+4.009339E-01, +9.075730E-01, +1.000000E+00, +0.000000E+00
282call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
283cov
284+1.000000E+00, +1.600000E+01, +1.634704E+00, +6.414942E+00
285+2.043380E-01, +1.000000E+00, +4.000000E+00, +7.260584E+00
286+4.009339E-01, +9.075730E-01, +1.000000E+00, +1.600000E+01
287
288cov = getFilled(0._TKG, ndim, ndim + 1)
289call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
290cov
291+0.000000E+00, +1.000000E+00, +2.043380E-01, +4.009339E-01
292+0.000000E+00, +0.000000E+00, +1.000000E+00, +9.075730E-01
293+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
294call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
295cov
296+1.600000E+01, +1.000000E+00, +2.043380E-01, +4.009339E-01
297+1.634704E+00, +4.000000E+00, +1.000000E+00, +9.075730E-01
298+6.414942E+00, +7.260584E+00, +1.600000E+01, +1.000000E+00
299
300
301ndim = getUnifRand(1, 7)
302ndim
303+3
304std = getUnifRand(1, 10, ndim)
305std
306+1.000000E+00, +1.000000E+01, +1.000000E+00
307cor = getCovRand(1., ndim)
308cor
309+1.000000E+00, +6.648872E-01, -3.382753E-01
310+6.648872E-01, +1.000000E+00, -1.525386E-01
311-3.382753E-01, -1.525386E-01, +1.000000E+00
312cov = getFilled(0._TKG, ndim, ndim + 1)
313call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
314cov
315+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
316+6.648872E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00
317-3.382753E-01, -1.525386E-01, +1.000000E+00, +0.000000E+00
318call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
319cov
320+1.000000E+00, +1.000000E+00, +6.648872E+00, -3.382753E-01
321+6.648872E-01, +1.000000E+00, +1.000000E+02, -1.525386E+00
322-3.382753E-01, -1.525386E-01, +1.000000E+00, +1.000000E+00
323
324cov = getFilled(0._TKG, ndim, ndim + 1)
325call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
326cov
327+0.000000E+00, +1.000000E+00, +6.648872E-01, -3.382753E-01
328+0.000000E+00, +0.000000E+00, +1.000000E+00, -1.525386E-01
329+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
330call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
331cov
332+1.000000E+00, +1.000000E+00, +6.648872E-01, -3.382753E-01
333+6.648872E+00, +1.000000E+02, +1.000000E+00, -1.525386E-01
334-3.382753E-01, -1.525386E+00, +1.000000E+00, +1.000000E+00
335
336
337ndim = getUnifRand(1, 7)
338ndim
339+7
340std = getUnifRand(1, 10, ndim)
341std
342+8.000000E+00, +4.000000E+00, +1.000000E+01, +8.000000E+00, +5.000000E+00, +4.000000E+00, +9.000000E+00
343cor = getCovRand(1., ndim)
344cor
345+1.000000E+00, +9.621283E-01, -6.292422E-01, -7.141774E-01, +3.926840E-01, +2.214031E-01, -4.519387E-01
346+9.621283E-01, +1.000000E+00, -7.547417E-01, -8.737954E-01, +4.880659E-01, +1.197928E-01, -5.189089E-01
347-6.292422E-01, -7.547417E-01, +1.000000E+00, +8.021672E-01, -7.937289E-01, -2.737021E-01, +4.582914E-01
348-7.141774E-01, -8.737954E-01, +8.021672E-01, +1.000000E+00, -5.158389E-01, +1.213096E-01, +5.098246E-01
349+3.926840E-01, +4.880659E-01, -7.937289E-01, -5.158389E-01, +1.000000E+00, +9.855510E-02, -5.040165E-01
350+2.214031E-01, +1.197928E-01, -2.737021E-01, +1.213096E-01, +9.855510E-02, +1.000000E+00, -1.514607E-01
351-4.519387E-01, -5.189089E-01, +4.582914E-01, +5.098246E-01, -5.040165E-01, -1.514607E-01, +1.000000E+00
352cov = getFilled(0._TKG, ndim, ndim + 1)
353call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
354cov
355+1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
356+9.621283E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
357-6.292422E-01, -7.547417E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
358-7.141774E-01, -8.737954E-01, +8.021672E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
359+3.926840E-01, +4.880659E-01, -7.937289E-01, -5.158389E-01, +1.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00
360+2.214031E-01, +1.197928E-01, -2.737021E-01, +1.213096E-01, +9.855510E-02, +1.000000E+00, +0.000000E+00, +0.000000E+00
361-4.519387E-01, -5.189089E-01, +4.582914E-01, +5.098246E-01, -5.040165E-01, -1.514607E-01, +1.000000E+00, +0.000000E+00
362call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
363cov
364+1.000000E+00, +6.400000E+01, +3.078811E+01, -5.033938E+01, -4.570735E+01, +1.570736E+01, +7.084900E+00, -3.253959E+01
365+9.621283E-01, +1.000000E+00, +1.600000E+01, -3.018967E+01, -2.796145E+01, +9.761318E+00, +1.916685E+00, -1.868072E+01
366-6.292422E-01, -7.547417E-01, +1.000000E+00, +1.000000E+02, +6.417337E+01, -3.968645E+01, -1.094808E+01, +4.124623E+01
367-7.141774E-01, -8.737954E-01, +8.021672E-01, +1.000000E+00, +6.400000E+01, -2.063355E+01, +3.881907E+00, +3.670737E+01
368+3.926840E-01, +4.880659E-01, -7.937289E-01, -5.158389E-01, +1.000000E+00, +2.500000E+01, +1.971102E+00, -2.268074E+01
369+2.214031E-01, +1.197928E-01, -2.737021E-01, +1.213096E-01, +9.855510E-02, +1.000000E+00, +1.600000E+01, -5.452584E+00
370-4.519387E-01, -5.189089E-01, +4.582914E-01, +5.098246E-01, -5.040165E-01, -1.514607E-01, +1.000000E+00, +8.100000E+01
371
372cov = getFilled(0._TKG, ndim, ndim + 1)
373call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
374cov
375+0.000000E+00, +1.000000E+00, +9.621283E-01, -6.292422E-01, -7.141774E-01, +3.926840E-01, +2.214031E-01, -4.519387E-01
376+0.000000E+00, +0.000000E+00, +1.000000E+00, -7.547417E-01, -8.737954E-01, +4.880659E-01, +1.197928E-01, -5.189089E-01
377+0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +8.021672E-01, -7.937289E-01, -2.737021E-01, +4.582914E-01
378+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -5.158389E-01, +1.213096E-01, +5.098246E-01
379+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, +9.855510E-02, -5.040165E-01
380+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00, -1.514607E-01
381+0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +0.000000E+00, +1.000000E+00
382call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
383cov
384+6.400000E+01, +1.000000E+00, +9.621283E-01, -6.292422E-01, -7.141774E-01, +3.926840E-01, +2.214031E-01, -4.519387E-01
385+3.078811E+01, +1.600000E+01, +1.000000E+00, -7.547417E-01, -8.737954E-01, +4.880659E-01, +1.197928E-01, -5.189089E-01
386-5.033938E+01, -3.018967E+01, +1.000000E+02, +1.000000E+00, +8.021672E-01, -7.937289E-01, -2.737021E-01, +4.582914E-01
387-4.570735E+01, -2.796145E+01, +6.417337E+01, +6.400000E+01, +1.000000E+00, -5.158389E-01, +1.213096E-01, +5.098246E-01
388+1.570736E+01, +9.761318E+00, -3.968645E+01, -2.063355E+01, +2.500000E+01, +1.000000E+00, +9.855510E-02, -5.040165E-01
389+7.084900E+00, +1.916685E+00, -1.094808E+01, +3.881907E+00, +1.971102E+00, +1.600000E+01, +1.000000E+00, -1.514607E-01
390-3.253959E+01, -1.868072E+01, +4.124623E+01, +3.670737E+01, -2.268074E+01, -5.452584E+00, +8.100000E+01, +1.000000E+00
391
392
393ndim = getUnifRand(1, 7)
394ndim
395+1
396std = getUnifRand(1, 10, ndim)
397std
398+1.000000E+01
399cor = getCovRand(1., ndim)
400cor
401+1.000000E+00
402cov = getFilled(0._TKG, ndim, ndim + 1)
403call setMatCopy(cov(1:ndim, 1:ndim), rdpack, cor, rdpack, lowDia)
404cov
405+1.000000E+00, +0.000000E+00
406call setCov(cov(1:ndim, 2:ndim+1), uppDia, cov(1:ndim, 1:ndim), lowDia, std) ! convert lower-triangle correlation matrix to upper-triangle covariance matrix.
407cov
408+1.000000E+00, +1.000000E+02
409
410cov = getFilled(0._TKG, ndim, ndim + 1)
411call setMatCopy(cov(1:ndim, 2:ndim+1), rdpack, cor, rdpack, uppDia)
412cov
413+0.000000E+00, +1.000000E+00
414call setCov(cov(1:ndim, 1:ndim), lowDia, cov(1:ndim, 2:ndim+1), uppDia, std) ! convert upper-triangle correlation matrix to lower-triangle covariance matrix.
415cov
416+1.000000E+02, +1.000000E+00
417
418
419!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421!Compute the covariance matrix of a 2-D sample.
422!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424
425ndim = 2; nsam = 10; dim = 2
426sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])
427sample
428+5.00000000, +8.00000000, +4.00000000, +2.00000000, +15.0000000, +20.0000000, +4.00000000, +18.0000000, +7.00000000, +1.00000000
429+12.0000000, +17.0000000, +8.00000000, +13.0000000, +3.00000000, +19.0000000, +10.0000000, +9.00000000, +3.00000000, +16.0000000
430mean = getMean(sample, dim)
431mean
432+8.40000057, +11.0000000
433samShifted = getShifted(sample, dim, -mean)
434samShifted
435-3.40000057, -0.400000572, -4.40000057, -6.40000057, +6.59999943, +11.5999994, -4.40000057, +9.59999943, -1.40000057, -7.40000057
436+1.00000000, +6.00000000, -3.00000000, +2.00000000, -8.00000000, +8.00000000, -1.00000000, -2.00000000, -8.00000000, +5.00000000
437cov = getFilled(0., ndim, ndim)
438call setCov(cov, uppDia, mean, sample, dim)
439cov
440+41.8400002, -0.599999845
441+0.00000000, +27.2000008
442cov = getFilled(0., ndim, ndim)
443call setCov(cov, uppDia, samShifted, dim) ! same result as above.
444cov
445+41.8400002, -0.599999845
446+0.00000000, +27.2000008
447
448cov = getFilled(0., ndim, ndim)
449call setCov(cov, lowDia, mean, sample, dim)
450cov
451+41.8400002, +0.00000000
452-0.599999845, +27.2000008
453cov = getFilled(0., ndim, ndim)
454call setCov(cov, lowDia, samShifted, dim) ! same result as above.
455cov
456+41.8400002, +0.00000000
457-0.599999845, +27.2000008
458
459'Compute the sample covariance along the first dimension.'
460
461dim = 1
462cov = getFilled(0., ndim, ndim)
463call setCov(cov, uppDia, mean, transpose(sample), dim)
464cov
465+41.8400002, -0.599999845
466+0.00000000, +27.2000008
467cov = getFilled(0., ndim, ndim)
468call setCov(cov, uppDia, transpose(samShifted), dim) ! same result as above.
469cov
470+41.8400002, -0.599999845
471+0.00000000, +27.2000008
472
473cov = getFilled(0., ndim, ndim)
474call setCov(cov, lowDia, mean, transpose(sample), dim)
475cov
476+41.8400002, +0.00000000
477-0.599999845, +27.2000008
478cov = getFilled(0., ndim, ndim)
479call setCov(cov, lowDia, transpose(samShifted), dim) ! same result as above.
480cov
481+41.8400002, +0.00000000
482-0.599999845, +27.2000008
483
484'Compute the full sample covariance for a pair of time series.'
485
486cov = getFilled(0., ndim, ndim)
487call setCov(cov, mean, sample(1,:), sample(2,:))
488cov
489+41.8400002, -0.599999845
490-0.599999845, +27.2000008
491cov = getFilled(0., ndim, ndim)
492call setCov(cov, samShifted(1,:), samShifted(2,:)) ! same result as above.
493cov
494+41.8400002, -0.599999845
495-0.599999845, +27.2000008
496
497
498!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500!Compute the biased covariance matrix of a weighted 2-D sample.
501!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503
504ndim = 2; nsam = 10; dim = 2
505sample = reshape(getUnifRand(1, 20, ndim * nsam), shape = [ndim, nsam], order = [2, 1])
506sample
507+18.0000000, +11.0000000, +5.00000000, +18.0000000, +17.0000000, +13.0000000, +13.0000000, +9.00000000, +9.00000000, +17.0000000
508+16.0000000, +11.0000000, +12.0000000, +19.0000000, +5.00000000, +17.0000000, +15.0000000, +20.0000000, +11.0000000, +16.0000000
509call setResized(mean, ndim)
510iweight = getUnifRand(1, 10, nsam) ! integer-valued weights.
511iweight
512+6, +3, +7, +1, +7, +7, +1, +1, +3, +2
513call setMean(mean, sample, dim, iweight, iweisum)
514mean
515+12.8157892, +12.7894735
516iweisum
517+38
518rweight = iweight ! or real-valued weights.
519iweight
520+6, +3, +7, +1, +7, +7, +1, +1, +3, +2
521call setMean(mean, sample, dim, rweight, rweisum)
522mean
523+12.8157892, +12.7894735
524rweisum
525+38.0000000
526
527!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528!Compute the covariance matrix integer weights.
529!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530
531samShifted = getShifted(sample, dim, -mean)
532samShifted
533+5.18421078, -1.81578922, -7.81578922, +5.18421078, +4.18421078, +0.184210777, +0.184210777, -3.81578922, -3.81578922, +4.18421078
534+3.21052647, -1.78947353, -0.789473534, +6.21052647, -7.78947353, +4.21052647, +2.21052647, +7.21052647, -1.78947353, +3.21052647
535cov = getFilled(0., ndim, ndim)
536call setCov(cov, uppDia, mean, sample, dim, iweight, iweisum)
537cov
538+22.1502762, -0.459834158
539+0.00000000, +19.7451534
540cov = getFilled(0., ndim, ndim)
541call setCov(cov, uppDia, samShifted, dim, iweight, sum(iweight)) ! same result as above.
542cov
543+22.1502762, -0.459834158
544+0.00000000, +19.7451534
545
546cov = getFilled(0., ndim, ndim)
547call setCov(cov, lowDia, mean, sample, dim, iweight, iweisum)
548cov
549+22.1502762, +0.00000000
550-0.459833741, +19.7451534
551cov = getFilled(0., ndim, ndim)
552call setCov(cov, lowDia, samShifted, dim, iweight, sum(iweight)) ! same result as above.
553cov
554+22.1502762, +0.00000000
555-0.459833741, +19.7451534
556
557'Compute the sample covariance along the first dimension.'
558
559dim = 1
560cov = getFilled(0., ndim, ndim)
561call setCov(cov, uppDia, mean, transpose(sample), dim, iweight, iweisum)
562cov
563+22.1502762, -0.459833443
564+0.00000000, +19.7451534
565cov = getFilled(0., ndim, ndim)
566call setCov(cov, uppDia, transpose(samShifted), dim, iweight, sum(iweight)) ! same result as above.
567cov
568+22.1502762, -0.459833443
569+0.00000000, +19.7451534
570
571cov = getFilled(0., ndim, ndim)
572call setCov(cov, lowDia, mean, transpose(sample), dim, iweight, iweisum)
573cov
574+22.1502762, +0.00000000
575-0.459833443, +19.7451534
576cov = getFilled(0., ndim, ndim)
577call setCov(cov, lowDia, transpose(samShifted), dim, iweight, sum(iweight)) ! same result as above.
578cov
579+22.1502762, +0.00000000
580-0.459833443, +19.7451534
581
582'Compute the full sample covariance for a pair of time series.'
583
584cov = getFilled(0., ndim, ndim)
585call setCov(cov, mean, sample(1,:), sample(2,:), iweight, iweisum)
586cov
587+22.1502762, -0.459834158
588-0.459834158, +19.7451534
589cov = getFilled(0., ndim, ndim)
590call setCov(cov, samShifted(1,:), samShifted(2,:), iweight, sum(iweight)) ! same result as above.
591cov
592+22.1502762, -0.459834158
593-0.459834158, +19.7451534
594
595
596!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597!Compute the covariance matrix real weights.
598!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599
600dim = 2_IK
601samShifted = getShifted(sample, dim, -mean)
602samShifted
603+5.18421078, -1.81578922, -7.81578922, +5.18421078, +4.18421078, +0.184210777, +0.184210777, -3.81578922, -3.81578922, +4.18421078
604+3.21052647, -1.78947353, -0.789473534, +6.21052647, -7.78947353, +4.21052647, +2.21052647, +7.21052647, -1.78947353, +3.21052647
605cov = getFilled(0., ndim, ndim)
606call setCov(cov, uppDia, mean, sample, dim, rweight, rweisum)
607cov
608+22.1502762, -0.459834158
609+0.00000000, +19.7451534
610cov = getFilled(0., ndim, ndim)
611call setCov(cov, uppDia, samShifted, dim, rweight, sum(rweight)) ! same result as above.
612cov
613+22.1502762, -0.459834158
614+0.00000000, +19.7451534
615
616cov = getFilled(0., ndim, ndim)
617call setCov(cov, lowDia, mean, sample, dim, rweight, rweisum)
618cov
619+22.1502762, +0.00000000
620-0.459833741, +19.7451534
621cov = getFilled(0., ndim, ndim)
622call setCov(cov, lowDia, samShifted, dim, rweight, sum(rweight)) ! same result as above.
623cov
624+22.1502762, +0.00000000
625-0.459833741, +19.7451534
626
627'Compute the sample covariance along the first dimension.'
628
629dim = 1
630cov = getFilled(0., ndim, ndim)
631call setCov(cov, uppDia, mean, transpose(sample), dim, rweight, rweisum)
632cov
633+22.1502762, -0.459833443
634+0.00000000, +19.7451534
635cov = getFilled(0., ndim, ndim)
636call setCov(cov, uppDia, transpose(samShifted), dim, rweight, sum(rweight)) ! same result as above.
637cov
638+22.1502762, -0.459833443
639+0.00000000, +19.7451534
640
641cov = getFilled(0., ndim, ndim)
642call setCov(cov, lowDia, mean, transpose(sample), dim, rweight, rweisum)
643cov
644+22.1502762, +0.00000000
645-0.459833443, +19.7451534
646cov = getFilled(0., ndim, ndim)
647call setCov(cov, lowDia, transpose(samShifted), dim, rweight, sum(rweight)) ! same result as above.
648cov
649+22.1502762, +0.00000000
650-0.459833443, +19.7451534
651
652'Compute the full sample covariance for a pair of time series.'
653
654cov = getFilled(0., ndim, ndim)
655call setCov(cov, mean, sample(1,:), sample(2,:), rweight, rweisum)
656cov
657+22.1502762, -0.459834158
658-0.459834158, +19.7451534
659cov = getFilled(0., ndim, ndim)
660call setCov(cov, samShifted(1,:), samShifted(2,:), rweight, sum(rweight)) ! same result as above.
661cov
662+22.1502762, -0.459834158
663-0.459834158, +19.7451534
664
665
Benchmarks:


Benchmark :: The runtime performance of setCov along different sample dimensions.

1! Test the performance of Cholesky factorization computation using an assumed-shape interface vs. explicit-shape interface.
2program benchmark
3
4 use pm_kind, only: IK, LK, RKG => RKD, SK
5 use pm_sampleCov, only: uppDia
6 use pm_bench, only: bench_type
7
8 implicit none
9
10 integer(IK) :: itry, ntry
11 integer(IK) :: i
12 integer(IK) :: iarr
13 integer(IK) :: fileUnit
14 integer(IK) , parameter :: NARR = 18_IK
15 integer(IK) , allocatable :: rperm(:)
16 real(RKG) , allocatable :: samdim1(:,:)
17 real(RKG) , allocatable :: samdim2(:,:)
18 type(bench_type), allocatable :: bench(:)
19 integer(IK) , parameter :: nsammax = 2**NARR
20 integer(IK) , parameter :: ndim = 5_IK
21 real(RKG) :: mean(ndim), cov(ndim, ndim)
22 integer(IK) :: idim, jdim, isam, nsam
23 real(RKG) :: dumm
24
25 bench = [ bench_type(name = SK_"intrinsicDIM1", exec = intrinsicDIM1, overhead = setOverhead) &
26 , bench_type(name = SK_"intrinsicDIM2", exec = intrinsicDIM2, overhead = setOverhead) &
27 , bench_type(name = SK_"setCovDIM1", exec = setCovDIM1, overhead = setOverhead) &
28 , bench_type(name = SK_"setCovDIM2", exec = setCovDIM2, overhead = setOverhead) &
29 ]
30
31 write(*,"(*(g0,:,' '))")
32 write(*,"(*(g0,:,' '))") "sample covariance benchmarking..."
33 write(*,"(*(g0,:,' '))")
34
35 open(newunit = fileUnit, file = "main.out", status = "replace")
36
37 write(fileUnit, "(*(g0,:,','))") "nsam", (bench(i)%name, i = 1, size(bench))
38
39 dumm = 0._RKG
40 loopOverMatrixSize: do iarr = 1, NARR - 1
41
42 nsam = 2**iarr
43 ntry = nsammax / nsam
44 allocate(samdim1(nsam, ndim), samdim2(ndim, nsam))
45 write(*,"(*(g0,:,' '))") "Benchmarking setCovDIM1() vs. setCovDIM2()", nsam, ntry
46
47 do i = 1, size(bench)
48 bench(i)%timing = bench(i)%getTiming()
49 end do
50
51 write(fileUnit,"(*(g0,:,','))") nsam, (bench(i)%timing%mean / ntry, i = 1, size(bench))
52 deallocate(samdim1, samdim2)
53
54 end do loopOverMatrixSize
55 write(*,"(*(g0,:,' '))") dumm
56
57 close(fileUnit)
58
59contains
60
61 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 ! procedure wrappers.
63 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64
65 subroutine setOverhead()
66 do itry = 1, ntry
67 call random_number(mean)
68 dumm = dumm - mean(1)
69 end do
70 end subroutine
71
72 subroutine setSamDIM1()
73 do isam = 1, nsam
74 call random_number(samdim1(isam, 1 : ndim))
75 end do
76 end subroutine
77
78 subroutine setSamDIM2()
79 do isam = 1, nsam
80 call random_number(samdim2(1 : ndim, isam))
81 end do
82 end subroutine
83
84 subroutine setCovDIM1()
85 block
86 use pm_sampleCov, only: setCov
87 do itry = 1, ntry
88 call setSamDIM1()
89 call setCov(cov, uppDia, samdim1, dim = 1_IK)
90 dumm = dumm + cov(1,1)
91 end do
92 end block
93 end subroutine
94
95 subroutine setCovDIM2()
96 block
97 use pm_sampleCov, only: setCov
98 do itry = 1, ntry
99 call setSamDIM2()
100 call setCov(cov, uppDia, samdim2, dim = 2_IK)
101 dumm = dumm + cov(1,1)
102 end do
103 end block
104 end subroutine
105
106 subroutine intrinsicDIM1()
107 do itry = 1, ntry
108 call setSamDIM1()
109 do jdim = 1, ndim
110 do idim = 1, jdim
111 cov(idim, jdim) = dot_product(samdim1(1 : nsam, idim), samdim1(1 : nsam, jdim)) / size(samdim1, dim = 1)
112 end do
113 end do
114 dumm = dumm + cov(1,1)
115 end do
116 end subroutine
117
118 subroutine intrinsicDIM2()
119 do itry = 1, ntry
120 call setSamDIM2()
121 do jdim = 1, ndim
122 do idim = 1, jdim
123 cov(idim, jdim) = dot_product(samdim2(idim, 1 : nsam), samdim2(jdim, 1 : nsam)) / size(samdim2, dim = 2)
124 end do
125 end do
126 dumm = dumm + cov(1,1)
127 end do
128 end subroutine
129
130end program benchmark
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Definition: pm_bench.F90:574
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
Definition: pm_bench.F90:41
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
Definition: pm_kind.F90:568
This is the class for creating benchmark and performance-profiling objects.
Definition: pm_bench.F90:386

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

Postprocessing of the benchmark output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6
7import os
8dirname = os.path.basename(os.getcwd())
9
10fontsize = 14
11
12df = pd.read_csv("main.out", delimiter = ",")
13colnames = list(df.columns.values)
14
15
18
19ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
20ax = plt.subplot()
21
22for colname in colnames[1:]:
23 plt.plot( df[colnames[0]].values
24 , df[colname].values
25 , linewidth = 2
26 )
27
28plt.xticks(fontsize = fontsize)
29plt.yticks(fontsize = fontsize)
30ax.set_xlabel(colnames[0], fontsize = fontsize)
31ax.set_ylabel("Runtime [ seconds ]", fontsize = fontsize)
32ax.set_title(" vs. ".join(colnames[1:])+"\nLower is better.", fontsize = fontsize)
33ax.set_xscale("log")
34ax.set_yscale("log")
35plt.minorticks_on()
36plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
37ax.tick_params(axis = "y", which = "minor")
38ax.tick_params(axis = "x", which = "minor")
39ax.legend ( colnames[1:]
40 #, loc='center left'
41 #, bbox_to_anchor=(1, 0.5)
42 , fontsize = fontsize
43 )
44
45plt.tight_layout()
46plt.savefig("benchmark." + dirname + ".runtime.png")
47
48
51
52ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
53ax = plt.subplot()
54
55plt.plot( df[colnames[0]].values
56 , np.ones(len(df[colnames[0]].values))
57 , linestyle = "--"
58 #, color = "black"
59 , linewidth = 2
60 )
61for colname in colnames[2:]:
62 plt.plot( df[colnames[0]].values
63 , df[colname].values / df[colnames[1]].values
64 , linewidth = 2
65 )
66
67plt.xticks(fontsize = fontsize)
68plt.yticks(fontsize = fontsize)
69ax.set_xlabel(colnames[0], fontsize = fontsize)
70ax.set_ylabel("Runtime compared to {}".format(colnames[1]), fontsize = fontsize)
71ax.set_title("Runtime Ratio Comparison. Lower means faster.\nLower than 1 means faster than {}().".format(colnames[1]), fontsize = fontsize)
72ax.set_xscale("log")
73ax.set_yscale("log")
74plt.minorticks_on()
75plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
76ax.tick_params(axis = "y", which = "minor")
77ax.tick_params(axis = "x", which = "minor")
78ax.legend ( colnames[1:]
79 #, bbox_to_anchor = (1, 0.5)
80 #, loc = "center left"
81 , fontsize = fontsize
82 )
83
84plt.tight_layout()
85plt.savefig("benchmark." + dirname + ".runtime.ratio.png")

Visualization of the benchmark output

Benchmark moral
  1. The procedures under the generic interface setCov can compute the covariance under two different sample axes.
  2. Recall that C is a row-major language while Fortran is a column-major language.
  3. As such, one would expect the computations for a sample whose observations are stored along the second axis would be faster in the Fortran programming language.
  4. However, such an expectation does not appear to hold at all times and appears to depend significantly on the computing architecture and the number of data attributes involved.
  5. The higher the number of data attributes, the more likely the computations along the second axis of sample will be faster.
  6. Note that for small number of data attributes, the computations along the second data axis involve a small loop that has significant computational cost due to the implicit branching involved in the loop.
Test:
test_pm_sampleCov
Todo:
Normal Priority: The examples of this generic interface should be extended to corrected weighted covariance matrices.


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, Monday March 6, 2017, 3:22 pm, Institute for Computational Engineering and Sciences (ICES), The University of Texas at Austin.
Fatemeh Bagheri, Monday 02:15 AM, September 27, 2021, Dallas, TX

Definition at line 1694 of file pm_sampleCov.F90.


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