Runoob Tutorial ArchiveEnglish

Introduction to Probability — Sample Space, Events, and Conditional Probability

Introduction to Probability -- Sample Space, Events, and Conditional Probability

Probability theory is a core tool for AI to handle uncertainty. From classifier outputs to generative model sampling, probability is everywhere.


Concept Analysis

** Sample Space

The set of all possible outcomes

Rolling a die: {1,2,3,4,5,6}

** Event

Subset of the sample space

"Even numbers" = {2,4,6}

Probability P(A)

The possibility of A occurring, [0,1]

0=impossible, 1=certain

Conditional probability: updating judgment after knowing some information

[ P(A|B) = \frac{P(A \cap B)}{P(B)} ]

Intuition: shrinking the sample space to the range where B occurs, and seeing what proportion A occupies within it.

Law of Total Probability: Summing over cases

[ P(A) = \sum_i P(A|B_i) P(B_i) ]


Real-life examples

**Law of Large Numbers

If you toss a coin 10 times, you might get 4 heads and 6 tails. If you toss it 10000 times, the proportion of heads will be very close to 0.5.

The more trials, the more stable the frequency—this is the foundation of probability theory.


Python hands-on practice

Instance

import

numpy

as

np

Law of large numbers verification

for

n

in

[

10

,

100

,

1000

,

10000

]

:

tosses

=

np.

random

.

choice

(

[

'H'

,

'T'

]

,

size

=

n

)

freq

=

np.

mean

(

tosses

==

'H'

)

   

print

(

f

"RUNOOB rolls {n:5d} times, heads frequency={freq:.4f}"

)

Monty Hall Problem: Probability of winning by switching is 2/3

def

monty_hall

(

switch

=

True

,

trials

=

10000

)

:

wins

=

0

   

for

_

in

range

(

trials

)

:

car

=

np.

random

.

randint

(

0

,

3

)

choice

=

np.

random

.

randint

(

0

,

3

)

revealed

=

np.

random

.

choice

(

[

d

for

d

in

range

(

3

)

if

d

!=

car

and

d

!=

choice

]

)

       

if

switch:

choice

=

[

d

for

d

in

range

(

3

)

if

d

!=

choice

and

d

!=

revealed

]

[

0

]

wins +

=

(

choice

==

car

)

   

return

wins/trials

print

(

f

"\nMonty Hall: No switch={monty_hall(False):.3f}, Switch={monty_hall(True):.3f} (Theoretical: 1/3, 2/3)"

)

Output:

Original prompt (Chinese, left untranslated)

RUNOOB 掷   10次, 正面频率=0.8000

RUNOOB 掷  100次, 正面频率=0.5500

RUNOOB 掷 1000次, 正面频率=0.4870

RUNOOB 掷10000次, 正面频率=0.4987



蒙提霍尔: 不换=0.329, 换=0.663 (理论: 1/3, 2/3)


Application scenarios in AI

Classifier output = probability distribution

Softmax outputs the probability for each class, and the sum of all class probabilities is 1. The model doesn't just tell you "this is a cat," but also "80% probability is a cat, 15% is a dog, 5% is other." This probabilistic output is crucial for risk assessment and confidence calibration.

Generative model = sampling from a probability distribution

VAE generates new images by sampling from the learned latent distribution, GAN generates realistic images by sampling from a random noise distribution, and diffusion models generate high-definition images by gradually denoising starting from pure noise. The core operation of these generative processes is "sampling from a certain probability distribution."

Stochastic policies in reinforcement learning

In policy gradient methods, the agent's action is not deterministic but is sampled from the probability distribution output by the policy network (e.g., 70% left, 30% right). This randomness ensures exploration—if it always chooses the action with the highest probability, it might never discover a better policy.