Public Function DM(Alfa, beta, numsin)

S = 0

For i = 1 To numsin

Y = WorksheetFunction.Gamma_Inv(Rdn, Alfa, 1 / beta)

S = S + Y

Next i

DM = S

End Function

Public Function DM(Alfa, beta, numsin)

S = 0

For i = 1 To numsin

Y = WorksheetFunction.Gamma_Inv(Rdn, Alfa, 1 / beta)

S = S + Y

Next i

DM = S

End Function

I doubt that I understand your function correctly. I have never used VBA so I had to read the documentation on the Gamma_Inv() function. The key information is

If p = GAMMA_DIST(x,...), then GAMMA_INV(p,...) = x.

where Gamma_Dist is the gamma cumulative distribution. So Gamma_Inv() is the same as qgamma() in R .

I wrote two versions of your function. The first mimics the logic of your function. The second avoids the *for* loop. You do not define Rdn anywhere, so I assigned it outside of the function.

Calculating the cumulative sum of the constant x returned by Gamma_Inv() seems like a strange thing to do and that is why I doubt that I have understood your function.

```
DM <- function(Alfa, beta, numsin) {
S <- 0
for( i in 1:numsin) {
Y <- qgamma(Rdn, Alfa, rate = beta)
S = S + Y
}
return(S)
}
Rdn <- 0.6
DM(3,2,5)
#> [1] 7.763446
#Second version of the function
DM2 <- function(Alfa, beta, numsin) {
qgamma(Rdn, Alfa, rate = beta) * numsin
}
DM2(3,2,5)
#> [1] 7.763446
```

^{Created on 2022-06-22 by the reprex package (v2.0.1)}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.