helboukkouri
commited on
Commit
·
56db230
1
Parent(s):
90199c6
initial commit
Browse files- README.md +7 -7
- app.py +134 -0
- requirements.txt +198 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
-
title: Probability Basics
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
1 |
---
|
2 |
+
title: Probability Basics (Pt. 1)
|
3 |
+
emoji: 🎲
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.20.1
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Learn about the basics of Probability Theory, including concepts like: `sample spaces`, `outcomes`, and `events`. See how these concepts come together to calculate the `probability of an event` occurring.
|
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
OUTCOMES = [1, 2, 3, 4, 5, 6]
|
5 |
+
|
6 |
+
|
7 |
+
# Function to simulate a simple die roll experiment
|
8 |
+
def roll_die():
|
9 |
+
result = random.choice(OUTCOMES)
|
10 |
+
return result
|
11 |
+
|
12 |
+
# Roll the die and check if the result is even
|
13 |
+
def test_event():
|
14 |
+
result = roll_die()
|
15 |
+
return result, "Yes" if result % 2 == 0 else "No"
|
16 |
+
|
17 |
+
# Compute the probability of an event
|
18 |
+
def compute_event_probability(favorable_outcomes, possible_outcomes):
|
19 |
+
if favorable_outcomes == "" or possible_outcomes == "":
|
20 |
+
return "Please input the set of favorable and possible outcomes"
|
21 |
+
|
22 |
+
favorable_outcomes = favorable_outcomes.split(",")
|
23 |
+
possible_outcomes = possible_outcomes.split(",")
|
24 |
+
|
25 |
+
try:
|
26 |
+
favorable_outcomes = list(map(int, favorable_outcomes))
|
27 |
+
except ValueError:
|
28 |
+
return "Please input a valid set of favorable outcomes"
|
29 |
+
|
30 |
+
try:
|
31 |
+
possible_outcomes = list(map(int, possible_outcomes))
|
32 |
+
except ValueError:
|
33 |
+
return "Please input a valid set of possible outcomes"
|
34 |
+
|
35 |
+
if not set(favorable_outcomes).issubset(set(possible_outcomes)):
|
36 |
+
return "Favorable outcomes should be a subset of possible outcomes"
|
37 |
+
|
38 |
+
probability = len(favorable_outcomes) / len(possible_outcomes)
|
39 |
+
return f"The probability of the event is: {probability:.0%}"
|
40 |
+
|
41 |
+
# Produce randomized favorable and possible outcomes
|
42 |
+
def randomize_outcomes():
|
43 |
+
n_favorable_outcomes = random.randint(1, 6)
|
44 |
+
favorable_outcomes = random.sample(OUTCOMES, n_favorable_outcomes)
|
45 |
+
possible_outcomes = OUTCOMES
|
46 |
+
return ",".join(map(str, favorable_outcomes)), ",".join(map(str, possible_outcomes))
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
# Create a Gradio interface
|
51 |
+
css = """
|
52 |
+
.gradio-container {
|
53 |
+
width: 40%!important;
|
54 |
+
min-width: 800px;
|
55 |
+
}
|
56 |
+
"""
|
57 |
+
with gr.Blocks(css=css) as demo:
|
58 |
+
gr.Markdown(
|
59 |
+
"""
|
60 |
+
# Probability Basics (Pt. 1)
|
61 |
+
<div align="center">
|
62 |
+
<br>
|
63 |
+
<img src="https://media.giphy.com/media/UThpUbZef3ulWxgvfn/giphy.gif?cid=790b7611d3ky210i43bt6fubpzql9nu8ilbsnhma17ozfas7&ep=v1_gifs_search&rid=giphy.gif&ct=g" />
|
64 |
+
<p>Let's explore some basics of Probability theory!</p>
|
65 |
+
<br>
|
66 |
+
</div>
|
67 |
+
|
68 |
+
In probability theory, `sample spaces`, `outcomes`, and `events` are fundamental concepts that provide the foundation
|
69 |
+
for understanding and working with probabilities. Let's explore these concepts and how they relate to each other.
|
70 |
+
"""
|
71 |
+
)
|
72 |
+
gr.Markdown(
|
73 |
+
"""
|
74 |
+
## Sample Spaces and Outcomes
|
75 |
+
The `sample space` is the **set of all possible `outcomes`** of a random experiment or random process.
|
76 |
+
|
77 |
+
For example, if you roll a `six-sided die`, the sample space would be `{1, 2, 3, 4, 5, 6}`, as these are all outcomes that can occur.
|
78 |
+
"""
|
79 |
+
)
|
80 |
+
with gr.Row():
|
81 |
+
die_roll = gr.Button(value="Roll the Die! 🎲🎲")
|
82 |
+
die_roll_output = gr.Textbox(label="You have rolled a..", placeholder="", interactive=False)
|
83 |
+
die_roll.click(roll_die, [], [die_roll_output])
|
84 |
+
|
85 |
+
gr.Markdown(
|
86 |
+
"""
|
87 |
+
## Events
|
88 |
+
|
89 |
+
An `event` is any **subset of the `sample space`**, representing a specific outcome or a combination of outcomes.
|
90 |
+
<br>
|
91 |
+
Events are events of interest or events we want to analyze in terms of their probability.
|
92 |
+
|
93 |
+
Continuing with our example, an event like `rolling an even number` would correspond to the subset: `{2, 4, 6}`
|
94 |
+
|
95 |
+
"""
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
event = gr.Button(value="Try to roll an even number! 🎲🎲")
|
100 |
+
with gr.Row():
|
101 |
+
event_die_roll_output = gr.Textbox(label="You have rolled a..", placeholder="", interactive=False)
|
102 |
+
event_test_output = gr.Textbox(label="Did the event occur?", placeholder="", interactive=False)
|
103 |
+
event.click(test_event, [], [event_die_roll_output, event_test_output])
|
104 |
+
|
105 |
+
gr.Markdown(
|
106 |
+
r"""
|
107 |
+
## Probability
|
108 |
+
|
109 |
+
The `probability` of an `event` is expressed as a **ratio between** the number of `favorable` and `possible outcomes`.
|
110 |
+
$$ P(\text{Event}) = \frac{\text{Number of (Favorable Outcomes)}}{\text{Number of (Possible Outcomes)}} $$
|
111 |
+
|
112 |
+
- `Favorable outcomes` are those that satisfy the condition of the `event`.
|
113 |
+
- `Possible outcomes` are all outcomes, regardless of whether they are part of the event or not.
|
114 |
+
|
115 |
+
You can use the space below to calculate the probability of an event.
|
116 |
+
<br>
|
117 |
+
Play around with different sets of favorable and possible outcomes to see how the probability changes.
|
118 |
+
"""
|
119 |
+
)
|
120 |
+
|
121 |
+
with gr.Column():
|
122 |
+
with gr.Row():
|
123 |
+
randomize = gr.Button(value="Randomize the favorable and possible outcomes")
|
124 |
+
probability = gr.Button(value="Calculate the probability")
|
125 |
+
with gr.Row():
|
126 |
+
with gr.Column():
|
127 |
+
favorable_outcomes = gr.Textbox(label="Favorable Outcomes", value="2, 4, 6")
|
128 |
+
possible_outcomes = gr.Textbox(label="Possible Outcomes", value="1, 2, 3, 4, 5, 6")
|
129 |
+
probability_output = gr.Textbox(label="Probability", placeholder="", interactive=False)
|
130 |
+
randomize.click(randomize_outcomes, [], [favorable_outcomes, possible_outcomes])
|
131 |
+
probability.click(compute_event_probability, [favorable_outcomes, possible_outcomes], [probability_output])
|
132 |
+
|
133 |
+
# Launch the Blocks interface
|
134 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
# This file is autogenerated by pip-compile with Python 3.10
|
3 |
+
# by the following command:
|
4 |
+
#
|
5 |
+
# pip-compile requirements.in
|
6 |
+
#
|
7 |
+
aiofiles==23.2.1
|
8 |
+
# via gradio
|
9 |
+
altair==5.2.0
|
10 |
+
# via gradio
|
11 |
+
annotated-types==0.6.0
|
12 |
+
# via pydantic
|
13 |
+
anyio==4.3.0
|
14 |
+
# via
|
15 |
+
# httpx
|
16 |
+
# starlette
|
17 |
+
attrs==23.2.0
|
18 |
+
# via
|
19 |
+
# jsonschema
|
20 |
+
# referencing
|
21 |
+
certifi==2024.2.2
|
22 |
+
# via
|
23 |
+
# httpcore
|
24 |
+
# httpx
|
25 |
+
# requests
|
26 |
+
charset-normalizer==3.3.2
|
27 |
+
# via requests
|
28 |
+
click==8.1.7
|
29 |
+
# via
|
30 |
+
# typer
|
31 |
+
# uvicorn
|
32 |
+
colorama==0.4.6
|
33 |
+
# via typer
|
34 |
+
contourpy==1.2.0
|
35 |
+
# via matplotlib
|
36 |
+
cycler==0.12.1
|
37 |
+
# via matplotlib
|
38 |
+
exceptiongroup==1.2.0
|
39 |
+
# via anyio
|
40 |
+
fastapi==0.110.0
|
41 |
+
# via gradio
|
42 |
+
ffmpy==0.3.2
|
43 |
+
# via gradio
|
44 |
+
filelock==3.13.1
|
45 |
+
# via huggingface-hub
|
46 |
+
fonttools==4.49.0
|
47 |
+
# via matplotlib
|
48 |
+
fsspec==2024.2.0
|
49 |
+
# via
|
50 |
+
# gradio-client
|
51 |
+
# huggingface-hub
|
52 |
+
gradio==4.19.2
|
53 |
+
# via -r requirements.in
|
54 |
+
gradio-client==0.10.1
|
55 |
+
# via gradio
|
56 |
+
h11==0.14.0
|
57 |
+
# via
|
58 |
+
# httpcore
|
59 |
+
# uvicorn
|
60 |
+
httpcore==1.0.4
|
61 |
+
# via httpx
|
62 |
+
httpx==0.27.0
|
63 |
+
# via
|
64 |
+
# gradio
|
65 |
+
# gradio-client
|
66 |
+
huggingface-hub==0.21.4
|
67 |
+
# via
|
68 |
+
# gradio
|
69 |
+
# gradio-client
|
70 |
+
idna==3.6
|
71 |
+
# via
|
72 |
+
# anyio
|
73 |
+
# httpx
|
74 |
+
# requests
|
75 |
+
importlib-resources==6.1.3
|
76 |
+
# via gradio
|
77 |
+
jinja2==3.1.3
|
78 |
+
# via
|
79 |
+
# altair
|
80 |
+
# gradio
|
81 |
+
jsonschema==4.21.1
|
82 |
+
# via altair
|
83 |
+
jsonschema-specifications==2023.12.1
|
84 |
+
# via jsonschema
|
85 |
+
kiwisolver==1.4.5
|
86 |
+
# via matplotlib
|
87 |
+
markdown-it-py==3.0.0
|
88 |
+
# via rich
|
89 |
+
markupsafe==2.1.5
|
90 |
+
# via
|
91 |
+
# gradio
|
92 |
+
# jinja2
|
93 |
+
matplotlib==3.8.3
|
94 |
+
# via gradio
|
95 |
+
mdurl==0.1.2
|
96 |
+
# via markdown-it-py
|
97 |
+
numpy==1.26.4
|
98 |
+
# via
|
99 |
+
# altair
|
100 |
+
# contourpy
|
101 |
+
# gradio
|
102 |
+
# matplotlib
|
103 |
+
# pandas
|
104 |
+
orjson==3.9.15
|
105 |
+
# via gradio
|
106 |
+
packaging==23.2
|
107 |
+
# via
|
108 |
+
# altair
|
109 |
+
# gradio
|
110 |
+
# gradio-client
|
111 |
+
# huggingface-hub
|
112 |
+
# matplotlib
|
113 |
+
pandas==2.2.1
|
114 |
+
# via
|
115 |
+
# altair
|
116 |
+
# gradio
|
117 |
+
pillow==10.2.0
|
118 |
+
# via
|
119 |
+
# gradio
|
120 |
+
# matplotlib
|
121 |
+
pydantic==2.6.3
|
122 |
+
# via
|
123 |
+
# fastapi
|
124 |
+
# gradio
|
125 |
+
pydantic-core==2.16.3
|
126 |
+
# via pydantic
|
127 |
+
pydub==0.25.1
|
128 |
+
# via gradio
|
129 |
+
pygments==2.17.2
|
130 |
+
# via rich
|
131 |
+
pyparsing==3.1.2
|
132 |
+
# via matplotlib
|
133 |
+
python-dateutil==2.9.0.post0
|
134 |
+
# via
|
135 |
+
# matplotlib
|
136 |
+
# pandas
|
137 |
+
python-multipart==0.0.9
|
138 |
+
# via gradio
|
139 |
+
pytz==2024.1
|
140 |
+
# via pandas
|
141 |
+
pyyaml==6.0.1
|
142 |
+
# via
|
143 |
+
# gradio
|
144 |
+
# huggingface-hub
|
145 |
+
referencing==0.33.0
|
146 |
+
# via
|
147 |
+
# jsonschema
|
148 |
+
# jsonschema-specifications
|
149 |
+
requests==2.31.0
|
150 |
+
# via huggingface-hub
|
151 |
+
rich==13.7.1
|
152 |
+
# via typer
|
153 |
+
rpds-py==0.18.0
|
154 |
+
# via
|
155 |
+
# jsonschema
|
156 |
+
# referencing
|
157 |
+
ruff==0.3.1
|
158 |
+
# via gradio
|
159 |
+
semantic-version==2.10.0
|
160 |
+
# via gradio
|
161 |
+
shellingham==1.5.4
|
162 |
+
# via typer
|
163 |
+
six==1.16.0
|
164 |
+
# via python-dateutil
|
165 |
+
sniffio==1.3.1
|
166 |
+
# via
|
167 |
+
# anyio
|
168 |
+
# httpx
|
169 |
+
starlette==0.36.3
|
170 |
+
# via fastapi
|
171 |
+
tomlkit==0.12.0
|
172 |
+
# via gradio
|
173 |
+
toolz==0.12.1
|
174 |
+
# via altair
|
175 |
+
tqdm==4.66.2
|
176 |
+
# via huggingface-hub
|
177 |
+
typer[all]==0.9.0
|
178 |
+
# via gradio
|
179 |
+
typing-extensions==4.10.0
|
180 |
+
# via
|
181 |
+
# altair
|
182 |
+
# anyio
|
183 |
+
# fastapi
|
184 |
+
# gradio
|
185 |
+
# gradio-client
|
186 |
+
# huggingface-hub
|
187 |
+
# pydantic
|
188 |
+
# pydantic-core
|
189 |
+
# typer
|
190 |
+
# uvicorn
|
191 |
+
tzdata==2024.1
|
192 |
+
# via pandas
|
193 |
+
urllib3==2.2.1
|
194 |
+
# via requests
|
195 |
+
uvicorn==0.27.1
|
196 |
+
# via gradio
|
197 |
+
websockets==11.0.3
|
198 |
+
# via gradio-client
|