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+7, +3, +8, +6, +1, +8
5correction = getVarCorrection(real(sum(weight), RKG))
6correction
7+1.03125000
8correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
9correction
10+1.25750577
11
12
13weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
14weight
15+1, +6, +3, +4
16correction = getVarCorrection(real(sum(weight), RKG))
17correction
18+1.07692313
19correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
20correction
21+1.46268654
22
23
24weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
25weight
26+2, +9
27correction = getVarCorrection(real(sum(weight), RKG))
28correction
29+1.10000002
30correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
31correction
32+3.36111116
33
34
35weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
36weight
37+5, +3, +6, +1, +7, +9, +3, +2, +3, +7, +5, +6, +1, +9, +9, +6, +7
38correction = getVarCorrection(real(sum(weight), RKG))
39correction
40+1.01136363
41correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
42correction
43+1.07915533
44
45
46weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
47weight
48+4, +6, +5, +4, +8, +9, +1, +4, +4
49correction = getVarCorrection(real(sum(weight), RKG))
50correction
51+1.02272725
52correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
53correction
54+1.15450394
55
56
57weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
58weight
59+2, +4, +5, +3, +7, +8, +8, +2, +7, +2, +5, +6, +8
60correction = getVarCorrection(real(sum(weight), RKG))
61correction
62+1.01515150
63correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
64correction
65+1.10132480
66
67
68weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
69weight
70+8, +3
71correction = getVarCorrection(real(sum(weight), RKG))
72correction
73+1.10000002
74correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
75correction
76+2.52083325
77
78
79weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
80weight
81+8, +2, +7
82correction = getVarCorrection(real(sum(weight), RKG))
83correction
84+1.06250000
85correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
86correction
87+1.68023252
88
89
90weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
91weight
92+3, +2, +3, +4, +4, +8, +3, +4, +2, +5, +7, +1, +2, +4, +4, +8, +2
93correction = getVarCorrection(real(sum(weight), RKG))
94correction
95+1.01538467
96correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
97correction
98+1.08089328
99
100
101weight = getUnifRand(1, 9, getUnifRand(2_IK, 20_IK))
102weight
103+9, +2, +5, +3, +5
104correction = getVarCorrection(real(sum(weight), RKG))
105correction
106+1.04347825
107correction = getVarCorrection(real(sum(weight), RKG), real(sum(weight**2), RKG))
108correction
109+1.33333337
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: