Sub Poisson(k As Int, Lambda As Double)
Dim sum As Double
If (k < 0 ) Then
dres(0) = -999
dres(1) = -999
Return
End If
dres(1) = Power(Lambda,k)* Power(cE,-Lambda)/ Factorial(k)
sum = 1
For i = 1 To k
sum = sum + Power(Lambda,i)/Factorial(i)
Next
dres(0) = sum * Power(cE,-Lambda)
End Sub
Sub InvPoisson(Pp As Double , Lambda As Double) As Int
Dim k, dk,flag,oldflag As Int
k = 10
dk = 8
flag = 1
oldflag = 1
Poisson(k,Lambda)
Do While (dk >= 1)
If (dres(0) > Pp + 1e-6) Then
flag = -1
Else
flag = 1
End If
k = k + flag * dk
If (k < 1 ) Then k = 0
If(flag <> oldflag) Then dk = dk/2
Poisson(k,Lambda)
Loop
If (dres(0) > Pp + 1e-6 ) Then
Return k-1
Else
Return k
End If
End Sub