第2章準備
2.6.1. A Simple Example: Tossing Coins2.6.1 簡単な例:硬貨投げ
Imagine that we plan to toss a coin and want to quantify how likely we are to see heads (vs. tails). If the coin is fair, then both outcomes (heads and tails), are equally likely. Moreover if we plan to toss the coin times then the fraction of heads that we expect to see should exactly match the expected fraction of tails. One intuitive way to see this is by symmetry: for every possible outcome with heads and tails, there is an equally likely outcome with heads and tails. Note that this is only possible if on average we expect to see of tosses come up heads and come up tails. Of course, if you conduct this experiment many times with tosses each, you might never see a trial where exactly.
英語のヒント
硬貨を投げるつもりで、表が出る(裏ではなく)可能性がどれほどかを数値で表したいとしよう。硬貨が公正なら、両方の結果(表と裏)は同じくらい起こりやすい。しかも、硬貨を回投げるつもりなら、表が出ると期待される割合は、裏が出ると期待される割合とちょうど一致するはずである。これを直観的に理解する一つの方法は対称性である。表が回、裏が回の起こりうる結果それぞれに対して、表が回、裏が回の同じくらい起こりやすい結果がある。これは、平均して投げた回数ので表が、で裏が出ると期待する場合にしか成り立たないことに注意してほしい。もちろん、回ずつ投げる実験を何度も行っても、がちょうど成り立つ試行には一度も出会わないかもしれない。
Formally, the quantity is called a probability and here it captures the certainty with which any given toss will come up heads. Probabilities assign scores between and to outcomes of interest, called events. Here the event of interest is and we denote the corresponding probability . A probability of indicates absolute certainty (imagine a trick coin where both sides were heads) and a probability of indicates impossibility (e.g., if both sides were tails). The frequencies and are not probabilities but rather statistics. Probabilities are theoretical quantities that underly the data generating process. Here, the probability is a property of the coin itself. By contrast, statistics are empirical quantities that are computed as functions of the observed data. Our interests in probabilistic and statistical quantities are inextricably intertwined. We often design special statistics called estimators that, given a dataset, produce estimates of model parameters such as probabilities. Moreover, when those estimators satisfy a nice property called consistency, our estimates will converge to the corresponding probability. In turn, these inferred probabilities tell about the likely statistical properties of data from the same population that we might encounter in the future.
英語のヒント
形式的には、量は確率と呼ばれ、ここでは、ある一回の投げで表が出ることの確からしさをとらえている。確率は、事象と呼ばれる関心のある結果に、からのあいだの得点を割り当てる。ここで関心のある事象はで、対応する確率をと書く。確率は絶対的な確実さを(両面が表のいかさま硬貨を想像してほしい)、確率は不可能を(たとえば両面が裏の場合)示す。頻度とは確率ではなく、統計量である。確率は、データを生み出す過程の根底にある理論的な量である。ここで確率は硬貨そのものの性質である。これに対して統計量は、観測データの関数として計算される経験的な量である。確率的な量と統計的な量への関心は、切り離せないほど絡み合っている。データセットが与えられたとき確率のようなモデルのパラメータの推定値を出す、推定量と呼ばれる特別な統計量を設計することがよくある。さらに、こうした推定量が一致性という好ましい性質を満たすとき、推定値は対応する確率に収束する。そして今度は、こうして推定された確率が、将来出会うかもしれない同じ母集団のデータの、ありそうな統計的性質について教えてくれる。
Suppose that we stumbled upon a real coin for which we did not know the true . To investigate this quantity with statistical methods, we need to (i) collect some data; and (ii) design an estimator. Data acquisition here is easy; we can toss the coin many times and record all the outcomes. Formally, drawing realizations from some underlying random process is called sampling. As you might have guessed, one natural estimator is the ratio of the number of observed heads to the total number of tosses.
英語のヒント
本当のがわからない現実の硬貨に出くわしたとしよう。この量を統計的手法で調べるには、(i)データを集め、(ii)推定量を設計する必要がある。ここでのデータ収集は簡単で、硬貨を何度も投げてすべての結果を記録すればよい。形式的には、何らかの元にある確率過程から実現値を取り出すことを、サンプリングという。察しがつくかもしれないが、自然な推定量の一つは、観測された表の回数と投げた総数との比である。
Now, suppose that the coin was in fact fair, i.e., . To simulate tosses of a fair coin, we can invoke any random number generator. There are some easy ways to draw samples of an event with probability . For example Python’s
random.random yields numbers in the interval where the probability of lying in any sub-interval is equal to . Thus we can get out 0 and 1 with probability 0.5 each by testing whether the returned float number is greater than 0.5:英語のヒント
さて、硬貨は実は公正、つまりだったとしよう。公正な硬貨の投げをシミュレーションするには、どんな乱数生成器を呼び出してもよい。確率の事象のサンプルを取り出す簡単な方法がいくつかある。たとえばPythonの
random.randomは、区間の数を返し、任意の部分区間に入る確率はに等しい。したがって、返された浮動小数点数が0.5より大きいかどうかを調べれば、0と1をそれぞれ確率0.5と0.5で得られる。num_tosses = 100
heads = sum([random.random() > 0.5 for _ in range(num_tosses)])
tails = num_tosses - heads
print("heads, tails: ", [heads, tails])heads, tails: [44, 56]More generally, we can simulate multiple draws from any variable with a finite number of possible outcomes (like the toss of a coin or roll of a die) by calling the multinomial function, setting the first argument to the number of draws and the second as a list of probabilities associated with each of the possible outcomes. To simulate ten tosses of a fair coin, we assign probability vector
[0.5, 0.5], interpreting index 0 as heads and index 1 as tails. The function returns a vector with length equal to the number of possible outcomes (here, 2), where the first component tells us the number of occurrences of heads and the second component tells us the number of occurrences of tails.英語のヒント
より一般に、(硬貨投げやさいころ振りのような)起こりうる結果の数が有限の変数からの複数回の取り出しは、multinomial関数を呼び出すことでシミュレーションできる。第1引数に取り出しの回数を、第2引数に起こりうる各結果に対応する確率のリストを設定する。公正な硬貨を10回投げることをシミュレーションするには、確率ベクトル
[0.5, 0.5]を割り当て、インデックス0を表、インデックス1を裏と解釈する。この関数は、長さが起こりうる結果の数(ここでは2)に等しいベクトルを返す。その第1成分は表の出た回数を、第2成分は裏の出た回数を教えてくれる。fair_probs = torch.tensor([0.5, 0.5])
Multinomial(100, fair_probs).sample()tensor([50., 50.])Each time you run this sampling process, you will receive a new random value that may differ from the previous outcome. Dividing by the number of tosses gives us the frequency of each outcome in our data. Note that these frequencies, just like the probabilities that they are intended to estimate, sum to .
英語のヒント
このサンプリングの過程を実行するたびに、前の結果とは違うかもしれない新しい乱数値が得られる。投げた回数で割ると、データのなかでの各結果の頻度が得られる。これらの頻度は、推定しようとしている確率と同じく、和がになることに注意してほしい。
Multinomial(100, fair_probs).sample() / 100tensor([0.4800, 0.5200])Here, even though our simulated coin is fair (we ourselves set the probabilities
[0.5, 0.5]), the counts of heads and tails may not be identical. That is because we only drew a relatively small number of samples. If we did not implement the simulation ourselves, and only saw the outcome, how would we know if the coin were slightly unfair or if the possible deviation from was just an artifact of the small sample size? Let’s see what happens when we simulate 10,000 tosses.英語のヒント
ここで、シミュレーションした硬貨は公正である(確率
[0.5, 0.5]は私たち自身が設定した)にもかかわらず、表と裏の回数は同じにならないかもしれない。それは、取り出したサンプルが比較的少ないからである。もし自分でシミュレーションを実装しておらず、結果だけを見たのなら、硬貨がわずかに不公正なのか、それともからのずれはサンプルの大きさが小さいことによる見かけにすぎないのか、どうやって知ればよいだろうか。10,000回投げるシミュレーションをすると何が起きるかを見てみよう。counts = Multinomial(10000, fair_probs).sample()
counts / 10000tensor([0.4966, 0.5034])In general, for averages of repeated events (like coin tosses), as the number of repetitions grows, our estimates are guaranteed to converge to the true underlying probabilities. The mathematical formulation of this phenomenon is called the law of large numbers and the central limit theorem tells us that in many situations, as the sample size grows, these errors should go down at a rate of . Let’s get some more intuition by studying how our estimate evolves as we grow the number of tosses from 1 to 10,000.
英語のヒント
一般に、(硬貨投げのような)繰り返される出来事の平均については、繰り返しの回数が増えるにつれて、推定値は根底にある本当の確率に収束することが保証されている。この現象を数学的に定式化したものを大数の法則といい、中心極限定理は、多くの状況で、サンプルの大きさが増えるにつれて誤差がの割合で小さくなっていくはずだと教えてくれる。投げる回数を1から10,000に増やすにつれて推定値がどう変わっていくかを調べ、もう少し直観をつかもう。
counts = Multinomial(1, fair_probs).sample((10000,))
cum_counts = counts.cumsum(dim=0)
estimates = cum_counts / cum_counts.sum(dim=1, keepdims=True)
estimates = estimates.numpy()
d2l.set_figsize((4.5, 3.5))
d2l.plt.plot(estimates[:, 0], label=("P(coin=heads)"))
d2l.plt.plot(estimates[:, 1], label=("P(coin=tails)"))
d2l.plt.axhline(y=0.5, color='black', linestyle='dashed')
d2l.plt.gca().set_xlabel('Samples')
d2l.plt.gca().set_ylabel('Estimated probability')
d2l.plt.legend();Each solid curve corresponds to one of the two values of the coin and gives our estimated probability that the coin turns up that value after each group of experiments. The dashed black line gives the true underlying probability. As we get more data by conducting more experiments, the curves converge towards the true probability. You might already begin to see the shape of some of the more advanced questions that preoccupy statisticians: How quickly does this convergence happen? If we had already tested many coins manufactured at the same plant, how might we incorporate this information?
英語のヒント
実線の曲線はそれぞれ硬貨の二つの値の一方に対応し、実験の各回の後で硬貨がその値を出す確率の推定値を示す。黒い破線は、根底にある本当の確率を示す。実験を重ねてデータが増えるほど、曲線は本当の確率に向かって収束する。統計学者が頭を悩ませる、もっと発展的な問いの形がもう見え始めているかもしれない。この収束はどれほど速く起こるのか。同じ工場で作られた硬貨をすでにたくさん試していたら、その情報をどう取り込めばよいのか。