Determine the likelihood of achieving the desired sum through various lottery drawings

If I were to participate in multiple lotteries with varying prize amounts and different odds of winning, how can I determine the likelihood of winning at least a specified value if I play each lottery simultaneously and only once?

  • Lottery A: Has a 50% chance of winning 5 gold.
  • Lottery B: Offers a 40% chance of winning 6 gold.
  • Lottery C: Provides a 30% chance of winning 7 gold.

I am interested in finding a method to calculate the probability of winning an amount equal to or greater than a set target value (such as 10 gold) when participating in all these lotteries simultaneously. This solution should be scalable to help me analyze around 40 different lotteries.

The input would consist of a list containing tuples representing the probability and prize size of each lottery, for example:

lottery_list = [(0.5, 5), (0.4, 6), (0.3, 7)]

Following this, a function could be utilized to compute the probability of winning at least the specified target value, such as 10 gold in this case:

prob = win_at_least(lottery_list, target_val=10)

Answer №1

After implementing a solution on my own, I was able to optimize the code by excluding irrelevant combinations of 'downstream' lotteries if the set of lotteries has already produced the target value. This enhancement significantly improved performance, reducing the processing time to about 10 ms for 40 lotteries while maintaining scalability.

import numpy as np
import pandas as pd


def calculate_lottery_odds(lottery_list: list, target: float) -> float:
    """
    Determines the probability of winning at least the target
    value from a list of lotteries with varying win probabilities and values.

    Args:
        lottery_list (list): List of tuples containing lottery
            probabilities and values.
        target (float): Target value to be achieved.

    Returns:
        float: Probability of winning at least the target value.
    """
    # Create a dataframe using the lottery list.
    df = pd.DataFrame(lottery_list, columns=["probability", "value"])

    # Sort the data frame in descending order of value.
    df_sorted = df.sort_values("value", ascending=False, ignore_index=True)

    probs = df_sorted["probability"].values
    values = df_sorted["value"].values

    length = len(df_sorted)
    mask = np.ones(length, dtype=int)

    total_odds = 0

    while True:
        odds = 1
        value = 0

        for idx, prob in enumerate(probs):
            if mask[idx] == 1:
                odds *= prob
                value += values[idx]
            else:
                odds *= 1 - prob

            if value >= target:
                total_odds += odds
                mask[idx] = 0
                break

            elif idx == length - 1:
                largest_active_idx = 0

                for i, v in enumerate(mask):
                    if v == 1:
                        largest_active_idx = i

                mask[largest_active_idx] = 0
                mask[largest_active_idx + 1 :] = 1

        if np.sum(mask) == 0:
            break

    return total_odds

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Waiting for Elements to be Added to Parent in a Lazy Loading Website with Selenium and Python

When working with Selenium's expected conditions for wait, such as those mentioned here: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions, I find myself unsure about which one to use or if that is e ...

How to remove a specified number of characters from a pandas column based on the values in another column in Python

Looking at my DataFrame: df = pd.DataFrame({'A':['elisabeth','adamms','jorhgen'], 'B':[2,2,3]}) I am trying to remove the last characters from the strings in column A based on the values in column B w ...

Python's lightning-fast combinatoric generator

Currently working on a python project, I am in need of a high-speed generator function that can create all potential sets of non-negative integer numbers less than n. These sets must contain no more than s elements and the gap between the largest and small ...

Is there a way to transform JSON into a CSV file using Python?

