Skip to content

Instantly share code, notes, and snippets.

@willkurt
Last active September 16, 2021 17:25
Show Gist options
  • Save willkurt/6c209c14bf777470543393302d1b523c to your computer and use it in GitHub Desktop.
Save willkurt/6c209c14bf777470543393302d1b523c to your computer and use it in GitHub Desktop.
Implementing probability as logic using Python's data model methods
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Probability as logic using Python's data model\n",
"\n",
"In this short notebook we'll show how probability really is just an extension of boolean logic. We'll make a class `P`, that uses Python's data model methods to implement a working example of probability as logic using `-`,`&` and `|`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class P:\n",
" \"\"\"\n",
" Example of Probability as logic using Python's data model\n",
" In this simple example these probabilites are assumed to \n",
" be conditionally independent.\n",
" \"\"\"\n",
" def __init__(self,prob):\n",
" assert prob >= 0, \"probabilities can't be negative!\" \n",
" assert prob <= 1, \"probabilities can't be great than 1!\"\n",
" self.prob = prob\n",
" \n",
" def __repr__(self):\n",
" return \"P({})\".format(self.prob)\n",
"\n",
" def __neg__(self):\n",
" return P(1-self.prob)\n",
" \n",
" def __and__(self,P2):\n",
" return P(self.prob * P2.prob)\n",
" \n",
" def __or__(self,P2):\n",
" return P(self.prob + P2.prob - (self & P2).prob)\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can then use this to work out probabilities of various events happening using python!\n",
"\n",
"Suppose, for example, you know that there is a 0.3 probability of rain tomorrow and you'll get rained of if forget your umbrella or your umbrella is broken. Then let's say you forget your umbrella with a probability 0.1 and you think your umbrella might be the broken, we'll give that a probability of 0.7. \n",
"\n",
"Now let's use logic to answer: What's the probability you will *not* get wet?\n",
"\n",
"Let's start with our facts:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"rain = P(0.3)\n",
"forget = P(0.1)\n",
"broken = P(0.7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The probability of being `wet` is:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"wet = rain & (forget | broken)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and logically the probability of being dry is:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"P(0.781)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-wet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With probability as logic, no matter how complicated our reasoning, we can know trivially code up a solution in Python!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment