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

Generate and return the bias correction factor for the computation of the variance of a (weighted) sample.
More...

Detailed Description

Generate and return the bias correction factor for the computation of the variance of a (weighted) sample.

When the population mean is replaced with sample mean, multiplying the output of this generic interface with the output (co)variance from setVar or setCov yields an unbiased estimate of the sample variance.
See the documentation of the parent module pm_sampleVar for algorithmic details and variance definitions.

Parameters
[in]weisum: The input scalar of,
  1. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing either,
  1. the size of an unweighted sample, only if the input argument correction is missing or is not rweight_type,
  2. the quantity sum(weight) of a weighted sample, only if the input argument correction is missing or is not rweight_type,

based upon which the bias correction is computed.
The vector weight(1:nsam) refers to the weights of a sample of size nsam.
[in]weisqs: The input scalar of the same type and kind as weisum, containing the sum of squared sample weight: sum(weight**2).
(optional. If missing, the output corresponds to the Bessel correction (i.e., frequency weight is assumed).
If present, the correction corresponding to reliability weights is returned.)
Returns
normfac : The output scalar of the same type and kind as weisum containing the bias correction factor of variance equation.
Multiplying normfac with the sum of the sample weight (or its size, if unweighted) yields in the Bias Correction factor.


Possible calling interfaces

normfac = getVarCorrection(weisum) ! unweighted or frequency-weighted sample variance correction
normfac = getVarCorrection(weisum, weisqs) ! reliability-weighted sample variance correction
!
Generate and return the bias correction factor for the computation of the variance of a (weighted) sa...
This module contains classes and procedures for computing the properties related to the covariance ma...
Warning
The condition 0 < weisum must hold for the corresponding input arguments.
The condition 0 < weisqs must hold for the corresponding input arguments.
This condition is 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.
Remarks
The procedures under discussion are elemental.
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).
See also
getVar
setVar
getCov
setCov


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_io, only: display_type
5 use pm_distUnif, only: getUnifRand
7
8 implicit none
9
10 integer(IK) :: itry, ntry = 10
11 type(display_type) :: disp
12 disp = display_type(file = "main.out.F90")
13
14 block
15 use pm_kind, only: RKG => RKS ! All other real types are also supported.
16 integer(RKG), allocatable :: weight(:)
17 real(RKG) :: correction
18 do itry = 1, ntry
19 call disp%skip()
20 call disp%show("weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))")
21 weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
22 call disp%show("weight")
23 call disp%show( weight )
24 call disp%show("correction = getVarCorrection(real(sum(weight), RKG))")
25 correction = getVarCorrection(real(sum(weight), RKG))
26 call disp%show("correction")
27 call disp%show( correction )
28 call disp%show("correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))")
29 correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
30 call disp%show("correction")
31 call disp%show( correction )
32 call disp%skip()
33 end do
34 end block
35
36 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 ! Output an example rand array for visualization.
38 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 block
41 use pm_kind, only: RKG => RK
42 use pm_arraySpace, only: getLogSpace
43 use pm_io, only: getErrTableWrite, trans
44 real(RKG), allocatable :: sampleSize(:)
45 sampleSize = getLogSpace(log(1.1_RKG), log(1001._RKG), 1000_IK)
46 if (0 /= getErrTableWrite("getVarCorrection.RK.txt", reshape([sampleSize, getVarCorrection(sampleSize)], [size(sampleSize), 2]), header = SK_"Sample Size,Bessel Correction")) error stop 'table write failed.'
47 end block
48
49end program example
Generate count evenly-logarithmically-spaced points over the interval [base**logx1,...
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
Generate and return the iostat code resulting from writing the input table of rank 1 or 2 to the spec...
Definition: pm_io.F90:5940
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
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
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
2weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
3weight
4+1, +7, +6, +3, +2, +6, +8, +6, +1, +1, +8, +2, +8, +4, +5
5correction = getVarCorrection(real(sum(weight), RKG))
6correction
7+1.01492536
8correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
9correction
10+1.09729469
11
12
13weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
14weight
15+2, +2, +6, +6, +7, +9, +2, +9, +9, +8, +1, +9, +6, +5, +2, +1, +1, +3, +7, +5
16correction = getVarCorrection(real(sum(weight), RKG))
17correction
18+1.01010096
19correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
20correction
21+1.07204115
22
23
24weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
25weight
26+5, +2, +3, +8, +2, +7
27correction = getVarCorrection(real(sum(weight), RKG))
28correction
29+1.03846157
30correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
31correction
32+1.27003479
33
34
35weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
36weight
37+5, +1, +5, +1, +7, +7, +5, +3, +2, +2, +9, +4, +7, +3, +7, +7, +1, +6, +5, +3
38correction = getVarCorrection(real(sum(weight), RKG))
39correction
40+1.01123595
41correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
42correction
43+1.06803799
44
45
46weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
47weight
48+8, +7, +1, +7, +1, +3, +8
49correction = getVarCorrection(real(sum(weight), RKG))
50correction
51+1.02941179
52correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
53correction
54+1.23987854
55
56
57weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
58weight
59+1, +3, +3, +1, +5, +3, +9, +9, +7, +3, +2, +2, +7, +5
60correction = getVarCorrection(real(sum(weight), RKG))
61correction
62+1.01694918
63correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
64correction
65+1.10974109
66
67
68weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
69weight
70+1, +8, +5, +5, +2, +6, +6
71correction = getVarCorrection(real(sum(weight), RKG))
72correction
73+1.03125000
74correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
75correction
76+1.21269488
77
78
79weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
80weight
81+4, +8, +2, +6, +4, +4, +4, +2, +5, +3, +1, +2, +1, +4, +4, +2, +4, +3, +9, +9
82correction = getVarCorrection(real(sum(weight), RKG))
83correction
84+1.01250005
85correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
86correction
87+1.07100880
88
89
90weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
91weight
92+9, +1, +6, +9, +4, +5, +2, +8, +1, +1, +3, +2, +6, +5, +7, +2, +9, +4
93correction = getVarCorrection(real(sum(weight), RKG))
94correction
95+1.01204824
96correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
97correction
98+1.08187675
99
100
101weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
102weight
103+9, +3, +2, +5, +9, +5, +5, +2, +7, +2, +7, +7, +7, +5, +1, +4, +6, +5, +6
104correction = getVarCorrection(real(sum(weight), RKG))
105correction
106+1.01041663
107correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
108correction
109+1.06726408
110
111

Postprocessing of the example output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6import glob
7import sys
8
9linewidth = 2
10fontsize = 17
11
12pattern = "*.RK.txt"
13fileList = glob.glob(pattern)
14if len(fileList) == 1:
15
16 df = pd.read_csv(fileList[0], delimiter = ",")
17
18 fig = plt.figure(figsize = 1.25 * np.array([6.4, 4.8]), dpi = 200)
19 ax = plt.subplot()
20
21 plt.plot( df.values[:, 0]
22 , df.values[:,1:]
23 , linewidth = linewidth
24 #, color = "r"
25 )
26 ax.legend ( list(df.columns.values[1:])
27 , fontsize = fontsize
28 )
29
30 plt.xticks(fontsize = fontsize - 2)
31 plt.yticks(fontsize = fontsize - 2)
32 ax.set_xscale("log")
33 ax.set_yscale("log")
34 ax.set_xlabel(df.columns.values[0], fontsize = 17)
35 ax.set_ylabel("Correction", fontsize = 17)
36
37 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
38 ax.tick_params(axis = "y", which = "minor")
39 ax.tick_params(axis = "x", which = "minor")
40
41 plt.savefig(fileList[0].replace(".txt",".png"))
42
43elif len(fileList) > 1:
44
45 sys.exit("Ambiguous file list exists.")

Visualization of the example output
Test:
test_pm_sampleVar


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:
Fatemeh Bagheri, Monday 02:15 AM, September 27, 2021, Dallas, TX

Definition at line 481 of file pm_sampleVar.F90.


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