Is there a way to convert the logs in the JSON format shown below into a CSV file using Python? JSON Input (log.json): {"time":"14472","abc":"0","Def":[{"name":"C","value":77},{"name":"N","value":88}]} {"time":"1447","abc":"1","Def":[{"name":"C","valu ...

I have developed a selenium script specifically for the odoo framework using Python. Can this script be utilized for conducting load testing? If so, what would be the recommended approach for implementing

After developing a script with Selenium in the Odoo framework using Python, I am now looking to perform load testing. What would be the most suitable tool for this task? Do I need to create a separate script specifically for load testing, or can I utiliz ...

Having a tough time getting Selenium to successfully complete the automated checkout process

After experimenting with Selenium and attempting to automate the checkout process, I've encountered a stumbling block in the final part of my code. Despite numerous configurations of 'sleeps' and 'implicit_waits', I keep receiving ...

Unpredictable chunks of information in Pandas

I am looking to extract random blocks of data from a dataframe named df. While using df.sample(10) gives me individual samples, it doesn't provide contiguous blocks. Is there a method to sample random blocks (e.g., blocks of 6 consecutive data points) ...

Scrapy error: Unable to locate module mail.smtp

Operating System: Ubuntu 14.04 I successfully installed scrapy by running the command sudo pip install scrapy. Currently, I am working through a tutorial which can be found here. However, I encountered an issue when executing the command scrapy crawl dm ...

Automate Data Extraction from Tables Using Python Selenium and Convert them into a DataFrame

I am just starting to learn Python and selenium, and I'm facing a challenge that I need help with. Currently, I am attempting to extract data from a particular website: "" The goal is to convert the table on this website into a dataframe similar to ...

It is impossible to load JSON into a variable

I have been using this amazing module called scholarly.py for my research work. If you want to check it out, here is the link: https://pypi.org/project/scholarly/ This module functions flawlessly, but I am facing an issue where I cannot store the output ...

What is the best way to determine the position of a letter within a string? (Using Python, JavaScript, Ruby, PHP, etc...)

Although I am familiar with: alphabet = 'abcdefghijklmnopqrstuvwxyz' print alphabet[0] # outputs a print alphabet[25] #outputs z I am curious about the reverse, for instance: alphabet = 'abcdefghijklmnopqrstuvwxyz' 's' = al ...

The mysterious results of the 2F1 function in the scipy module

Why does hyp2f1 from scipy return 1? Shouldn't it be something else? https://i.stack.imgur.com/iEWIr.png Below is the code I used import numpy as np from scipy import special x = np.arange(0,2,0.01) y = special.hyp2f1(-1/2, 1/2, 2, -400/(1-8/9*np.c ...

Python script transforms access.log file into JSON format

Having some trouble converting my nginx access.log file into json format due to the following error: Index error: list index out of range import json i = 1 result = {} with open('access.log') as f: lines = f.readlines() for line in li ...

Determining the sunrise and sunset times based on a datetime index using the ephem library in Python

My challenge involves working with a daily time series that has a DateTime index. I am aiming to compute the specific sunrise and sunset times for each day within the DataFrame. The desired output will appear in columns named rise and set. In my approach, ...

Inverting color schemes or defining specific hues within a matplotlib/pandas chart

I have been experimenting with some code snippets from the book Python for Data Analysis and encountered an intriguing challenge. I am curious to know if there is a technique to invert the colors of a colormap, like transitioning from blue to pink in rever ...

Exploring the Efficiency of XPath Child Navigation Techniques

Currently, I am utilizing lxml with Python 2.7. If we have a node called node and a child element named child_element, what distinguishes the following two operations: node.xpath('./child_element') node.xpath("*[local-name()='child_element ...

Issue: "TypeError: 'module' object is not callable" encountered while attempting to visualize a decision tree for a machine learning model

After writing the code to visualize the decision tree model, I initially faced errors such as 'graphviz's executables not found'. However, I managed to resolve this issue by adding the path to the environment variables and re-installing the ...

Displaying dataframes with Pandas

While attempting to follow the "Machine Learning" tutorial by Microsoft, I encountered an issue during the practical part. I simply copied the code and tried running it from the Linux terminal, but unfortunately, nothing was returned. The video demonstrati ...

"Python Selenium: How to utilize innerHTML and implement wait functionality

I am facing an issue with waiting for the innerHTML element to load in my code. Here is a snippet of what I have tried: element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, XPATH))) element = element.get_attribute('inn ...

Engaging with website components that modify HTML content values

I'm using Selenium to automate the process of clicking through parking garage markers on the website http://seattle.bestparking.com/. The goal is to open the pop-up info window for each marker and then extract information from the "rate" page in the p ...