File size: 3,238 Bytes
ce05002
 
2baf94a
ce04ad4
2273894
54796ce
ce05002
e0ff385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54796ce
 
 
 
 
 
e0ff385
54796ce
e0ff385
54796ce
e0ff385
54796ce
e0ff385
54796ce
e0ff385
 
 
 
e5d5105
6fd93c8
 
 
 
 
 
 
 
 
 
 
 
e5d5105
 
 
 
 
 
 
54796ce
 
 
 
 
e0ff385
 
 
 
 
 
 
 
54796ce
 
 
 
 
6fd93c8
e0ff385
54796ce
 
 
f04f86a
54796ce
 
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
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):
    errors = []
    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.")
    if U < A + B + C - AB - AC - BC + ABC:
        errors.append("El conjunto universal U es menor que la suma total de los conjuntos y sus intersecciones.")
    return errors

def suggest_intersections(A, B, C, U):
    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)

    min_AB = max(0, A + B + C - A - B - C + ABC - U)
    min_AC = max(0, A + C + B - A - C - B + ABC - U)
    min_BC = max(0, B + C + A - B - C - A + ABC - U)
    min_ABC = max(0, ABC - (A + B + C - AB - AC - BC + ABC))

    suggestions = {
        "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

# This function should be integrated with the main part of the app or interface
# It could be connected to a gradio UI, for example, to allow interactive use