Spaces:
Sleeping
Sleeping
File size: 5,556 Bytes
56db230 c60d7b7 56db230 c60d7b7 56db230 c60d7b7 56db230 c60d7b7 56db230 c60d7b7 56db230 c60d7b7 56db230 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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)
<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])
# Launch the Blocks interface
demo.launch()
|