import random
import gradio as gr
OUTCOMES = [1, 2, 3, 4, 5, 6]
# Function to simulate a simple die roll experiment
def roll_die():
result = random.choice(OUTCOMES)
return result
# Roll the die and check if the result is even
def test_event():
result = roll_die()
return result, "Yes!" if result % 2 == 0 else "No.."
# Compute the probability of an event
def compute_event_probability(favorable_outcomes, possible_outcomes):
if favorable_outcomes == "" or possible_outcomes == "":
return "Please input the set of favorable and possible outcomes"
favorable_outcomes = favorable_outcomes.split(",")
possible_outcomes = possible_outcomes.split(",")
try:
favorable_outcomes = list(map(int, favorable_outcomes))
except ValueError:
return "Please input a valid set of favorable outcomes"
try:
possible_outcomes = list(map(int, possible_outcomes))
except ValueError:
return "Please input a valid set of possible outcomes"
if not set(favorable_outcomes).issubset(set(possible_outcomes)):
return "Favorable outcomes should be a subset of possible outcomes"
probability = len(favorable_outcomes) / len(possible_outcomes)
return f"The probability of the event is: {probability:.0%}"
# Produce randomized favorable and possible outcomes
def randomize_outcomes():
n_favorable_outcomes = random.randint(1, 6)
favorable_outcomes = random.sample(OUTCOMES, n_favorable_outcomes)
possible_outcomes = OUTCOMES
return ",".join(map(str, favorable_outcomes)), ",".join(map(str, possible_outcomes))
# Create a Gradio interface
css = """
.gradio-container {
width: 40%!important;
min-width: 800px;
}
"""
with gr.Blocks(css=css) as demo:
gr.Markdown(
"""
# Probability Basics (Pt. 1)
Let's explore some basics of Probability theory!
In probability theory, `sample spaces`, `outcomes`, and `events` are fundamental concepts that provide the foundation
for understanding and working with probabilities. Let's explore these concepts and how they relate to each other.
"""
)
gr.Markdown(
"""
## Sample Spaces and Outcomes
The `sample space` is the **set of all possible `outcomes`** of a random experiment or random process.
For example, if you roll a `six-sided die` 🎲, the sample space is all the possible configurations the die could end up in.
These are either with the side facing up showing the value 1, or 2 or... We can summarize this as: `{1, 2, 3, 4, 5, 6}`
"""
)
with gr.Row():
die_roll = gr.Button(value="Roll the Die! 🎲🎲")
die_roll_output = gr.Textbox(label="You side facing up shows a..", placeholder="", interactive=False)
die_roll.click(roll_die, [], [die_roll_output])
gr.Markdown(
"""
## Events
An `event` is any **subset of the `sample space`**, representing a specific outcome or a combination of outcomes.
This often corresponds to some event of interest (e.g. whether if will rain tomorrow) but may be sometimes an interemediary step when dealing with more complex probability problems.
Continuing with our example, an event like `rolling an even number` would correspond to the subset: `{2, 4, 6}`
"""
)
with gr.Row():
event = gr.Button(value="Try to roll an even number! 🎲🎲")
with gr.Row():
event_die_roll_output = gr.Textbox(label="You have rolled a..", placeholder="", interactive=False)
event_test_output = gr.Textbox(label="Did the event occur?", placeholder="", interactive=False)
event.click(test_event, [], [event_die_roll_output, event_test_output])
gr.Markdown(
r"""
## Probability
The `probability` of an `event` is expressed as a **ratio between** the number of `favorable` and `possible outcomes`.
$$ P(\text{Event}) = \frac{\text{Number of (Favorable Outcomes)}}{\text{Number of (Possible Outcomes)}} $$
- `Favorable outcomes` are those that satisfy the condition of the `event`.
- `Possible outcomes` are all the outcomes, regardless of whether they satisfy the event or not.
You can use the space below to calculate the probability of an event.
Play around with different sets of favorable and possible outcomes to see how the probability changes!
"""
)
with gr.Column():
with gr.Row():
randomize = gr.Button(value="Randomize the favorable and possible outcomes")
probability = gr.Button(value="Calculate the probability")
with gr.Row():
with gr.Column():
favorable_outcomes = gr.Textbox(label="Favorable Outcomes", value="2, 4, 6")
possible_outcomes = gr.Textbox(label="Possible Outcomes", value="1, 2, 3, 4, 5, 6")
probability_output = gr.Textbox(label="Probability", placeholder="", interactive=False)
randomize.click(randomize_outcomes, [], [favorable_outcomes, possible_outcomes])
probability.click(compute_event_probability, [favorable_outcomes, possible_outcomes], [probability_output])
# Launch the Blocks interface
demo.launch()