$ 首先回忆一下概率计算单事件的概率为P(A),不发生的概率就是1-P(A) $
$ 两个独立事件的交集是 P(A n B) = P(A) P(B) $
$ 如果两个事件互斥,也就是同时只能发生一个,那事件不发生的概率就是 P(A) + P(B) $
$如果事件不是相互排斥的,那么,两个事件同时不反升的概率就是 P(A U B) = P(A) + P(B) - P(A n B)
总结:概率的交集用乘法概率的并集用加法$
import numpy as np
import matplotlib.pyplot as plt
def P(SET,A):
m = SET.shape[0]
return np.dot((SET==A).astype(int),np.ones(m))/np.array([m])
def PContrary(SET,A):
return 1-P(SETA,A)
def PUnin(SETList,eventList):
if len(SETList)==len(eventList):
step1 = np.array([P(sets , even) for sets , even in zip(SETList,eventList)])
return np.cumprod(step1)[-1:]
def PUninContrary(SETList,eventList):
return 1-PUnin(SETList,eventList)
def PIndependentUnin(SETAList,eventList):
if len(SETAList)==len(eventList):
m = len(SETAList)
step = np.array([P(sets,even) for sets , even in zip(SETAList,eventList)])
return np.dot(step.T,np.ones((m)))
else:
return np.nan
def PIndependentUninsContrary(SETList,eventList):
m = len(SETList)
return m-PIndependentUnin(SETList,eventList)
def P2(SETList,eventList):
return PIndependentUnin(SETList,eventList)-PUnin(SETList,eventList)
def PAandB(SETA,SETB,A,B):
step1 , step2 = (P(SETA,A),P(SETB,B))
return PUnin([SETA,SETB],A)/P(SETB,B)