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

Return the regularized Upper Incomplete Gamma function for the specified shape parameter ( \(\kappa\)) and lower limit of the integral x. More...

Detailed Description

Return the regularized Upper Incomplete Gamma function for the specified shape parameter ( \(\kappa\)) and lower limit of the integral x.

The regularized Upper Incomplete Gamma function is defined as,

\begin{equation} \large Q(\kappa, x) = \frac{1}{\Gamma(\kappa)} \int_x^{+\infty}~t^{\kappa-1}{\mathrm e}^{-t} ~ dt ~, \end{equation}

where \((\kappa > 0, x > 0)\) should hold, with \(\kappa\) representing the shape parameter of the Gamma function (or distribution) and \(x\) representing the lower limit in the integral of the Upper Incomplete Gamma function.
Note that this integral is bounded between zero and one ( \([0,1]\)).
The regularized Upper Incomplete Gamma function also represents the complement of the Cumulative Distribution Function (CDF) of the univariate Gamma distribution with the specified shape parameter and standardized x (with the scale parameter of unity).

Parameters
[out]gammaIncUpp: The output scalar of same type and kind as the input argument x representing the Upper Incomplete Gamma function for the specified kappa and lower limit.
Note that gammaIncUpp is, by definition, always positive.
[in]x: The input scalar of the type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128), representing the lower limit in the integral of the Lower Incomplete Gamma function \(P(\kappa,x)\).
[in]logGammaKappa: The input scalar of the same type and kind as the input x, representing the precomputed \(\log(\Gamma(\kappa))\) which can be computed by calling the Fortran intrinsic function log_gamma(kappa).
[in]kappa: The input scalar of the same type and kind as x, representing the shape parameter ( \(\kappa\)) of the Lower Incomplete Gamma function \(P(\kappa,x)\).
[out]info: The input scalar or array of the same shape as other input arguments of type integer of default kind IK.
On output, it is set to (positive) number of iterations taken for the algorithm to converge or its negative if the algorithm fails to converge.
A convergence failure could happen if the input value for kappa is too large.
A negative value implies the lack of convergence.
[in]tol: The input scalar of the same type and kind as x, representing the relative accuracy in the convergence checking of the Gamma series or its Legendre continued fraction representation.
(optional, if it is missing, the default is set by setGammaIncUppContFracNR or setGammaIncLowSeriesNR).


Possible calling interfaces

call setGammaIncUppNR(gammaIncUpp, x, logGammaKappa, kappa, info, tol = tol)
Return the regularized Upper Incomplete Gamma function for the specified shape parameter ( ) and lowe...
This module contains procedures and generic interfaces for the Lower and Upper Incomplete Gamma funct...
Warning
The kappa and x input arguments must be positive real numbers with logGammaKappa = log_gamma(kappa) where log_gamma() is a Fortran intrinsic function.
Furthermore, tol << 1. must hold, if present as an input argument.
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.
Remarks
The procedures under discussion are elemental.
These procedures are particularly useful and needed in computing the PDF of the Gamma and related distributions.
The logic behind pre-computing and passing logGammaKappa = log_gamma(kappa) is to speed up the calculations since log_gamma() is computationally expensive and its recomputation can be avoided in repeated calls to setGammaIncUppNR with the same shape parameter but different x.
See also
getGammaIncUppNR
getGammaIncLowNR


Example usage

