Alguien me puede explicar el siguiente código

import matplotlib.pyplot as plt import numpy as np import pandas as pd import scipy.stats as stats import seaborn as sns import pymc3 as pm import theano.tensor as tt from sklearn import datasets from sklearn.naive_bayes import GaussianNB from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split

%matplotlib inline

Problema de la hierba mojada

niter = 10000 # 10000 tune = 5000 # 5000

modelo = pm.Model()

with modelo: tv = [1] lluvia = pm.Bernoulli(‘lluvia’, 0.2, shape=1, testval=tv) rociador_p = pm.Deterministic(‘rociador_p’, pm.math.switch(lluvia, 0.01, 0.40)) rociador = pm.Bernoulli(‘rociador’, rociador_p, shape=1, testval=tv) hierba_mojada_p = pm.Deterministic(‘hierba_mojada_p’, pm.math.switch(lluvia, pm.math.switch(rociador, 0.99, 0.80), pm.math.switch(rociador, 0.90, 0.0))) hierba_mojada = pm.Bernoulli(‘hierba_mojada’, hierba_mojada_p, observed=np.array([1]), shape=1)

trace = pm.sample(20000,                    step=[pm.BinaryGibbsMetropolis([lluvia, rociador])],                    tune=tune, random_seed=124) 

pm.traceplot(trace)

dictionary = { ‘lluvia’: [1 if ii[0] else 0 for ii in trace[‘lluvia’].tolist() ], ‘rociador’: [1 if ii[0] else 0 for ii in trace[‘rociador’].tolist() ], ‘rociador_p’: [ii[0] for ii in trace[‘rociador_p’].tolist()], ‘hierba_mojada_p’: [ii[0] for ii in trace[‘hierba_mojada_p’].tolist()], } df = pd.DataFrame(dictionary)

p_lluvia = df[(df[‘lluvia’] == 1)].shape[0] / df.shape[0] print(“\nProbabilidad de que la hierba este mojada por la lluvia: {0}” .format(p_lluvia))

p_rociador = df[(df[‘rociador’] == 1)].shape[0] / df.shape[0] print(“Probabilidad de que la hierba este mojada por el rociador: {0}” .format(p_rociador))