hafizahzulkipli88
New Member
- Joined
- Jan 26, 2017
- Messages
- 2
Hi. I try to integrate this function but I'm not sure if what I'm doing is right or wrong.
I appreciate if someone can help. The function follow gamma distribution.
I appreciate if someone can help. The function follow gamma distribution.
Code:
Sub Calculateloss()
'Numerical integration accuracy
Dim dx As Double
Dim qOpt As Double
Dim dx As Double
Dim k As Integer
Dim beta As Double
'Parameter values
k=2
beta=143
dx = 0.001
qOpt=550
'Variables to calculate results
Dim I1 As Double 'First part of Integral
I1 = 0
Dim I2 As Double 'Second
I2 = 0
'Variable x
Dim x As Double
Dim f_x As Double
Dim f_xplusdx As Double
For x = 0 To (qOpt - dx) Step dx
f_x = WorksheetFunction.Gamma_Dist(x, k, beta, False)
f_xplusdx = WorksheetFunction.Gamma_Dist(x + dx, k, beta, False)
I1 = I1 + 0.5 * (x * f_x + (x + dx) * f_xplusdx) * dx
I2 = I2 + 0.5 * (f_x + f_xplusdx) * dx
Next x
'Expected loss
Dim loss As Double
loss = (Av - qOpt + qOpt * I2 - I1)
'Output
Cells(2, 1) = loss
End Sub