Sulprobil
Search…
sbRandPDF
With an explicit form of a cumulative distribution function inverse you should use sbRandCDFInv but if there is no such one then you can apply a linear approximation with the probability distribution function sbRandPDF. Unfortunately the sbRandPDF approach is computationally quite expensive, even if you reduce the number of linear points in case of identical or almost identical slopes.
Please read my Disclaimer.
1
Function sbRandPDF(Optional dParam1, Optional dParam2, _
2
Optional dParam3, Optional dRandom = 1#) As Double
3
'Reverse(moc.LiborPlus.www) 12-Sep-2014 PB V0.15
4
Dim dRand As Double
5
Dim i As Long
6
Static dPar1 As Double
7
Static dPar2 As Double
8
Static dPar3 As Double
9
Static vX(0 To 1000) As Variant
10
Static vY(0 To 1000) As Variant
11
If dRandom < 0# Or dRandom > 1# Then
12
sbRandPDF = CVErr(xlErrValue)
13
Exit Function
14
End If
15
If dRandom = 1# Then
16
dRand = Rnd()
17
Else
18
dRand = dRandom
19
End If
20
If dParam1 <> dPar1 Or dParam2 <> dPar2 Or dParam3 <> dPar3 Then
21
dPar1 = dParam1
22
dPar2 = dParam2
23
dPar3 = dParam3
24
'Initialize RandGeneral call parameters
25
For i = 0 To 1000
26
vX(i) = dPar1 + i * (dPar3 - dPar1) / 1000#
27
'Now we can insert an arbitrary PDF function
28
If vX(i) < dPar2 Then
29
vY(i) = (vX(i) - dPar1) / ((dPar3 - dPar1) * (dPar2 - dPar1))
30
If vY(i) < 0# Then vY(i) = 0#
31
Else
32
vY(i) = (dPar3 - vX(i)) / ((dPar3 - dPar1) * (dPar3 - dPar2))
33
If vY(i) < 0# Then vY(i) = 0#
34
End If
35
Next i
36
End If
37
'Depending on the PDF input range you need to feed start
38
'and end values to sbRandGeneral
39
sbRandPDF = sbRandGeneral(dPar1, dPar3, vX, vY, dRand)
40
End Function
Copied!
This function will provide with a stratified sample:
Last modified 1yr ago
Copy link