Spaces:
Sleeping
Sleeping
File size: 4,717 Bytes
ce05002 2baf94a ce04ad4 2273894 54796ce ce05002 e0ff385 7baf806 e0ff385 7baf806 e0ff385 e58ed12 e0ff385 e58ed12 e0ff385 e58ed12 21cbe45 7baf806 e0ff385 b0bc027 3f2b305 7baf806 7f263ab 6fa47f7 54796ce e0ff385 e58ed12 e0ff385 b0bc027 e0ff385 b0bc027 e0ff385 b0bc027 54796ce b0bc027 e0ff385 b0bc027 85ec3d1 b0bc027 3f2b305 85ec3d1 b0bc027 85ec3d1 b0bc027 85ec3d1 b0bc027 85ec3d1 b0bc027 85ec3d1 b0bc027 85ec3d1 b0bc027 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import gradio as gr
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
from io import BytesIO
from PIL import Image
import pandas as pd
def validate_inputs(A, B, C, AB, AC, BC, ABC, U):
union_ABC = A + B + C - AB - AC - BC + ABC
errors = []
if U < union_ABC:
errors.append(f"El conjunto universal U ({U}) no puede ser menor que la unión de A, B, C ({union_ABC}).")
if A < AB + AC - ABC:
errors.append(f"A no puede ser menor que A∩B + A∩C - A∩B∩C: {AB + AC - ABC}")
if B < AB + BC - ABC:
errors.append(f"B no puede ser menor que A∩B + B∩C - A∩B∩C: {AB + BC - ABC}")
if C < AC + BC - ABC:
errors.append(f"C no puede ser menor que A∩C + B∩C - A∩B∩C: {AC + BC - ABC}")
if ABC > AC:
errors.append(f"A∩B∩C no puede ser mayor que A∩C: {AC}")
if ABC > AB:
errors.append(f"A∩B∩C no puede ser mayor que A∩B: {AC}")
if ABC > BC:
errors.append(f"A∩B∩C no puede ser mayor que B∩C: {AC}")
return errors
def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
# Probabilidades individuales
P_A = A / U
P_B = B / U
P_C = C / U
# Probabilidades condicionales
P_A_given_B = AB / B
P_B_given_A = AB / A
P_A_given_C = AC / C
P_C_given_A = AC / A
P_B_given_C = BC / C
P_C_given_B = BC / B
# Probabilidad total para A, B y C
P_A_total = P_B * P_A_given_B + P_C * P_A_given_C - P_B_given_C * P_C_given_B * P_A_given_B * P_A_given_C
P_B_total = P_A * P_B_given_A + P_C * P_B_given_C - P_A_given_C * P_C_given_A * P_B_given_A * P_B_given_C
P_C_total = P_A * P_C_given_A + P_B * P_C_given_B - P_A_given_B * P_B_given_A * P_C_given_A * P_C_given_B
# Teorema de Bayes (probabilidad de A dado B, B dado A, etc.)
P_A_given_B_bayes = (P_B_given_A * P_A) / P_B
P_B_given_A_bayes = (P_A_given_B * P_B) / P_A
P_A_given_C_bayes = (P_C_given_A * P_A) / P_C
P_C_given_A_bayes = (P_A_given_C * P_C) / P_A
P_B_given_C_bayes = (P_C_given_B * P_B) / P_C
P_C_given_B_bayes = (P_B_given_C * P_C) / P_B
return {
"P(A)": P_A,
"P(B)": P_B,
"P(C)": P_C,
"P(A|B)": P_A_given_B,
"P(B|A)": P_B_given_A,
"P(A|C)": P_A_given_C,
"P(C|A)": P_C_given_A,
"P(B|C)": P_B_given_C,
"P(C|B)": P_C_given_B,
"P(A) total": P_A_total,
"P(B) total": P_B_total,
"P(C) total": P_C_total,
"P(A|B) Bayes": P_A_given_B_bayes,
"P(B|A) Bayes": P_B_given_A_bayes,
"P(A|C) Bayes": P_A_given_C_bayes,
"P(C|A) Bayes": P_C_given_A_bayes,
"P(B|C) Bayes": P_B_given_C_bayes,
"P(C|B) Bayes": P_C_given_B_bayes
}
def suggest_intersections(A, B, C, AB, AC, BC, ABC, U):
union_ABC = A + B + C - AB - AC - BC + ABC
max_AB = min(A, B, ABC)
max_AC = min(A, C, ABC)
max_BC = min(B, C, ABC)
max_ABC = min(AB, AC, BC)
suggestions = {
"Mínimo valor sugerido para U": union_ABC,
"Máximo valor sugerido para A ∩ B": max_AB,
"Máximo valor sugerido para A ∩ C": max_AC,
"Máximo valor sugerido para B ∩ C": max_BC,
"Máximo valor sugerido para A ∩ B ∩ C": max_ABC
}
return suggestions
def plot_venn(A, B, C, AB, AC, BC, ABC):
plt.figure(figsize=(8,8))
venn = venn3(subsets = (A, B, AB, C, AC, BC, ABC), set_labels=('A', 'B', 'C'))
buf = BytesIO()
plt.savefig(buf, format='png')
plt.close()
buf.seek(0)
return Image.open(buf)
def main(A, B, C, AB, AC, BC, ABC, U):
# Mostrar gráfico de Venn
venn_img = plot_venn(A, B, C, AB, AC, BC, ABC)
# Validar entradas
validation_errors = validate_inputs(A, B, C, AB, AC, BC, ABC, U)
if validation_errors:
return None, None, validation_errors
# Sugerir intersecciones
suggestions = suggest_intersections(A, B, C, AB, AC, BC, ABC, U)
# Calcular probabilidades
probabilities = calculate_probabilities(A, B, C, AB, AC, BC, ABC, U)
return venn_img, suggestions, probabilities
iface = gr.Interface(
fn=main,
inputs=[
gr.Number(label="A"),
gr.Number(label="B"),
gr.Number(label="C"),
gr.Number(label="A ∩ B"),
gr.Number(label="A ∩ C"),
gr.Number(label="B ∩ C"),
gr.Number(label="A ∩ B ∩ C"),
gr.Number(label="U")
],
outputs=[
gr.Image(type="pil"),
gr.JSON(label="Sugerencias"),
gr.JSON(label="Probabilidades")
],
title="Cálculo de Probabilidades y Teorema de Bayes con Diagramas de Venn"
)
if __name__ == "__main__":
iface.launch()
|