Skip to content

Instantly share code, notes, and snippets.

@catethos
Created December 21, 2021 07:04
Show Gist options
  • Save catethos/dc2fff3803efefc41281d82e8524ff6a to your computer and use it in GitHub Desktop.
Save catethos/dc2fff3803efefc41281d82e8524ff6a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "37782a97-d2df-485d-9046-40e20d06338c",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Classes to represent Probability Space"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "0a3343c2-04e9-410e-8b32-c67509d3a994",
"metadata": {},
"outputs": [],
"source": [
"class Event:\n",
" def __init__(self, pred, condition=None):\n",
" self.pred = pred\n",
" self.condition = condition\n",
" \n",
" def __or__(self, pred):\n",
" return Event(self.pred, pred.pred)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "f44d0c58-abf2-4de7-8832-dee17f010c16",
"metadata": {},
"outputs": [],
"source": [
"class Probability:\n",
" def __init__(self, sample_space):\n",
" self.sample_space = sample_space\n",
" \n",
" def __call__(self, event):\n",
" if event.condition:\n",
" num = len({x for x in self.sample_space if event.pred(x) and event.condition(x)})\n",
" den = len({x for x in self.sample_space if event.condition(x)})\n",
" return num / den\n",
" else:\n",
" num = len({x for x in self.sample_space if event.pred(x)})\n",
" den = len(self.sample_space)\n",
" return num/den"
]
},
{
"cell_type": "markdown",
"id": "d55d994c-ff72-423d-b9dd-a395631d8e0a",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Sample Usecases"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "f1e316fb-03af-47e1-aeb8-a616d5cf811d",
"metadata": {},
"outputs": [],
"source": [
"# dice has 6 outcomes\n",
"sample_space = {1,2,3,4,5,6}\n",
"p = Probability(sample_space)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "7768fad2-a5c4-43fa-b283-75bfe260a4df",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# to find the probability of even numbers\n",
"is_even = Event(lambda x : x%2 == 0)\n",
"p(is_even)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "b3c6f26a-2ff4-4bc1-acbf-aad61cee910d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6666666666666666"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# to find the probability of even numbers given it is > 3\n",
"greater_3 = Event(lambda x : x>3)\n",
"p(is_even | greater_3) # 2/3"
]
},
{
"cell_type": "markdown",
"id": "397fea1c-389a-459f-aea7-e8f3035320cb",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Questions 1, 2"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "4b2e7eee-5ee0-4c83-9294-e728fcefe1c7",
"metadata": {},
"outputs": [],
"source": [
"sample_space = {\"GG\",\"GB\",\"BG\",\"BB\"}\n",
"p = Probability(sample_space1)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "d27a8988-5a86-4491-a2d0-5a9d3c2729a5",
"metadata": {},
"outputs": [],
"source": [
"older_is_boy = Event(lambda x: x[0] == \"B\")\n",
"at_least_one_boy = Event(lambda x: \"B\" in x)\n",
"both_boy = Event(lambda x: x.count(\"B\") == 2)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "8aaa70bf-3ea1-4327-af63-322c62621afb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p(both_boy | older_is_boy)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "c1102f00-3ff2-412c-a330-712b8c942dac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3333333333333333"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p(both_boy | at_least_one_boy)"
]
},
{
"cell_type": "markdown",
"id": "5950521a-68a0-4599-9fa1-610865b4846f",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"# Question 3"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "655f3c36-53a0-40e1-a1b5-4fc08b620986",
"metadata": {},
"outputs": [],
"source": [
"sample_space = {f\"{c1}{d1}{c2}{d2}\" \n",
" for c1 in [\"B\",\"G\"] for d1 in [1,2,3,4,5,6,7]\n",
" for c2 in [\"B\",\"G\"] for d2 in [1,2,3,4,5,6,7]}\n",
"p2 = Probability(sample_space) "
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "d071b382-d4f7-44f7-8426-105b257d7fc9",
"metadata": {},
"outputs": [],
"source": [
"at_least_one_on_tues = Event(lambda x: \"B2\" in x)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "1222da4b-9e56-4762-ae95-9e895e88f54d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.48148148148148145"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p2(both_boy | at_least_one_on_tues)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "bayesian",
"language": "python",
"name": "bayesian"
},
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment