Content uploaded by Eugene d'Eon
Author content
All content in this area was uploaded by Eugene d'Eon on May 10, 2020
Content may be subject to copyright.
Randomvariate
generation/Importancesampling
usingintegraltransforms
Eugened’Eon
NVIDIA
Wellington,NewZealand
April5,2020(updatedMay10,2020)
Abstract
Wediscussvariousanalyticandexactmethodsforderivingimportance-samplingproceduresfor
1Ddistributionsf(r)onthehalfliner∈[0,∞).Specifically:
◼FindingconvolutionsusingtheforwardLaplacetransform
◼SamplingasasuperpositionofexponentialsusingtheinverseLaplacetransform
◼TheGaussiantransform
◼GeneralizedBox-Mullerprojections
◼Samplingusingatrack-lengthestimator
1.Introduction
Simulatingthelineartransportoflightorparticleso�eninvolvesdrawingrandomvariatesfrom
somegivendistributiononthehalfline,suchasfree-pathlengthsamplinginhomogeneousclassi-
calmediawherewedrawsamplesfromthecollision-ratedensityσte-σtrusingtheCDFinversion
method,leadingto
r= -Log[1-RandomReal[]] σt.
Therearetimeswherewewishtosampleotherdistributionsf(r)onthehalflinewheretheCDF
inversionmethodisnotpossibleanalytically.Forexample,innon-classicaltransportwherefree-
pathlengthsbetweencollisionarenotexponentially-distributed,weneedsamplingproceduresfor
alargeclassofdistributionsthatgivethechord-lengthsinagivenclassofrandommicrostructure.
Inthispaperwediscussmethodsforsamplingfroma1Ddistributionf(r)thatisnormalizedonthe
halfline ∫0
∞f(r)ⅆr=1.Inmostcases,morethan1randomnumberwillberequiredtosamplef(r).
2.Deconvolution
Iff(r)canbeexpressedasaconvolutionofmorethan1distribution,eachofwhichhasknown
samplingprocedures,thenf(r)canbesampledasawholebysamplingeachofthedistributionsin
itsconvolutionandsummingthevaluesri.TheLaplacetransformcanbeusedtofactorf(r)intoits
deconvolvedcomponentsusingtheconvolutionpropertyofLaplacetransforms:theLaplace
transformofaconvolutionistheproductofthetwoLaplacetransforms,
ℒr[f1(r) * f2(r)](s) = ℒr[f1(r)] (s)ℒr[f2(r)] (s).
Example2.1:
ConsidertheErlang-2/Gamma-2distribution,f(r)=e-rr.
Clear[f];
f[r_] :=Exp[-r]r;
Integrate[f[r],{r, 0, Infinity}]
1
Wecan’tanalyticallyinverttheCDFoff(r):
Integrate[f[r],{r, 0, k}, Assumptions →k>0]
1-ⅇ-k1+k
UsingtheLaplacetransformoff(r):
LaplaceTransform[f[r], r, s]
1
(1+s)2
wenotethatfistheconvolutionofafunctionwithitself,whoselaplacetransformis
1
1+s. Using the inverse Laplace transform, we find
InverseLaplaceTransform1
1+s
, s, r
ⅇ-r
Wefindtheexponential,whichweknowhowtosample.Therefore,Erlang-2canbesampledusing
thesumof2callstosample()procedurefortheexponential:
sampleExp[] := -Log[RandomReal[]];
2sampling_using_integral_transforms.nb
Show[
Histogram[
Table[sampleExp[] + sampleExp[],{i, Range[100 000]}], 200, "PDF"],
Plot[f[r],{r, 0, 7}, PlotRange →All]
]
0123456
0.1
0.2
0.3
IngeneraltheGammadistributionf(r)=
e-rra-1/Gamma[a]can be sampled as the sum of aexponential random
variates. In the case of non -integer a, Marsaglia ' smethod can be used.
3.DiscreteDecomposition
Sometimesf(r)canbewrittenasthesumofseveralnon-negativedistributionsf(r)= ∑i=1
nwifi(r),
eachofwhichisnormalized ∫0
∞fi(r)ⅆr=1andananalyticsamplingprocedureisknownforeach
fi(r).Inthiscase,onerandomnumbercanbeusedtofirstselectoneofthe
fidistributions using aCDF built from the weights wi,andthenasecondindependentrandom
numbercanbeusedtosamplefi. The resulting distance ris then distributed by f(r).
4.Continuoussuperpositions
Acontinuousgeneralizationofthepreviousdiscretedecompositioniswhenf(r)canbeexpressed
asacontinuoussuperpositionofafamilyofdistributionsg(r,s),whereanexactanalyticsampling
procedureforg(r,s)isknown:f(r)= ∫0
∞g(r,s)w(s)ⅆs,wheresisaparameterfordistributiong(such
asthemeanofg)andw(s)≥0,fors≥0.Ifasamplingprocedureisalsoknownforw(s),thenwecan
firstsamplesfromw,andthensampleg(r,s)withthesampledsparametervalue.Seealso
[Devroye2006-Section1.2].
Example4.1-BesselK 0
Considerthedistribution 2
πK0(r):
sampling_using_integral_transforms.nb 3
Clear[f];
f[r_] :=2
Pi
BesselK[0, r];
Integrate[f[r],{r, 0, Infinity}]
1
Wecanwritef(r)asthesuperpositionofexponentials:
w[s_] :=2
πs-1+s2
HeavisideTheta[s-1]
Integrate[w[s]s Exp[-s r],{s, 0, Infinity}, Assumptions →r>0]
2 BesselK[0, r]
π
Wechecktoseewecansampleinversemfpsfromw(s).TheCDFoftheweightfunctionw(s)is
Integrate[w[s],{s, 1, S}, Assumptions →S>1]
2 ArcSec[S]
π
Hereweintegratestartingfroms=1,sincetheHeavisidefunctioninw[s]makesthedistribution0
belows=1.Wecaninverttheresult,
Solveξ == 2 ArcSec[S]
π, S
S→ConditionalExpressionSecπ ξ
2,ξ ≠ 1 && 0 <πRe[ξ] < 2π ||
π Re[ξ]⩵0 && πIm[ξ]≥0|| π Re[ξ]⩵2π&& πIm[ξ]≤0
SowecansampleinversemfpsusingSecπ ξ1
2whereξ1isauniformrandomrealin[0,1],and
thensampletheexponentialusing:-log (ξ2)/s:
Show
HistogramTable-Log[RandomReal[]]
SecπRandomReal[]
2,{i, Range[50 000]}, 450, "PDF",
Plot[f[r],{r, 0, 10}, PlotRange →{0, 3}]
0.5 1.0 1.5 2.0 2.5
0.5
1.0
1.5
2.0
2.5
3.0
4sampling_using_integral_transforms.nb
5.Findingcontinuoussuperpositionsofagivenf(r)
WhentheinverseLaplacetransform ℒr
-1f(r)(s)isknown(superpositionof
exponentials)
WhentheinverseLaplacetransformℒr
-1[f(r)] (s)off(r)isknown,thenaweightfunctionfora
superpositionofexponentialsse-s r is w(s) = ℒr
-1[f(r)] (s)
s.Ifw(s)canbesampled,thenwehaveanew
2-random-numbersamplingprocedureforf(r).
Example5.1:
Considerthedistributionf(r)givenby:
f[r_] :=2
π-ⅇr2
2r Erfcr
2
WechecktheinverseLaplace:
1
s
FullSimplify[InverseLaplaceTransform[f[r], r, s], Assumptions →s>0]
ⅇ-s2
2
2
π
Thisisaweightingfunctionw(s),whichissimplyaGaussian/Normaldistribution,whichiseasily
sampled.
Show
Histogram
Table-Log[RandomReal[]]
Abs[RandomVariate[NormalDistribution[]]] ,{i, Range[30 000]},
{0, 10, 0.03}, "PDF",
Plot[f[r],{r, 0, 10}, PlotRange →All]
0 2 4 6 8 10
0.2
0.4
0.6
0.8
sampling_using_integral_transforms.nb 5
WhentheinverseLaplacetransform ℒr
-1fr(s)isknown(superpositions
ofGaussians)
SometimestheinverseLaplacetransformoff(r)isnotknownbuttheinverseLaplacetransformof
fris. In this case, it may be possible to sample f(r)usingtheGaussian(orNormal)distribution.
Todothis,usetheGaussiantransform,describednext.
6.TheGaussianTransform
The1DGaussian/Normaldistributiononthehalflinewithvariancesis:
Gaussian[r_, s_] :=2
2 Pi s
Exp-r22 s
andisnormalizedonthehalfline:
Integrate[Gaussian[r, s],{r, 0, Infinity}, Assumptions →s>0]
1
Thisone-sidedGaussian/NormaldistributioncanbesampledusinginverseErf[]ortheBox-Muller
algorithm.
TheGaussiantransform[Alecuetal.2006]canbeusedtosolveforaweightfunctionw[s]suchthat
f(r)canbeexpressedasasuperpositionofGaussians:f(r)= ∫0
∞Gaussian(r,s)w(s)ⅆs.
Example6.1-w[s]isErlang-2/Gamma-2:
w[s_] :=Exp[-s]s
Integrate[w[s],{s, 0, Infinity}]
1
Findf(r)givenw(s)astheweightingfunctionofGaussians:
f=Integrate[w[s] × Gaussian[r, s],{s, 0, Infinity}, Assumptions →r>0]
ⅇ-2 r 1+2 r
2
TrytheGaussiantransformtofindw(s)givenf(r):
1
2 s
Pi
2 s
InverseLaplaceTransformf/. r →r , r, t/. t →1
2 s
ⅇ-ss
Example6.2-BesselK 0
w[s_] :=Exp[-s]s-1/2
Pi
6sampling_using_integral_transforms.nb
Integrate[w[s],{s, 0, Infinity}]
1
Findf(r)givenw(s)astheweightingfunctionofGaussians:
f=Integrate[w[s] × Gaussian[r, s],{s, 0, Infinity}, Assumptions →r>0]
2 2 BesselK0, 2 r
π
TrytheGaussiantransformtofindw(s)givenf(r):
1
2 s
Pi
2 s
InverseLaplaceTransformf/. r →r , r, t/. t →1
2 s
ⅇ-s1
s
π
Example6.3
w[s_] :=1
1+s
Findf(r)givenw(s)astheweightingfunctionofGaussians:
f=Integrate[w[s] × Gaussian[r, s],{s, 0, Infinity}, Assumptions →r>0]
ⅇr2
22πErfcr
2
TrytheGaussiantransformtofindw(s)givenf(r):
1
2 s
Pi
2 s
InverseLaplaceTransformf/. r →r , r, t/. t →1
2 s // Simplify
1
1+s
Example6.4
w[s_] :=1
s(1+s)
Findf(r)givenw(s)astheweightingfunctionofGaussians:
f=Integrate[w[s] × Gaussian[r, s],{s, 0, Infinity}, Assumptions →r>0]
2
r
-ⅇr2
22πErfcr
2
TrytheGaussiantransformtofindw(s)givenf(r):
sampling_using_integral_transforms.nb 7
1
2 s
Pi
2 s
InverseLaplaceTransformf/. r →r , r, t/. t →1
2 s // Simplify
1
s+s2
Example6.5:CauchysuperpositionofGaussians
w[s_] :=1
1+s2
2
Pi
Integrate[w[s],{s, 0, Infinity}]
1
Findf(r)givenw(s)astheweightingfunctionofGaussians:
f=Integrate[w[s] × Gaussian[r, s],{s, 0, Infinity}, Assumptions →r>0]
2π Cosr2
2 1-2 FresnelCr
π +π 1-2 FresnelSr
π Sinr2
2
π3/2
Plot2π Cosr2
2 1-2 FresnelCr
π+π 1-2 FresnelSr
π Sinr2
2
π3/2,{r, 0, 5}
12345
0.2
0.4
0.6
0.8
1.0
TrytheGaussiantransformtofindw(s)givenf(r):
1
2 s
Pi
2 s
InverseLaplaceTransformf/. r →r , r, t/. t →1
2 s // Simplify
1
2π
1
s
3/2
InverseLaplaceTransform
πCosr
21-2 FresnelCr
π+π1-2 FresnelSr
πSinr
2, r, 1
2 s
Mathematicaisnotalwaysabletofindit.
7.GeneralizedBox-Muller
8sampling_using_integral_transforms.nb
Wenowshowhowtosolveforisotropicrandomflightsinddimensionthatprojectontoasingle
axistogivesomedesiredsymmetricdistribution.Thisisageneralizationofpolarmethods
[Devroye2006-Section1.2].
WeneedthesurfaceareaofthesphereofradiusrindD:
SA[d_, r_] :=dPid/2
Gammad
2+1rd-1
andtheinverseFourier/HankeltransformindD[Dutka1985]
inv[d_, f_] :=r1-d/22 Pi-d/2Integratezd/2BesselJd2-1, z rf,
{z, 0, Infinity}, Assumptions →d≥1 && r >0 && assumps
Givenadesireddistributionf(r)onthehalflinewewishtosample,wefirstformtheFourier
transform
F=2 Pi FourierTransform1
2
f[Abs[r]], r, z
andthensearchforsomedimensiondsuchthattheinverseHankeltransform
p=inv[d, F] × SA[d, r]
givesadistributionp(r)thatwecansampleanalytically.Ifso,thensamplingarandomisotropic
directionindD,andthenarandomstepfromp(r),andprojectingontoasingleaxis,andtakingthe
abs(x),givesasampleprocedureforf(r).
Example7.1-BoxMullersamplingoftheGaussian
Supposewewishtosamplearandomvariatefromaone-sidedGaussian/Normaldistribution
f[r_] :=Gaussian[r, 1]
f[r]
ⅇ-r2
2
2
π
Integrate[f[r],{r, 0, Infinity}]
1
Followingthe2stepsabove,andknowingthattheBox-MullertransformworksinFlatland,d=2,
wefind
F=2 Pi FourierTransform1
2
f[Abs[r]], r, z
ⅇ-z2
2
p=inv[2, F] × SA[2, r]
ⅇ-r2
2r
Wefindthatasingle-steprandomflightin2Dwithanisotropicstartingdirectionandfree-path
lengthdrawnfromⅇ-r2
2rwillproduceaGaussianrandomnumberwhenprojectedontooneaxis.
sampling_using_integral_transforms.nb 9
SamplingpbyCDFinverseleadsto:
Integrateⅇ-r2
2r, {r, 0, R}, Assumptions →R>0
1-ⅇ-R2
2
Solve1-ⅇ-R2
2⩵xi, R
R→ConditionalExpression-2 2 ⅈ π 1+Log1
1-xi ,1∈ℤ,
R→ConditionalExpression2 2 ⅈ π 1+Log1
1-xi ,1∈ℤ
Andsowesamplearadiususing 2 Log1
1-xi
Show
Histogram
TableAbs2 Log1
1-RandomReal[] Cos[2 Pi RandomReal[]],
{i, Range[100 000]}, 200, "PDF",
Plot[f[r],{r, 0, 7}, PlotRange →All]
0.5 1.0 1.5 2.0 2.5 3.0
0.2
0.4
0.6
0.8
Example7.2-SamplingGGXdistributionofvisiblenormals
InsamplingthedistributionofvisiblenormalsfortheGGXnormaldistributionthefollowingdistribu
-
tionrequiressampling[Heitzandd’Eon2014]inordertosampleslopeqconditionedonhaving
sampledslopep:
f[q_] :=2
π 1+q22.
Thiswassampledusinganapproximateinversein[Heitzandd’Eon2014]andlateranexact
schemewasprovided[Heitz2018].However,thegeneralizedBox-Mullerapproachin2Dalso
works.Wefind(usingavariantforf(q)onthefullline):
10 sampling_using_integral_transforms.nb
F=2 Pi FourierTransform[f[Abs[r]], r, z]
ⅇ-Abs[z]1+Abs[z]
p=inv[2, F] × SA[2, r]
3 r
1+r25/2
Thisdistributioniseasilysampled
Integrate3 r
1+r25/2,{r, 0, R}, Assumptions →R>0
1-1
1+R23/2
Solvexi == 1-1
1+R23/2, R
R→- -1+1
1-xi2/3,R→-1+1
1-xi2/3
Show
Plot[f[q],{q, -3, 3}],
Histogram
Table
Sin[2 Pi RandomReal[]] -1+1
1-RandomReal[]2/3
,{i, Range[10 000]}
, 500, "PDF"
-3-2-1 1 2 3
0.1
0.2
0.3
0.4
0.5
0.6
8.Combiningvarioustechniques
Weconsiderhereanexamplethatcombinesseveraloftheabovetechniquestosamplethedistribu-
tionofvisiblenormalsforGGXbyusingtheexactsamplingmethodforBeckmann.Thisisfarless
sampling_using_integral_transforms.nb 11
efficientthanknownsamplingproceduresfortheGGXvNDFsampling[Heitz2018].However,the
procedurehighlightsseveraloftheaboveapproaches.BasedontheobservationthatGGXissimply
asuperpositionofBeckmanns:
DBeckmann[u_,α_] :=1
Pi α2(u)4E
-
1
(u)2
-1
α2
DGGX[u_,α_] :=α21
(u)4
π α2+1
(u)2-12
Integrate2ⅇ-GGXm2
m2GGXm2
m3DBeckmann[u, m],{m, 0, Infinity},
Assumptions →GGXm >0 && 0 <u<1-DGGX[u, GGXm] // FullSimplify
0
wefindthattheGGXvNDFcanbesampledusinganexactvNDFprocedureforBeckmann[Heitzand
d’Eon2014]byfirstsamplingBeckmannroughnessm’from:
mprime[m_, u_] := - ⅇm
-1+u2(-1+u)
πm-m u2
+
ⅇ-mu 1 +Erfu-m
-1+u2
1+u
whereu=cos
θiand mis the roughness of the desired GGX NDF. To sample the m'(m)distribution we consider :
LaplaceTransform[mprime[m, u], m, s]
u
(1+s) (1+u)+u
(1+s)1+ (1+s)-1+1
u2(1+u)
--1+u
1+s-s u2
Weseparatethisinto3componentsm(s)=m1(s) + m2(s)+m3(s):
m1[s_] :=u
1+u
1
1+s
Werecognizethisasanexponentialdistributionfrom 1
1+s.Thesecondtermcansimplify,
FullSimplifyu
1+ (1+s)-1+1
u2(1+u)
, Assumptions →s>0 && 0 <u<1
u2
(1+u)1+s-s u2
m2[s_] :=u2
1+u
1
1+s
1
1+s-s u2
Werecognizethisastheconvolutionofanexponential 1
1+swithanewdistribution 1
1+s-s u2
,
whichalsoappearsinthethirdterm:
m3[s_] := (1-u)1
1+s-s u2
12 sampling_using_integral_transforms.nb
Wenowhavethreeterms.Theirselectionweightsarefoundfroms=0,
{m1[s], m2[s], m3[s]} /. s →0
u
1+u
,u2
1+u
, 1 -u
Whichweverifysumto1:
% // Total // FullSimplify
1
WejustneedtofindasamplingprocedurefortheinverseLaplacetransformof 1
1+s-s u2
,
InverseLaplaceTransform1
1+s-s u2
, s, r
ⅇ-r
1-u2
πr 1 -u2
TryCDFinversion,
FullSimplifyIntegrateⅇ-r
1-u2
πr 1 -u2
,{r, 0, R}, Assumptions →R>0,
Assumptions →0<u<1 && R >0
Erf-R
-1+u2
Solvexi == Erf-R
-1+u2, R
R→--1+u2InverseErf[xi]2
Wecansamplem2andm3usingthis,assumingwehaveanefficientInverseErf(),
sampling_using_integral_transforms.nb 13
samplemprime[u_] :=Module{xi1},
xi1 =RandomReal[];(* term selection variable *)
Ifxi1 <u
1+u
,
-Log[RandomReal[]] (* sample m1(s) *)
,
Ifxi1 <u
1+u+ (1-u),
--1+u2InverseErf[RandomReal[]]2(* sample m3 *)
,
-Log[RandomReal[]] - -1+u2InverseErf[RandomReal[]]2
(* sample m2 -by convolution *)
Verifyourderivation:
With[{u=0.3},
Show[
Histogram[Table[samplemprime[u],{i, Range[100 000]}
], 1300, "PDF"],
Plot[mprime[m, u],{m, 0, 5}, PlotRange →{0, 5}]
]
]
0.5 1.0 1.5 2.0 2.5 3.0
2
4
6
8
9.Track-lengthestimators
Inthecasethatatrack-lengthestimator[HeitzandBelcour2018]isusedtosamplef(r)byscoring
atallpositionsin[0,r]whendistancerissampled,wenotethatthercanbesampledasasuperposi-
tionofexponentialsbysamplinginversemfpsfrom
ℒ
r
-1[f(r)] (s)asopposedto ℒr
-1[f(r)] (s)
sforstan-
dardsampling,whichfollowsfromthepropertyoftheLaplacetransformoftheintegralofafunc-
tionf(r).
14 sampling_using_integral_transforms.nb
Example9.1:
Supposewewanttosamplef(r)usingatrack-lengthestimatorwheref(r)isgivenas:
f[r_] :=1+r+ⅇrr2+rExpIntegralEi[-r]
Integrate[f[r],{r, 0, Infinity}]
1
Knowingthatf(r)isgivenbyaLaplacetransform(contrivedexample)
LaplaceTransform2 s
(1+s)3, s, r// FullSimplify
1+r+ⅇrr2+rExpIntegralEi[-r]
Wecansamplesfrom 2 s
(1+s)3andthenrfromse-r s and the track -length estimatorwillsamplef(r).
CDFsamplingof 2 s
(1+s)3yields
Solvexi == Integrate2 s
(1+s)3,{s, 0, S}, Assumptions →S>0, S
S→-xi -xi
-1+xi ,S→xi -xi
-1+xi
Verifythatatrack-lengthestimatorsamplesf(r):
tls =
SumHeavisideTheta[# - t]&-Log[RandomReal[]]
- # -#
-1+# &[RandomReal[]] ,{i, Range[700]}
700
;
Show[
Plot[f[t],{t, 0, 6}, PlotRange →{0, 1}],
Plot[tls, {t, 0, 16}, Filling →Axis, PlotRange →{0, 1}]
]
0 1 2 3 4 5 6
0.2
0.4
0.6
0.8
1.0
sampling_using_integral_transforms.nb 15
10.References
◼Alecuetal.2006-TheGaussianTransformofDistributions:Definition,Computationand
Application.IEEETRANSACTIONSONSIGNALPROCESSING,VOL.54,NO.8,AUGUST2006.doi:
10.1109/TSP.2006.877657
◼Devroye,L.2006-Nonuniformrandomvariategeneration,inHandbooksinoperationsresearch
andmanagementscience13,Chapter4,83-121.doi:10.1016/S0927-0507(06)13004-2
◼Dutka,J.1985.Ontheproblemofrandomflights.Arch.Hist.ExactSci.32(3–4):351–75.
doi:10.1007/BF00348451
◼Heitz,E.andd’Eon,E.2014-Importancesamplingmicrofacet-basedBSDFsusingthe
distributionofvisiblenormals.InProceedingsofthe25thEurographicsSymposiumonRendering,
EurographicsAssociation,Aire-la-Ville,Switzerland,EGSR’14,103–112.URL:
https://hal.inria.fr/hal-00996995v2.
◼HeitzandBelcour2018-Anoteontrack-lengthsamplingwithnon-exponentialdistributions.
https://hal.inria.fr/hal-01788593v1
◼Heitz,E.2018-SamplingtheGGXDistributionofVisibleNormals,JCGT.
http://jcgt.org/published/0007/04/01/
16 sampling_using_integral_transforms.nb