Hi,
Bit of a noob with VBA and a bit limited with it at the moment.
I am trying to build a calc that runs a binomial calculation based on each individual trial having a probability that CAN be different from any other.
I have a table as below for 5
Then outputs the following
My current method is like this, but I intend to go up to 40/50 selections and it's laborious to code, and I think excel is starting to struggle to compute it fast enough and certainly will at the upper end.
ElseIf eventCount = 3 Then............... and so on.
Any helping hand would be much appreciated
Bit of a noob with VBA and a bit limited with it at the moment.
I am trying to build a calc that runs a binomial calculation based on each individual trial having a probability that CAN be different from any other.
I have a table as below for 5
Then outputs the following
My current method is like this, but I intend to go up to 40/50 selections and it's laborious to code, and I think excel is starting to struggle to compute it fast enough and certainly will at the upper end.
Code:
Function TrialsPermProb(TrialsWonCount As Integer, eventCount As Integer) As Double
Dim TrialsWon As Integer
Dim TrialsProb As Double
Dim sumPermProb As Double
Dim eventNo As Integer
Dim eventName As String
Dim outcome As Integer
Dim e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16 As Integer
ReDim TrialsPermProb2(0 To eventCount) As Double
Dim i As Integer
Dim TrialsCount As Integer
Dim market As Integer
ReDim probArr(1 To eventCount, 1 To 2) As Double
For i = 1 To eventCount
For outcome = 1 To 2
probArr(i, 1) = Range("probYesRange").Cells(i, 1).Value
probArr(i, 2) = 1 - Range("probYesRange").Cells(i, 1).Value
Next outcome
Next i
If eventCount = 1 Then
For e1 = 1 To 2
If e1 = 1 Then
TrialsCount = TrialsCount + 1
TrialsProb = probArr(1, 1)
Else
TrialsProb = probArr(1, 2)
End If
TrialsPermProb2(TrialsCount) = TrialsPermProb2(TrialsCount) + TrialsProb
TrialsProb = 0
TrialsCount = 0
Next e1
TrialsPermProb = TrialsPermProb2(TrialsWonCount)
ElseIf eventCount = 2 Then
For e1 = 1 To 2
For e2 = 1 To 2
If e1 = 1 Then
TrialsCount = TrialsCount + 1
TrialsProb = probArr(1, 1)
Else
TrialsProb = probArr(1, 2)
End If
If e2 = 1 Then
TrialsCount = TrialsCount + 1
TrialsProb = TrialsProb * probArr(2, 1)
Else
TrialsProb = TrialsProb * probArr(2, 2)
End If
TrialsPermProb2(TrialsCount) = TrialsPermProb2(TrialsCount) + TrialsProb
TrialsProb = 0
TrialsCount = 0
Next e2
Next e1
TrialsPermProb = TrialsPermProb2(TrialsWonCount)
ElseIf eventCount = 3 Then............... and so on.
Any helping hand would be much appreciated