[Probability] Conditional probability (2)

A new observation that is consistent with an existing belief could make us more sure of that belief, while a surprising observation could throw that belief into question.

Conditional probability is the concept that addresses this fundamental question: how should we update our beliefs in light of the evidence we observe?

2.1 The importance of thinking conditionally

In fact, a useful perspective is that all probabilities are conditional; whether or not it’s written explicitly, there is always background knowledge (or assumptions) built into every probability.

Definition 2.2.1

(Conditional probability). If A and B are events with P(B) > 0, then the conditional probability of A given B, denoted by P(A|B), is defined as   P(A|B)= \frac{P(A\cap B)}{P(B)}

We call P(A) the prior probability of A and P(A|B) the posterior probability of A (“prior” means before updating based on the evidence, and “posterior” means after updating based on the evidence)

P(A|B) is the probability of A given the evidence B, not the probability of some entity called A|B. As discussed, there is no such event as A|B.

For any event A, P(A|A) = P(A ∩ A)/P(A) = 1.

Intuition 2.2 (Pebble World).

Consider a finite sample space, with the outcomes visualized as pebbles with total mass 1. Since A is an event, it is a set of pebbles, and likewise for B. Figure 2.1(a) shows an example.

Intuition 2.2 (Frequentist interpretation).

Recall that the frequentist interpretation of probability is based on relative frequency over a large number of repeated trials. Imagine repeating our experiment many times, generating a long list of observed outcomes. The conditional probability of A given B can then be thought of in a natural way: it is the fraction of times that A occurs, restricting attention to the trials where B occurs

(Two children Problem)

[Probability] Conditional Probability-CSDN博客

2.3 Bayes’ rule and the law of total probability

Theorem 2.3.1 (Probability of the intersection of two events).

For any events A and B with positive probabilities, P(A ∩ B) = P(B)P(A|B) = P(A)P(B|A).

Theorem 2.3.2 (Probability of the intersection of n events).

Theorem 2.3.3 (Bayes’ rule).

P(A|B) =\frac{P(B|A)P(A)}{P(B)}

Another way to write Bayes’ rule is in terms of odds(赔率) rather than probability

Definition 2.3.4 (Odds).

The odds of an event A are   odds(A) = \frac{P(A)}{P(A^c)}

For example, if P(A) = 2/3, we say the odds in favor of A are 2 to 1. (This is sometimes written as 2 : 1, and is sometimes stated as 1 to 2 odds against A; care is needed since some sources do not explicitly state whether they are referring to odds in favor or odds against an event.) Of course we can also convert from odds back to probability:  P(A) = \frac{odds(A)}{(1 + odds(A))}

Theorem 2.3.5 (Odds form of Bayes’ rule).

For any events A and B with positive probabilities, the odds of A after conditioning on B are\frac{P(A|B)}{P(A^c|B)}= (\frac{P(B|A)}{P(B|A^c)})\frac{P(A)}{P(A^c)}

(from \frac{P(A|B)P(B)}{P(A^c|B)P(B)}= \frac{P(B|A)}{P(B|A^c)}\frac{P(A)}{P(A^c)})

In words, this says that the posterior odds P(A|B)/P(Ac|B) are equal to the prior odds P(A)/P(Ac ) times the factor P(B|A)/P(B|Ac ), which is known in statistics as the likelihood ratio.

Theorem 2.3.6 (Law of total probability)

The law of total probability (LOTP) relates conditional probability to unconditional probability. It is essential for fulfilling the promise that conditional probability can be used to decompose complicated probability problems into simpler pieces, and it is often used in tandem with(与……协同;与……配合) Bayes’ rule.

A well-chosen partition will reduce a complicated problem into simpler pieces, whereas a poorly chosen partition will only exacerbate our problems, requiring us to calculate n difficult probabilities instead of just one!

(Random coin)

You have one fair coin, and one biased coin which lands Heads with probability 3/4. You pick one of the coins at random and flip it three times. It lands Heads all three times. Given this information, what is the probability that the coin you picked is the fair one?

The key is how to compute the P(A) using the LOTP.

h 2.3.8 (Prior vs. posterior).

It would not be correct in the calculation in the above example to say after the first step, “P(A) = 1 because we know A happened.” It is true that P(A|A) = 1, but P(A) is the prior probability of A and P(F) is the prior probability of F—both are the probabilities before we observe any data in the 56 Introduction to Probability experiment. These must not be confused with posterior probabilities conditional on the evidence A.

Example 2.3.9 (Testing for a rare disease)

Most people find it surprising to learn that the conditional probability of having the disease given a positive test result is only 16%, even though the test is 95% accurate (see Gigerenzer and Hoffrage [13]). The key to understanding this surprisingly low posterior probability of having the disease is to realize that there are two factors at play: the evidence from the test, and our prior information about the prevalence of the disease.

  • 22
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import numpy as np def loaddata(): X = np.array([[1,'S'],[1,'M'],[1,'M'],[1,'S'], [1, 'S'], [2, 'S'], [2, 'M'], [2, 'M'], [2, 'L'], [2, 'L'], [3, 'L'], [3, 'M'], [3, 'M'], [3, 'L'], [3, 'L']]) y = np.array([-1,-1,1,1,-1,-1,-1,1,1,1,1,1,1,1,-1]) return X, y def Train(trainset,train_labels): m = trainset.shape[0] n = trainset.shape[1] prior_probability = {}# 先验概率 key是类别值,value是类别的概率值 conditional_probability ={}# 条件概率 key的构造:类别,特征,特征值 #类别的可能取值 labels = set(train_labels) # 计算先验概率(此时没有除以总数据量m) for label in labels: prior_probability[label] = len(train_labels[train_labels == label])+1 #计算条件概率 for i in range(m): for j in range(n): # key的构造:类别,特征,特征值 #补充计算条件概率的代码-1; key = str(train_labels[i])+','+str(j)+','+str(trainset[i][j]) conditional_probability[key] = (conditional_probability[key]+1 if (key in conditional_probability) else 1) conditional_probability_final = {} for key in conditional_probability: #补充计算条件概率的代码-2; label = key.split(',')[0] conditional_probability[key]+=1 key1 = int(key.split(',')[1]) Ni = len(set(trainset[:,key1])) conditional_probability_final[key] =conditional_probability[key]/(prior_probability[int(label)]+Ni) # 最终的先验概率(此时除以总数据量m) for label in labels: prior_probability[label] = prior_probability[label]/ (m+len(labels)) return prior_probability,conditional_probability_final,labels def predict(data): result={} for label in train_labels_set: temp=1.0 #补充预测代码; print('result=',result) #排序返回标签值 result[label] = temp*prior_probability[label] for i in range (len(data)): key = str(label)+ ','+str(i)+','+str(data[i]) result[label]*=conditional_probability_final[key] print('result=',result) #排序返回标签值 return sorted(result.items(), key=lambda x: x[1],reverse=True)[0][0] X,y = loaddata() prior_probability,conditional_probability,train_labels_set = Train(X,y) r_label = predict([2,'S']) print(' r_label =', r_label)运行次python代码
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值