ENع
→Random variables
الدرس 15 · الاحتمالات والإحصاء

Random variables and expectation

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

A random variable turns each outcome of a chance experiment into a number. Follow three fair coin flips: list the 8 outcomes, let X count the heads, and watch the outcomes stack into the distribution 1/8, 3/8, 3/8, 1/8. Then add value × probability to get the expected value E[X] = 1.5, split X into three simpler pieces, measure the spread with the variance, and see a Python simulation settle near 1.5.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 15 · تفاعلي
01 / 10

Three flips, eight outcomes

Flip a fair coin three times. There are 2 × 2 × 2 = 8 possible outcomes, from HHH to TTT. All are equally likely, so each has probability 1/8. The set of every outcome is the sample space.

>>> from itertools import product
>>> S = list(product('HT', repeat=3))
>>> len(S), S[0], S[-1]
(8, ('H', 'H', 'H'), ('T', 'T', 'T'))
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Three flips, eight outcomes

    Flip a fair coin three times. There are 2 × 2 × 2 = 8 possible outcomes, from HHH to TTT. All are equally likely, so each has probability 1/8. The set of every outcome is the sample space.

    >>> from itertools import product
    >>> S = list(product('HT', repeat=3))
    >>> len(S), S[0], S[-1]
    (8, ('H', 'H', 'H'), ('T', 'T', 'T'))
  2. X turns outcomes into numbers

    A random variable is a rule that gives every outcome a number. Let X count the heads: HHT gives 2 and TTT gives 0. The rule itself is fixed; the chance lies in which outcome happens.

    >>> from itertools import product
    >>> S = list(product('HT', repeat=3))
    >>> [w.count('H') for w in S]      # X of each outcome
    [3, 2, 2, 1, 2, 1, 1, 0]
  3. Stack outcomes by value

    Group the outcomes by their value of X. Each drops a 1/8 block on its column: one block at 0 heads, three at 1, three at 2, one at 3. The list of P(X = x) is the distribution of X.

    >>> from collections import Counter
    >>> from itertools import product
    >>> Counter(w.count('H') for w in product('HT', repeat=3))
    Counter({2: 3, 1: 3, 3: 1, 0: 1})
  4. Chances add up to 1

    Exactly one outcome happens on every try, so a distribution always adds up to 1: 1/8 + 3/8 + 3/8 + 1/8 = 8/8 = 1. One head and two heads are the most likely values.

    >>> from fractions import Fraction as F
    >>> p = {0: F(1, 8), 1: F(3, 8), 2: F(3, 8), 3: F(1, 8)}
    >>> sum(p.values())
    Fraction(1, 1)
  5. Expected value

    The expected value E[X] weights each value by its probability, then adds: 0 × 1/8 + 1 × 3/8 + 2 × 3/8 + 3 × 1/8 = 12/8 = 1.5. It is the long-run average of X over many tries.

    >>> from fractions import Fraction as F
    >>> p = {0: F(1, 8), 1: F(3, 8), 2: F(3, 8), 3: F(1, 8)}
    >>> E = sum(x * p[x] for x in p)
    >>> E, float(E)
    (Fraction(3, 2), 1.5)
  6. The balance point

    Picture the blocks as weights on a beam. E[X] = 1.5 is the point where the beam balances. It need not be a value X can take: no single try gives one and a half heads.

    >>> E = 1.5
    >>> # weighted distances from E cancel out
    >>> sum((x - E) * n for x, n in {0: 1, 1: 3, 2: 3, 3: 1}.items())
    0.0
  7. Linearity of expectation

    Or split X: X = X1 + X2 + X3, where Xk is 1 if flip k is heads, else 0. Each flip is heads in 4 of 8 outcomes, so E[Xk] = 1/2. Expectations add: E[X] = 1/2 + 1/2 + 1/2 = 1.5.

    >>> from itertools import product
    >>> S = list(product('HT', repeat=3))
    >>> [sum(w[k] == 'H' for w in S) / 8 for k in range(3)]
    [0.5, 0.5, 0.5]
    >>> 0.5 + 0.5 + 0.5
    1.5
  8. Variance: the spread

    Variance measures spread: Var(X) = E[X²] − (E[X])². Here E[X²] = (0 + 3 + 12 + 9)/8 = 3, so Var(X) = 3 − 1.5² = 0.75. Its square root, the standard deviation, is about 0.87.

    >>> from fractions import Fraction as F
    >>> p = {0: F(1, 8), 1: F(3, 8), 2: F(3, 8), 3: F(1, 8)}
    >>> EX2 = sum(x**2 * p[x] for x in p)
    >>> EX2, EX2 - F(3, 2)**2
    (Fraction(3, 1), Fraction(3, 4))
    >>> float(EX2 - F(3, 2)**2) ** 0.5      # standard deviation
    0.8660254037844386
  9. Averages settle near E[X]

    Now simulate it. With random.seed(1), Python's average of X is 1.9 after 10 tries, 1.5 after 100 and 1.4937 after 10,000. The law of large numbers says the average settles near E[X] = 1.5.

    >>> import random
    >>> random.seed(1)
    >>> def X(): return random.choices('HT', k=3).count('H')
    ...
    >>> for n in (10, 100, 10_000):
    ...     xs = [X() for _ in range(n)]
    ...     print(n, sum(xs) / n)
    ...
    10 1.9
    100 1.5
    10000 1.4937
  10. The exact answer in code

    In code, list the sample space with itertools.product, apply X to every outcome and take the mean. All 8 outcomes are equally likely, so sum(X) / len(S) is exactly E[X] = 1.5.

    >>> from itertools import product
    >>> S = list(product('HT', repeat=3))
    >>> X = [w.count('H') for w in S]
    >>> sum(X) / len(S)
    1.5
    >>> from fractions import Fraction
    >>> Fraction(sum(X), len(S))
    Fraction(3, 2)

اختبار يتاح بعد الخطوة الأخيرة.