Spaces:
Sleeping
Sleeping
File size: 9,042 Bytes
ce05002 38331be ce04ad4 2273894 54796ce ce05002 f80c3bc 7baf806 e0ff385 7baf806 e0ff385 77d2459 e0ff385 77d2459 e0ff385 77d2459 21cbe45 77d2459 21cbe45 77d2459 7baf806 e0ff385 38331be f80c3bc 7baf806 171a24a 6fa47f7 54796ce e0ff385 e58ed12 e811249 7f7b110 e0ff385 f80c3bc 7f7b110 df2ec4c 54796ce 23d24c0 5db5c15 23d24c0 9eb5610 ea1c73b 9eb5610 7f7b110 8f20d69 adc8c2e bd91003 ea1c73b 7f7b110 e0ff385 7f7b110 85ec3d1 f80c3bc 7f7b110 9352ede 7f7b110 f80c3bc 7f7b110 2dfb9c3 05a28b8 9352ede f80c3bc b0bc027 2dfb9c3 85ec3d1 38331be 7f7b110 85ec3d1 7f7b110 85ec3d1 9352ede 7f7b110 9352ede 85ec3d1 38331be 7f7b110 85ec3d1 38331be 85ec3d1 38331be |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
import gradio as gr
import matplotlib.pyplot as plt
from matplotlib_venn import venn2, venn3
from io import BytesIO
from PIL import Image
import pandas as pd
def validate_inputs(U, A, B, C, AB, AC, BC, ABC):
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 ({A}) no puede ser menor que la suma de A∩B y A∩C, menos A∩B∩C ({AB + AC - ABC}).")
if B < AB + BC - ABC:
errors.append(f"B ({B}) no puede ser menor que la suma de A∩B y B∩C, menos A∩B∩C ({AB + BC - ABC}).")
if C < AC + BC - ABC:
errors.append(f"C ({C}) no puede ser menor que la suma de A∩C y B∩C, menos A∩B∩C ({AC + BC - ABC}).")
if ABC > AB:
errors.append(f"A∩B∩C ({ABC}) no puede ser mayor que A∩B ({AB}).")
if ABC > AC:
errors.append(f"A∩B∩C ({ABC}) no puede ser mayor que A∩C ({AC}).")
if ABC > BC:
errors.append(f"A∩B∩C ({ABC}) no puede ser mayor que B∩C ({BC}).")
return errors
def suggest_intersections(U, A, B, C, AB, AC, BC, ABC):
union_ABC = A + B + C - AB - AC - BC + ABC
min_AB = min(A, B, ABC)
min_AC = min(A, C, ABC)
min_BC = min(B, C, ABC)
max_AB = min(A, B)
max_AC = min(A, C)
max_BC = min(B, C)
max_ABC = min(AB, AC, BC)
suggestions = {
"Mínimo valor sugerido para U": union_ABC,
"Valor sugerido para A ∩ B ": f"{min_AB} - {max_AB}",
"Valor sugerido para A ∩ C": f"{min_AC} - {max_AC}",
"Valor sugerido para B ∩ C": f"{min_BC} - {max_BC}",
"Máximo valor sugerido para A ∩ B ∩ C": max_ABC,
}
return suggestions
def calculate_probabilities(U, A, B, C, AB, AC, BC, ABC):
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
PB_given_A = P_AB / P_A if P_A > 0 else 0
PC_given_A = P_AC / P_A if P_A > 0 else 0
PC_given_B = P_BC / P_B if P_B > 0 else 0
# Cálculo de las probabilidades condicionales utilizando el teorema de Bayes
P_A_given_B_bayes = (PB_given_A * P_A) / P_B if P_B > 0 else 0
P_B_given_A_bayes = (PA_given_B * P_B) / P_A if P_A > 0 else 0
P_A_given_C_bayes = (PC_given_A * P_A) / P_C if P_C > 0 else 0
P_C_given_A_bayes = (PA_given_C * P_C) / P_A if P_A > 0 else 0
P_B_given_C_bayes = (PC_given_B * P_B) / P_C if P_C > 0 else 0
P_C_given_B_bayes = (PB_given_C * P_C) / P_B if P_B > 0 else 0
# Probabilidades de uniones
P_A_union_B = P_A + P_B - P_AB
P_A_union_C = P_A + P_C - P_AC
P_B_union_C = P_B + P_C - P_BC
P_A_union_B_union_C = P_A + P_B + P_C - P_AB - P_AC - P_BC + P_ABC
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 ∩ B) / P(B)) = ({P_AB:.4f} / {P_B:.4f}) = ((A ∩ B) / B) = ({AB} / {B})",
"P(A | C)": f"{PA_given_C:.2%} (P(A ∩ C) / P(C)) = ({P_AC:.4f} / {P_C:.4f}) = ((A ∩ C) / C) = ({AC} / {C})",
"P(B | C)": f"{PB_given_C:.2%} (P(B ∩ C) / P(C)) = ({P_BC:.4f} / {P_C:.4f}) = ((B ∩ C) / C) = ({BC} / {C})",
"P(B | A)": f"{PB_given_A:.2%} (P(B ∩ A) / P(A)) = ({P_AB:.4f} / {P_A:.4f}) = ((B ∩ A) / A) = ({AB} / {A})",
"P(C | A)": f"{PC_given_A:.2%} (P(C ∩ A) / P(A)) = ({P_AC:.4f} / {P_A:.4f}) = ((C ∩ A) / A) = ({AC} / {A})",
"P(C | B)": f"{PC_given_B:.2%} (P(C ∩ B) / P(B)) = ({P_BC:.4f} / {P_B:.4f}) = ((C ∩ B) / B) = ({BC} / {B})",
"P(A | B) (T. Bayes)": f"{P_A_given_B_bayes:.2%} (Teorema de Bayes: (P(B | A) * P(A)) / P(B)) = ({PB_given_A:.4f} * {P_A:.4f} / {P_B:.4f})",
"P(A | C) (T. Bayes)": f"{P_A_given_C_bayes:.2%} (Teorema de Bayes: (P(C | A) * P(A)) / P(C)) = ({PC_given_A:.4f} * {P_A:.4f} / {P_C:.4f})",
"P(B | C) (T. Bayes)": f"{P_B_given_C_bayes:.2%} (Teorema de Bayes: (P(C | B) * P(B)) / P(C)) = ({PC_given_B:.4f} * {P_B:.4f} / {P_C:.4f})",
"P(B | A) (T. Bayes)": f"{P_B_given_A_bayes:.2%} (Teorema de Bayes: (P(A | B) * P(B)) / P(A)) = ({PA_given_B:.4f} * {P_B:.4f} / {P_A:.4f})",
"P(C | A) (T. Bayes)": f"{P_C_given_A_bayes:.2%} (Teorema de Bayes: (P(A | C) * P(C)) / P(A)) = ({PA_given_C:.4f} * {P_C:.4f} / {P_A:.4f})",
"P(C | B) (T. Bayes)": f"{P_C_given_B_bayes:.2%} (Teorema de Bayes: (P(B | C) * P(C)) / P(B)) = ({PB_given_C:.4f} * {P_C:.4f} / {P_B:.4f})",
"P(A ∪ B)": f"{P_A_union_B:.2%} (P(A) + P(B) - P(A ∩ B)) = ({P_A:.4f} + {P_B:.4f} - {P_AB:.4f}) = (A + B - A ∩ B) / U = ({A} + {B} - {AB}) / {U}",
"P(A ∪ C)": f"{P_A_union_C:.2%} (P(A) + P(C) - P(A ∩ C)) = ({P_A:.4f} + {P_C:.4f} - {P_AC:.4f}) = (A + C - A ∩ C) / U = ({A} + {C} - {AC}) / {U} ",
"P(B ∪ C)": f"{P_B_union_C:.2%} (P(B) + P(C) - P(B ∩ C)) = ({P_B:.4f} + {P_C:.4f} - {P_BC:.4f}) = (B + C - B ∩ C) / U = ({B} + {C} - {BC}) /{U} ",
"P(A ∪ B ∪ C)": f"{P_A_union_B_union_C:.2%} (P(A) + P(B) + P(C) - P(A ∩ B) - P(A ∩ C) - P(B ∩ C) + P(A ∩ B ∩ C)) = ({P_A:.4f} + {P_B:.4f} + {P_C:.4f} - {P_AB:.4f} - {P_AC:.4f} - {P_BC:.4f} + {P_ABC:.4f})",
"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(U, A, B, C, AB, AC, BC, ABC):
plt.figure(figsize=(10,10))
venn = venn3(subsets=(max(0, A - AB - AC + ABC), max(0,B - AB - BC + ABC), max(0,AB - ABC), max(0,C- AC - BC + ABC), max(AC - ABC, 0), max(BC - ABC,0), ABC), set_labels=('A', 'B', 'C'))
img = BytesIO()
plt.title(f"Diagrama de Venn U = {U}")
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(U, A, B, C, AB, AC, BC, ABC)
if errors:
# Devolver None para la imagen y el DataFrame si hay errores
return None, pd.DataFrame({"Errores de validación": errors}), {}
venn_image = draw_venn(U, A, B, C, AB, AC, BC, ABC)
probabilities_df = calculate_probabilities(U, A, B, C, AB, AC, BC, ABC)
suggestions = suggest_intersections(U, A, B, C, AB, AC, BC, ABC)
return venn_image, probabilities_df, suggestions
def draw_venn_2(A, B, AB):
plt.figure(figsize=(10, 10))
venn = venn2(subsets=(A - AB, B - AB, AB), set_labels=('A', 'B'))
img = BytesIO()
plt.title(f"Diagrama de Venn para A y B")
plt.savefig(img, format='png')
img.seek(0)
image = Image.open(img)
return image
def main_2(A, B, AB):
venn_image = draw_venn_2(A, B, AB)
return venn_image
# Interfaz para dos conjuntos A y B
interface_2 = gr.Interface(
fn=main_2,
inputs=[
gr.Number(label="A"),
gr.Number(label="B"),
gr.Number(label="A ∩ B")
],
outputs=gr.Image(type="pil", label="Diagrama de Venn"),
title="Diagrama de Venn para dos conjuntos (A y B)",
description="Genera un diagrama de Venn para dos conjuntos A y B.",
live=True
)
# Interfaz para tres conjuntos A, B y C (usando tu código original)
interface_3 = 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.Image(type="pil", label="Diagrama de Venn"),
gr.Dataframe(label="Tabla de Probabilidades"),
gr.JSON(label="Sugerencias de Intersección")
],
title="Calculadora de Probabilidades y Diagrama de Venn (Tres conjuntos)",
description="Calcula las probabilidades, intersecciones sugeridas y genera un diagrama de Venn para tres conjuntos.",
live=True
)
# Combinar ambas interfaces en una tabbed interface
tabbed_interface = gr.TabbedInterface([interface_2, interface_3], ["Dos conjuntos (A y B)", "Tres conjuntos (A, B y C)"])
if __name__ == "__main__":
tabbed_interface.launch()
|