Created
February 7, 2019 21:15
-
-
Save rcshubhadeep/c0a436245ce94151898d04e192703f27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0.16666666666666666,\n", | |
" 0.16666666666666666,\n", | |
" 0.16666666666666666,\n", | |
" 0.16666666666666666,\n", | |
" 0.16666666666666666,\n", | |
" 0.16666666666666666]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#import important libs\n", | |
"import tensorflow as tf\n", | |
"import tensorflow_probability as tfp\n", | |
"import numpy as np\n", | |
"# Set the mode to eager execution\n", | |
"tf.enable_eager_execution()\n", | |
"\n", | |
"# We have a fair die, so the probability of each side is equally likely\n", | |
"probs = [1/6.] * 6\n", | |
"probs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# This is where we are going to store the final result of each experiment\n", | |
"var_arr = []" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# We do the same experiment 100 times\n", | |
"for k in range(0, 100):\n", | |
" # Each time we throw our fair die 1000 times and record how many times each side is shown\n", | |
" dices = tfp.distributions.Multinomial(total_count=1000, probs=probs)\n", | |
" n = dices.sample()\n", | |
" # Then conver the tf.tensor to a numpy array\n", | |
" l = n.numpy()\n", | |
" L = []\n", | |
" # For each side of the die\n", | |
" for i, nums in enumerate(l):\n", | |
" # For how many times that side has turned up\n", | |
" for _ in range(int(nums)):\n", | |
" # if it is an odd number fase of the die (1, 3, 5) then toss the fair coin 3 times else 2 times\n", | |
" mul_by = 3 if (i + 1) % 2 != 0 else 2\n", | |
" # It is a fair coin thus each side is equally probable\n", | |
" tosses = tfp.distributions.Bernoulli(probs=0.5)\n", | |
" coin_flip_data = tosses.sample(mul_by)\n", | |
" l2 = coin_flip_data.numpy()\n", | |
" # How many times head turned up (assuming HEAD = 1)\n", | |
" num_heads = np.sum(l2)\n", | |
" L += [num_heads]\n", | |
" # Calculate the variance and store it\n", | |
" var_arr.append(np.var(L))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.68355216" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.mean(var_arr)" | |
] | |
} | |
], | |
"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.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment