Γεια σας (.)DotNetZone !!
Έφτιαξα ένα function για να μου υπολογίζει υπεργεωμετρικές κατανομές, αλλά έχω ένα πρόβλημα. Όταν του βάζω να υπολογίσει το 80 ανά 20 δεν μπορεί να το κάνει γιατί στο πρώτο βήμα που πάει να υπολογίσει το (80)20 ( 80*79*78*77*76*75*74*73*72*71*70*69*68*67*66*65*64*63*62*61*60 ) κολλάει στο νούμερο 5974790569203456000 το οποίο είναι ουσιαστικά το 80*79*78*77*76*75*74*73*72*71. Δηλαδή δεν έφτασε ούτε στη μέση...
Το έβαλα ulong μιας που ξέρω ότι θα υπολογίζω μόνο θετικούς αριθμούς, και το έβαλα και long για να δέχεται τεράστιες τιμές (64bit).
Παρόλαυτα όμως ξέρω ότι μπορεί να γίνει, γιατί στην R ( http://www.r-project.org/ ) πάτησα να μου κάνει τον πολλαπλασιασμό και μου το έβγαλε ολόκληρο χωρίς πρόβλημα..
Είναι κρίμα, γιατί σαν function δουλεύει μια χαρά, αλλά υπάρχει αυτό το πρόβλημα όταν μπει μεγάλος αριθμός
Σαν τι να το ορίσω? δεν βρήκα μεγαλύτερο...
Κάτω παραθέτω τα function, όποιος θέλει να το χρησιμοποιήσει είναι ελεύθερος:
Public Function Hypergeometric_Distribution(ByVal ν As Long, ByVal k As Long, ByVal N As Long, ByVal r As Long) As Decimal
Dim Result As Decimal = BinomialCoefficient(ν, k) * BinomialCoefficient(N - ν, r - k) / BinomialCoefficient(N, r)
Return Result
End Function
Public Function BinomialCoefficient(ByVal Kati As Long, ByVal AnaKati As Long) As Decimal
Dim Result As Decimal = 0
If Kati <> AnaKati And Kati <> 0 And AnaKati <> 0 And Kati > AnaKati Then
Dim LoopCount As Integer = 0
Dim FacUp As Long = Kati
Dim FacDown1st As Long = AnaKati
Dim FacDown2nd As Long = Kati - AnaKati
Dim DividerDown As ULong = 1
Dim DividendUp As ULong = 1
If Kati / AnaKati <= 2 Then 'if First is <=2times the Second then the 1st Factorial is eliminated
For i As Long = FacUp To FacDown1st + 1 Step -1
DividendUp = DividendUp * CULng(FacUp - (LoopCount)) 'Calculating the Factorial Up
LoopCount += 1
Next
FacDown1st = 1 '1st Factorial gets eliminated
If FacDown2nd <> 0 AndAlso FacDown2nd <> 1 Then
Dim BeginningOfLoop As Long = FacDown2nd - 1
For i As Long = BeginningOfLoop To 1 Step -1
FacDown2nd = FacDown2nd * i 'Calculating the Factorial Down
Next
Else
FacDown2nd = 1 'Factorial 1 and 0 = 1
End If
Else 'if First is >=2times the Second then the 2nd Factorial is eliminated
For i As Long = FacUp To FacDown2nd + 1 Step -1
DividendUp = DividendUp * CULng(FacUp - (LoopCount)) 'Calculating the Factorial Up
LoopCount += 1
Next
FacDown2nd = 1 '2nd Factorial gets eliminated
If FacDown1st <> 0 AndAlso FacDown1st <> 1 Then
Dim BeginningOfLoop As Long = FacDown1st - 1
For i As Long = BeginningOfLoop To 1 Step -1
FacDown1st = FacDown1st * i 'Calculating the Factorial Down
Next
Else
FacDown1st = 1 'Factorial 1 and 0 = 1
End If
End If
DividerDown = CULng(FacDown1st * FacDown2nd)
Result = CDec(DividendUp / DividerDown)
Else
Result = 0
End If
Return Result
End Function