Probability & Statistic in Machine Learning(I)

Probability & Statistic in Machine Learning(I)

็™ผไฝˆๆ™‚้–“

What is probability?#

Ex: If there are ten child, three of them know how to play soccer. Find the probability that a child picked at randoms play soccer.

Ans:

P(soccer)=soccertotal=310P(soccer) = \frac{soccer}{total} = \frac{3}{10}

Complement of Probability#

According to the previous example. Fine the probability of child NOT playing soccer.

Ans:

P(notsoccer)=1โˆ’P(soccer)=1โˆ’310=710P(notsoccer) = 1-P(soccer) = 1-\frac{3}{10}=\frac{7}{10}

Sum of Probability#

Consider the joint event. Kids can play as many sports as they want. What is the probability that childs play soccer or basketball?

Ans:

P(soccerโ€…โ€Šโˆชโ€…โ€Šbasketball)=P(soccer)+P(basketball)โˆ’p(soccerโ€…โ€Šโˆฉbasketball)P(soccer \; \cup \;basketball) = P(soccer) + P(basketball) - p(soccer \; \cap basketball)

Independence#

Product Rule

P(Aโ€…โ€Šโˆฉโ€…โ€ŠB)=P(A)โ‹…P(B)P(A \; \cap \; B) = P(A) \cdot P(B)

Condition Probability#

Product Rule

P(Aโ€…โ€Šโˆฉโ€…โ€ŠB)=P(A)โ‹…P(BโˆฃA)P(A \; \cap \; B) = P(A) \cdot P(B|A)

Bayes Theoram#

P(AโˆฃB)=P(BโˆฃA)P(A)P(B)P(A\vert{}B) = \frac{P(B\vert{}A) P(A)}{P(B)}

Monty hall problem#

Define the events:

  • ๐ธ1ย = the car is behind door 1
  • ๐ธ2ย = the car is behind door 2
  • ๐ธ3ย = the car is behind door 3

Or, in a more concise way:ย ๐ธ๐‘–ย = the car is behind doorย ๐‘–ย forย ๐‘–=1,2,3.

Note that these events areย mutually exclusive, in other words, you cannot have a car simuntaneously in two doors, because of the rules of the game. This means that,

๐‘ƒ(๐ธ1 โˆฉ ๐ธ2)=0,ย ๐‘ƒ(๐ธ1 โˆฉ ๐ธ3)=0ย andย ๐‘ƒ(๐ธ2 โˆฉ ๐ธ3) = 0.

You can say it also by writing that ๐‘ƒ(๐ธ๐‘– โˆฉ ๐ธ๐‘—) = 0ย forย ๐‘–โ‰ ๐‘—.

Another fact, due to the rules of the game, is thatย the car is behind one of the three doors, so ๐‘ƒ(๐ธ1 โˆช ๐ธ2 โˆช ๐ธ3)=1.

This is, in fact, theย sample space, orย universe,ย ฮฉ, because it is the set of all possible outcomes.

Let's suppose you've chosenย door number 1. Since there is an equal chance of the car being behind one of the three doors, we know that ๐‘ƒ(๐ธ1) = 1/3.

By theย complement rule, we know thatย ๐‘ƒ(๐ธ๐‘1) = 1โˆ’๐‘ƒ(๐ธ1) = 1โˆ’1/3 = 2/3

Since the universe is given byย ๐ธ1โˆช๐ธ2โˆช๐ธ3ย (the car is behind door 1 OR door 2 OR door 3), thenย ๐ธ๐‘1 = ๐ธ2โˆช๐ธ3, thereforeย ๐‘ƒ(๐ธ2โˆช๐ธ3)= 2/3. You can have a visual idea in the image below.

Screenshot_2026-07-13_at_2.13.16_PM.png

Now that you chose door 1, the Host then opens door 3, revealing a goat and asks you if you want to switch doors. If you don't switch, the probability of winning remainsย 13ย because this is your initial choice. If youย doย switch, then, you can notice that the Hostย gave you an additional information. They showed to you that door 3 does not have a car, which means that ๐‘ƒ(๐ธ3)=0.

