Spaces:
Sleeping
Sleeping
File size: 4,645 Bytes
ce05002 2baf94a ce04ad4 2273894 54796ce ce05002 e0ff385 7baf806 e0ff385 7baf806 e0ff385 7baf806 e0ff385 3f2b305 7baf806 e0ff385 54796ce 7baf806 21d8dc9 48aeae8 54796ce e0ff385 7baf806 54796ce e0ff385 54796ce e0ff385 54796ce e0ff385 54796ce e0ff385 e5d5105 6fd93c8 e5d5105 54796ce e0ff385 54796ce 6fd93c8 e0ff385 54796ce 85ec3d1 115272a 85ec3d1 7baf806 85ec3d1 3f2b305 85ec3d1 115272a 327f38a 115272a 85ec3d1 327f38a 85ec3d1 e2055f4 115272a 85ec3d1 |
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 |
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("A no puede ser menor que la suma de AB y AC menos ABC.")
if B < AB + BC - ABC:
errors.append("B no puede ser menor que la suma de AB y BC menos ABC.")
if C < AC + BC - ABC:
errors.append("C no puede ser menor que la suma de AC y BC menos ABC.")
return errors
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, U - (A + B + C - A - B))
max_AC = min(A, C, U - (A + B + C - A - C))
max_BC = min(B, C, U - (A + B + C - B - C))
max_ABC = min(max_AB, max_AC, max_BC)
# Los valores mínimos deben ser >= 0
min_AB = max(0, AB)
min_AC = max(0, AC)
min_BC = max(0, BC)
min_ABC = max(0, ABC)
suggestions = {
"Máximo valor sugerido para U": union_ABC,
"Mínimo valor sugerido para A ∩ B": min_AB,
"Máximo valor sugerido para A ∩ B": max_AB,
"Mínimo valor sugerido para A ∩ C": min_AC,
"Máximo valor sugerido para A ∩ C": max_AC,
"Mínimo valor sugerido para B ∩ C": min_BC,
"Máximo valor sugerido para B ∩ C": max_BC,
"Mínimo valor sugerido para A ∩ B ∩ C": min_ABC,
"Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
}
return suggestions
def calculate_probabilities(A, B, C, AB, AC, BC, ABC, U):
total = U if U > 0 else (A + B + C - AB - AC - BC + ABC)
if total == 0:
return {
"P(A)": 0,
"P(B)": 0,
"P(C)": 0,
"P(A ∩ B)": 0,
"P(A ∩ C)": 0,
"P(B ∩ C)": 0,
"P(A ∩ B ∩ C)": 0,
}
P_A = A / total
P_B = B / total
P_C = C / total
P_AB = AB / total
P_AC = AC / total
P_BC = BC / total
P_ABC = ABC / total
PA_given_B = P_AB / P_B if P_B > 0 else 0
PA_given_C = P_AC / P_C if P_C > 0 else 0
PB_given_C = P_BC / P_C if P_C > 0 else 0
formatted_probs = {
"P(A)": f"{P_A:.2%} ({A}/{total})",
"P(B)": f"{P_B:.2%} ({B}/{total})",
"P(C)": f"{P_C:.2%} ({C}/{total})",
"P(A ∩ B)": f"{P_AB:.2%} ({AB}/{total})",
"P(A ∩ C)": f"{P_AC:.2%} ({AC}/{total})",
"P(B ∩ C)": f"{P_BC:.2%} ({BC}/{total})",
"P(A ∩ B ∩ C)": f"{P_ABC:.2%} ({ABC}/{total})",
"P(A | B)": f"{PA_given_B:.2%}",
"P(A | C)": f"{PA_given_C:.2%}",
"P(B | C)": f"{PB_given_C:.2%}",
"U (Universal Set)": total,
"Complemento de A U B U C": U - (A + B + C - AB - AC - BC + ABC)
}
# Convert to DataFrame for better visualization
df = pd.DataFrame(list(formatted_probs.items()), columns=["Descripción", "Valor"])
return df
def draw_venn(A, B, C, AB, AC, BC, ABC):
plt.figure(figsize=(10,10))
venn = venn3(subsets=(A, B, AB, C, AC, BC, ABC), set_labels=('A', 'B', 'C'))
img = BytesIO()
plt.savefig(img, format='png')
img.seek(0)
image = Image.open(img)
return image
def main(U, A, B, C, AB, AC, BC, ABC):
errors = validate_inputs(A, B, C, AB, AC, BC, ABC, U)
if errors:
return {"Errores de validación": errors}, None, None
suggestions = suggest_intersections(A, B, C, AB, AC, BC, ABC, U)
probabilities_df = calculate_probabilities(A, B, C, AB, AC, BC, ABC, U)
venn_image = draw_venn(A, B, C, AB, AC, BC, ABC)
return suggestions, probabilities_df, venn_image
# Gradio Interface
interface = gr.Interface(
fn=main,
inputs=[
gr.Number(label="U (Universal Set)"),
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")
],
outputs=[
gr.JSON(label="Sugerencias de Intersección"),
gr.Dataframe(label="Tabla de Probabilidades"),
gr.Image(type="pil", label="Diagrama de Venn")
],
title="Calculadora de Probabilidades y Diagrama de Venn",
description="Calcula las probabilidades, intersecciones sugeridas y genera un diagrama de Venn.",
live=True
)
if __name__ == "__main__":
interface.launch()
|