213 lines
6.8 KiB
Plaintext
213 lines
6.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"### Solution for Bonus Assignment 3 of the course \"Introduction to Machine Learning\" at the University of Leoben.\n",
|
|
"##### Author: Fotios Lygerakis\n",
|
|
"##### Semester: SS 2022/2023"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Bonus Exercise: Bayes' Rule\n",
|
|
"### Description\n",
|
|
"In this exercise, you will implement a game that uses Bayes' rule to calculate the posterior probability of a card being red or black. The game will be played with a deck of four cards, two red and two black. The player will draw a card from the deck and the game will calculate the posterior probability of the other card being red or black. The player will continue to draw cards until the deck is empty."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"outputs": [],
|
|
"source": [
|
|
"import random"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"outputs": [],
|
|
"source": [
|
|
"class CardDeck:\n",
|
|
" def __init__(self):\n",
|
|
" self.cards = ['R', 'R', 'B', 'B']\n",
|
|
" random.shuffle(self.cards)\n",
|
|
" self.prior_red = 0.5\n",
|
|
" self.likelihood = 0.5\n",
|
|
"\n",
|
|
"\n",
|
|
" def draw_card(self, index):\n",
|
|
" card = self.cards.pop(index)\n",
|
|
" color = 'Red' if card == 'R' else 'Black'\n",
|
|
" return (card, color)\n",
|
|
"\n",
|
|
" def get_card_color(self, index):\n",
|
|
" card = self.cards[index]\n",
|
|
" color = 'Red' if card == 'R' else 'Black'\n",
|
|
" return color\n",
|
|
"\n",
|
|
" def get_remainder(self):\n",
|
|
" return self.cards\n",
|
|
"\n",
|
|
" \"\"\"\n",
|
|
" Update the prior probability based on Bayes' rule\n",
|
|
" function name: update_probabilities\n",
|
|
" \"\"\"\n",
|
|
" def update_probabilities(self, prior_probability, likelihood, marginal_likelihood):\n",
|
|
" posterior_probability = (likelihood * prior_probability) / marginal_likelihood\n",
|
|
" return posterior_probability\n",
|
|
"\n",
|
|
" \"\"\"\n",
|
|
" Calculate the posterior probability based on the prior probability, likelihood, and marginal likelihood\n",
|
|
" function name: get_posterior\n",
|
|
" \"\"\"\n",
|
|
" def get_posterior(self, likelihood, marginal_likelihood):\n",
|
|
" posterior_red = self.update_probabilities(self.prior_red, likelihood, marginal_likelihood)\n",
|
|
" posterior_black = 1 - posterior_red\n",
|
|
" self.prior_red = posterior_red\n",
|
|
" return (posterior_red, posterior_black)\n",
|
|
"\n",
|
|
" def play_game(self):\n",
|
|
" while self.cards:\n",
|
|
" for i, card in enumerate(self.cards):\n",
|
|
" print(f'{i}: Unknown')\n",
|
|
" # only one card left\n",
|
|
" if len(self.cards) == 1:\n",
|
|
" likelihood = 1\n",
|
|
" marginal_likelihood = 1\n",
|
|
" self.cards.pop()\n",
|
|
" else:\n",
|
|
" index = int(input(f\"Remainder deck: {self.get_remainder()}\\nEnter the index of the card you want to draw:\"))\n",
|
|
" card, color = self.draw_card(index-1)\n",
|
|
" print(f'You drew a {color} card with value {card}')\n",
|
|
"\n",
|
|
" # Calculate the likelihood and marginal likelihood based on the color of the drawn card\n",
|
|
" if color == 'Red':\n",
|
|
" likelihood = self.cards.count('R') / len(self.cards)\n",
|
|
" marginal_likelihood = 0.5\n",
|
|
" elif color == 'Black':\n",
|
|
" likelihood = self.cards.count('B') / len(self.cards)\n",
|
|
" marginal_likelihood = 0.5\n",
|
|
" else:\n",
|
|
" print(\"Invalid input. Please enter 'r' or 'b'.\")\n",
|
|
" continue\n",
|
|
" # Update the prior probability based on Bayes' rule\n",
|
|
" posterior_red, posterior_black = self.get_posterior(likelihood, marginal_likelihood)\n",
|
|
" self.prior_red = posterior_red\n",
|
|
"\n",
|
|
" # Print the posterior probabilities\n",
|
|
" print(f\"After drawing a {card} card:\")\n",
|
|
" print(f\"The probability that the other card is red: {posterior_red:.2f}\")\n",
|
|
" print(f\"The probability that the other card is black: {posterior_black:.2f}\")\n",
|
|
" print(\"The deck is empty.\")\n",
|
|
"\n",
|
|
"\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"play the game by running the following code:\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0: Unknown\n",
|
|
"1: Unknown\n",
|
|
"2: Unknown\n",
|
|
"3: Unknown\n",
|
|
"You drew a Red card with value R\n",
|
|
"After drawing a R card:\n",
|
|
"The probability that the other card is red: 0.33\n",
|
|
"The probability that the other card is black: 0.67\n",
|
|
"0: Unknown\n",
|
|
"1: Unknown\n",
|
|
"2: Unknown\n",
|
|
"You drew a Black card with value B\n",
|
|
"After drawing a B card:\n",
|
|
"The probability that the other card is red: 0.33\n",
|
|
"The probability that the other card is black: 0.67\n",
|
|
"0: Unknown\n",
|
|
"1: Unknown\n",
|
|
"You drew a Red card with value R\n",
|
|
"After drawing a R card:\n",
|
|
"The probability that the other card is red: 0.00\n",
|
|
"The probability that the other card is black: 1.00\n",
|
|
"0: Unknown\n",
|
|
"After drawing a B card:\n",
|
|
"The probability that the other card is red: 0.00\n",
|
|
"The probability that the other card is black: 1.00\n",
|
|
"The deck is empty.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"deck = CardDeck()\n",
|
|
"deck.play_game()\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"outputs": [],
|
|
"source": [],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|