Now you are mostly done, because as you know,ย 2/3=๐‘ƒ(๐ธ2 โˆช ๐ธ3)=๐‘ƒ(๐ธ2)+๐‘ƒ(๐ธ3)โˆ’๐‘ƒ(๐ธ2 โˆฉ ๐ธ3). You already know thatย ๐‘ƒ(๐ธ2 โˆฉ ๐ธ3)=0, because they are mutually exclusive events (the car is behind in onlyย oneย of the three doors), and the Host gave you a very importante piece of additional information:ย ๐‘ƒ(๐ธ3)=0. With this, you can easily conclude that:

๐‘ƒ(๐‘ค๐‘–๐‘›|๐‘ ๐‘ค๐‘–๐‘ก๐‘โ„Ž)=๐‘ƒ(๐ธ2)=2/3.

In other words, the probability that the car is behind door 2,ย given thatย it is not behind door 3 isย 2/3โ‰ˆ0.67ย as you have just seen in your simulations!

Consider a more complex situation

Now, the game is:

  • There areย ๐‘› doors, and you must choose one door.
  • Host opensย ๐‘˜ doors and revealing goats.
  • You may or may not change your previously chosen door.

The question is: is it always better to switch doors? Will it depend onย ๐‘˜?

To answer this question analyticaly, first define the following events:

  • ๐ธ๐‘–=ย the car is behind door i. In this case,ย ๐‘–=1,โ€ฆ,๐‘›.

Again, theย ๐ธ๐‘–'s are independent from each other, because there is onlyย 1ย car available.

Note that, since the Host never opens the same door the player chose and also never opens the winning door, there is an upper bound forย ๐‘˜, which isย ๐‘›โˆ’2, so 0โ‰ค๐‘˜โ‰ค๐‘›โˆ’2.

Two facts can be assumed:

  • The player chooses door 1
  • The host opens doors 2,โ€ฆ,๐‘˜+1

This is because we can always rename the doors to get this result. For instance, if the player chooses door numberย 10, we can rename it as doorย 1ย and doorย 1ย will become doorย 10. This is just to avoid getting too complex on indices notations. In math terminology, it is usually said that we can do thisย without loss of generality, since it will not affect the final result.

Now that there areย ๐‘›ย doors, the probability that the car is behind doorย 1ย isย 1/๐‘›, i.e.,

๐‘ƒ(๐ธ1)=1/๐‘›.

By the complement rule, the probability that the car isย notย behind doorย 1ย is:

๐‘ƒ(๐ธ๐‘1)=1โˆ’๐‘ƒ(๐ธ1)=1โˆ’1/๐‘›=๐‘›โˆ’1/๐‘›.

Note that

๐ธ๐‘1 = ๐ธ2โˆช๐ธ3โˆชโ€ฆโˆช๐ธ๐‘›.

There is a notation to simplify the right hand side equation above, we can write it as:

โ‹ƒ๐‘–=2๐‘›๐ธ๐‘–โ‹ƒ_{๐‘–=2}^๐‘›๐ธ_๐‘–

This works in the same fashion as a summation symbol, but the opeartion being performed is set union.

So, we know that

๐‘ƒ(โ‹ƒ๐‘–=2๐‘›๐ธ๐‘–)=๐‘›โˆ’1๐‘›.๐‘ƒ(โ‹ƒ_{๐‘–=2}^๐‘›๐ธ_๐‘–)=\frac{๐‘›โˆ’1}{๐‘›}.

Now we can answer the question: What is the probability of winning, given that we switch doors?

Let's take a look on the following image:

Screenshot 2026-07-13 at 2.03.40โ€ฏPM.png

If the player switches to a random available door, then they must choose one of theย ๐‘˜+2,๐‘˜+3,โ€ฆ,๐‘›โˆ’1,๐‘›. Therefore, the probability of picking the car is:

The probability ofย not picking the carย in doorย 1ย (๐‘ƒ(๐ธ๐‘1)=๐‘›โˆ’1/๐‘›)ย times the probability of picking the carย now, which isย 1๐‘›โˆ’๐‘˜โˆ’1ย because this is the number of remaining doors.

So, the final probability is given by