1program example
2
3 use pm_kind, only: SK
4 use pm_kind, only: IK
5 use pm_kind, only: LK
6 use pm_kind, only: RKS, RKD, RKH
7 use pm_io, only: display_type
9
10 implicit none
11
12 integer(IK) , parameter :: NP = 1000_IK
13 real(RKH) :: gamIncUpp_RKH, x_RKH, kappa_RKH
14 real(RKD) :: gamIncUpp_RKD, x_RKD, kappa_RKD
15 real(RKS) :: gamIncUpp_RKS, x_RKS, kappa_RKS
16 integer(IK) :: info
17
18 type(display_type) :: disp
19 disp = display_type(file = "main.out.F90")
20
21 kappa_RKH = 1.5_RKH
22 kappa_RKD = 1.5_RKD
23 kappa_RKS = 1.5_RKS
24
25 x_RKH = 2._RKH
26 x_RKD = 2._RKD
27 x_RKS = 2._RKS
28
29 call disp%skip()
30 call disp%show("x_RKS")
31 call disp%show( x_RKS )
32 call disp%show("kappa_RKS")
33 call disp%show( kappa_RKS )
34 call disp%show("call setGammaIncUppNR(gamIncUpp_RKS, x_RKS, logGammaKappa = log_gamma(kappa_RKS), kappa = kappa_RKS, info = info)")
35 call setGammaIncUppNR(gamIncUpp_RKS, x_RKS, logGammaKappa = log_gamma(kappa_RKS), kappa = kappa_RKS, info = info)
36 call disp%show("gamIncUpp_RKS")
37 call disp%show( gamIncUpp_RKS )
38 call disp%show("info")
39 call disp%show( info )
40 call disp%skip()
41
42 call disp%skip()
43 call disp%show("x_RKD")
44 call disp%show( x_RKD)
45 call disp%show("kappa_RKD")
46 call disp%show( kappa_RKD)
47 call disp%show("call setGammaIncUppNR(gamIncUpp_RKD, x_RKD, logGammaKappa = log_gamma(kappa_RKD), kappa = kappa_RKD, info = info)")
48 call setGammaIncUppNR(gamIncUpp_RKD, x_RKD, logGammaKappa = log_gamma(kappa_RKD), kappa = kappa_RKD, info = info)
49 call disp%show("gamIncUpp_RKD")
50 call disp%show( gamIncUpp_RKD)
51 call disp%show("info")
52 call disp%show( info )
53 call disp%skip()
54
55 call disp%skip()
56 call disp%show("x_RKH")
57 call disp%show( x_RKH )
58 call disp%show("kappa_RKH")
59 call disp%show( kappa_RKH )
60 call disp%show("call setGammaIncUppNR(gamIncUpp_RKH, x_RKH, logGammaKappa = log_gamma(kappa_RKH), kappa = kappa_RKH, info = info)")
61 call setGammaIncUppNR(gamIncUpp_RKH, x_RKH, logGammaKappa = log_gamma(kappa_RKH), kappa = kappa_RKH, info = info)
62 call disp%show("gamIncUpp_RKH")
63 call disp%show( gamIncUpp_RKH )
64 call disp%show("info")
65 call disp%show( info )
66 call disp%skip()
67
68 block
69 use pm_kind, only: RKG => RKS
70 integer(IK) :: i, rprecision
71 integer(IK), allocatable :: exprange(:), info(:)
72 real(RKG), allocatable :: gamIncUpp(:)
73 call disp%skip()
74 call disp%show("rprecision = precision(0._RKG) / 2")
75 rprecision = precision(0._RKG) / 2
76 call disp%show("rprecision")
77 call disp%show( rprecision )
78 call disp%show("exprange = [(i, i = -rprecision, rprecision)]")
79 exprange = [(i, i = -rprecision, rprecision)]
80 call disp%show("exprange")
81 call disp%show( exprange )
82 call disp%show("allocate(gamIncUpp(size(exprange)), info(size(exprange)))")
83 allocate(gamIncUpp(size(exprange)), info(size(exprange)))
84 call disp%show("call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)")
85 call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
86 call disp%show("gamIncUpp")
87 call disp%show( gamIncUpp )
88 call disp%show("info")
89 call disp%show( info )
90 call disp%skip()
91 end block
92
93 block
94 use pm_kind, only: RKG => RKD
95 integer(IK) :: i, rprecision
96 integer(IK), allocatable :: exprange(:), info(:)
97 real(RKG), allocatable :: gamIncUpp(:)
98 call disp%skip()
99 call disp%show("rprecision = precision(0._RKG) / 2")
100 rprecision = precision(0._RKG) / 2
101 call disp%show("rprecision")
102 call disp%show( rprecision )
103 call disp%show("exprange = [(i, i = -rprecision, rprecision)]")
104 exprange = [(i, i = -rprecision, rprecision)]
105 call disp%show("exprange")
106 call disp%show( exprange )
107 call disp%show("allocate(gamIncUpp(size(exprange)), info(size(exprange)))")
108 allocate(gamIncUpp(size(exprange)), info(size(exprange)))
109 call disp%show("call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)")
110 call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
111 call disp%show("gamIncUpp")
112 call disp%show( gamIncUpp )
113 call disp%show("info")
114 call disp%show( info )
115 call disp%skip()
116 end block
117
118 block
119 use pm_kind, only: RKG => RKH
120 integer(IK) :: i, rprecision
121 integer(IK), allocatable :: exprange(:), info(:)
122 real(RKG), allocatable :: gamIncUpp(:)
123 call disp%skip()
124 call disp%show("rprecision = precision(0._RKG) / 2")
125 rprecision = precision(0._RKG) / 2
126 call disp%show("rprecision")
127 call disp%show( rprecision )
128 call disp%show("exprange = [(i, i = -rprecision, rprecision)]")
129 exprange = [(i, i = -rprecision, rprecision)]
130 call disp%show("exprange")
131 call disp%show( exprange )
132 call disp%show("allocate(gamIncUpp(size(exprange)), info(size(exprange)))")
133 allocate(gamIncUpp(size(exprange)), info(size(exprange)))
134 call disp%show("call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)")
135 call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
136 call disp%show("gamIncUpp")
137 call disp%show( gamIncUpp )
138 call disp%show("info")
139 call disp%show( info )
140 call disp%skip()
141 end block
142
143 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 ! Output an example array of the regularized Upper Incomplete Gamma function for visualization.
145 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 block
148
149 use pm_arraySpace, only: setLinSpace
150 real(RKS) :: x_RKS(NP)
151 integer :: fileUnit, i
152
153 call setLinSpace(x_RKS, 0._RKS, 8._RKS)
154 open(newunit = fileUnit, file = "setGammaIncUppNR.RK.txt")
155 do i = 1, NP
156 call setGammaIncUppNR(gamIncUpp_RKS, x_RKS(i), logGammaKappa = log_gamma(kappa_RKS), kappa = kappa_RKS, info = info)
157 write(fileUnit,"(2(g0,:,' '))") x_RKS(i), gamIncUpp_RKS
158 end do
159 close(fileUnit)
160
161 end block
162
163end program example
Return the linSpace output argument with size(linSpace) elements of evenly-spaced values over the int...
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 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 RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
Definition: pm_kind.F90:568
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 RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
Definition: pm_kind.F90:858
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
2x_RKS
3+2.00000000
4kappa_RKS
5+1.50000000
6call setGammaIncUppNR(gamIncUpp_RKS, x_RKS, logGammaKappa = log_gamma(kappa_RKS), kappa = kappa_RKS, info = info)
7gamIncUpp_RKS
8+0.261464179
9info
10+11
11
12
13x_RKD
14+2.0000000000000000
15kappa_RKD
16+1.5000000000000000
17call setGammaIncUppNR(gamIncUpp_RKD, x_RKD, logGammaKappa = log_gamma(kappa_RKD), kappa = kappa_RKD, info = info)
18gamIncUpp_RKD
19+0.26146412994911072
20info
21+21
22
23
24x_RKH
25+2.00000000000000000000000000000000000
26kappa_RKH
27+1.50000000000000000000000000000000000
28call setGammaIncUppNR(gamIncUpp_RKH, x_RKH, logGammaKappa = log_gamma(kappa_RKH), kappa = kappa_RKH, info = info)
29gamIncUpp_RKH
30+0.261464129949110622202822075975921249
31info
32+36
33
34
35rprecision = precision(0._RKG) / 2
36rprecision
37+3
38exprange = [(i, i = -rprecision, rprecision)]
39exprange
40-3, -2, -1, +0, +1, +2, +3
41allocate(gamIncUpp(size(exprange)), info(size(exprange)))
42call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
43gamIncUpp
44+0.631248951E-2, +0.396528244E-1, +0.172448397, +0.367879391, +0.457929194, +0.486702442, +0.495647073
45info
46+2, +3, +5, +9, +19, +51, +144
47
48
49rprecision = precision(0._RKG) / 2
50rprecision
51+7
52exprange = [(i, i = -rprecision, rprecision)]
53exprange
54-7, -6, -5, -4, -3, -2, -1, +0, +1, +2, +3, +4, +5, +6, +7
55allocate(gamIncUpp(size(exprange)), info(size(exprange)))
56call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
57gamIncUpp
58+0.15540868092411841E-5, +0.13238209089494468E-4, +0.10935130095757195E-3, +0.86295813100534247E-3, +0.63123532911392166E-2, +0.39652576478489632E-1, +0.17244824041414908, +0.36787944117144233, +0.45792971447185082, +0.48670120172086928, +0.49579475581981880, +0.49867019166480753, +0.49957947781728729, +0.49986701958132673, +0.49995795435734636
59info
60+3, +3, +3, +4, +5, +6, +9, +16, +35, +89, +255, +770, +2365, +7304, +22572
61
62
63rprecision = precision(0._RKG) / 2
64rprecision
65+16
66exprange = [(i, i = -rprecision, rprecision)]
67exprange
68-16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, +13, +14, +15, +16
69allocate(gamIncUpp(size(exprange)), info(size(exprange)))
70call setGammaIncUppNR(gamIncUpp, 10._RKG**exprange, logGammaKappa = log_gamma(10._RKG**exprange), kappa = 10._RKG**exprange, info = info)
71gamIncUpp
72+0.362641458230031325076828129542474766E-14, +0.339615607300085775277864670886475238E-13, +0.316589756370101134867553294329700904E-12, +0.293563905439781533950555715921222342E-11, +0.270538054506628836199548154653849778E-10, +0.247512203549880797881142114345206485E-9, +0.224486352400241094382073832681308249E-8, +0.201460499709356778860957793291373752E-7, +0.178434635053293640783839339393679058E-6, +0.155408680923619091775019872787391912E-5, +0.132382090896648326218821852228554586E-4, +0.109351300956329854758268594627563903E-3, +0.862958131006599938564454065107016909E-3, +0.631235329113970979344136715350855536E-2, +0.396525764784908016731185218404744951E-1, +0.172448240414149458549686503831842920, +0.367879441171442321595523770161460873, +0.457929714471852208314164857059333906, +0.486701201720851335142685743435971364, +0.495794755819784491496222156397972042, +0.498670191660044799617257748700958660, +0.499579477889634823306687420993386096, +0.499867019239127408755677182042854039, +0.499957947791276301666243835240964160, +0.499986701923985880128751122750594547, +0.499995794779129943037581224570753625, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000, +1.00000000000000000000000000000000000
73info
74+3, +3, +3, +3, +3, +3, +4, +4, +4, +5, +6, +6, +8, +10, +13, +18, +30, +59, +143, +402, +1211, +3747, +11701, +36641, +114821, +359862, -1000001, -1000001, -1000001, -1000001, -1000001, -1000001, -1000001
75
76

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
9fontsize = 17
10
11marker ={ "CK" : "-"
12 , "IK" : "."
13 , "RK" : "-"
14 }
15xlab = { "CK" : r"x ( real/imaginary )"
16 , "IK" : r"x ( integer-valued )"
17 , "RK" : r"x ( real-valued )"
18 }
19labels = [r"shape parameter: $\kappa = 2$"]
20
21for kind in ["IK", "CK", "RK"]:
22
23 pattern = "*." + kind + ".txt"
24 fileList = glob.glob(pattern)
25 if len(fileList) == 1:
26
27 df = pd.read_csv(fileList[0], delimiter = " ")
28
29 fig = plt.figure(figsize = 1.25 * np.array([6.4, 4.8]), dpi = 200)
30 ax = plt.subplot()
31
32 if kind == "CK":
33 plt.plot( df.values[:, 0]
34 , df.values[:,2]
35 , marker[kind]
36 , color = "r"
37 )
38 plt.plot( df.values[:,1]
39 , df.values[:,3]
40 , marker[kind]
41 , color = "blue"
42 )
43 else:
44 plt.plot( df.values[:, 0]
45 , df.values[:,1]
46 , marker[kind]
47 , color = "r"
48 )
49
50 plt.xticks(fontsize = fontsize - 2)
51 plt.yticks(fontsize = fontsize - 2)
52 ax.set_xlabel(xlab[kind], fontsize = fontsize)
53 ax.set_ylabel("Regularized Upper\nIncomplete Gamma Function", fontsize = fontsize)
54
55 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
56 ax.tick_params(axis = "y", which = "minor")
57 ax.tick_params(axis = "x", which = "minor")
58
59 ax.legend ( labels
60 , fontsize = fontsize
61 #, loc = "center left"
62 #, bbox_to_anchor = (1, 0.5)
63 )
64
65 plt.savefig(fileList[0].replace(".txt",".png"))
66
67 elif len(fileList) > 1:
68
69 sys.exit("Ambiguous file list exists.")

Visualization of the example output
Test:
test_pm_mathGammaNR
Remarks
See Numerical Recipes by Press et al. 1992 for further details of the Incomplete Gamma function.


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 12:36 pm, August 16, 2021, Dallas TX

Definition at line 649 of file pm_mathGammaNR.F90.


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