The World Changes When You Know Something 🌍कुछ पता चलते ही दुनिया बदल जाती है 🌍知道一点信息,世界就变了 🌍
Monday morning. With no other information, you think there is a 10% chance your bus will be late. Then your phone shows a major accident on the main road. Now you think the chance is more like 75%. The bus has not changed. Your knowledge has changed — and the probability should change with it.
सोमवार की सुबह। बिना किसी और जानकारी के आप मानते हैं कि बस के लेट होने का 10% chance है। फिर फ़ोन पर ख़बर आती है कि मुख्य सड़क पर बड़ा हादसा हुआ है। अब आप chance को 75% के आसपास मानते हैं। बस नहीं बदली। आपकी जानकारी बदली है — और probability को भी उसके साथ बदलना चाहिए।
周一早上。没有其他信息时,你觉得公交迟到的概率是 10%。然后手机提示主干道发生了严重车祸,你觉得概率大约变成了 75%。公交车没变,变的是你掌握的信息 —— 概率也应随之改变。
This is conditional probability. It answers: "Given that I know B has happened, how likely is A?" It is written P(A | B) and read "the probability of A given B". The bar "|" means "given that". It powers medical diagnosis, spam filters, recommendations, the weighing of evidence in court, and Bayesian machine learning.
यही conditional probability है। यह बताती है: "जब पता है कि B हो चुका है, तो A कितना संभव है?" इसे P(A | B) लिखते हैं और पढ़ते हैं "B दिया हो तो A की probability"। खड़ी रेखा "|" का मतलब "दिया हो तो"। यह medical diagnosis, spam filters, recommendations, अदालत में सबूत तौलने और Bayesian machine learning को चलाती है।
这就是条件概率。它回答:“已知 B 已经发生,A 发生的可能性有多大?”记作 P(A | B),读作“在 B 条件下 A 的概率”。竖线“|”表示“已知”。医学诊断、垃圾邮件过滤、推荐系统、法庭证据评估和贝叶斯机器学习都依赖它。
Conditional probability shrinks the sample space. Once you learn B happened, outcomes outside B no longer matter. You zoom in on the world where B is true and ask: how much of that world also has A?
Conditional probability sample space को छोटा करती है। जैसे ही पता चलता है कि B हुआ, B के बाहर के नतीजे मायने नहीं रखते। आप उस दुनिया में zoom करते हैं जहाँ B सच है और पूछते हैं: उस दुनिया के कितने हिस्से में A भी है?
条件概率会缩小样本空间。一旦知道 B 发生了,B 之外的结果就不再重要。你把视野聚焦到 B 为真的世界,再问:这个世界中有多大比例也包含 A?
The Definition & Formula 📐परिभाषा और Formula 📐定义与公式 📐
Why the Formula Is What It IsFormula ऐसा क्यों है公式为什么是这样
Given that B happened, we want the share of B's outcomes that also include A. That share is the overlap A ∩ B divided by all of B. This ratio is the definition.
B हुआ है, तो हम B के उन नतीजों का हिस्सा चाहते हैं जिनमें A भी है। वह हिस्सा है overlap A ∩ B भाग पूरा B। यही अनुपात परिभाषा है।
已知 B 发生,我们想知道 B 的结果中同时包含 A 的比例。这个比例就是重叠部分 A ∩ B 除以整个 B。这就是定义。
Conditional Probability in Pictures — Understand Before the Maths 📊तस्वीरों में Conditional Probability — गणित से पहले समझें 📊图解条件概率 — 先理解,再算数 📊
We use a real table of 200 students (full details in the next section): 90 study Data Science (DS) and 72 of them passed; 110 study Business (BUS) and 66 of them passed. In the picture, area = number of students.
हम 200 छात्रों की असली table लेते हैं (पूरा विवरण अगले section में): 90 Data Science (DS) पढ़ते हैं और उनमें 72 pass हुए; 110 Business (BUS) पढ़ते हैं और उनमें 66 pass हुए। तस्वीर में area = छात्रों की संख्या।
我们用一个 200 名学生的真实表格(详见下一节):90 人学数据科学(DS),其中 72 人通过;110 人学商科(BUS),其中 66 人通过。图中面积 = 学生人数。
Each dot is a student. Choose what you are given; everyone else disappears and the rest regroup. Then choose what you ask about. Try "given DS, ask passed" and then "given passed, ask DS".
हर बिंदु एक छात्र है। चुनें कि आपको क्या दिया गया है; बाक़ी सब ग़ायब होते हैं और बचे हुए फिर से जमते हैं। फिर चुनें कि सवाल क्या है। "DS दिया हो, pass पूछें" और फिर "pass दिया हो, DS पूछें" आज़माएँ।
每个点是一名学生。选择“已知”条件,其他人会消失,剩下的人重新排列。再选择要问的事件。试试“已知 DS,问通过”,再试“已知通过,问 DS”。
Building Intuition — The Classroom Story 🎓समझ बनाना — Classroom की कहानी 🎓建立直觉 — 课堂故事 🎓
A college has 200 students. Each studies Data Science (DS) or Business (BUS), and each passed or failed the statistics exam:
एक college में 200 छात्र हैं। हर कोई Data Science (DS) या Business (BUS) पढ़ता है, और हर कोई statistics exam में pass या fail हुआ:
某学院有 200 名学生,每人学数据科学(DS)或商科(BUS),统计学考试要么通过要么未通过:
| Passed ✅Pass ✅通过 ✅ | Failed ❌Fail ❌未通过 ❌ | Totalकुल合计 | |
|---|---|---|---|
| Data Science (DS) | 72 | 18 | 90 |
| Business (BUS) | 66 | 44 | 110 |
| Totalकुल合计 | 138 | 62 | 200 |
The Prosecutor's Fallacy — Swapping the Condition ⚖️Prosecutor's Fallacy — शर्त की अदला-बदली ⚖️检察官谬误 — 把条件弄反 ⚖️
A prosecutor says: "If the defendant were innocent, the chance of this DNA match would be 1 in a million. So the chance he is innocent is 1 in a million." This mixes up two different numbers. P(match | innocent) is 1 in a million. P(innocent | match) depends on how many other people could have left the DNA and on all the other evidence.
Prosecutor कहता है: "अगर आरोपी बेगुनाह होता, तो इस DNA match का chance 10 लाख में 1 होता। इसलिए उसके बेगुनाह होने का chance 10 लाख में 1 है।" यह दो अलग संख्याओं को मिला देता है। P(match | बेगुनाह) 10 लाख में 1 है। P(बेगुनाह | match) इस पर निर्भर है कि और कितने लोग DNA छोड़ सकते थे और बाक़ी सबूत क्या कहते हैं।
检察官说:“如果被告无辜,这次 DNA 匹配的概率只有百万分之一,所以他无辜的概率是百万分之一。”这把两个不同的数混为一谈。P(匹配 | 无辜) 是百万分之一;P(无辜 | 匹配) 则取决于还有多少人可能留下 DNA,以及其他所有证据。
In a city of 1 million possible people, about one innocent person is expected to match by chance, as well as the real culprit. With no other evidence, a match alone gives roughly 50/50 — nowhere near "1 in a million".
10 लाख संभावित लोगों के शहर में, असली अपराधी के अलावा लगभग एक बेगुनाह व्यक्ति के संयोग से match होने की उम्मीद है। कोई और सबूत न हो तो सिर्फ़ match से लगभग 50/50 मिलता है — "10 लाख में 1" के आसपास भी नहीं।
在有 100 万可能人选的城市里,除了真正的罪犯,预计还会有约 1 名无辜者偶然匹配。若没有其他证据,仅凭匹配大约是 50/50 —— 远不是“百万分之一”。
Set how many people could have left the DNA and how rare a chance match is. Each red dot is an innocent person who would also match. Compare the two bars: they answer different questions.
चुनें कि कितने लोग DNA छोड़ सकते थे और संयोगवश match कितना दुर्लभ है। हर लाल बिंदु एक बेगुनाह व्यक्ति है जो match भी करेगा। दोनों bars की तुलना करें: ये अलग सवालों के जवाब हैं।
设置可能留下 DNA 的人数,以及偶然匹配有多罕见。每个红点是一名同样会匹配的无辜者。比较两根条:它们回答的是不同的问题。
In 1999 Sally Clark was convicted of murdering her two baby sons. An expert told the jury that two cot deaths in such a family had a chance of 1 in 73 million. That number wrongly assumed the two deaths were independent, and it was easily heard as "1 in 73 million that she is innocent" — the prosecutor's fallacy. The Royal Statistical Society publicly criticised the statistics, and her conviction was overturned in 2003.
1999 में Sally Clark को अपने दो शिशु बेटों की हत्या का दोषी ठहराया गया। एक विशेषज्ञ ने jury से कहा कि ऐसे परिवार में दो cot deaths का chance 7.3 करोड़ में 1 है। इस संख्या ने ग़लत माना कि दोनों मौतें independent थीं, और इसे आसानी से "बेगुनाह होने का chance 7.3 करोड़ में 1" समझा गया — prosecutor's fallacy। Royal Statistical Society ने इन आँकड़ों की खुलकर आलोचना की, और 2003 में उनकी सज़ा रद्द हुई।
1999 年,萨莉·克拉克被判谋杀两个幼子。一位专家告诉陪审团,这种家庭发生两次婴儿猝死的概率是 7,300 万分之一。这个数字错误地假设两次死亡相互独立,而且很容易被理解成“她无辜的概率是 7,300 万分之一”—— 正是检察官谬误。英国皇家统计学会公开批评了这一统计论证,她的定罪于 2003 年被撤销。
Application 1 — Spam Filtering 📧उपयोग 1 — Spam Filtering 📧应用 1 — 垃圾邮件过滤 📧
The Story: A Simple Word-Based Filterकहानी: एक सरल शब्द-आधारित Filter故事:一个简单的关键词过滤器
You receive 1,000 emails a month: 200 spam and 800 real. Of the 200 spam emails, 160 contain the word "FREE". Of the 800 real ones, 40 do (sales, free webinars). An email arrives with "FREE". How likely is it to be spam? We need P(spam | FREE) — not P(FREE | spam) = 160/200 = 80%, which is the other direction.
आपको हर महीने 1,000 emails आते हैं: 200 spam और 800 असली। 200 spam में से 160 में "FREE" शब्द है। 800 असली में से 40 में (sales, free webinars)। "FREE" वाला email आता है। इसके spam होने का chance क्या है? हमें P(spam | FREE) चाहिए — P(FREE | spam) = 160/200 = 80% नहीं, जो उल्टी दिशा है।
你每月收到 1,000 封邮件:200 封垃圾、800 封正常。200 封垃圾中有 160 封含“FREE”,800 封正常邮件中有 40 封含它(促销、免费讲座)。一封含“FREE”的邮件到来,它是垃圾邮件的概率是多少?我们要的是 P(垃圾 | FREE),而不是反方向的 P(FREE | 垃圾) = 160/200 = 80%。
spam score = P(spam) × P(FREE | spam) × P(WINNER | spam)
real score = P(real) × P(FREE | real) × P(WINNER | real)
P(spam | both words) = spam score / (spam score + real score). Treating the words as independent within each class is the "naive" part, yet it works well in practice.
spam score = P(spam) × P(FREE | spam) × P(WINNER | spam)
real score = P(real) × P(FREE | real) × P(WINNER | real)
P(spam | दोनों शब्द) = spam score / (spam score + real score)। हर class के अंदर शब्दों को independent मानना "naive" हिस्सा है, फिर भी यह व्यवहार में अच्छा काम करता है।
垃圾得分 = P(垃圾) × P(FREE | 垃圾) × P(WINNER | 垃圾)
正常得分 = P(正常) × P(FREE | 正常) × P(WINNER | 正常)
P(垃圾 | 两个词) = 垃圾得分 / (垃圾得分 + 正常得分)。在每个类别内把词视为独立就是“朴素”之处,但实践中效果很好。
Each square is one email (red = spam, green = real; a white dot means it contains FREE). Apply the filters and watch the world shrink. Then move the sliders: P(spam | FREE) moves away from P(FREE | spam).
हर खाना एक email है (लाल = spam, हरा = असली; सफ़ेद बिंदु = इसमें FREE है)। Filters लगाएँ और दुनिया को सिकुड़ते देखें। फिर sliders खिसकाएँ: P(spam | FREE), P(FREE | spam) से अलग हो जाता है।
每个方块是一封邮件(红 = 垃圾,绿 = 正常;白点表示含 FREE)。应用筛选,看世界缩小。再拖动滑块:P(垃圾 | FREE) 会与 P(FREE | 垃圾) 分开。
Application 2 — Recommendation Systems 🎬उपयोग 2 — Recommendation Systems 🎬应用 2 — 推荐系统 🎬
An illustrative streaming platform has 100,000 users. 20,000 watched Movie A (a thriller), 10,000 watched Movie B (another thriller), and 7,000 watched both. Should it recommend B to someone who just finished A?
एक उदाहरण streaming platform पर 100,000 users हैं। 20,000 ने Movie A (thriller) देखी, 10,000 ने Movie B (दूसरी thriller), और 7,000 ने दोनों। क्या A ख़त्म करने वाले को B suggest करनी चाहिए?
假设一个流媒体平台有 100,000 名用户。20,000 人看过电影 A(惊悚片),10,000 人看过电影 B(另一部惊悚片),7,000 人两部都看过。应该向刚看完 A 的人推荐 B 吗?
Product suggestions, music playlists and video autoplay all estimate something like P(user engages with item X | the user's history). The models differ — co-occurrence counts, matrix factorisation, deep networks — but the question they answer is a conditional probability.
Product suggestions, music playlists और video autoplay सब कुछ ऐसा अनुमान लगाते हैं: P(user item X से जुड़ेगा | user का इतिहास)। Models अलग होते हैं — co-occurrence counts, matrix factorisation, deep networks — पर जिस सवाल का जवाब देते हैं वह conditional probability है।
商品推荐、音乐歌单和视频自动播放,都在估计类似 P(用户会与物品 X 互动 | 用户历史) 的量。模型各不相同 —— 共现计数、矩阵分解、深度网络 —— 但它们回答的问题都是条件概率。
Application 3 — Medical Testing 🏥उपयोग 3 — Medical Testing 🏥应用 3 — 医学检测 🏥
A disease affects 2% of people aged 40–60. A new test has 92% sensitivity (it catches 92% of sick people) and 88% specificity (it clears 88% of healthy people). A 50-year-old tests positive. First instinct: "92% accurate — I almost surely have it." That ignores the base rate.
एक बीमारी 40–60 उम्र के 2% लोगों को होती है। नए test की sensitivity 92% है (92% बीमारों को पकड़ता है) और specificity 88% (88% स्वस्थ लोगों को साफ़ बताता है)। एक 50 साल के व्यक्ति का test positive आया। पहला ख़याल: "92% सटीक — मुझे लगभग पक्का बीमारी है।" यह base rate को नज़रअंदाज़ करता है।
某疾病在 40–60 岁人群中的患病率为 2%。新检测的灵敏度 92%(能查出 92% 的患者),特异度 88%(能排除 88% 的健康人)。一位 50 岁的人检测阳性。第一反应:“准确率 92%,我几乎肯定得病了。”这忽视了基础比率。
Each square is one positive test (per 1,000 people tested).
हर खाना एक positive test है (हर 1,000 जाँचे गए लोगों में)।
每个方块代表一个阳性结果(每检测 1,000 人)。
| Has disease (20)बीमारी है (20)患病(20) | No disease (980)बीमारी नहीं (980)未患病(980) | Row totalपंक्ति कुल行合计 | |
|---|---|---|---|
| Test positiveTest positive检测阳性 | True positive: 18.4 (20 × 0.92)True positive: 18.4 (20 × 0.92)真阳性:18.4(20 × 0.92) | False positive: 117.6 (980 × 0.12)False positive: 117.6 (980 × 0.12)假阳性:117.6(980 × 0.12) | 136 |
| Test negativeTest negative检测阴性 | False negative: 1.6 (20 × 0.08)False negative: 1.6 (20 × 0.08)假阴性:1.6(20 × 0.08) | True negative: 862.4 (980 × 0.88)True negative: 862.4 (980 × 0.88)真阴性:862.4(980 × 0.88) | 864 |
| Column totalस्तंभ कुल列合计 | 20 | 980 | 1,000 |
People hear "92% accurate" and think "92% chance I am sick". This is base-rate neglect. In a classic 1978 study, 60 Harvard medical students and doctors were asked about a disease with prevalence 1 in 1,000 and a 5% false-positive rate. The correct answer is about 2%, but the most common answer was 95%, and only 11 of the 60 got it right.
लोग "92% सटीक" सुनकर सोचते हैं "मेरे बीमार होने का chance 92% है"। यह base-rate neglect है। 1978 की एक मशहूर study में Harvard के 60 medical छात्रों और doctors से 1,000 में 1 prevalence और 5% false-positive दर वाली बीमारी के बारे में पूछा गया। सही जवाब लगभग 2% है, पर सबसे आम जवाब 95% था, और 60 में से सिर्फ़ 11 सही थे।
人们听到“准确率 92%”就以为“我有 92% 的概率患病”。这就是忽视基础比率。在 1978 年的一项经典研究中,研究者向哈佛的 60 名医学生和医生提问:某病患病率为千分之一,假阳性率 5%。正确答案约 2%,但最常见的回答是 95%,60 人中只有 11 人答对。
Application 4 — The Hiring Decision 🎯उपयोग 4 — Hiring का फ़ैसला 🎯应用 4 — 招聘决策 🎯
Last year a company assessed 500 applicants with a coding test (pass/fail) and a later judgement of "good fit" or "not a fit":
पिछले साल एक कंपनी ने 500 आवेदकों को coding test (pass/fail) और बाद में "good fit" या "not a fit" के आकलन से परखा:
去年一家公司用编程测试(通过/未通过)和之后的“合适/不合适”评估考察了 500 名应聘者:
| Good fit (GF)Good fit (GF)合适(GF) | Not a fit (NF)Not a fit (NF)不合适(NF) | Totalकुल合计 | |
|---|---|---|---|
| Passed coding (PC)Coding pass (PC)编程通过(PC) | 180 | 70 | 250 |
| Failed coding (FC)Coding fail (FC)编程未通过(FC) | 45 | 205 | 250 |
| Totalकुल合计 | 225 | 275 | 500 |
The Law of Total Probability 🧮Law of Total Probability 🧮全概率公式 🧮
If the sample space splits into non-overlapping groups B₁, B₂, …, Bₙ that cover everything, then the chance of A is a weighted average of the chances of A inside each group. The weights are the group sizes.
अगर sample space बिना overlap वाले groups B₁, B₂, …, Bₙ में बँटा हो जो सब कुछ ढकते हैं, तो A का chance हर group के अंदर A के chance का weighted average है। Weights groups के आकार हैं।
若样本空间被划分为互不重叠且覆盖全部的组 B₁、B₂、…、Bₙ,那么 A 的概率就是各组内 A 的概率的加权平均,权重是各组的大小。
Multiply along each red path, then add the paths.
हर लाल रास्ते पर गुणा करें, फिर रास्ते जोड़ें।
沿每条红色路径相乘,再把路径相加。
Each column's width is a factory's share and its height is the defect rate, so each coloured area is P(factory ∩ defect). The bar on the right splits the defects by factory — that is P(factory | defect). Inspect 1,000 laptops to compare with a real sample.
हर column की चौड़ाई factory का हिस्सा और ऊँचाई defect दर है, इसलिए हर रंगीन area P(factory ∩ defect) है। दाईं ओर का bar defects को factory के अनुसार बाँटता है — यही P(factory | defect) है। असली sample से तुलना के लिए 1,000 laptops जाँचें।
每列宽度是工厂份额,高度是次品率,所以每块彩色面积就是 P(工厂 ∩ 次品)。右侧条形按工厂拆分次品 —— 即 P(工厂 | 次品)。检查 1,000 台笔记本与真实样本比较。
Simpson's Paradox — When the Condition Reverses the Answer 🔄Simpson's Paradox — जब शर्त जवाब पलट दे 🔄辛普森悖论 — 条件让结论反转 🔄
A real 1986 study (Charig et al., British Medical Journal) compared two kidney-stone treatments: A (open surgery) and B (percutaneous nephrolithotomy, a keyhole method). Overall, B looked better. Yet A did better for small stones and for large stones. How?
1986 की एक असली study (Charig et al., British Medical Journal) ने गुर्दे की पथरी के दो इलाजों की तुलना की: A (open surgery) और B (percutaneous nephrolithotomy, एक keyhole तरीका)। कुल मिलाकर B बेहतर दिखा। फिर भी A छोटी पथरी में और बड़ी पथरी में बेहतर था। कैसे?
1986 年的一项真实研究(Charig 等,《英国医学杂志》)比较了两种肾结石治疗:A(开放手术)和 B(经皮肾镜取石术,一种微创方法)。总体上 B 看起来更好,但 A 在小结石和大结石上都更好。这怎么可能?
| Treatmentइलाज治疗 | Small stonesछोटी पथरी小结石 | Large stonesबड़ी पथरी大结石 | Overallकुल总体 |
|---|---|---|---|
| A (open surgery)A (open surgery)A(开放手术) | 93% (81/87) | 73% (192/263) | 78% (273/350)78% (273/350)78%(273/350) |
| B (keyhole)B (keyhole)B(微创) | 87% (234/270) | 69% (55/80) | 83% (289/350)83% (289/350)83%(289/350) |
A was used mostly on large stones (263 of 350), which are harder to treat. B was used mostly on small stones (270 of 350). The overall rates mix easy and hard cases in very different amounts. Comparing within stone size — P(success | treatment, size) — removes that imbalance: A is better in both groups. Stone size is a confounder: it affected both the choice of treatment and the outcome.
A ज़्यादातर बड़ी पथरी (350 में 263) पर इस्तेमाल हुआ, जिनका इलाज कठिन है। B ज़्यादातर छोटी पथरी (350 में 270) पर। कुल दरें आसान और कठिन मामलों को बहुत अलग मात्रा में मिलाती हैं। पथरी के आकार के अंदर तुलना — P(सफलता | इलाज, आकार) — वह असंतुलन हटाती है: A दोनों groups में बेहतर है। पथरी का आकार एक confounder है: इसने इलाज के चुनाव और नतीजे दोनों पर असर डाला।
A 主要用于大结石(350 例中 263 例),这些更难治。B 主要用于小结石(350 例中 270 例)。总体比率以非常不同的比例混合了易治和难治病例。按结石大小分组比较 —— P(成功 | 治疗, 大小) —— 消除了这种失衡:A 在两组中都更好。结石大小是一个混杂因素:它同时影响了治疗选择和结果。
The success rates inside each stone size are fixed at the real values. Only the mix of patients changes. Move the sliders and watch the "overall" pair flip.
हर पथरी आकार के अंदर सफलता दरें असली मानों पर तय हैं। सिर्फ़ मरीज़ों का मिश्रण बदलता है। Sliders खिसकाएँ और "कुल" जोड़ी को पलटते देखें।
各结石大小内的成功率固定为真实值,只改变病例构成。拖动滑块,看“总体”那一对如何翻转。
Not always. Split by a variable that comes before the treatment and affects both who gets it and the outcome (like stone size). Do not split by something the treatment itself changes. Deciding which is which needs knowledge of how the data were produced — the numbers alone cannot tell you. This is why randomised trials, which balance confounders automatically, are so valuable.
हमेशा नहीं। उस variable से बाँटें जो इलाज से पहले आता है और इलाज किसे मिलेगा व नतीजे दोनों पर असर डालता है (जैसे पथरी का आकार)। उस चीज़ से न बाँटें जिसे इलाज ख़ुद बदलता है। कौन-सा कौन है, यह तय करने के लिए जानना पड़ता है कि data कैसे बना — सिर्फ़ संख्याएँ यह नहीं बता सकतीं। इसीलिए randomised trials, जो confounders को अपने आप संतुलित करते हैं, इतने क़ीमती हैं।
不一定。应按发生在治疗之前、且同时影响谁接受治疗和结果的变量拆分(如结石大小)。不要按治疗本身会改变的变量拆分。要判断属于哪种,需要了解数据是如何产生的 —— 光看数字无法判断。这正是随机对照试验如此宝贵的原因:它能自动平衡混杂因素。
Python ImplementationPython ImplementationPython 实现
Row and column conditionals with pandaspandas से row और column conditionals用 pandas 计算按行与按列的条件概率
# 1) Conditional probability from a table: the classroom
import pandas as pd
df = pd.DataFrame({
"major": ["DS"] * 90 + ["BUS"] * 110,
"result": ["Pass"] * 72 + ["Fail"] * 18 + ["Pass"] * 66 + ["Fail"] * 44,
})
print(pd.crosstab(df.major, df.result, margins=True))
print()
print("P(Pass | major) -> divide each ROW by its row total")
print(pd.crosstab(df.major, df.result, normalize="index").round(3))
print()
print("P(major | result) -> divide each COLUMN by its column total")
print(pd.crosstab(df.major, df.result, normalize="columns").round(3))
A spam filter, from one word to Naive BayesSpam filter: एक शब्द से Naive Bayes तक垃圾邮件过滤:从单词到朴素贝叶斯
# 2) Spam filter: one word, then Naive Bayes with two words
p_spam = 0.20
p_free = {"spam": 0.80, "ham": 0.05} # P("FREE" | class)
p_win = {"spam": 0.30, "ham": 0.01} # P("WINNER" | class)
num = p_free["spam"] * p_spam
den = num + p_free["ham"] * (1 - p_spam)
print(f'P(spam | FREE) = {num / den:.3f}')
# Naive Bayes: multiply the word likelihoods for each class, then normalise
s = p_spam * p_free["spam"] * p_win["spam"]
h = (1 - p_spam) * p_free["ham"] * p_win["ham"]
print(f'P(spam | FREE, WINNER) = {s / (s + h):.4f}')
Medical test: formula and simulation agreeMedical test: formula और simulation मेल खाते हैं医学检测:公式与模拟一致
# 3) Medical test: Bayes by formula and by simulation
import numpy as np
prev, sens, spec = 0.02, 0.92, 0.88
p_pos = sens * prev + (1 - spec) * (1 - prev) # total probability
print(f"P(positive) = {p_pos:.4f}")
print(f"P(disease | positive) = {sens * prev / p_pos:.4f}")
rng = np.random.default_rng(0)
n = 1_000_000
sick = rng.random(n) < prev
pos = np.where(sick, rng.random(n) < sens, rng.random(n) < 1 - spec)
print(f"simulated, {n:,} people: {sick[pos].mean():.4f}")
Total probability and the reverse questionTotal probability और उल्टा सवाल全概率与反向问题
# 4) Law of total probability and the reverse question
share = {"A": 0.30, "B": 0.45, "C": 0.25}
defect = {"A": 0.02, "B": 0.05, "C": 0.08}
joint = {f: share[f] * defect[f] for f in share}
p_def = sum(joint.values())
print(f"P(defect) = {p_def:.4f}")
for f in share:
print(f"P({f} | defect) = {joint[f] / p_def:.3f} (share of supply {share[f]:.2f})")
Simpson's paradox in a few linesकुछ lines में Simpson's paradox几行代码看辛普森悖论
# 5) Simpson's paradox: kidney stones (Charig et al., BMJ 1986)
import pandas as pd
d = pd.DataFrame({
"treatment": ["A", "A", "B", "B"],
"stone": ["small", "large", "small", "large"],
"success": [81, 192, 234, 55],
"patients": [87, 263, 270, 80],
})
d["rate"] = (d.success / d.patients).round(3)
print(d.pivot(index="treatment", columns="stone", values="rate"))
overall = d.groupby("treatment")[["success", "patients"]].sum()
print((overall.success / overall.patients).round(3).rename("overall"))
Complete Formula Referenceपूरा Formula सारांश公式速查
| FormulaFormula公式 | ExpressionExpression表达式 | Use whenकब इस्तेमाल करें何时使用 |
|---|---|---|
| Conditional probabilityConditional probability条件概率 | P(A|B) = P(A∩B) / P(B) | Chance of A inside the world where B happenedउस दुनिया में A का chance जहाँ B हुआ在 B 已发生的世界里 A 的概率 |
| Multiplication ruleMultiplication rule乘法法则 | P(A∩B) = P(A|B) × P(B) | Chance both happenदोनों होने का chance两者同时发生的概率 |
| IndependenceIndependence独立性 | P(A|B) = P(A) | Does knowing B change A?क्या B जानने से A बदलता है?知道 B 会改变 A 吗? |
| Total probabilityTotal probability全概率 | P(A) = Σ P(A|Bᵢ) × P(Bᵢ) | Average over a partitionPartition पर औसत对划分加权平均 |
| Bayes' theoremBayes' theorem贝叶斯定理 | P(H|E) = P(E|H) × P(H) / P(E) | Flip the direction of the conditionशर्त की दिशा पलटना翻转条件的方向 |
| Chain ruleChain rule链式法则 | P(A∩B∩C) = P(A) P(B|A) P(C|A∩B) | Events in sequenceक्रम में events依次发生的事件 |
| Lift | P(B|A) / P(B) | How much A raises the chance of BA, B का chance कितना बढ़ाता हैA 把 B 的概率提高了多少 |
Where Conditional Probability Lives in Data ScienceData Science में Conditional Probability कहाँ है条件概率在数据科学中的位置
| Applicationउपयोग应用 | Methodतरीका方法 | Condition (given)शर्त (दिया हुआ)条件(已知) | Targetलक्ष्य目标 |
|---|---|---|---|
| 📧 Spam filteringSpam filtering垃圾邮件过滤 | Naive Bayes | Words in the emailEmail के शब्द邮件中的词 | P(spam | words) |
| 🎬 RecommendationRecommendation推荐 | Collaborative filteringCollaborative filtering协同过滤 | Items the user likedUser को पसंद items用户喜欢过的物品 | P(likes Z | liked X, Y) |
| 🏥 Medical diagnosisMedical diagnosis医学诊断 | Bayesian diagnosticsBayesian diagnostics贝叶斯诊断 | Symptoms + test resultsलक्षण + test नतीजे症状 + 检测结果 | P(disease | evidence) |
| 🔐 Fraud detectionFraud detection欺诈检测 | Risk scoring modelsRisk scoring models风险评分模型 | Location, time, amountजगह, समय, राशि地点、时间、金额 | P(fraud | features) |
| 📈 Credit scoringCredit scoring信用评分 | Logistic regressionLogistic regression逻辑回归 | Income, history, debt ratioआय, इतिहास, debt ratio收入、历史、负债率 | P(default | profile) |
| 🚗 Robot / car localisationRobot / car localisation机器人 / 汽车定位 | Bayesian filters (Kalman, particle)Bayesian filters (Kalman, particle)贝叶斯滤波(卡尔曼、粒子) | Sensor readings so farअब तक की sensor readings迄今的传感器读数 | P(position | readings) |
| 🔤 Language modelsLanguage models语言模型 | Next-token predictionNext-token prediction下一个词元预测 | Previous tokensपिछले tokens前面的词元 | P(next token | context) |
| 🧬 GenomicsGenomics基因组学 | Disease risk modelsDisease risk models疾病风险模型 | Genetic variantsGenetic variants基因变异 | P(disease | genotype) |
A language model such as GPT, Claude or Gemini outputs, at each step, a probability distribution P(next token | all previous tokens). It is trained to make these conditional probabilities match real text, and later tuned further. The text you see is produced by picking tokens from these distributions one after another — the chain rule in action.
GPT, Claude या Gemini जैसा language model हर step पर एक probability distribution देता है: P(अगला token | पिछले सभी tokens)। इसे इन conditional probabilities को असली text से मिलाने के लिए train किया जाता है, और बाद में और tune किया जाता है। आप जो text देखते हैं वह इन distributions से एक के बाद एक tokens चुनकर बनता है — chain rule काम करते हुए।
像 GPT、Claude 或 Gemini 这样的语言模型,每一步都会输出一个概率分布 P(下一个词元 | 之前所有词元)。它先被训练得让这些条件概率贴近真实文本,之后再进一步调整。你看到的文字就是从这些分布中依次挑选词元生成的 —— 正是链式法则在起作用。
The Golden Rules of Conditional ProbabilityConditional Probability के सुनहरे नियम条件概率的黄金法则
Conditional probability is how we reason when new information arrives. Bayesian updating, classifiers, recommenders, diagnostic tests and language models all use it. Master three ideas — conditioning shrinks the world, P(A | B) ≠ P(B | A), and base rates matter — and you will read data far more clearly.
Conditional probability वह तरीका है जिससे हम नई जानकारी आने पर सोचते हैं। Bayesian updating, classifiers, recommenders, diagnostic tests और language models सब इसका इस्तेमाल करते हैं। तीन विचार पक्के करें — conditioning दुनिया को छोटा करती है, P(A | B) ≠ P(B | A), और base rates मायने रखते हैं — और आप data को कहीं ज़्यादा साफ़ पढ़ेंगे।
条件概率是我们在获得新信息时的推理方式。贝叶斯更新、分类器、推荐系统、诊断检测和语言模型都离不开它。掌握三个要点 —— 条件化缩小世界、P(A | B) ≠ P(B | A)、基础比率很重要 —— 你读数据会清楚得多。