Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mccurcio/e06f60efddbde9f8dcc1e48354c179b3 to your computer and use it in GitHub Desktop.
Save mccurcio/e06f60efddbde9f8dcc1e48354c179b3 to your computer and use it in GitHub Desktop.
Analyzing a UNFAIR Coin
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "840e59e6",
"metadata": {},
"source": [
"## Probability Of An Unfair Coin\n",
"\n",
"Let's say, we have a coin that is **biased** in such a way to generate **5 heads for every 2 tails**\n",
"\n",
"### Investigte Sample Space\n",
"\n",
"1. The sample space of an FAIR or UNFAIR coin is the set of all the possible outcomes. \n",
"\n",
"- With a **Fair coin**, the coin will land on either heads or tails. Thus, the coin flip will produce one of two measurable outcomes: _Heads_ or _Tails_.\n",
"\n",
"- With a sample space: *sample_space = {'H', 'T'}*\n",
"\n",
"2. With an **UNFAIR coin** the sample space (can?) be changed to relfect the biased outcome. \n",
"\n",
"- For example, If a coin were **biased** in such a way to generate **5 heads for every 2 tails**, we should be able to change the sample space to reflect that. **NO?**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ef1364db",
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a sample space of UNFAIR Coin-Flips\n",
"\n",
"sample_space = ['H','H','H','H','H','T','T'] # 5 to 2 odds\n",
"\n",
"len(sample_space)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "674a0f40",
"metadata": {},
"source": [
"The outcome of Heads in our `sample_space` should produce\n",
"\n",
"Probability('H') = (Count when head occurs) / len(sample_space)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f4ea37cf",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pr(H) = 0.7142857142857143\n"
]
}
],
"source": [
"# Computing the probability of heads\n",
"prob_head = sample_space.count('H') / len(sample_space)\n",
"\n",
"print('Pr(H) = ', prob_head)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e51486a7-398d-46e2-9236-95867064fe52",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pr(T) = 0.2857142857142857\n"
]
}
],
"source": [
"# Computing the probability of tails\n",
"prob_tail = sample_space.count('T') / len(sample_space)\n",
"\n",
"print('Pr(T) = ', prob_tail)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f71d6cf4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pr(H): 0.7142857142857143 + Pr(T): 0.2857142857142857 = 1.0\n"
]
}
],
"source": [
"print('Pr(H):', prob_head, '+', 'Pr(T):', prob_tail, '=', prob_head+prob_tail)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c5e4ee7c",
"metadata": {},
"source": [
"- BUT, we’ll need to define the concept of an *event*. \n",
"\n",
"- Where an event is the subset of those elements within sample_space that satisfy some event condition. \n",
"\n",
"- An *event condition* is a simple Boolean function whose input is a single sample_space element. \n",
"\n",
"**Define All EVENT CONDITIONS** \n",
"\n",
"- **NOTE**: This should not change for a fair coin and an unfair coin. NO?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f67edbcd",
"metadata": {
"trusted": true
},
"outputs": [],
"source": [
"# Condition 1\n",
"def is_heads(outcome):\n",
" return outcome == 'H'\n",
"\n",
"# Condition 2\n",
"def is_tails(outcome):\n",
" return outcome == 'T'\n",
"\n",
"# Condition 3\n",
"def is_hd_or_tl(outcome):\n",
" return outcome in {'H', 'T'}\n",
"\n",
"# Condition 4\n",
"def is_neither(outcome):\n",
" return not is_hd_or_tl(outcome)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6e7bd8cc",
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_hd_or_tl('H') # Same for tail=>True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "090bf4a0",
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_neither('A') "
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "81e2c338",
"metadata": {},
"source": [
"- *For the sake of being complete*, let's define event conditions for the two events where the coin satisfies exactly **one of our four potential outcomes**.\n",
"\n",
"- We can pass event conditions into a generalized `get_matching_event` function. The function iterates through our defined `sample_space` and returns the set (OR should this be a list?) of outcomes where `event_condition(outcome)` is `True`.\n",
"\n",
"**Defining an event detection function**\n",
"\n",
"- Our list comprehension of outcomes =\n",
" - *[outcome for outcome in sample_space if event_condition(outcome)]*\n",
"\n",
"time: 27:30"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "97a65249",
"metadata": {
"trusted": true
},
"outputs": [],
"source": [
"def get_matching_event(event_condition, sample_space):\n",
" return [outcome for outcome in sample_space if event_condition(outcome)]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a7882218",
"metadata": {},
"source": [
"- Execute *get_matching_event* on all 4 event conditions.\n",
"\n",
"**At mark 30:30, he discusses strategy to find prob of finding 21 for six rolled dices.**\n",
"\n",
"**Listing & Detecting events using event conditions**\n",
"\n",
"Find all the events that occur\n",
"\n",
"time: 34:34"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "88a00059",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Event Condition: is_hd_or_tl\n",
"Event: ['H', 'H', 'H', 'H', 'H', 'T', 'T']\n",
"\n",
"Event Condition: is_neither\n",
"Event: []\n",
"\n",
"Event Condition: is_heads\n",
"Event: ['H', 'H', 'H', 'H', 'H']\n",
"\n",
"Event Condition: is_tails\n",
"Event: ['T', 'T']\n",
"\n"
]
}
],
"source": [
"# In TOTAL, there are 4 conditions that can occur.\n",
"event_conditions = [is_hd_or_tl, is_neither, is_heads, is_tails]\n",
"\n",
"# NOW we can test for any one of these conditions\n",
"for event_condition in event_conditions:\n",
" print(f'Event Condition: {event_condition.__name__}')\n",
" event = get_matching_event(event_condition, sample_space)\n",
" print(f'Event: {event}\\n')\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c5ccfb24",
"metadata": {},
"source": [
"Now we are ready to calculate Prob of events by: \n",
"\n",
"Pr = $\\Large\\frac{len(event)}{len(sample~space)}$\n",
"\n",
"**Computing Event Probabilities** \n",
"\n",
"time 37 min"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "12aa6142-93e7-4353-95f6-aca5f18922eb",
"metadata": {
"trusted": true
},
"outputs": [],
"source": [
"def compute_prob(event_condition, sample_space):\n",
" event = get_matching_event(event_condition, sample_space)\n",
" return len(event)/len(sample_space)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ebbfbe47-9dc2-4d58-a223-0b09f20e347b",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Probability of event for is_hd_or_tl: 1.0\n",
"Probability of event for is_neither: 0.0\n",
"Probability of event for is_heads: 0.7142857142857143\n",
"Probability of event for is_tails: 0.2857142857142857\n"
]
}
],
"source": [
"for event_condition in event_conditions:\n",
" prob = compute_prob(event_condition, sample_space)\n",
" name = event_condition.__name__\n",
" print(f'Probability of event for {name}: {prob}')\n",
" \n",
"# see lecture time = 41:54 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2852f44c-212e-49fd-ba15-7aeb91fab1bd",
"metadata": {
"trusted": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/e06f60efddbde9f8dcc1e48354c179b3"
},
"gist": {
"data": {
"description": "Analyzing a UNFAIR Coin",
"public": true
},
"id": "e06f60efddbde9f8dcc1e48354c179b3"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment