#Sorting by frequency and recency with variable weights (Userscript)

22 messages · Page 1 of 1 (latest)

scarlet bough
#

I can make a nice clean object out of the data from a local lotto history/tracker website.

If I pull all the numbers into an array I could count the frequency and then generate a set of numbers scored by frequency/how often they appeared in the draws.

Alternatively I could count how many days have passed for each draw and then generate a set of numbers scored on recency where it is an average of all the days passed for each number in the draws.

Finally a pair of slider inputs could allow us to make a final sort based on if the user wants to prioritize the frequency vs. recency.

Frequency (number drawn, number of draws):
7, 4
12, 1
22, 5
...

Recency (days ago added and divided by the total count):
7, ||(14 + 31 + 77 + 105)/4 = ||56.75
12, 14
22, 38.92
...

Looking at the sample data, if there's a preference for frequency then the numbers picked would likely be 22, 7, and then some others due to them sorting high. If the preference swings to overdue numbers then we would expect 7 and 22 to be picked before 12.

To get this we'd want to multiply the frequency by a factor that scales based on the weight, and then we could use the sort order of summed arrays to get a weighted sort, assuming we can scale the Frequency to be in the same range as the Recency?

This last part is really what is kicking my butt for logic as I've never had to solve it before. I asked AI for help but it gets my goals all wrong and just ignores the Frequency weight and gives the numbers sorted by recency. I can get overdue numbers, the most recent numbers, and shades in between, but the amount of times they showed up does not have enough weight to change the sort?

Perhaps there's a glaring way to get the averages of both sorts and then auto scale the frequency values to better match the recency? 🤔

dense pawn
#

Me when the independence of events (a little gambler's fallacy)

scarlet bough
#

And this isn't for me, so I don't need a lecture about gambling, it's for a lifelong gambler in my family that has asked me some questions about this, and it's quite interesting to have them ask about anything nerdy so I'm obliged to habe some fun learning.

dense pawn
#

How you want to weigh frequency vs recency is up to you in the end, you can definitely scale them to be in the same range tho

scarlet bough
#

I called it as I was typing it you got ahead of me. Dork. 🙂

scarlet bough
#

Lemme habe my fun!

dense pawn
#

When you sort by recency, shouldn't you just sort by recency?

scarlet bough
#

Well what if I want the most recent in terms of drawn that aren't picked often?

dense pawn
#

Then you'd need to define some threshold for "not picked often"

scarlet bough
#

Yeah that's what's happening, at 50 % the sort just flips to recency. ```js
// Function to pick numbers based on weights
function pickNumbers(data, frequencyWeight, recentWeight) {
const frequencies = calculateFrequencies(data);
const recentness = calculateRecentness(data);

    const weightedNumbers = [];
    Object.keys(frequencies).forEach((number) => {
        const frequencyScore = frequencies[number] * (100 - frequencyWeight) / 100;
        let recentnessScore;
        if (recentWeight < 50) {
            // Prioritize numbers that have been seen less recently
            recentnessScore = 1 / recentness[number];
        } else {
            // Prioritize numbers that have been seen more recently
            recentnessScore = recentness[number];
        }
        const score = frequencyScore + (recentWeight / 100) * recentnessScore;
        weightedNumbers.push({ number, score });
    });

    // Sort the numbers by their scores
    weightedNumbers.sort((a, b) => a.score - b.score);

    // Pick the top 5 numbers with unique values
    const pickedNumbers = [];
    for (let i = 0; i < 6; i++) {
        if (!pickedNumbers.includes(weightedNumbers[i].number)) {
            pickedNumbers.push(weightedNumbers[i].number);
        }
    }

    return { numbers: pickedNumbers.slice(0,5), extra: pickedNumbers[6] };
}
dense pawn
#

Why at 50%?

scarlet bough
#

Because I was lazy and asked AI to help me brainstorm it vs. do it the hard way, so I'm here now. 😄

dense pawn
#

😂

scarlet bough
#

I'm probably just a really strong coffee or two away from tackling it. My brain is on vacation this month.

dense pawn
#

So the sorting criteria are more like hints I guess?

#

And not actual "just sort by this"

scarlet bough
#

Well they should be weights to skew the output.
I saw this done in PHP over a decade ago for a $10k contract. Back then the savvy dev made the weights based on some silly questions like "favorite color" and "your middle initial" so they could give "lucky" random picks that are always skewed the same way if the person puts everything back in the same.

#

And everyone has a crazy idea of what's going on, this gambler likes to play "heavy balls" that seem to come up often while also playing tickets that have numbers he hasn't seen much recently.

#

The more I look at this stuff it makes me really want to go scrape all the payout data and then use the data for 2023 to search for the set of numbers that would have paid out the most in 2023. Probably a jackpot set but who knows, there might have been a set that matched enough to pay out better than the jackpot? And if we ruled out any set that matches a jackpot, what are the leftover numbers? 🤓

scarlet bough
#

Well right now, improperly, and I'm thinking it's due to the scale, but I could be overthinking it.
If you sort the numbers by frequency, and make a separate sort by recency, the index of each array is a nice common scale? 🤔