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

Return a (collection) of random vector(s) of size ndim from the ndim-dimensional MultiVariate Normal (MVN) distribution, optionally with the specified input mean(1:ndim) and the specified subset of the Cholesky Factorization of the Covariance matrix of the MVN distribution. More...

Detailed Description

Return a (collection) of random vector(s) of size ndim from the ndim-dimensional MultiVariate Normal (MVN) distribution, optionally with the specified input mean(1:ndim) and the specified subset of the Cholesky Factorization of the Covariance matrix of the MVN distribution.

Parameters
[in,out]rng: The input/output scalar that can be an object of,
  1. type rngf_type, implying the use of intrinsic Fortran uniform RNG.
  2. type xoshiro256ssw_type, implying the use of xoshiro256** uniform RNG.
(optional, default = rngf_type.)
[out]rand: The output contiguous vector of shape (1:ndim) or matrix of shape (1:ndim, 1:nsam) of
  1. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the Multivariate Normal distributed random output vector.
[in]mean: The input contiguous vector of shape (1:ndim), of the same type and kind as the output rand, representing the mean of the Multivariate Normal distribution.
(optional, default = [(0., i = 1, size(rand))]. It must be present if the input argument chol is missing.)
[in]chol: The input contiguous matrix of shape (ndim, ndim) whose specified triangular subset contains the Cholesky Factorization of the covariance matrix of the MVN distribution.
(optional, the default is the Identity matrix of rank ndim. It must be present if and only if the input argument subset is also present.)
[in]subset: The input scalar constant that can be any of the following:
  1. the constant uppDia or an object of type uppDia_type implying that the upper-diagonal triangular block of the input chol must be used while the lower subset is not referenced.
  2. the constant lowDia or an object of type lowDia_type implying that the lower-diagonal triangular block of the input chol must be used while the upper subset is not referenced.
This argument is merely a convenience to differentiate the different procedure functionalities within this generic interface.
(optional. It must be present if and only if the input argument chol is present.)


Possible calling interfaces

! single vector, using default rng
call setMultiNormRand(rand(1:ndim), mean(1:ndim))
call setMultiNormRand(rand(1:ndim), chol(1:ndim, 1:ndim), subset)
call setMultiNormRand(rand(1:ndim), mean(1:ndim), chol(1:ndim, 1:ndim), subset)
! single vector, using custom rng
call setMultiNormRand(rng, rand(1:ndim), mean(1:ndim))
call setMultiNormRand(rng, rand(1:ndim), chol(1:ndim, 1:ndim), subset)
call setMultiNormRand(rng, rand(1:ndim), mean(1:ndim), chol(1:ndim, 1:ndim), subset)
! collection of `nsam` vectors, using default rng
call setMultiNormRand(rand(1:ndim, 1:nsam), mean(1:ndim))
call setMultiNormRand(rand(1:ndim, 1:nsam), chol(1:ndim, 1:ndim), subset)
call setMultiNormRand(rand(1:ndim, 1:nsam), mean(1:ndim), chol(1:ndim, 1:ndim), subset)
! collection of `nsam` vectors, using custom rng
call setMultiNormRand(rng, rand(1:ndim, 1:nsam), mean(1:ndim))
call setMultiNormRand(rng, rand(1:ndim, 1:nsam), chol(1:ndim, 1:ndim), subset)
call setMultiNormRand(rng, rand(1:ndim, 1:nsam), mean(1:ndim), chol(1:ndim, 1:ndim), subset)
Return a (collection) of random vector(s) of size ndim from the ndim-dimensional MultiVariate Normal ...
This module contains classes and procedures for computing various statistical quantities related to t...
Warning
The condition size(mean, 1) == size(rand, 1) must hold for the corresponding input arguments.
The condition all(shape(chol) == size(rand, 1)) must hold for the corresponding input arguments.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
Remarks
The procedures under discussion are impure. The procedures of this generic interface are pure when the input argument rng is set to xoshiro256ssw_type and the compile-time macro CHECK_ENABLED is set to 0 or is undefined.
See also
getNormRand
setNormRand
getMultiNormLogPDF


Example usage

1program example
2
3 use pm_kind, only: SK
4 use pm_kind, only: IK, LK, RKG => RKS ! all real kinds are supported.
5 use pm_io, only: display_type
8 use pm_distMultiNorm, only: setMultiNormRand, uppDia, lowDia
9
10 implicit none
11
12 real(RKG), allocatable :: mean(:), cov(:,:), chol(:,:), rand(:)
13 integer(IK) :: info
14
15 type(display_type) :: disp
16 disp = display_type(file = "main.out.F90")
17
18 call disp%skip()
19 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
20 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
21 call disp%show("! Generate random numbers from the (Standard) Multivariate Normal distribution.")
22 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
23 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
24 call disp%skip()
25
26 call disp%skip()
27 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
28 call disp%show("! Multivariate Normal random vector from a Standard distribution.")
29 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
30 call disp%skip()
31
32 call disp%skip()
33 call disp%show("call setResized(rand, 3_IK)")
34 call setResized(rand, 3_IK)
35 call disp%show("call setMultiNormRand(rand)")
36 call setMultiNormRand(rand)
37 call disp%show("rand")
38 call disp%show( rand )
39 call disp%skip()
40
41 call disp%skip()
42 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
43 call disp%show("! Multivariate Normal random vector with a particular mean and Identity covariance matrix.")
44 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
45 call disp%skip()
46
47 call disp%skip()
48 call disp%show("mean = [-5., -5.]")
49 mean = [-5., -5.]
50 call disp%show("mean")
51 call disp%show( mean )
52 call disp%show("call setResized(rand, size(mean, 1, IK))")
53 call setResized(rand, size(mean, 1, IK))
54 call disp%show("call setMultiNormRand(rand, mean)")
55 call setMultiNormRand(rand, mean)
56 call disp%show("rand")
57 call disp%show( rand )
58 call disp%skip()
59
60 call disp%skip()
61 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
62 call disp%show("! Multivariate Normal random vector with zero mean and Covariance matrix specified via the Cholesky Lower Triangle.")
63 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
64 call disp%skip()
65
66 call disp%skip()
67 call disp%show("cov = reshape([1., 1., 1., 4.], shape = [2, 2])")
68 cov = reshape([1., 1., 1., 4.], shape = [2, 2])
69 call disp%show("cov")
70 call disp%show( cov )
71 call disp%show("chol = getMatChol(cov, uppDia)")
72 chol = getMatChol(cov, uppDia)
73 call disp%show("call setResized(rand, size(chol, 1, IK))")
74 call setResized(rand, size(chol, 1, IK))
75 call disp%show("call setMultiNormRand(rand, chol, uppDia)")
76 call setMultiNormRand(rand, chol, uppDia)
77 call disp%show("rand")
78 call disp%show( rand )
79 call disp%skip()
80
81 call disp%skip()
82 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
83 call disp%show("! Multivariate Normal random vector with given mean and Covariance matrix specified via the Cholesky Lower Triangle.")
84 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
85 call disp%skip()
86
87 call disp%skip()
88 call disp%show("mean = [-5., -5.]")
89 mean = [-5., -5.]
90 call disp%show("mean")
91 call disp%show( mean )
92 call disp%show("cov = reshape([1., 1., 1., 4.], shape = [2, 2])")
93 cov = reshape([1., 1., 1., 4.], shape = [2, 2])
94 call disp%show("cov")
95 call disp%show( cov )
96 call disp%show("chol = getMatChol(cov, uppDia)")
97 chol = getMatChol(cov, uppDia)
98 call disp%show("call setResized(rand, size(chol, 1, IK))")
99 call setResized(rand, size(chol, 1, IK))
100 call disp%show("call setMultiNormRand(rand, mean, chol, uppDia)")
101 call setMultiNormRand(rand, mean, chol, uppDia)
102 call disp%show("rand")
103 call disp%show( rand )
104 call disp%skip()
105
106 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 ! Output an example rand array for visualization.
108 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 block
111
112 use pm_io, only: getErrTableWrite, trans
113 real(RKG) :: rand(2, 5000)
114
115 call setMultiNormRand(rand)
116 if (0 /= getErrTableWrite("setMultiNormRand.RK.txt", rand, trans)) error stop 'table write failed.'
117
118 call setMultiNormRand(rand, mean)
119 if (0 /= getErrTableWrite("setMultiNormRandMean.RK.txt", rand, trans)) error stop 'table write failed.'
120
121 call setMultiNormRand(rand, chol, uppDia)
122 if (0 /= getErrTableWrite("setMultiNormRandChol.RK.txt", rand, trans)) error stop 'table write failed.'
123
124 call setMultiNormRand(rand, chol, uppDia)
125 if (0 /= getErrTableWrite("setMultiNormRandMeanChol.RK.txt", rand, trans)) error stop 'table write failed.'
126
127 end block
128
129end program example
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
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
Generate and return the upper or the lower Cholesky factorization of the input symmetric positive-def...
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
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 for computing the Cholesky factorization of po...
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! Generate random numbers from the (Standard) Multivariate Normal distribution.
5!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
8
9!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10! Multivariate Normal random vector from a Standard distribution.
11!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12
13
14call setResized(rand, 3_IK)
15call setMultiNormRand(rand)
16rand
17+1.22118151, -1.95640302, +0.382133037
18
19
20!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21! Multivariate Normal random vector with a particular mean and Identity covariance matrix.
22!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23
24
25mean = [-5., -5.]
26mean
27-5.00000000, -5.00000000
28call setResized(rand, size(mean, 1, IK))
29call setMultiNormRand(rand, mean)
30rand
31-3.60802031, -4.93973255
32
33
34!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35! Multivariate Normal random vector with zero mean and Covariance matrix specified via the Cholesky Lower Triangle.
36!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
38
39cov = reshape([1., 1., 1., 4.], shape = [2, 2])
40cov
41+1.00000000, +1.00000000
42+1.00000000, +4.00000000
43chol = getMatChol(cov, uppDia)
44call setResized(rand, size(chol, 1, IK))
45call setMultiNormRand(rand, chol, uppDia)
46rand
47-0.149348021, +0.639313519
48
49
50!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51! Multivariate Normal random vector with given mean and Covariance matrix specified via the Cholesky Lower Triangle.
52!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53
54
55mean = [-5., -5.]
56mean
57-5.00000000, -5.00000000
58cov = reshape([1., 1., 1., 4.], shape = [2, 2])
59cov
60+1.00000000, +1.00000000
61+1.00000000, +4.00000000
62chol = getMatChol(cov, uppDia)
63call setResized(rand, size(chol, 1, IK))
64call setMultiNormRand(rand, mean, chol, uppDia)
65rand
66-3.47706270, -4.25022602
67
68

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
12for kind in ["RK"]:
13
14 pattern = "*." + kind + ".txt"
15 fileList = glob.glob(pattern)
16
17 for file in fileList:
18
19 df = pd.read_csv(file, delimiter = ",", header = None)
20
21 # definitions for the axes
22 left, width = 0.1, 0.65
23 bottom, height = 0.1, 0.65
24 spacing = 0.015
25
26 # start with a square Figure
27 fig = plt.figure(figsize = (8, 8))
28
29 plt.rcParams.update({'font.size': fontsize - 2})
30 ax = fig.add_axes([left, bottom, width, height]) # scatter plot
31 ax_histx = fig.add_axes([left, bottom + height + spacing, width, 0.2], sharex = ax) # histx
32 ax_histy = fig.add_axes([left + width + spacing, bottom, 0.2, height], sharey = ax) # histy
33
34 for axes in [ax, ax_histx, ax_histy]:
35 axes.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
36 axes.tick_params(axis = "y", which = "minor")
37 axes.tick_params(axis = "x", which = "minor")
38
39 # no labels
40 ax_histy.tick_params(axis = "y", labelleft = False)
41 ax_histx.tick_params(axis = "x", labelbottom = False)
42
43 # the scatter plot:
44 ax.scatter ( df.values[:, 0]
45 , df.values[:, 1]
46 , s = 8
47 , zorder = 1000
48 )
49
50 ax_histx.hist(df.values[:, 0], bins = 50, zorder = 1000)
51 ax_histy.hist(df.values[:, 1], bins = 50, orientation = "horizontal", zorder = 1000)
52
53 ax.set_xlabel("X", fontsize = 17)
54 ax.set_ylabel("Y", fontsize = 17)
55
56 plt.savefig(file.replace(".txt",".png"))

Visualization of the example output
Test:
test_pm_distMultiNorm
Internal naming convention:
The following illustrates the internal naming convention used for the procedures within this generic interface.
setMNR_RNGD_DM_DC_XXX_D1_RK5()
|||| || || ||| || |||
|||| || || ||| || |||
|||| || || ||| || |||
|||| || || ||| || |The Kind of the output array.
|||| || || ||| || The type of the output array: R => Real.
|||| || || ||| The Dimension of the output array.
|||| || || The subset of the Cholesky factor: D/U/L => Default/Upper/Lower.
|||| || The Cholesky factor of the covariance matrix of the distribution: DC/AC => Default/Arbitrary Cholesky
|||| The mean of the distribution: DM/AM => Default/Arbitrary Mean
The random number generator: RNG D/F/X => Default/Fortran/Xoroshiro256++
Todo:
Normal Priority: The access pattern for the upper-diagonal subset of chol is non contiguous in the current implementation.
The access pattern can be likely made contiguous by an appropriate implementation.


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, April 23, 2017, 12:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin

Definition at line 3264 of file pm_distMultiNorm.F90.


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