|
import random |
|
import gradio as gr |
|
|
|
OUTCOMES = [1, 2, 3, 4, 5, 6] |
|
|
|
|
|
|
|
def roll_die(): |
|
result = random.choice(OUTCOMES) |
|
return result |
|
|
|
|
|
def test_event(): |
|
result = roll_die() |
|
return result, "Yes!" if result % 2 == 0 else "No.." |
|
|
|
|
|
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%}" |
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
css = """ |
|
.gradio-container { |
|
width: 40%!important; |
|
min-width: 800px; |
|
} |
|
""" |
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown( |
|
""" |
|
# Probability Basics (Pt. 1) |
|
<div align="center"> |
|
<br> |
|
<img src="https://media.giphy.com/media/UThpUbZef3ulWxgvfn/giphy.gif?cid=790b7611d3ky210i43bt6fubpzql9nu8ilbsnhma17ozfas7&ep=v1_gifs_search&rid=giphy.gif&ct=g" /> |
|
<p>Let's explore some basics of Probability theory!</p> |
|
<br> |
|
</div> |
|
|
|
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. |
|
<br>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. |
|
<br> |
|
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. |
|
<br> |
|
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]) |
|
|
|
|
|
demo.launch() |
|
|