I’m having trouble computing some probabilities. Here’s the basics:
The class ItemSet can have any number of entries that will be chosen at random. The algorithm will choose between a user-defined min and max. What I’m trying to do is compute the odds of an entry being selected. So for example, if there are 4 entries in a set and the min and max are both 2, all entries would have a 50% chance of being chosen.
This part I’ve got down actually. Here’s the monkeywrench I haven’t been able to deal with. Each entry has a weight value. These values are relative to each other, so 2 entries with the same weight will have the same chances. If entry A has a weight of 0.8 and entry B has a weight of 0.4, entry A should be twice as likely to be chosen.
Here’s the code I have so far, slightly modified for clarity.
[code] Dim EntryCount As UInteger = UBound(Self.mEntries) + 1
If EntryCount = 0 Then
Dim MinNumItems As UInteger = Max(Min(EntryCount, Self.MinNumItems, Self.MaxNumItems), 1)
Dim MaxNumItems As UInteger = Max(Min(Self.MaxNumItems, EntryCount), MinNumItems)
Dim TotalCombinations, TotalMatches As Double
For I As Integer = MaxNumItems DownTo MinNumItems
Dim Combinations As Double = Beacon.Combinations(EntryCount, I)
Dim Matches As Double = (I / EntryCount) * Combinations
TotalCombinations = TotalCombinations + Combinations
TotalMatches = TotalMatches + Matches
If TotalCombinations = 1 Then
// Feels dirty, but this is true
ElseIf TotalCombinations = 0 Then
Dim Chance As Double = Max(Min(TotalMatches / TotalCombinations, 1), 0.01)
This code can be found in the RelativeWeight method here: https://github.com/thommcgrath/Beacon/blob/master/Project/Modules/Beacon/ItemSet.xojo_code which is part of my Beacon project: https://github.com/thommcgrath/Beacon, so you could even download the project and run it yourself if you so desire.
Just to be clear, I’m not looking for help actually performing the selection. I’m looking for help computing the odds that an entry will be chosen.