๐‘ƒ(๐‘ค๐‘–๐‘›โˆฃ๐‘ ๐‘ค๐‘–๐‘ก๐‘h)=๐‘›โˆ’1๐‘›โˆ—1๐‘›โˆ’๐‘˜โˆ’1๐‘ƒ(๐‘ค๐‘–๐‘›|๐‘ ๐‘ค๐‘–๐‘ก๐‘โ„Ž)=\frac{๐‘›โˆ’1}๐‘›*\frac{1}{๐‘›โˆ’๐‘˜โˆ’1}

It can be rewriten in the following manner:

๐‘ƒ(๐‘ค๐‘–๐‘›โˆฃ๐‘ ๐‘ค๐‘–๐‘ก๐‘h)=๐‘›โˆ’1๐‘›โˆ—1๐‘›โˆ’๐‘˜โˆ’1=1๐‘›โˆ—๐‘›โˆ’1๐‘›โˆ’๐‘˜โˆ’1โ‰ฅ1๐‘›=๐‘ƒ(๐ธ1)=๐‘ƒ(๐‘ค๐‘–๐‘›โˆฃ๐‘›๐‘œ๐‘ก๐‘ ๐‘ค๐‘–๐‘ก๐‘h).๐‘ƒ(๐‘ค๐‘–๐‘›|๐‘ ๐‘ค๐‘–๐‘ก๐‘โ„Ž)=\frac{๐‘›โˆ’1}{๐‘›}*\frac{1}{๐‘›โˆ’๐‘˜โˆ’1}=\frac{1}{๐‘›}*\frac{๐‘›โˆ’1}{๐‘›โˆ’๐‘˜โˆ’1}โ‰ฅ\frac{1}{๐‘›}=๐‘ƒ(๐ธ1)=๐‘ƒ(๐‘ค๐‘–๐‘›|๐‘›๐‘œ๐‘ก ๐‘ ๐‘ค๐‘–๐‘ก๐‘โ„Ž).

And the equality only holds whenย ๐‘˜=0. This means that the host does not open any door.

Therefore,ย it is always better to switch doors. This may sound counterintuitive at first, but think that switching doors you are using theย new piece of informationย that the host gave you, whereas if you choose not to switch, you will be ignoring this new information.

Prior and Posterior#

P(AโˆฃB)=P(BโˆฃA)P(A)P(B)P(A\vert{}B) = \frac{P(B\vert{}A) P(A)}{P(B)}

P(A|B)๏ผšPosterior

P(A)๏ผšPrior

P(B|A)๏ผšLikelihood

Probability in ML#

Example Problem: Face generation

  • Generate a group of pixels such that resulting image lools like a human face
  • Goal: generate images such P(face | pixels ) is high

Random Variables#

Why random variables๏ผŸ

Random variables allow us to model the whole experiment at once.

Event: A set of outcomes

Random variables: A function mapping outcomes to real numbers

Probability Distribution#

  • Binomial Distribution
  • Bernoulli Distribution
  • Uniform Distribution
  • Gussian(Normal) Distribution
  • Chi-Squared Distribution

Discrete Probability Distribution#

PMF (Probability Mass Function)#

For a discrete random variable XX, the PMF is defined as:

PX(x)=P(X=x)P_X(x) = P(X = x)

Properties of PMF:

1. Non-negativity

PX(x)โ‰ฅ0ย forย allย xP_X(x) \geq 0 \text{ for all } x

2. Normalization (Completeness)

The sum of all probabilities must equal 1:

โˆ‘xPX(x)=1\sum_{x} P_X(x) = 1

Proof:

Since XX must take on one of its possible values, all possible outcomes form the sample space:

{X=x1}โˆช{X=x2}โˆชโ‹ฏโˆช{X=xn}=ฮฉ\{X = x_1\} \cup \{X = x_2\} \cup \cdots \cup \{X = x_n\} = \Omega

These events are mutually exclusive (disjoint), so by the law of total probability:

P(ฮฉ)=โˆ‘i=1nP(X=xi)=1P(\Omega) = \sum_{i=1}^{n} P(X = x_i) = 1

Continuous Probability Distribution#

PDF (Probability Density Function)#

For a continuous random variable XX, the PDF is denoted as fX(x)f_X(x). Unlike PMF, the PDF does not represent the probability at a point. Instead, it describes the relative likelihood of values.

Key Properties:

1. Non-negativity

fX(x)โ‰ฅ0ย forย allย xf_X(x) \geq 0 \text{ for all } x

2. Normalization (Total Probability)

The total area under the curve equals 1:

โˆซโˆ’โˆžโˆžfX(x)โ€‰dx=1\int_{-\infty}^{\infty} f_X(x) \, dx = 1

Proof:

The probability that XX takes any value in its range must be 1:

P(โˆ’โˆž<X<โˆž)=โˆซโˆ’โˆžโˆžfX(x)โ€‰dx=1P(-\infty < X < \infty) = \int_{-\infty}^{\infty} f_X(x) \, dx = 1

3. Probability over an Interval

For any interval [a,b][a, b]:

P(aโ‰คXโ‰คb)=โˆซabfX(x)โ€‰dxP(a \leq X \leq b) = \int_{a}^{b} f_X(x) \, dx

CDF (Cumulative Distribution Function)#

The CDF is a universal concept defined for both discrete and continuous random variables:

FX(x)=P(Xโ‰คx)F_X(x) = P(X \leq x)

For Discrete Random Variables:

FX(x)=โˆ‘xiโ‰คxPX(xi)F_X(x) = \sum_{x_i \leq x} P_X(x_i)

For Continuous Random Variables:

FX(x)=โˆซโˆ’โˆžxfX(t)โ€‰dtF_X(x) = \int_{-\infty}^{x} f_X(t) \, dt

Properties of CDF:

1. Monotonicity

If a<ba < b, then:

FX(a)โ‰คFX(b)F_X(a) \leq F_X(b)

Proof:

If a<ba < b, then the event {Xโ‰คa}\{X \leq a\} is a subset of {Xโ‰คb}\{X \leq b\}:

{Xโ‰คa}โŠ‚{Xโ‰คb}โ€…โ€ŠโŸนโ€…โ€ŠP(Xโ‰คa)โ‰คP(Xโ‰คb)\{X \leq a\} \subset \{X \leq b\} \implies P(X \leq a) \leq P(X \leq b)

2. Boundary Conditions

limโกxโ†’โˆ’โˆžFX(x)=0andlimโกxโ†’โˆžFX(x)=1\lim_{x \to -\infty} F_X(x) = 0 \quad \text{and} \quad \lim_{x \to \infty} F_X(x) = 1

3. Relationship between CDF and PDF

For a continuous random variable, the PDF is the derivative of the CDF:

fX(x)=dFX(x)dxf_X(x) = \frac{dF_X(x)}{dx}

Proof:

By the Fundamental Theorem of Calculus:

FX(x)=โˆซโˆ’โˆžxfX(t)โ€‰dtโ‡’dFX(x)dx=fX(x)F_X(x) = \int_{-\infty}^{x} f_X(t) \, dt \quad \Rightarrow \quad \frac{dF_X(x)}{dx} = f_X(x)

4. Probability Calculation using CDF

For any interval (a,b](a, b]:

P(a<Xโ‰คb)=FX(b)โˆ’FX(a)P(a < X \leq b) = F_X(b) - F_X(a)

Proof:

FX(b)โˆ’FX(a)=P(Xโ‰คb)โˆ’P(Xโ‰คa)=P(a<Xโ‰คb)F_X(b) - F_X(a) = P(X \leq b) - P(X \leq a) = P(a < X \leq b)

Sampling from a Normal Distribution#

Use the CDF as shown in the following illustration:

Screenshot 2026-07-13 at 3.40.25โ€ฏPM.png

This inverse-CDF approach works well when FXโˆ’1F_X^{-1} is easy to evaluate, but it is not the only way to sample from a distribution. In a later post, we will cover more general sampling methods โ€” such as importance sampling, rejection sampling, and MCMC โ€” which are needed when the target distribution's CDF is hard to invert, or when we only know the PDF up to a normalizing constant.