首页 > 解决方案 > 创建具有多个标准的维恩图

问题描述

感谢您阅读我的问题。我完全是 python 的初学者,英语不是我的主要语言。如果你看不懂我的英语,请询问。谢谢您的帮助。

这是问题。我想使用这些标准创建一个维恩图:

  1. 10*P(B) = P(A)
  2. P(C and notA) = 13 * P(B and notC and notA)
  3. P(B 和 C 和 notA) = 5 * P(B 和 C 和 A)
  4. P(B 和 C) = 0.18
  5. P(B 或 C 而不是 A) = 0.07
  6. P(非C) = 0.922 -> P(C) = 0.078
  7. P(B 和 A) = 0.01

我尝试使用“随机”,但我认为这样做很愚蠢。这是我的代码,但如果您有更好的解决方案,请告诉我。

import random
# a = lowgrade fever
# b = headace
# c = Muscle ache
def haha():
    oa = random.random()
    ob = random.random()
    oc = random.random()

    abnc = random.random()
    acnb = random.random()
    bcna = random.random()

    abc = random.random()

    a = oa + abnc + acnb + abc
    b = ob + abnc + bcna + abc
    c = oc + acnb + bcna + abc

    ab = abnc + abc
    ac = acnb + abc
    bc = bcna + abc

    # oa + ob + oc + abnc + acnb+ bcna + abc = 1

    if (10 * b == a):
        c1 = True
    if ((oc + bcna) == 13 * ob):
        c2 = True
    if ((ob + bcna + oc) == 5*abc):
        c3 = True
    if ((bcna + abc) == 0.018):
        c4 = True
    if(ob + bcna + oc == 0.07):
        c5 = True
    if(1 - oc + bcna + abc + acnb == 0.922):
        c6 = True
    if(abnc + abc == 0.01):
        c7 = True

    if (c1 and c2 and c3 and c4 and c5 and c6 and c7):
        allc = True

    if allc:
        print(oa)
        print(ob)
        print(oc)
        print(bcna)
        print(acnb)
        print(abnc)
        print(abc)

    return allc

haha()
while (allc == False):
    haha()

感谢你们。

标签: pythonvenn-diagram

解决方案


您正在寻找 8 种不同的概率。其中~A 表示不是 A。这些概率是我们问题的未知数。

P( A &  B &  C) [1]
P( A &  B & ~C) [2]
P( A & ~B &  C) [3]
P( A & ~B & ~C) [4]
P(~A &  B &  C) [5]
P(~A & ~B &  C) [6]
P(~A & ~B & ~C) [7]
P(~A &  B & ~C) [8]

第一步是在上面写的变量中写下你的方程。我们将[1]用作简写P(A & B & C)

<=> 10P(B) - P(A)=0
<=> 10*([1] + [2] + [5] + [8]) - 1*(([1] + [2] + [3] + [4]) = 0
<=> 9*[1] + 9*[2] + -1*[3]-1*[4] + 10*[5] + 0*[6] + 0*[7] + 10*[8] = 0

我们可以对所有 7 个方程执行此操作。在线性代数中,这被写成一个系统Ax = b。如果您有 8 个未知变量,您可能还记得需要 8 个方程。要获得完整的解决方案,您必须找到一个额外的方程。(提示:所有概率的总和是多少?)

要解决python中的练习可以使用以下代码。

import numpy as np

A = np.array([
   [9, 9, -1, -1, 10, 0, 0, 10],  # 10P(B) = P(A)
   ...                            # Insert 7 more equations here
])

b = np.array([0, ....])           # Insert 7 more numbers here

x = np.linalg.solve(A,b)

print(x)

推荐阅读