This file contains hidden or 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
import math | |
increment = 0.1 | |
startingPoint = [1, 1] | |
point1 = [1,5] | |
point2 = [6,4] | |
point3 = [5,2] | |
point4 = [2,1] | |
def distance(x1, y1, x2, y2): |
This file contains hidden or 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
i=0 | |
fitness = 0 | |
counter = 0 | |
while i < max_iter: | |
if counter > n_repeats: | |
STOP | |
Y = [ ] | |
fit = [ ] | |
pairs = randompairsfrom (X) | |
for j in pairs: |
This file contains hidden or 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
try: #If running in colab | |
import google.colab | |
IN_COLAB = True | |
%tensorflow_version 2.x | |
except: | |
IN_COLAB = False | |
import tensorflow as tf | |
if (not tf.__version__.startswith('2')): #Checking if tf 2.0 is installed | |
print('Please install tensorflow 2.0 to run this notebook') |
This file contains hidden or 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
try: #If running in colab | |
import google.colab | |
IN_COLAB = True | |
%tensorflow_version 2.x | |
except: | |
IN_COLAB = False | |
import tensorflow as tf | |
if (not tf.__version__.startswith('2')): #Checking if tf 2.0 is installed | |
print('Please install tensorflow 2.0 to run this notebook') |
This file contains hidden or 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
#Solution for model assumption. Different number of dollar signs. | |
from scipy.stats import binom | |
#Define the considered numbers of dollar signs on the die (zero to six): | |
ndollar = np.asarray(np.linspace(0,6,7), dtype='int') | |
#Calculate corresponding probability of 2 $-signs in 10 throws | |
pdollar = binom.pmf(k=2, n=10, p=ndollar/6) | |
plt.stem(ndollar, pdollar) | |
plt.xlabel('Number of dollar signs on the dice') | |
plt.ylabel('Probability observing 2 dollar signs in 10 throws') | |
plt.title('Unnormalized likelihoods') |
This file contains hidden or 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
# Execute this cell to be sure to have a compatible TF (2.0) and TFP (0.8) version. | |
# If you are bold you can skip this cell. | |
try: #If running in colab | |
import google.colab | |
!pip install tensorflow==2.0.0 | |
!pip install tensorflow_probability==0.8.0 | |
except: | |
print('Not running in colab') | |
try: #If running in colab |
This file contains hidden or 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
#Reordering so x values are in increasiong order | |
order_idx_train=np.squeeze(x_train.argsort(axis=0)) | |
x_train=x_train[order_idx_train] | |
y_train=y_train[order_idx_train] | |
order_idx_val=np.squeeze(x_val.argsort(axis=0)) | |
x_val=x_val[order_idx_val] | |
y_val=y_val[order_idx_val] | |
order_idx_test=np.squeeze(x_test.argsort(axis=0)) |
This file contains hidden or 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
try: #If running in colab | |
import google.colab | |
IN_COLAB = True | |
%tensorflow_version 2.x | |
except: | |
IN_COLAB = False | |
import tensorflow as tf | |
if (not tf.__version__.startswith('2')): #Checking if tf 2.0 is installed | |
print('Please install tensorflow 2.0 to run this notebook') |
This file contains hidden or 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
theta=np.arange(0.05,1,0.05) | |
print(theta) | |
prior = 1/len(theta) #The normalizing constant of the prior | |
#Evaluate joint likelihood and unnormalized posterior at one specific #$\theta = 0.5$ | |
dist = tfp.distributions.Bernoulli(probs=0.5) #one specific theta | |
print(np.prod(dist.prob(obs_data))) #joint likelihood | |
print(np.prod(dist.prob(obs_data))*prior) #unnormalized posterior | |
#Repeat the process for all thetas, range 0.05 - 0.95 |
This file contains hidden or 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
py1_post = np.sum((res["theta"])*res["post"]) | |
py0_post = 1.0 - py1_post | |
py0_post, py1_post | |
py1_prior = np.sum((res["theta"])*res["prior"]) | |
py0_prior = 1 - py1_prior | |
py0_prior, py1_prior | |
#Plot posterior and prior | |
plt.figure(figsize=(16,12)) |