Probabilistic Programming
=====
and Bayesian Methods for Hackers
========

#####Version 0.1
Welcome to *Bayesian Methods for Hackers*. The full Github repository is available at [github/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers](https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers). The other chapters can be found on the project's [homepage](camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/). We hope you enjoy the book, and we encourage any contributions!

第1章
======
***

ベイズ推論の考え方
------
<!--
The Philosophy of Bayesian Inference
------
-->

> あなたは優秀なプログラマーだ．しかし，だれしも書いたコードにバグはある．実装するのが非常に難しいアルゴリズムのコードをなんとか書いた後，そのコードが正しいかどうかを簡単な例題でテストしようと考えた．OK，テストにパスした．次にもっと難しい問題でコードをテストした．今度もパスした．そして，なんと*もっともっと難しい問題*でも，パスした！　だからあなたは，このコードにはバグはないかもしれないと思い始めてしまった．．．
<!--
> You are a skilled programmer, but bugs still slip into your code. After a particularly difficult implementation of an algorithm, you decide to test your code on a trivial example. It passes. You test the code on a harder problem. It passes once again. And it passes the next, *even more difficult*, test too! You are starting to believe that there may be no bugs in this code...
-->

もしこのように考えたことがあるのなら，おめでとう！　あなたはベイズ的に考る人の仲間入りだ．ベイズ推論とは，
新しい証拠が得られるたびに自分の考えを改めるというものである．
ベイズ的に考える人は，ある結果が必ず起こるとは考えない．たぶん起こるだろうと考えるのである．
上の例のように，普通は，プログラムに100%まったくバグがないとは考えない．
ないと言い切るには，実際にはありえないような場合も含めて，すべての場合についてチェックしなければならないだろう．
それよりも，たくさんの問題にたいしてテストして，それら全てにパスしたら，
プログラムにバグは「たぶんないだろう」と思うのである．しかし「まったくない」とは言い切れない．
ベイズ推論も同じである．情報が得られたら信念を更新する．すべての可能な場合をチェックしなければ，絶対に，とは言わないのである．

<!--
If you think this way, then congratulations, you already are thinking Bayesian! Bayesian inference is simply updating your beliefs after considering new evidence. A Bayesian can rarely be certain about a result, but he or she can be very confident. Just like in the example above, we can never be 100% sure that our code is bug-free unless we test it on every possible problem; something rarely possible in practice. Instead, we can test it on a large number of problems, and if it succeeds we can feel more *confident* about our code, but still not certain.  Bayesian inference works identically: we update our beliefs about an outcome; rarely can we be absolutely sure unless we rule out all other alternatives.
-->

### 考え方のベイズ的な考え方
<!-- ### The Bayesian state of mind -->

ベイス推論が伝統的な統計的推論と異なるのは，
「不確実」なものは不確実なままにするという点である．
不確実なままということに，最初はダメな方法だと思うかもしれない．
統計とはランダムな現象から確実さを引き出すものではなかったのか？
これを理解するには，ベイズ的に考えることが必要になる．

<!--
Bayesian inference differs from more traditional statistical inference by preserving *uncertainty*. At first, this sounds like a bad statistical technique. Isn't statistics all about deriving *certainty* from randomness? To reconcile this, we need to start thinking like Bayesians.
-->

ベイズ的な考え方，つまりベイズ主義(Bayesian)では，
確率を「ある出来事がどのくらい信頼できるか」を表す指標と解釈する．
つまり，ある事象が生じるということを，
どのくらい確かだと思っているのか表すものと考える．
すぐあとで見るように，実際にこれが確率を解釈する自然な方法である．
<!--
The Bayesian world-view interprets probability as measure of *believability in an event*, that is, how confident we are in an event occurring. In fact, we will see in a moment that this is the natural interpretation of probability.
-->

この解釈をもっと分かりやすくするために，
確率のもう一つの解釈を考えてみよう．
それは「頻度主義」(*Frequentist*)というものであり，
もっと*古典的*な統計学である．
頻度主義では，確率を「長期間における事象の頻度」とみなす
（だから「頻度主義」という名前で呼ばれている）．
たとえば，*飛行機事故の確率*を頻度主義で考えれば，
「長期間における飛行機事故の頻度」になる．
この考え方は，多くの場合，事象の確率として意味がある．
しかし長期間にわたって事象が発生しないような場合には，
理解することが難しくなる．
例えば大統領選挙の結果の確率を計算しようとしても，
ある特定の選挙は1回きりしか行われないのだ！
頻度主義でこの問題を避けるには，
他のすべての選挙も考慮して，
これらの発生する頻度で確率を定義することになる．

<!--
For this to be clearer, we consider an alternative interpretation of probability: *Frequentist*, known as the more *classical* version of statistics, assume that probability is the long-run frequency of events (hence the bestowed title). For example, the *probability of plane accidents* under a frequentist philosophy is interpreted as the *long-term frequency of plane accidents*. This makes logical sense for many probabilities of events, but becomes more difficult to understand when events have no long-term frequency of occurrences. Consider: we often assign probabilities to outcomes of presidential elections, but the election itself only happens once! Frequentists get around this by invoking alternative realities and saying across all these realities, the frequency of occurrences defines the probability.
-->

一方のベイズ主義では，もっと直感的に考える．
ベイズ主義では，確率を，
ある事象が発生する信念(*belief*)
もしくは確信(confidence)の度合いとみなす．
確率とは，思っていることを要約したものであるだけなのである．
ある人が，ある事象の信念を0だと思っている場合，
その事象が発生するとは考えていないことになる．
反対に，ある事象の信念を1だと思っている場合，
その事象が必ず発生すると考えていることになる．
信念を0から1の実数値で表せば，それを使って
他の結果に重みを付けることができる．
この定義を使えば，飛行機事故の確率の例を
うまく表現することができる．
飛行機事故の頻度が得られた時に，他の情報が何もなければ，
ある人の信念はその頻度に一致するべきである．
同様に，確率が信念であるという定義を使えば，
大統領選挙の結果の確率（信念）というものを考えてもよいだろう．
つまり，ある候補者Aが当選することをどのくらい確信しているのか，
を表しているのである．

<!--
Bayesians, on the other hand, have a more intuitive approach. Bayesians interpret a probability as measure of *belief*, or confidence, of an event occurring. Simply, a probability is a summary of an opinion. An individual who assigns a belief of 0 to an event has no confidence that the event will occur; conversely, assigning a belief of 1 implies that the individual is absolutely certain of an event occurring. Beliefs between 0 and 1 allow for weightings of other outcomes. This definition agrees with the probability of a plane accident example, for having observed the frequency of plane accidents, an individual's belief should be equal to that frequency, excluding any outside information. Similarly, under this definition of probability being equal to beliefs, it is meaningful to speak about probabilities (beliefs) of presidential election outcomes: how confident are you candidate *A* will win?
-->

上のパラグラフでは，一般的な信念（確率）ではなく，
「ある人」の信念（確率）というものを説明していたことに注意して欲しい．
これが面白いのは，人によって信念が違うということを定義が許していることになる点である．
これは日常では見かける．この世界について持っている情報は人それぞれ違うので，
ある人の信念は別の人の信念とは違うのである．
信念が違っているということは，「誰かが間違っている」ということではない．
以下の例で，各個人が持っている信念と確率との関係を考えてみてみよう．

<!--
Notice in the paragraph above, I assigned the belief (probability) measure to an *individual*, not to Nature. This is very interesting, as this definition leaves room for conflicting beliefs between individuals. Again, this is appropriate for what naturally occurs: different individuals have different beliefs of events occurring, because they possess different *information* about the world. The existence of different beliefs does not imply that anyone is wrong. Consider the following examples demonstrating the relationship between individual beliefs and probabilities:
-->

- 私がコインを投げて，表が出るか裏が出るか，わたしとあなたが賭けているとしよう．イカサマのコインでなければ，表が出る確率は1/2である．これはわたしもあなたも同意している．ここで，私だけがコインの結果を除いてみたとする．そうすると，私にとっては表か裏の確率のどちらかが1.0になる（コインの結果による）．では「コインが表である」について，あなたの信念はどうだろう？　私がコインの結果を知っても，コインの結果は変わらない．私とあなたの信念は，同じではなくなってしまった．
- あなたのプログラムにはバグがあるかもしれないし，ないかもしれない．あなたも私も，どちらが正しいのか分からないが，バグがあるのかないのかについての信念は持っている．
- 病院において，ある患者がx，y，zという症状を自覚している．それらの症状を発生する病気はたくさんあるが，どれか一つの病気が原因である．ある医師はその原因がある病気だろうという思っているが，別の医師はすこし違った原因を思っているかもしれない．

<!--
- I flip a coin, and we both guess the result. We would both agree, assuming the coin is fair, that the probability of Heads is 1/2. Assume, then, that I peek at the coin. Now I know for certain what the result is: I assign probability 1.0 to either Heads or Tails (whichever it is). Now what is *your* belief that the coin is Heads? My knowledge of the outcome has not changed the coin's results. Thus we assign different probabilities to the result.

-  Your code either has a bug in it or not, but we do not know for certain which is true, though we have a belief about the presence or absence of a bug.

-  A medical patient is exhibiting symptoms $x$, $y$ and $z$. There are a number of diseases that could be causing all of them, but only a single disease is present. A doctor has beliefs about which disease, but a second doctor may have slightly different beliefs.
-->

人間にとっては，確率を信念のように扱うということは自然なやり方である．この世の中で生きていくために，いつもこのようなやり方をしているし，真実というものが完全ではないという例もたくさん見ている．それでも信念から何か情報がえられないかと考えてもいる．もし頻度主義のように考えるとするなら，かなり訓練しなければならないだろう．

<!--
This philosophy of treating beliefs as probability is natural to humans. We employ it constantly as we interact with the world and only see partial truths, but gather evidence to form beliefs. Alternatively, you have to be *trained* to think like a frequentist.
-->

従来の確率論の記法に従って，
ある事象$A$が生じるという信念を$P(A)$と表し，
事前確率(*prior probability*)と呼ぶことにする．

<!--
To align ourselves with traditional probability notation, we denote our belief about event $A$ as $P(A)$. We call this quantity the *prior probability*.
-->

偉大な経済学者であり思想家であるジョン・メイナード・ケインズ曰く，
「事実が変わったならば，わたしは考えを改める．あなたはどうしますか？」
これは証拠が得られた後に信念を更新す，ベイズ主義的なやり方である．
もし，その証拠が最初に思っていた信念と相反することであったとしても，
証拠を無視することはできない．
この更新された信念を$P(A |X )$と表し，
証拠$X$が与えられた時の$A$の確率である，と解釈する．
事前確率に対応して，この更新された信念を事後確率(*posterior probability*)と呼ぶ．
例えば上記の例では，証拠$X$が得られた後の事後確率（事後信念と言ってもよい）は次のようになる．

<!--
John Maynard Keynes, a great economist and thinker, said "When the facts change, I change my mind. What do you do, sir?" This quote reflects the way a Bayesian updates his or her beliefs after seeing evidence. Even — especially — if the evidence is counter to what was initially believed, the evidence cannot be ignored. We denote our updated belief as $P(A |X )$, interpreted as the probability of $A$ given the evidence $X$. We call the updated belief the *posterior probability* so as to contrast it with the prior probability. For example, consider the posterior probabilities (read: posterior beliefs) of the above examples, after observing some evidence $X$:
-->

1\. $P(A): \;\;$ コインの表が出る確率は50パーセントである．$P(A | X):\;\;$ コインの結果をみて表が出ていたとする．この情報を$X$とする．明らかに，表の事後確率は1.0で，裏の事後確率は0.0である．

2\.   $P(A): \;\;$  この複雑で巨大なプログラムにはたぶんバグがある．$P(A | X): \;\;$ プログラムはすべてのテスト$X$にパスした．たぶんバグがあるかもしれないが，その可能性は非常に小さいだろう．

3\.  $P(A):\;\;$ ある患者が病気にかかっている．$P(A | X):\;\;$ 血液検査の結果，$X$という証拠が得られたので，いくつかの病気の可能性は排除してもよいだろう．

<!--
1\. $P(A): \;\;$ the coin has a 50 percent chance of being Heads. $P(A | X):\;\;$ You look at the coin, observe a Heads has landed, denote this information $X$, and trivially assign probability 1.0 to Heads and 0.0 to Tails.

2\.   $P(A): \;\;$  This big, complex code likely has a bug in it. $P(A | X): \;\;$ The code passed all $X$ tests; there still might be a bug, but its presence is less likely now.

3\.  $P(A):\;\;$ The patient could have any number of diseases. $P(A | X):\;\;$ Performing a blood test generated evidence $X$, ruling out some of the possible diseases from consideration.
-->

これらの例から明らかなように，新しい証拠$X$が得られたとしても，
事前の信念を完全に否定することはなく，新しい証拠を事前確率の重みとして使っている
（つまり，ある信念にはより大きい重み，つまり確信度を与えるのである）．
<!--
It's clear that in each example we did not completely discard the prior belief after seeing new evidence $X$, but we *re-weighted the prior* to incorporate the new evidence (i.e. we put more weight, or confidence, on some beliefs versus others).
-->

事前にある事象がどのくらい生じるのかということを考えても，それは非常に不確実である．
したがって，どんな結果を予想したとしても，間違っている可能性が高い．
データや証拠や情報が得られれば，信念を更新して，
間違っている可能性がもっと少ない予測をすることができるようになる．
コインの裏表を予測すること例では，正しい予測ができるようになる．
<!--
By introducing prior uncertainty about events, we are already admitting that any guess we make is potentially very wrong. After observing data, evidence, or other information, we update our beliefs, and our guess becomes *less wrong*. This is the alternative side of the prediction coin, where typically we try to be *more right*.
-->

### 実用的なベイズ推論
<!--
### Bayesian Inference in Practice
-->

頻度主義とベイス主義がプログラミング言語の関数だったら，統計的な問題を入力すると，ユーザーに返される結果は同じではないだろう．
頻度主義の推論関数の戻り値は，推定値を表す数値である（標本平均などの要約統計量であることが多い）．
一方でベイズ主義の推論関数は，「確率」を返す．
<!--
If frequentist and Bayesian inference were programming functions, with inputs being statistical problems, then the two would be different in what they return to the user. The frequentist inference function would return a number, representing an estimate (typically a summary statistic like the sample average etc.), whereas the Bayesian function would return *probabilities*.
-->

例えばデバッグの例題であれば，頻度主義関数の引数に
「このプログラムはテスト$X$のすべてをパスしたんだ．このプログラムにはバグがないかな？」を渡すと，
戻り値は「バグはありません」だろう．
しかしベイズ主義関数に
「プログラムを書くといつもバグがあるんだ．
このプログラムはテスト$X$のすべてをパスしたんだ．このプログラムにはバグがないかな？」という引数を渡すと，
「バグはありません」と「バグがあります」の答えのそれぞれに確率が返される．

<!--
For example, in our debugging problem above, calling the frequentist function with the argument "My code passed all $X$ tests; is my code bug-free?" would return a *YES*. On the other hand, asking our Bayesian function "Often my code has bugs. My code passed all $X$ tests; is my code bug-free?" would return something very different: probabilities of *YES* and *NO*. The function might return:
-->

> バグがない確率は0.8，バグがある確率は0.2です．
<!--
>    *YES*, with probability 0.8; *NO*, with probability 0.2
-->

この戻り値は頻度主義関数の戻り値とはまったく違うものである．
ベイズ主義関数は引数に「プログラムを書くといつもバグがあるんだ」という情報を追加していることに気がついてほしい．
これが**事前情報**(prior)である．
この事前情報パラメータを引数に与えることで，今の状況についての信念をベイズ主義関数に伝えている．
これを与えるかどうかはユーザーの自由だが，与えない場合には別の結果が得られることになる．
その例は後で見ることにしよう．
<!--
This is very different from the answer the frequentist function returned. Notice that the Bayesian function accepted an additional argument:  *"Often my code has bugs"*. This parameter is the *prior*. By including the prior parameter, we are telling the Bayesian function to include our belief about the situation. Technically this parameter in the Bayesian function is optional, but we will see excluding it has its own consequences.
-->

#### 証拠を取り入れる
<!--
####Incorporating evidence
-->

証拠をたくさん手に入れることができれば，事前の信念は，その多数の証拠にかき消されてしまう．
これは想像できるだろう．
例えば，あなたが「今日，太陽が爆発するんじゃないか」という事前信念を持っていたとすれば，日に日にその信念は揺らいでいき，
そして，どんな推論でもいいから自分の間違いを正してくれ，少なくともこの信念をもっとマシなものにしてくれ，
と思うようになる（かもしれない）．
そしてベイズ推論は，その信念を正してくれる．
<!--
As we acquire more and more instances of evidence, our prior belief is *washed out* by the new evidence. This is to be expected. For example, if your prior belief is something ridiculous, like "I expect the sun to explode today", and each day you are proved wrong, you would hope that any inference would correct you, or at least align your beliefs better. Bayesian inference will correct this belief.
-->

$N$を手に入る証拠の数とする．もし無限個の証拠が手に入れば，つまり$N \rightarrow \infty$ならば，
ベイズ推論の結果は頻度主義の結果と（多くの場合）一致する．
したがって$N$が大きくなれば，統計的推論は客観的なものになる．
反対に$N$が小さければ，推論は*不安定*なものになる．
頻度主義の推定値は分散も信頼区間も大きくなる．
そんな時にはベイズ推論の出番である．
事前分布を引数にとり，結果に（推定値ではなく）確率を出力する．
これは，$N$の小さいデータセットに対する統計的推論の不安定さを反映した，不確実さを表すものになっている．

<!--
Denote $N$ as the number of instances of evidence we possess. As we gather an *infinite* amount of evidence, say as $N \rightarrow \infty$, our Bayesian results (often) align with frequentist results. Hence for large $N$, statistical inference is more or less objective. On the other hand, for small $N$, inference is much more *unstable*: frequentist estimates have more variance and larger confidence intervals. This is where Bayesian analysis excels. By introducing a prior, and returning probabilities (instead of a scalar estimate), we *preserve the uncertainty* that reflects the instability of statistical inference of a small $N$ dataset.
-->

$N$が非常に大きい場合は，頻度主義とベイズ主義は似たような推論結果を出してくるので，二つの区別はつかなくなるだろう．
そのため，少ない計算で済む頻度主義を用いたくなるかもしれない．
もしそんな状況にあるのであれば，そうする前に以下の
[Andrew Gelman (2005)][1]の文章を読んでほしい．
<!--
One may think that for large $N$, one can be indifferent between the two techniques since they offer similar inference, and might lean towards the computationally-simpler, frequentist methods. An individual in this position should consider the following quote by Andrew Gelman (2005)[1], before making such a decision:
-->

> サンプル数が大きい場合，というものは存在しない．もし$N$が小さすぎて十分に正確な推定値を得ることができないのであれば，データをもっと増やす（もしくはもっと多くの仮定を使う）必要がある．しかし，もし$N$が「十分に大きい」のであれば，データを分割してもっと多くの情報を得ることができるだろう（例えば世論調査の場合には，全国区での良い推定値が得られたら，次は男女別，地域別，年齢別の推定値を得ることもできるだろう）．$N$が十分であることはない．もし「十分」だとしたら，あなたはもうすでにもっと多くのデータを必要とする次の問題に取り組んでいるのだ．

<!--
> Sample sizes are never large. If $N$ is too small to get a sufficiently-precise estimate, you need to get more data (or make more assumptions). But once $N$ is "large enough," you can start subdividing the data to learn more (for example, in a public opinion poll, once you have a good estimate for the entire country, you can estimate among men and women, northerners and southerners, different age groups, etc.). $N$ is never enough because if it were "enough" you'd already be on to the next problem for which you need more data.
-->

### じゃあ頻度主義は間違っているの？
<!--
### Are frequentist methods incorrect then?
-->

**間違ってはいない．**
<!--
**No.**
-->

頻度主義の方法は今でも多くの分野で有用であり，最先端で使われれいる．
最小二乗回帰やlasso回帰，EMアルゴリズムなどのツールはどれも優れていて処理も速い．
ベイズ主義の手法は，それらの手法を補うものである．
それらの手法が適用できない問題を解いたり，
もっと柔軟なモデル化で隠れた構造を解き明かしたりするのである．
<!--
Frequentist methods are still useful or state-of-the-art in many areas. Tools such as least squares linear regression, LASSO regression, and expectation-maximization algorithms are all powerful and fast. Bayesian methods complement these techniques by solving problems that these approaches cannot, or by illuminating the underlying system with more flexible modeling.
-->

### 「ビッグデータ」について
<!--
#### A note on *Big Data*
-->

逆説的に聞こえるかもしれないが，ビッグデータで予測したり解析したりする問題には，
実際には比較的単純なアルゴリズムが使われている[2][4]．
ビッグデータを用いた予測の難しさは，アルゴリズムにあるのではない．
ビッグデータを保存し読み出すストレージや
ビッグデータに対して実行する時の計算量が大変なのである．
（上述のGelmanの文章を読んで「自分は本当にビッグデータを持っているのだろうか？」と考えてみてほしい）
<!--
Paradoxically, big data's predictive analytic problems are actually solved by relatively simple algorithms [2][4]. Thus we can argue that big data's prediction difficulty does not lie in the algorithm used, but instead on the computational difficulties of storage and execution on big data. (One should also consider Gelman's quote from above and ask "Do I really have big data?" )
-->

解析するのがもっと難しい問題は，「ミディアムなデータ」の場合であり，
特に問題となるのは「スモールデータ」の場合である．
Gelmanの文章を借りるなら，ビッグデータの問題が「十分にビッグ」で実際には解けないのであれば，
「それほど十分にビッグではない」データを扱えばよいのである．
<!--
The much more difficult analytic problems involve *medium data* and, especially troublesome, *really small data*. Using a similar argument as  Gelman's above, if big data problems are *big enough* to be readily solved, then we should be more interested in the *not-quite-big enough* datasets.
-->

### ここでのベイズ推論の枠組み
<!--
### Our Bayesian framework
-->

計算するべき信念は，ベイズ的に考えた確率と解釈することができる．
ここで，ある事象$A$について「事前」信念を持っているとしよう
（例えば，テストを実行する前に，プログラムにバグがありそうかどうかについての信念）．

<!--
We are interested in beliefs, which can be interpreted as probabilities by thinking Bayesian. We have a *prior* belief in event $A$, beliefs formed by previous information, e.g., our prior belief about bugs being in our code before performing tests.
-->

次は，得られた証拠を使おう．バグありプログラムの例を使えば，
プログラムはテスト$X$にパスしたので，その情報を取り入れて信念を更新したい．
この更新された新しい信念を「事後」信念と呼ぶことにする．
以下の式を使えば，信念を更新することができる．
この式は，発見者のトーマス・ベイズにちなんで，ベイズの定理と呼ばれている．
<!--
Secondly, we observe our evidence. To continue our buggy-code example: if our code passes $X$ tests, we want to update our belief to incorporate this. We call this new belief the *posterior* probability. Updating our belief is done via the following equation, known as Bayes' Theorem, after its discoverer Thomas Bayes:
-->

\begin{align}
 P( A | X ) = & \frac{ P(X | A) P(A) } {P(X) } \\\\[5pt]
& \propto P(X | A) P(A)\;\; (\propto \text{は比例を表す} )
\end{align}

<!--
 P( A | X ) = & \frac{ P(X | A) P(A) } {P(X) } \\\\[5pt]
& \propto P(X | A) P(A)\;\; (\propto \text{is proportional to } )
-->

この公式はベイズ推論だけのものではない．ベイズ推論以外でも使われている数学的事実である．
ベイズ推論では，単にこの式を使って
初期の事前確率$P(A)$と更新後の事後確率$P(A | X)$を結びつけているだけである．
<!--
The above formula is not unique to Bayesian inference: it is a mathematical fact with uses outside Bayesian inference. Bayesian inference merely uses it to connect prior probabilities $P(A)$ with an updated posterior probabilities $P(A | X )$.
-->

##### 例題：だれもが一度はやる「コイン投げ」の問題
<!--
##### Example: Mandatory coin-flip example
-->

統計学のテキストであれば，コイン投げの問題を扱っていない本はない．
ちょっと変わったやり方でこの問題を扱ってみよう．
あなたは，コインの表が出る確率がよくわからないとする（本当は50%）．
何らかの比率（ここでは$p$とする）で表裏がでるということについては信じているが，
その$p$がどのくらいなのかについては，まったく情報を持っていない．
<!--
Every statistics text must contain a coin-flipping example, I'll use it here to get it out of the way. Suppose, naively, that you are unsure about the probability of heads in a coin flip (spoiler alert: it's 50%). You believe there is some true underlying ratio, call it $p$, but have no prior opinion on what $p$ might be.
-->

ではコイン投げを初めて，表$H$が出たのか裏$T$が出たのかを記録することにする．
ここでちょっと考えてみよう．
データが増えるにつれて，推論結果はどのように変わっていくのだろうか？
もっと正確に言えば，データが少ない時とデータが多い時とで，事後確率はどのように違うのだろうか？
<!--
We begin to flip a coin, and record the observations: either $H$ or $T$. This is our observed data. An interesting question to ask is how our inference changes as we observe more and more data? More specifically, what do our posterior probabilities look like when we have little data, versus when we have lots of data.
-->

以下のコードは，
（コイン投げの）データが増えるたびに更新される事後確率の系列をプロットするものである．
<!--
Below we plot a sequence of updating posterior probabilities as we observe increasing amounts of data (coin flips).
-->

```
"""
本書ではmatplotlibのグラフのスタイルを変更するために，matplotlibrcファイルをカスタマイズしている．
本書を実行して，本書のスタイルを使いたいのであれば，以下の2つの方法がある．
    1. 本書の style/ ディレクトリにあるrcフィアルで，自分の環境のmatplotlibrcを書き換える．
       http://matplotlib.org/users/customizing.htmlを参照．
    2. スタイルはbmh_matplotlibrc.jsonファイルにもある．これを使って以下のコードを実行すれば，
       本書にだけスタイルを適用することができる．
        import json
        s = json.load( open("../styles/bmh_matplotlibrc.json") )
        matplotlib.rcParams.update(s)
"""

# 以下のコードは読み飛ばして構わない．ここではあまり重要ではないし，
# まだ説明していない進んだ内容も含んでいる．その下のグラフを見て！
%matplotlib inline
from IPython.core.pylabtools import figsize
import numpy as np
from matplotlib import pyplot as plt
figsize(11, 9)

import scipy.stats as stats

dist = stats.beta
n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
x = np.linspace(0, 1, 100)

# 分かっている人へ：ここでは二項分布の共役事前分布を使っている．
for k, N in enumerate(n_trials):
    sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
    # u"$p$, 表が出る確率"
    plt.xlabel("$p$, probability of heads") \
        if k in [0, len(n_trials) - 1] else None
    plt.setp(sx.get_yticklabels(), visible=False)
    heads = data[:N].sum()
    y = dist.pdf(x, 1 + heads, 1 + N - heads)
    plt.plot(x, y, label="observe %d tosses,\n %d heads" % (N, heads)) # u"%d回投げて, \n 表は%d回"
    plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
    plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)

    leg = plt.legend()
    leg.get_frame().set_alpha(0.4)
    plt.autoscale(tight=True)

plt.suptitle("Bayesian updating of posterior probabilities", # u"ベイズ推論による事後確率の更新"
             y=1.02,
             fontsize=14)

plt.tight_layout()
```

事後確率は曲線で表されている．
不確実さは，この曲線の広がり具合に比例している．
上のグラフを見れば分かるように，
データが増えるたびに事後確率の曲線は右へ左へと動き回る．
最終的に，データがたくさん手に入れば（たくさんコインを投げたら），
事後確率曲線は，真の確率である$p=0.5$に次第に集まってくる．
<!--
The posterior probabilities are represented by the curves, and our uncertainty is proportional to the width of the curve. As the plot above shows, as we start to observe data our posterior probabilities start to shift and move around. Eventually, as we observe more and more data (coin-flips), our probabilities will tighten closer and closer around the true value of $p=0.5$ (marked by a dashed line).
-->

なお，曲線のピークの位置は0.5ではないし，そうである理由もない．$p$の値については何も知らない，という前提だったのだから．
実際のとこｒ，コイン投げの結果が極端な，たとえば8回投げて表が1回しかなかったような場合には，
事後確率曲線のピークは0.5から非常に離れているだろう
（事前情報がないのだから，8回投げて表が1回しか出ないコインにイカサマはないと，どのくらい確信できるだろう？）．
もっとデータが増えれば，確率はもっと$p=0.5$に近くなるだろう．
<!--
Notice that the plots are not always *peaked* at 0.5. There is no reason it should be: recall we assumed we did not have a prior opinion of what $p$ is. In fact, if we observe quite extreme data, say 8 flips and only 1 observed heads, our distribution would look very biased *away* from lumping around 0.5 (with no prior opinion, how confident would you feel betting on a fair coin after observing 8 tails and 1 head). As more data accumulates, we would see more and more probability being assigned at $p=0.5$,
though never all of it.
-->

次の例で，数学がベイズ推論でどのように使われるのか見てみよう．
<!--
The next example is a simple demonstration of the mathematics of Bayesian inference.
-->

##### 例題：バグか，仕様か？
<!--
##### Example: Bug, or just sweet, unintended feature?
-->

「プログラムにはバグがない」という事象を$A$とする．
「このプログラムがすべてのデバッグテストにパスする」という事象を$X$とする．
とりあえず，バグがないという事前確率$P(A)$を変数$p$にしておこう．
つまり$P(A) = p$とする．
<!--
Let $A$ denote the event that our code has **no bugs** in it. Let $X$ denote the event that the code passes all debugging tests. For now, we will leave the prior probability of no bugs as a variable, i.e. $P(A) = p$.
-->

今から考えるのはこの$P(A|X)$だ．
つまり，「デバッグテスト$X$をパスした時に，バグがない」確率である．
上の公式を使うために，いくつか計算しなければならない．
<!--
We are interested in $P(A|X)$, i.e. the probability of no bugs, given our debugging tests $X$. To use the formula above, we need to compute some quantities.
-->

では$P(X | A)$とは何だろう？　これは，「バグがない時にすべてのテスト$X$をパスする」確率である．明らかに，これは1である．バグがなければ，どんなテストにもパスするからだ．
<!--
What is $P(X | A)$, i.e., the probability that the code passes $X$ tests *given* there are no bugs? Well, it is equal to 1, for a code with no bugs will pass all tests.
-->

それよりも厄介なのは$P(X)$である．事象$X$が起きる可能性は2つある．
実はバグがある（これを$\sim A$や$\lnot A$と書いて*not $A$*と読む）にも関わらず事象$X$が起こっているのか，それともバグがないから事象$X$が起きているのか，である．
すると，$P(X)$は以下のように解釈できる．
<!--
$P(X)$ is a little bit trickier: The event $X$ can be divided into two possibilities, event $X$ occurring even though our code *indeed has* bugs (denoted $\sim A\;$, spoken *not $A$*), or event $X$ without bugs ($A$). $P(X)$ can be represented as:
-->

\begin{align}
P(X ) & = P(X \text{ and } A) + P(X \text{ and } \sim A) \\\\[5pt]
 & = P(X|A)P(A) + P(X | \sim A)P(\sim A)\\\\[5pt]
& = P(X|A)p + P(X | \sim A)(1-p)
\end{align}

すでに$P(X|A)$は計算してある．しかし$P(X | \sim A)$をどうするかは，主観的である．
プログラムはテストをパスしたが，それでもバグがあるのだ．
しかしバグがある確率は小さくなっている．
これは実行したテストの数や，テストがどれだけ精巧なのかにも依存する．
ここでは控えめに考えて，$P(X|\sim A) = 0.5$としよう．すると以下のようになる．
<!--
We have already computed $P(X|A)$ above. On the other hand, $P(X | \sim A)$ is
subjective: our code can pass tests but still have a bug in it, though the probability there is a bug present is reduced. Note this is dependent on the number of tests performed, the degree of complication in the tests, etc. Let's be conservative and assign $P(X|\sim A) = 0.5$. Then
-->
\begin{align}
P(A | X) & = \frac{1\cdot p}{ 1\cdot p +0.5 (1-p) } \\\\
& = \frac{ 2 p}{1+p}
\end{align}
これが事後確率である．これを事前確率パラメータ$p \in [0,1]$の関数としてみたら，
どんな形をしているだろう？
<!--
This is the posterior probability. What does it look like as a function of our prior, $p \in [0,1]$?
-->

```
figsize(12.5, 4) # グラフの縦横サイズを12.5:4にする
p = np.linspace(0, 1, 50) # 0から1までを50点に分割
plt.plot(p, 2 * p / (1 + p), color="#348ABD", lw=3) # 事後確率をプロット．色は青系，線幅は3
# plt.fill_between(p, 2*p/(1+p), alpha=.5, facecolor=["#A60628"]) # コメントを外して試してみよう
plt.scatter(0.2, 2 * (0.2) / 1.2, s=140, c="#348ABD") # p=0.2のところに点を描画．色は青系，サイズは140
plt.xlim(0, 1) # x軸の範囲を(0,1)に設定
plt.ylim(0, 1) # y軸の範囲を(0,1)に設定
plt.xlabel("Prior, $P(A) = p$") # x軸ラベル u"事前確率$P(A) = p$"
plt.ylabel("Posterior, $P(A|X)$, with $P(A) = p$") # y軸ラベル u"$P(A) = p$の時の事後確率$P(A|X)$"
plt.title("Are there bugs in my code?") # グラフのタイトル u"プログラムにバグがあるか？"
```

事前確率$p$が小さい時は，テスト$X$にパスしたという証拠が非常に効いている．
ここで事前確率の値を一つ決めてみよう．
私は優秀なプログラマーなので（自分ではそう思っている），0.20でも現実的だろう．
つまり，20%の確率でバグのないプログラムを書くことができる，というわけだ．
もっとも現実的には，この事前確率は
プログラムがどれだけ複雑で大規模なのかにもよるのだが，
とりあえず0.20としておこう．
すると，プログラムにはバグがないという更新された信念は0.33となる．
<!--
We can see the biggest gains if we observe the $X$ tests passed when the prior probability, $p$, is low. Let's settle on a specific value for the prior. I'm a strong programmer (I think), so I'm going to give myself a realistic prior of 0.20, that is, there is a 20% chance that I write code bug-free. To be more realistic, this prior should be a function of how complicated and large the code is, but let's pin it at 0.20. Then my updated belief that my code is bug-free is 0.33.
-->

ここで事前確率は確率である，ということを思い出しておこう．
$p$はバグがない事前確率で，$1-p$はバグがある事前確率である
<!--
Recall that the prior is a probability: $p$ is the prior probability that there *are no bugs*, so $1-p$ is the prior probability that there *are bugs*.
-->

同様に，事後確率も確率である．
$P(A | X)$は「すべてのテストをパスしてバグがない」確率，
$1-P(A|X)$は「すべてのテストをパスしてバグがある」確率である．
この事後確率はどんな値だろうか？
以下のグラフは，事前確率と事後確率を計算したものである．
<!--
Similarly, our posterior is also a probability, with $P(A | X)$ the probability there is no bug *given we saw all tests pass*, hence $1-P(A|X)$ is the probability there is a bug *given all tests passed*. What does our posterior probability look like? Below is a chart of both the prior and the posterior probabilities.
-->

```
figsize(12.5, 4)
colours = ["#348ABD", "#A60628"]

prior = [0.20, 0.80]
posterior = [1. / 3, 2. / 3]
plt.bar([0, .7], prior, alpha=0.70, width=0.25,
        color=colours[0], label="prior distribution", # u"事前確率"
        lw="3", edgecolor=colours[0])

plt.bar([0 + 0.25, .7 + 0.25], posterior, alpha=0.7,
        width=0.25, color=colours[1],
        label="posterior distribution", # u"事後確率"
        lw="3", edgecolor=colours[1])

plt.xticks([0.20, .95], ["Bugs Absent", "Bugs Present"]) # u"バグがない", u"バグがある
plt.title("Prior and Posterior probability of bugs present") # u"バグがある事前確率と事後確率"
plt.ylabel("Probability") # u"確率"
plt.legend(loc="upper left"); # 凡例の位置は左上
```

証拠となる事象$X$が得られた後では，バグがないという確率が大きくなっている．
テストの数を増やせば，バグがない（確率1）ということを確信できるだろう．
<!--
Notice that after we observed $X$ occur, the probability of bugs being absent increased. By increasing the number of tests, we can approach confidence (probability 1) that there are no bugs present.
-->

これはベイズ推論とベイズ則の非常に単純な例である．
残念ながら，もう少し複雑なベイズ推論を実行するための数学は，
非常に調整された例題でなければ，もっともっと難しくなってしまう．
あとで見るように，この手の数学的な解析は実際には必要ない．
いろいろなモデル化のためのツールを知るほうが先である．
次の節は「確率分布」を扱う．もしよく知っているなら，
読み飛ばして（もしくは斜め読みして）もよい．
よく知らなければ，非常に重要なのでよく理解してほしい．
<!--
This was a very simple example of Bayesian inference and Bayes rule. Unfortunately, the mathematics necessary to perform more complicated Bayesian inference only becomes more difficult, except for artificially constructed cases. We will later see that this type of mathematical analysis is actually unnecessary. First we must broaden our modeling tools. The next section deals with *probability distributions*. If you are already familiar, feel free to skip (or at least skim), but for the less familiar the next section is essential.
-->

_______

## 確率分布
<!--
##Probability Distributions
-->

**確率分布とは何かを簡単におさらいしよう．**
$Z$を確率変数とする．
$Z$が取る値それぞれに確率を与えるのが確率分布である．
グラフで描けば，確率分布は曲線で書くことができて，
その曲線の高さに比例して確率が大きくなる．
すでにこの章の最初の図で，確率分布の曲線の例を説明している．
<!--
**Let's quickly recall what a probability distribution is:** Let $Z$ be some random variable. Then associated with $Z$ is a *probability distribution function* that assigns probabilities to the different outcomes $Z$ can take. Graphically, a probability distribution is a curve where the probability of an outcome is proportional to the height of the curve. You can see examples in the first figure of this chapter.
-->

確率変数には以下のように3種類ある．
<!--
We can divide random variables into three classifications:
-->

- **$Z$が離散の場合**：離散確率変数は，与えられた値のリストの中のどれか一つの値を取る．人口，映画の評価，得票数などは離散確率変数の例である．離散確率変数は，以下の連続の場合と対比すると分かりやすい．
- **$Z$が連続の場合**：連続確率変数は任意精度の値を取る．例えば温度，速度，時間，色などは連続確率変数でモデル化される．これらの値は，いくらでも精度よく指定することができるからだ．
- **$Z$が混合型の場合**：混合型の確率変数は，離散と連続のどちらの値も取る．上の2つのタイプの組み合わせである．

<!--
-   **$Z$ is discrete**: Discrete random variables may only assume values on a specified list. Things like populations, movie ratings, and number of votes are all discrete random variables. Discrete random variables become more clear when we contrast them with...

-   **$Z$ is continuous**: Continuous random variable can take on arbitrarily exact values. For example, temperature, speed, time, color are all modeled as continuous variables because you can progressively make the values more and more precise.

- **$Z$ is mixed**: Mixed random variables assign probabilities to both discrete and continuous random variables, i.e. it is a combination of the above two categories.
-->

###離散の場合
<!--
###Discrete Case
-->

もし$Z$が離散なら，確率分布は*確率質量関数*と呼ばれる．
これは$Z$が値$k$を取る確率を$P(Z=k)$で表す．
確率質量関数は確率変数$Z$を完全に決定する．つまり
確率質量関数が分かれば$Z$がどのように振る舞うのかが分かるのである．
この先よく登場する有名な確率質量関数がいくつかあるが，
必要に応じて紹介する．一番最初に紹介する有用な確率質量関数は，ポワソン分布である．
$Z$の確率質量関数が以下の式の時，$Z$はポワソン分布に従うと言う．
<!--
If $Z$ is discrete, then its distribution is called a *probability mass function*, which measures the probability $Z$ takes on the value $k$, denoted $P(Z=k)$. Note that the probability mass function completely describes the random variable $Z$, that is, if we know the mass function, we know how $Z$ should behave. There are popular probability mass functions that consistently appear: we will introduce them as needed, but let's introduce the first very useful probability mass function. We say $Z$ is *Poisson*-distributed if:
-->

$$P(Z = k) =\frac{ \lambda^k e^{-\lambda} }{k!}, \; \; k=0,1,2, \dots $$

$\lambda$は分布のパラメータで，分布の形状を決める．
ポワソン分布の場合，$\lambda$は任意の正の実数である．
$\lambda$を大きくすると大きな値の確率が高くなり，
$\lambda$を小さくすると小さな値の確率が高くなる．
そのため，$\lambda$はポワソン分布の「強度」と考えてもよい．
<!--
$\lambda$ is called a parameter of the distribution, and it controls the distribution's shape. For the Poisson distribution, $\lambda$ can be any positive number. By increasing $\lambda$, we add more probability to larger values, and conversely by decreasing $\lambda$ we add more probability to smaller values. One can describe $\lambda$ as the *intensity* of the Poisson distribution.
-->

任意の正の実数である$\lambda$とは異なり，上の式での$k$の値は正の整数，つまり0, 1, 2, ... でなければならない．これは重要な事である．人数をモデル化しようとするなら，4.25人とか5.612人というのは意味が無いのだから．
<!--
Unlike $\lambda$, which can be any positive number, the value $k$ in the above formula must be a non-negative integer, i.e., $k$ must take on values 0,1,2, and so on. This is very important, because if you wanted to model a population you could not make sense of populations with 4.25 or 5.612 members.
-->

もし確率変数$Z$がポワソン分布に従うなら，それを以下のように書く．
<!--
If a random variable $Z$ has a Poisson mass distribution, we denote this by writing
-->

$$Z \sim \text{Poi}(\lambda) $$

ポワソン分布の便利な性質の一つは，期待値が分布パラメータに等しいということである．
<!--
One useful property of the Poisson distribution is that its expected value is equal to its parameter, i.e.:
-->

$$E[ \;Z\; | \; \lambda \;] = \lambda $$
<!--
E\large[ \;Z\; | \; \lambda \;\large] = \lambda
-->

この先この性質を利用するので覚えておいてほしい．
以下のグラフは，いくつかの$\lambda$の値について確率質量関数をプロットしたものである．
このグラフで注意してほしいことは2つ．1つ目は$\lambda$を大きくすれば，
大きな値の確率が高くなること．2つ目は，グラフの横軸は15で終わっているが，
分布はそうではない．すべての正の整数に対して確率が割り当てられている．
<!--
We will use this property often, so it's useful to remember. Below, we plot the probability mass distribution for different $\lambda$ values. The first thing to notice is that by increasing $\lambda$, we add more probability of larger values occurring. Second, notice that although the graph ends at 15, the distributions do not. They assign positive probability to every non-negative integer.
-->

```
figsize(12.5, 4)

import scipy.stats as stats
a = np.arange(16)
poi = stats.poisson
lambda_ = [1.5, 4.25]
colours = ["#348ABD", "#A60628"]

plt.bar(a, poi.pmf(a, lambda_[0]), color=colours[0],
        label="$\lambda = %.1f$" % lambda_[0], alpha=0.60,
        edgecolor=colours[0], lw="3")

plt.bar(a, poi.pmf(a, lambda_[1]), color=colours[1],
        label="$\lambda = %.1f$" % lambda_[1], alpha=0.60,
        edgecolor=colours[1], lw="3")

plt.xticks(a + 0.4, a)
plt.legend()
plt.ylabel("probability of $k$") # u"$k$の確率"
plt.xlabel("$k$")
plt.title("Probability mass function of a Poisson random variable; differing \
$\lambda$ values") # u"いくつかの$\lambda$に対するポワソン分布の確率質量関数"
```

###連続の場合
<!--
###Continuous Case
-->

連続確率変数は，確率質量関数ではなく確率密度分布関数で表される．
これは単なる名前の問題のように見えるかもしれないが，
密度関数と質量関数はまったく違うものなのである．
連続確率変数の例は，以下の式で表される指数分布である．
<!--
Instead of a probability mass function, a continuous random variable has a *probability density function*. This might seem like unnecessary nomenclature, but the density function and the mass function are very different creatures. An example of continuous random variable is a random variable with *exponential density*. The density function for an exponential random variable looks like this:
-->

$$f_Z(z | \lambda) = \lambda e^{-\lambda z }, \;\; z\ge 0$$

Like a Poisson random variable, an exponential random variable can take on only non-negative values. But unlike a Poisson variable, the exponential can take on *any* non-negative values, including non-integral values such as 4.25 or 5.612401. This property makes it a poor choice for count data, which must be an integer, but a great choice for time data, temperature data (measured in Kelvins, of course), or any other precise *and positive* variable. The graph below shows two probability density functions with different $\lambda$ values.

When a random variable $Z$ has an exponential distribution with parameter $\lambda$, we say *$Z$ is exponential* and write

$$Z \sim \text{Exp}(\lambda)$$

Given a specific $\lambda$, the expected value of an exponential random variable is equal to the inverse of $\lambda$, that is:

$$E[\; Z \;|\; \lambda \;] = \frac{1}{\lambda}$$

```
a = np.linspace(0, 4, 100)
expo = stats.expon
lambda_ = [0.5, 1]

for l, c in zip(lambda_, colours):
    plt.plot(a, expo.pdf(a, scale=1. / l), lw=3,
             color=c, label="$\lambda = %.1f$" % l)
    plt.fill_between(a, expo.pdf(a, scale=1. / l), color=c, alpha=.33)

plt.legend()
plt.ylabel("PDF at $z$")
plt.xlabel("$z$")
plt.ylim(0, 1.2)
plt.title("Probability density function of an Exponential random variable;\
 differing $\lambda$");
```

###But what is $\lambda \;$?

**This question is what motivates statistics**. In the real world, $\lambda$ is hidden from us. We see only $Z$, and must go backwards to try and determine $\lambda$. The problem is difficult because there is no one-to-one mapping from $Z$ to $\lambda$. Many different methods have been created to solve the problem of estimating $\lambda$, but since $\lambda$ is never actually observed, no one can say for certain which method is best!

Bayesian inference is concerned with *beliefs* about what $\lambda$ might be. Rather than try to guess $\lambda$ exactly, we can only talk about what $\lambda$ is likely to be by assigning a probability distribution to $\lambda$.

This might seem odd at first. After all, $\lambda$ is fixed; it is not (necessarily) random! How can we assign probabilities to values of a non-random variable? Ah, we have fallen for our old, frequentist way of thinking. Recall that under Bayesian philosophy, we *can* assign probabilities if we interpret them as beliefs. And it is entirely acceptable to have *beliefs* about the parameter $\lambda$.

##### Example: Inferring behaviour from text-message data

Let's try to model a more interesting example, one that concerns the rate at which a user sends and receives text messages:

>  You are given a series of daily text-message counts from a user of your system. The data, plotted over time, appears in the chart below. You are curious to know if the user's text-messaging habits have changed over time, either gradually or suddenly. How can you model this? (This is in fact my own text-message data. Judge my popularity as you wish.)

```
figsize(12.5, 3.5)
count_data = np.loadtxt("data/txtdata.csv")
n_count_data = len(count_data)
plt.bar(np.arange(n_count_data), count_data, color="#348ABD")
plt.xlabel("Time (days)")
plt.ylabel("count of text-msgs received")
plt.title("Did the user's texting habits change over time?")
plt.xlim(0, n_count_data);
```

Before we start modeling, see what you can figure out just by looking at the chart above. Would you say there was a change in behaviour during this time period?

How can we start to model this? Well, as we have conveniently already seen, a Poisson random variable is a very appropriate model for this type of *count* data. Denoting day $i$'s text-message count by $C_i$,

$$ C_i \sim \text{Poisson}(\lambda)  $$

We are not sure what the value of the $\lambda$ parameter really is, however. Looking at the chart above, it appears that the rate might become higher late in the observation period, which is equivalent to saying that $\lambda$ increases at some point during the observations. (Recall that a higher value of $\lambda$ assigns more probability to larger outcomes. That is, there is a higher probability of many text messages having been sent on a given day.)

How can we represent this observation mathematically? Let's assume that on some day during the observation period (call it $\tau$), the parameter $\lambda$ suddenly jumps to a higher value. So we really have two $\lambda$ parameters: one for the period before $\tau$, and one for the rest of the observation period. In the literature, a sudden transition like this would be called a *switchpoint*:

$$
\lambda =
\begin{cases}
\lambda_1  & \text{if } t \lt \tau \cr
\lambda_2 & \text{if } t \ge \tau
\end{cases}
$$

If, in reality, no sudden change occurred and indeed $\lambda_1 = \lambda_2$, then the $\lambda$s posterior distributions should look about equal.

We are interested in inferring the unknown $\lambda$s. To use Bayesian inference, we need to assign prior probabilities to the different possible values of $\lambda$. What would be good prior probability distributions for $\lambda_1$ and $\lambda_2$? Recall that $\lambda$ can be any positive number. As we saw earlier, the *exponential* distribution provides a continuous density function for positive numbers, so it might be a good choice for modeling $\lambda_i$. But recall that the exponential distribution takes a parameter of its own, so we'll need to include that parameter in our model. Let's call that parameter $\alpha$.

\begin{align}
&\lambda_1 \sim \text{Exp}( \alpha ) \\\
&\lambda_2 \sim \text{Exp}( \alpha )
\end{align}

$\alpha$ is called a *hyper-parameter* or *parent variable*. In literal terms, it is a parameter that influences other parameters. Our initial guess at $\alpha$ does not influence the model too strongly, so we have some flexibility in our choice.  A good rule of thumb is to set the exponential parameter equal to the inverse of the average of the count data. Since we're modeling $\lambda$ using an exponential distribution, we can use the expected value identity shown earlier to get:

$$\frac{1}{N}\sum_{i=0}^N \;C_i \approx E[\; \lambda \; |\; \alpha ] = \frac{1}{\alpha}$$

An alternative, and something I encourage the reader to try, would be to have two priors: one for each $\lambda_i$. Creating two exponential distributions with different $\alpha$ values reflects our prior belief that the rate changed at some point during the observations.

What about $\tau$? Because of the noisiness of the data, it's difficult to pick out a priori when $\tau$ might have occurred. Instead, we can assign a *uniform prior belief* to every possible day. This is equivalent to saying

\begin{align}
& \tau \sim \text{DiscreteUniform(1,70) }\\\\
& \Rightarrow P( \tau = k ) = \frac{1}{70}
\end{align}

So after all this, what does our overall prior distribution for the unknown variables look like? Frankly, *it doesn't matter*. What we should understand is that it's an ugly, complicated mess involving symbols only a mathematician could love. And things will only get uglier the more complicated our models become. Regardless, all we really care about is the posterior distribution.

We next turn to PyMC, a Python library for performing Bayesian analysis that is undaunted by the mathematical monster we have created.

Introducing our first hammer: PyMC
-----

PyMC is a Python library for programming Bayesian analysis [3]. It is a fast, well-maintained library. The only unfortunate part is that its documentation is lacking in certain areas, especially those that bridge the gap between beginner and hacker. One of this book's main goals is to solve that problem, and also to demonstrate why PyMC is so cool.

We will model the problem above using PyMC. This type of programming is called *probabilistic programming*, an unfortunate misnomer that invokes ideas of randomly-generated code and has likely confused and frightened users away from this field. The code is not random; it is probabilistic in the sense that we create probability models using programming variables as the model's components. Model components are first-class primitives within the PyMC framework.

B. Cronin [5] has a very motivating description of probabilistic programming:

>   Another way of thinking about this: unlike a traditional program, which only runs in the forward directions, a probabilistic program is run in both the forward and backward direction. It runs forward to compute the consequences of the assumptions it contains about the world (i.e., the model space it represents), but it also runs backward from the data to constrain the possible explanations. In practice, many probabilistic programming systems will cleverly interleave these forward and backward operations to efficiently home in on the best explanations.

Because of the confusion engendered by the term *probabilistic programming*, I'll refrain from using it. Instead, I'll simply say *programming*, since that's what it really is.

PyMC code is easy to read. The only novel thing should be the syntax, and I will interrupt the code to explain individual sections. Simply remember that we are representing the model's components ($\tau, \lambda_1, \lambda_2$ ) as variables:

```
import pymc as pm

alpha = 1.0 / count_data.mean()  # Recall count_data is the
                               # variable that holds our txt counts
with pm.Model() as model:
    lambda_1 = pm.Exponential("lambda_1", alpha)
    lambda_2 = pm.Exponential("lambda_2", alpha)

with model:
    tau = pm.DiscreteUniform("tau", lower=0, upper=n_count_data)
```

In the code above, we create the PyMC variables corresponding to $\lambda_1$ and $\lambda_2$. We assign them to PyMC's *stochastic variables*, so-called because they are treated by the back end as random number generators. We can demonstrate this fact by calling their built-in `random()` methods.

```
print "Random output:", tau.random(), tau.random(), tau.random()
```

```
@pm.deterministic
def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
    out = np.zeros(n_count_data)
    out[:tau] = lambda_1  # lambda before tau is lambda1
    out[tau:] = lambda_2  # lambda after (and including) tau is lambda2
    return out
```

This code creates a new function `lambda_`, but really we can think of it as a random variable: the random variable $\lambda$ from above. Note that because `lambda_1`, `lambda_2` and `tau` are random, `lambda_` will be random. We are **not** fixing any variables yet.

`@pm.deterministic` is a decorator that tells PyMC this is a deterministic function. That is, if the arguments were deterministic (which they are not), the output would be deterministic as well.

```
observation = pm.Poisson("obs", lambda_, value=count_data, observed=True)

model = pm.Model([observation, lambda_1, lambda_2, tau])
```

The variable `observation` combines our data, `count_data`, with our proposed data-generation scheme, given by the variable `lambda_`, through the `value` keyword. We also set `observed = True` to tell PyMC that this should stay fixed in our analysis. Finally, PyMC wants us to collect all the variables of interest and create a `Model` instance out of them. This makes our life easier when we retrieve the results.

The code below will be explained in Chapter 3, but I show it here so you can see where our results come from. One can think of it as a *learning* step. The machinery being employed is called *Markov Chain Monte Carlo* (MCMC), which I also delay explaining until Chapter 3. This technique returns thousands of random variables from the posterior distributions of $\lambda_1, \lambda_2$ and $\tau$. We can plot a histogram of the random variables to see what the posterior distributions look like. Below, we collect the samples (called *traces* in the MCMC literature) into histograms.

```
# Mysterious code to be explained in Chapter 3.
mcmc = pm.MCMC(model)
mcmc.sample(40000, 10000, 1)
```

```
lambda_1_samples = mcmc.trace('lambda_1')[:]
lambda_2_samples = mcmc.trace('lambda_2')[:]
tau_samples = mcmc.trace('tau')[:]
```

```
figsize(12.5, 10)
# histogram of the samples:

ax = plt.subplot(311)
ax.set_autoscaley_on(False)

plt.hist(lambda_1_samples, histtype='stepfilled', bins=30, alpha=0.85,
         label="posterior of $\lambda_1$", color="#A60628", normed=True)
plt.legend(loc="upper left")
plt.title(r"""Posterior distributions of the variables
    $\lambda_1,\;\lambda_2,\;\tau$""")
plt.xlim([15, 30])
plt.xlabel("$\lambda_1$ value")

ax = plt.subplot(312)
ax.set_autoscaley_on(False)
plt.hist(lambda_2_samples, histtype='stepfilled', bins=30, alpha=0.85,
         label="posterior of $\lambda_2$", color="#7A68A6", normed=True)
plt.legend(loc="upper left")
plt.xlim([15, 30])
plt.xlabel("$\lambda_2$ value")

plt.subplot(313)
w = 1.0 / tau_samples.shape[0] * np.ones_like(tau_samples)
plt.hist(tau_samples, bins=n_count_data, alpha=1,
         label=r"posterior of $\tau$",
         color="#467821", weights=w, rwidth=2.)
plt.xticks(np.arange(n_count_data))

plt.legend(loc="upper left")
plt.ylim([0, .75])
plt.xlim([35, len(count_data) - 20])
plt.xlabel(r"$\tau$ (in days)")
plt.ylabel("probability");
```

### Interpretation

Recall that Bayesian methodology returns a *distribution*. Hence we now have distributions to describe the unknown $\lambda$s and $\tau$. What have we gained? Immediately, we can see the uncertainty in our estimates: the wider the distribution, the less certain our posterior belief should be. We can also see what the plausible values for the parameters are: $\lambda_1$ is around 18 and $\lambda_2$ is around 23. The posterior distributions of the two $\lambda$s are clearly distinct, indicating that it is indeed likely that there was a change in the user's text-message behaviour.

What other observations can you make? If you look at the original data again, do these results seem reasonable?

Notice also that the posterior distributions for the $\lambda$s do not look like exponential distributions, even though our priors for these variables were exponential. In fact, the posterior distributions are not really of any form that we recognize from the original model. But that's OK! This is one of the benefits of taking a computational point of view. If we had instead done this analysis using mathematical approaches, we would have been stuck with an analytically intractable (and messy) distribution. Our use of a computational approach makes us indifferent to mathematical tractability.

Our analysis also returned a distribution for $\tau$. Its posterior distribution looks a little different from the other two because it is a discrete random variable, so it doesn't assign probabilities to intervals. We can see that near day 45, there was a 50% chance that the user's behaviour changed. Had no change occurred, or had the change been gradual over time, the posterior distribution of $\tau$ would have been more spread out, reflecting that many days were plausible candidates for $\tau$. By contrast, in the actual results we see that only three or four days make any sense as potential transition points.

###Why would I want samples from the posterior, anyways?

We will deal with this question for the remainder of the book, and it is an understatement to say that it will lead us to some amazing results. For now, let's end this chapter with one more example.

We'll use the posterior samples to answer the following question: what is the expected number of texts at day $t, \; 0 \le t \le 70$ ? Recall that the expected value of a Poisson variable is equal to its parameter $\lambda$. Therefore, the question is equivalent to *what is the expected value of $\lambda$ at time $t$*?

In the code below, let $i$ index samples from the posterior distributions. Given a day $t$, we average over all possible $\lambda_i$ for that day $t$, using $\lambda_i = \lambda_{1,i}$ if $t \lt \tau_i$ (that is, if the behaviour change has not yet occurred), else we use $\lambda_i = \lambda_{2,i}$.

```
figsize(12.5, 5)
# tau_samples, lambda_1_samples, lambda_2_samples contain
# N samples from the corresponding posterior distribution
N = tau_samples.shape[0]
expected_texts_per_day = np.zeros(n_count_data)
for day in range(0, n_count_data):
    # ix is a bool index of all tau samples corresponding to
    # the switchpoint occurring prior to value of 'day'
    ix = day < tau_samples
    # Each posterior sample corresponds to a value for tau.
    # for each day, that value of tau indicates whether we're "before"
    # (in the lambda1 "regime") or
    #  "after" (in the lambda2 "regime") the switchpoint.
    # by taking the posterior sample of lambda1/2 accordingly, we can average
    # over all samples to get an expected value for lambda on that day.
    # As explained, the "message count" random variable is Poisson distributed,
    # and therefore lambda (the poisson parameter) is the expected value of
    # "message count".
    expected_texts_per_day[day] = (lambda_1_samples[ix].sum()
                                   + lambda_2_samples[~ix].sum()) / N

plt.plot(range(n_count_data), expected_texts_per_day, lw=4, color="#E24A33",
         label="expected number of text-messages received")
plt.xlim(0, n_count_data)
plt.xlabel("Day")
plt.ylabel("Expected # text-messages")
plt.title("Expected number of text-messages received")
plt.ylim(0, 60)
plt.bar(np.arange(len(count_data)), count_data, color="#348ABD", alpha=0.65,
        label="observed texts per day")

plt.legend(loc="upper left");
```

Our analysis shows strong support for believing the user's behavior did change ($\lambda_1$ would have been close in value to $\lambda_2$ had this not been true), and that the change was sudden rather than gradual (as demonstrated by $\tau$'s strongly peaked posterior distribution). We can speculate what might have caused this: a cheaper text-message rate, a recent weather-to-text subscription, or perhaps a new relationship. (In fact, the 45th day corresponds to Christmas, and I moved away to Toronto the next month, leaving a girlfriend behind.)

##### Exercises

1\.  Using `lambda_1_samples` and `lambda_2_samples`, what is the mean of the posterior distributions of $\lambda_1$ and $\lambda_2$?

```
# type your code here.
```

2\.  What is the expected percentage increase in text-message rates? `hint:` compute the mean of `lambda_1_samples/lambda_2_samples`. Note that this quantity is very different from `lambda_1_samples.mean()/lambda_2_samples.mean()`.

```
# type your code here.
```

3\. What is the mean of $\lambda_1$ **given** that we know $\tau$ is less than 45.  That is, suppose we have been given new information that the change in behaviour occurred prior to day 45. What is the expected value of $\lambda_1$ now? (You do not need to redo the PyMC part. Just consider all instances where `tau_samples < 45`.)

```
# type your code here.
```

### References

-  [1] Gelman, Andrew. N.p.. Web. 22 Jan 2013. [N is never large enough](http://andrewgelman.com/2005/07/31/n_is_never_larg/).
-  [2] Norvig, Peter. 2009. [The Unreasonable Effectiveness of Data](http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/35179.pdf).
- [3] Patil, A., D. Huard and C.J. Fonnesbeck. 2010.
PyMC: Bayesian Stochastic Modelling in Python. Journal of Statistical
Software, 35(4), pp. 1-81.
- [4] Jimmy Lin and Alek Kolcz. Large-Scale Machine Learning at Twitter. Proceedings of the 2012 ACM SIGMOD International Conference on Management of Data (SIGMOD 2012), pages 793-804, May 2012, Scottsdale, Arizona.
- [5] Cronin, Beau. "Why Probabilistic Programming Matters." 24 Mar 2013. Google, Online Posting to Google . Web. 24 Mar. 2013. <https://plus.google.com/u/0/107971134877020469960/posts/KpeRdJKR6Z1>.

```
from IPython.core.display import HTML

def css_styling():
    styles = open("../styles/custom.css", "r").read()
    return HTML(styles)
css_styling()
```


```python
from IPython.display import Image
Image('../../../python_for_probability_statistics_and_machine_learning.jpg')
```

[Python for Probability, Statistics, and Machine Learning](https://www.springer.com/fr/book/9783319307152)

```python
from __future__ import division
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib

We considered Maximum Likelihood Estimation (MLE) and Maximum A-Posteriori
(MAP) estimation and in each case we started out with a probability density
function of some kind and we further assumed that the samples were identically
distributed and independent (iid). The idea behind robust statistics
[[maronna2006robust]](#maronna2006robust) is to construct estimators that can survive the
weakening of either or both of these assumptions. More concretely, suppose you
have a model that works great except for a few outliers. The temptation is to
just ignore the outliers and proceed. Robust estimation methods provide a
disciplined way to handle outliers without cherry-picking data that works for
your favored model.

### The Notion of Location

The first notion we need is *location*, which is  a generalization of the idea
of *central value*. Typically, we just use an estimate of the mean for this,
but we will see later why this could be a bad idea.  The general idea of
location satisfies the following requirements Let $X$ be a random variable with
distribution $F$, and let $\theta(X)$ be some descriptive measure of $F$. Then
$\theta(X)$ is said to be a measure of *location* if for any constants *a* and
*b*, we have the following:

$$
\begin{equation}
\theta(X+b)  = \theta(X) +b
\label{_auto1} \tag{1}
\end{equation}
$$

$$
\begin{equation} \
\theta(-X)   = -\theta(X)
\label{_auto2} \tag{2}
\end{equation}
$$

$$
\begin{equation} \
X \ge 0 \Rightarrow \theta(X)  \ge 0
\label{_auto3} \tag{3}
\end{equation}
$$

$$
\begin{equation} \
\theta(a X) = a\theta(X)
\label{_auto4} \tag{4}
\end{equation}
$$

 The first condition is called *location equivariance* (or *shift-invariance* in
signal processing lingo). The fourth condition is called *scale equivariance*,
which means that the units that $X$ is measured in should not effect the value
of the  location estimator.  These requirements capture the intuition of
*centrality* of a distribution, or where most of the
probability mass is located.

For example, the sample mean estimator is $ \hat{\mu}=\frac{1}{n}\sum X_i $. The first
requirement is obviously satisfied as $ \hat{\mu}=\frac{1}{n}\sum (X_i+b) = b +
\frac{1}{n}\sum X_i =b+\hat{\mu}$. Let us consider the second requirement:$
\hat{\mu}=\frac{1}{n}\sum -X_i = -\hat{\mu}$. Finally, the last requirement is
satisfied with $ \hat{\mu}=\frac{1}{n}\sum a X_i =a \hat{\mu}$.

### Robust Estimation and Contamination

Now that we have the generalized location of centrality embodied in the
*location* parameter, what can we do with it?  Previously, we assumed that our samples
were all identically distributed. The key idea is that the samples might be
actually coming from a *single* distribution that is contaminated by another nearby
distribution, as in the following:

$$
F(X) = \epsilon G(X) + (1-\epsilon)H(X)
$$

 where $ \epsilon $ randomly toggles between zero and one. This means
that our data samples $\lbrace X_i \rbrace$ actually derived from two separate
distributions, $ G(X) $ and $ H(X) $. We just don't know how they are mixed
together. What we really want  is an estimator  that captures the location of $
G(X) $ in the face of random intermittent contamination by $ H(X)$.  For
example, it may be that this contamination is responsible for the outliers in a
model that otherwise works well with the dominant $F$ distribution. It can get
even worse than that because we don't know that there is only one contaminating
$H(X)$ distribution out there. There may be a whole family of distributions
that are contaminating $G(X)$. This means that whatever estimators we construct
have to be derived from a more generalized family of distributions instead of
from a single distribution,  as the maximum-likelihood method assumes.  This is
what makes robust estimation so difficult --- it has to deal with *spaces* of
function distributions instead of parameters from a particular probability
distribution.

### Generalized Maximum Likelihood Estimators

M-estimators are generalized maximum likelihood estimators. Recall that for
maximum likelihood, we want to maximize the likelihood function as in the
following:

$$
L_{\mu}(x_i) = \prod f_0(x_i-\mu)
$$

 and then to find the estimator $\hat{\mu}$ so that

$$
\hat{\mu} = \arg \max_{\mu} L_{\mu}(x_i)
$$

  So far, everything is the same as our usual maximum-likelihood
derivation except for the fact that we don't assume a specific $f_0$ as the
distribution of the $\lbrace X_i\rbrace$. Making the definition of

$$
\rho = -\log f_0
$$

 we obtain the more convenient form of the likelihood product and the
optimal $\hat{\mu}$ as

$$
\hat{\mu} = \arg \min_{\mu} \sum \rho(x_i-\mu)
$$

 If $\rho$ is differentiable, then differentiating  this with respect
to $\mu$ gives

$$
\begin{equation}
\sum \psi(x_i-\hat{\mu}) = 0
\label{eq:muhat} \tag{5}
\end{equation}
$$

 with $\psi = \rho^\prime$, the first derivative of $\rho$ , and for technical reasons we will assume that
$\psi$ is increasing. So far, it looks like we just pushed some definitions
around, but the key idea is we want to consider general $\rho$ functions that
may not be maximum likelihood estimators for *any* distribution. Thus, our
focus is now on uncovering the nature of $\hat{\mu}$.

### Distribution of M-estimates

For a given distribution $F$, we define $\mu_0=\mu(F)$ as the solution to the
following

$$
\mathbb{E}_F(\psi(x-\mu_0))= 0
$$

 It is technical to show, but it turns out that $\hat{\mu} \sim
\mathcal{N}(\mu_0,\frac{v}{n})$ with

$$
v = \frac{\mathbb{E}_F(\psi(x-\mu_0)^2)}{(\mathbb{E}_F(\psi^\prime(x-\mu_0)))^2}
$$

 Thus, we can say that $\hat{\mu}$ is asymptotically normal with asymptotic
value $\mu_0$ and asymptotic variance $v$. This leads to the efficiency ratio
which is defined as  the following:

$$
\texttt{Eff}(\hat{\mu})= \frac{v_0}{v}
$$

 where $v_0$ is the asymptotic variance of the MLE and measures how
near $\hat{\mu}$ is to the optimum. In other words, this provides a sense of
how much outlier contamination costs in terms of samples. For example, if for
two estimates with asymptotic variances $v_1$ and $v_2$, we have $v_1=3v_2$,
then first estimate requires three times as many observations to obtain the
same variance as the second. Furthermore, for the sample mean (i.e.,
$\hat{\mu}=\frac{1}{n} \sum X_i$) with $F=\mathcal{N}$, we have $\rho=x^2/2$
and $\psi=x$ and also $\psi'=1$. Thus, we have $v=\mathbb{V}(x)$.
Alternatively, using the sample median as the estimator for the location, we
have $v=1/(4 f(\mu_0)^2)$.  Thus, if we have $F=\mathcal{N}(0,1)$, for the
sample median, we obtain $v={2\pi}/{4} \approx 1.571$. This means that the
sample median takes approximately 1.6 times as many samples to obtain the same
variance for the location as the sample mean. The sample median is
far more immune to the effects of outliers than the sample mean, so this
gives a sense of how much this robustness costs in samples.

** M-Estimates as Weighted Means.** One way to think about M-estimates is a
weighted means. Operationally, this
means that we want weight functions that can circumscribe the
influence of the individual data points, but, when taken as a whole,
still provide good estimated parameters. Most of the time, we have $\psi(0)=0$ and $\psi'(0)$ exists so
that $\psi$ is approximately linear at the origin. Using the following
definition:

$$
W(x)  =  \begin{cases}
                \psi(x)/x & \text{if} \: x \neq 0 \\\
                \psi'(x)  & \text{if} \: x =0
            \end{cases}
$$

 We can write our Equation ref{eq:muhat} as follows:

$$
\begin{equation}
\sum W(x_i-\hat{\mu})(x_i-\hat{\mu}) = 0
\label{eq:Wmuhat} \tag{6}
\end{equation}
$$

 Solving this for $\hat{\mu} $ yields the following,

$$
\hat{\mu} = \frac{\sum w_{i} x_i}{\sum w_{i}}
$$

 where $w_{i}=W(x_i-\hat{\mu})$. This is not practically useful
because the $w_i$ contains $\hat{\mu}$, which is what we are trying to solve
for. The question that remains is how to pick the $\psi$ functions. This is
still an open question, but the Huber functions are a well-studied choice.

### Huber Functions

The family of Huber functions is defined by the following:

$$
\rho_k(x ) = \begin{cases}
                x^2         & \mbox{if }  |x|\leq k \\\
                2 k |x|-k^2 & \mbox{if }  |x| >  k
                \end{cases}
$$

 with corresponding derivatives $2\psi_k(x)$ with

$$
\psi_k(x ) = \begin{cases}
                x              & \mbox{if } \: |x| \leq k \\\
                \text{sgn}(x)k & \mbox{if } \: |x| >  k
                \end{cases}
$$

 where the limiting cases $k \rightarrow \infty$ and $k \rightarrow 0$
correspond to the mean and median, respectively. To see this, take
$\psi_{\infty} = x$ and therefore $W(x) = 1$ and thus the defining Equation
ref{eq:Wmuhat} results in

$$
\sum_{i=1}^{n} (x_i-\hat{\mu}) = 0
$$

 and then solving this leads to $\hat{\mu} = \frac{1}{n}\sum x_i$.
Note that choosing $k=0$ leads to  the sample median, but that is not so
straightforward to solve for. Nonetheless, Huber functions provide a way
to move between two extremes of estimators for location (namely,
the mean vs. the median) with a tunable parameter $k$.
The $W$ function corresponding to Huber's $\psi$ is the following:

$$
W_k(x) = \min\Big{\lbrace} 1, \frac{k}{|x|} \Big{\rbrace}
$$

 [Figure](#fig:Robust_Statistics_0001) shows the Huber weight
function for $k=2$ with some sample points. The idea is that the computed
location, $\hat{\mu}$ is computed from Equation ref{eq:Wmuhat} to lie somewhere
in the middle of the weight function so that those terms (i.e., *insiders*)
have their values fully reflected in the location estimate. The black circles
are the *outliers* that have their values attenuated by the weight function so
that only a fraction of their presence is represented in the location estimate.

This shows the Huber weight function, $W_2(x)$ and some cartoon data points that are insiders or outsiders as far as the robust location estimate is concerned.

### Breakdown Point

So far, our discussion of robustness has been very abstract.  A more concrete
concept of robustness comes from the breakdown point.  In the simplest terms,
the breakdown point describes what happens when a single data point in an
estimator is changed in the most damaging way possible. For example, suppose we
have the sample mean, $\hat{\mu}=\sum x_i/n$, and we take one of the $x_i$
points to be infinite. What happens to this estimator? It also goes infinite.
This means that the breakdown point of the estimator is 0%. On the other hand,
the median has a breakdown point of 50%, meaning that half of the data for
computing the median could go infinite without affecting the median value. The median
is a *rank* statistic that cares more about the relative ranking of the data
than the values of the data, which explains its robustness.

The simpliest but still formal way to express the breakdown point is to
take $n$ data points, $\mathcal{D} = \lbrace (x_i,y_i) \rbrace$. Suppose $T$
is a regression estimator that yields a vector of regression coefficients,
$\boldsymbol{\theta}$,

$$
T(\mathcal{D}) = \boldsymbol{\theta}
$$

 Likewise, consider all possible corrupted samples of the data
$\mathcal{D}^\prime$. The maximum *bias* caused by this contamination is
the following:

$$
\texttt{bias}_{m} = \sup_{\mathcal{D}^\prime} \Vert T(\mathcal{D^\prime})-T(\mathcal{D}) \Vert
$$

 where the $\sup$ sweeps over all possible sets of $m$ contaminated samples.
Using this, the breakdown point is defined as the following:

$$
\epsilon_m = \min \Big\lbrace \frac{m}{n} \colon \texttt{bias}_{m} \rightarrow \infty \Big\rbrace
$$

 For example, in our least-squares regression, even one point at
infinity causes an infinite $T$. Thus, for least-squares regression,
$\epsilon_m=1/n$. In the limit $n \rightarrow \infty$, we have $\epsilon_m
\rightarrow 0$.

### Estimating Scale

In robust statistics, the concept of *scale* refers to a measure of the
dispersion of the data. Usually, we use the
estimated standard deviation for this, but this has a terrible breakdown point.
Even more troubling, in order to get a good estimate of location, we have to
either somehow know the scale ahead of time, or jointly estimate it. None of
these methods have easy-to-compute closed form solutions and must be computed
numerically.

The most popular method for estimating scale is the *median absolute deviation*

$$
\texttt{MAD} = \texttt{Med} (\vert \mathbf{x} - \texttt{Med}(\mathbf{x})\vert)
$$

 In words, take the median of the data $\mathbf{x}$ and
then subtract that median from the data itself, and then take the median of the
absolute value of the result. Another good dispersion estimate is the *interquartile range*,

$$
\texttt{IQR} = x_{(n-m+1)} - x_{(n)}
$$

 where $m= [n/4]$. The $x_{(n)}$ notation means the $n^{th}$ data
element after the data have been sorted. Thus, in this notation,
$\texttt{max}(\mathbf{x})=x_{(n)}$. In the case where $x \sim
\mathcal{N}(\mu,\sigma^2)$, then $\texttt{MAD}$ and $\texttt{IQR}$ are constant
multiples of $\sigma$ such that the normalized $\texttt{MAD}$ is the following,

$$
\texttt{MADN}(x) = \frac{\texttt{MAD} }{0.675}
$$

  The number comes from the inverse CDF of the normal distribution
corresponding to the $0.75$ level. Given the complexity of the
calculations, *jointly* estimating both location and scale is a purely
numerical matter. Fortunately, the Statsmodels module has many of these
ready to use. Let's create some contaminated data in the following code,

```python
import statsmodels.api as sm
from scipy import stats
data=np.hstack([stats.norm(10,1).rvs(10),stats.norm(0,1).rvs(100)])
```

 These data correspond to our model of contamination that we started
this section with. As shown in the  histogram in [Figure](#fig:Robust_Statistics_0002), there are two normal distributions, one
centered neatly at zero, representing the majority of the samples, and another
coming less regularly from the normal distribution on the right. Notice that
the group of infrequent samples on the right separates the mean and median
estimates (vertical dotted and dashed lines).  In the absence of the
contaminating distribution on the right, the standard deviation for this data
should be close to one. However, the usual non-robust estimate for standard
deviation (`np.std`) comes out to approximately three.  Using the
$\texttt{MADN}$ estimator (`sm.robust.scale.mad(data)`) we obtain approximately
1.25. Thus, the robust estimate of dispersion is less moved by the presence of
the  contaminating distribution.

Histogram of sample data. Notice that the group of infrequent samples on the right separates the mean and median estimates indicated by the vertical lines.

The generalized maximum likelihood M-estimation extends to joint
scale and location estimation using Huber functions. For example,

```python
huber = sm.robust.scale.Huber()
loc,scl=huber(data)
```

 which implements Huber's *proposal two* method of joint estimation of
location and scale. This kind of estimation is the key ingredient to robust
regression methods, many of which are implemented in Statsmodels in
`statsmodels.formula.api.rlm`. The corresponding documentation has more
information.


```python
from IPython.display import HTML

HTML('''
<form action="javascript:code_toggle()"><input type="submit" value="Code Toggle"></form>''')
```

<form action="javascript:code_toggle()"><input type="submit" value="Code Toggle"></form>

```javascript
%%javascript
    MathJax.Hub.Config({
      TeX: { equationNumbers: { autoNumber: "AMS" } }
    });
```

    <IPython.core.display.Javascript object>

```python
from IPython.display import HTML

HTML('''
<a href="https://raw.githubusercontent.com/{{ site.links.repo }}/master/benchmarks/benchmark5-hackathon.ipynb"
   download="benchmark5-hackathon.ipynb">
<button type="submit">Download Notebook</button>
</a>''')
```

<a href="https://raw.githubusercontent.com/{{ site.links.repo }}/master/benchmarks/benchmark5-hackathon.ipynb"
   download="benchmark5-hackathon.ipynb">
<button type="submit">Download Notebook</button>
</a>

# Benchmark Problem 5: Stokes Flow

```python
from IPython.display import HTML

HTML('''{% include jupyter_benchmark_table.html num="[5]" revision=0 %}''')
```

{% include jupyter_benchmark_table.html num="[5]" revision=0 %}

See the Overleaf document entitled ["Phase Field Benchmark Problems for Dendritic Growth and Linear Elasticity"][overleaf] for more details about the benchmark problems. Furthermore, read [the extended essay][benchmarks] for a discussion about the need for benchmark problems.

[benchmarks]: ../
[overleaf]: https://www.overleaf.com/read/nqjkdwyybvdz

# Overview

Flow of a liquid can be incorporated into phase field models, so we present this benchmark problem for incompressible fluid flow through a channel (the flow of many liquids can be modeled as incompressible). The flow of a fluid can generally be modeled via the Navier-Stokes equations.  When length scales are small, fluid velocities are low, and/or viscosity is large, such that the Reynolds number $Re<<1$, inertial forces are small compared with viscous forces, resulting in a simplification of Navier-Stokes flow to Stokes flow.

# Model Formulation

## Governing Equations

In this problem, two variables are used: the fluid velocity, $\textbf{u}$, which is a vector field, and the fluid pressure, $p$, which is a scalar field.  The Stokes momentum equation is given as

\begin{equation}
-\mu \nabla^{2} \textbf{u} + \nabla p - \rho \textbf{g} = 0,
\end{equation}

where $\rho$ is the density, assumed constant in this problem, $\mu$ is the dynamic viscosity, and $\textbf{g}$ is the acceleration due to gravity.  To fully describe fluid flow, the momentum balance equation is supplemented with the continuity equation for mass flow,

\begin{equation}
\frac{d\rho}{dt}+\nabla\cdot\left(\rho{\mathbf u}\right)=0;
\end{equation}

this simplifies to
\begin{equation}
\nabla \cdot \textbf{u} = 0
\end{equation}

for incompressible flow.  Use $\rho=100$, $\mu=1$, and $\textbf{g}=(0,-0.001)$.

## Domain

In this problem, we consider flow in a 2D channel (a) without and (b) with an obstruction.  The computational domain for case (b) is shown below with inlet boundary condition indicated by arrows for the Stokes flow benchmark problem with an obstruction (case (b)).  The domain and boundary conditions, etc., for case (a) are the same as that in case (b), but without the obstruction.

### Figure 1: Domain for variation (b)

## Boundary Conditions

All solid surfaces, including the boundary for the obstruction, have no-slip boundary conditions, that is, $u_x=u_y=0$.  The inlet velocity, on the left boundary, follows a parabolic profile described by

\begin{equation}
u_x(0,y) = -0.001(y-3)^2+0.009.
\end{equation}

The outlet velocity (on the right boundary) is left to the solver to determine, as is the pressure over the entire domain.  However, we specify that the pressure at point (30, 6) is zero.  Finally, the obstruction is described by an ellipse centered at (7, 2.5).  The semi-major axis (in the *y*-direction) is $a=1.5$, and the semi-minor axis (in the *x*-direction) is $b=1$.

## Submission Guidelines

Both variation (a) and (b) should be run to steady state.
Please submit the steady state pressure and the steady state velocity fields for both variation (a) and (b) along the $x=7$ and $y=5$ cut planes.

This will require two CSV or JSON files for each variation. Please,

 - link to your first CSV or JSON file, labeled x_cut_plane in the upload form; the columns or keys should be named y, velocity_x, velocity_y and pressure

 - link to your second CSV or JSON file, labeled y_cut_plane in the upload form; the columns or keys should be named x, velocity_x, velocity_y and pressure

Further data to upload can include images of the pressure and velocity fields at steady state. These are not required, but will help others view your work.


# Funciones de utilidad y aversión al riesgo

En el módulo anterior aprendimos
- qué es un portafolio, cómo medir su rendimiento esperado y su volatilidad;
- un portafolio de activos riesgosos tiene menos riesgo que la suma ponderada de los riesgos individuales,
- y que esto se logra mediante el concepto de diversificación;
- la diversificación elimina el riesgo idiosincrático, que es el que afecta a cada compañía en particular,
- sin embargo, el riesgo de mercado no se puede eliminar porque afecta a todos por igual.
- Finalmente, aprendimos conceptos importantes como frontera de mínima varianza, portafolios eficientes y el portafolio de mínima varianza, que son claves en el problema de selección óptima de portafolios.

Muy bien, sin embargo, para plantear el problema de selección óptima de portafolios necesitamos definir la función que vamos a optimizar: función de utilidad.

**Objetivos:**
- ¿Cómo tomamos decisiones según los economistas?
- ¿Cómo toman decisiones los inversionistas?
- ¿Qué son las funciones de utilidad?

*Referencia:*
- Notas del curso "Portfolio Selection and Risk Management", Rice University, disponible en Coursera.
___

## 1. Introducción

La teoría económica comienza con una suposición muy importante:
- **cada individuo actúa para obtener el mayor beneficio posible con los recursos disponibles**.
- En otras palabras, **maximizan su propia utilidad**

¿Qué es utilidad?
- Es un concepto relacionado con la felicidad, pero más amplio.
- Por ejemplo, yo obtengo utilidad de lavar mis dientes o comer sano. Ninguna de las dos me brindan felicidad, pero lo primero mantendrá mis dientes sanos y en el largo plazo, lo segundo probablemente contribuirá a una buena vejez.

Los economistas no se preocupan en realidad por lo que nos da utilidad, sino simplemente que cada uno de nosotros tiene sus propias preferencias.
- Por ejemplo, a mi me gusta el café, el fútbol, los perros, la academia, viajar, entre otros.
- Ustedes tienen sus propias preferencias también.

La vida es compleja y con demasiada incertidumbre. Debemos tomar decisiones a cada momento, y estas decisiones involucran ciertos "trade-off".
- Por ejemplo, normalmente tenemos una compensación entre utilidad hoy contra utilidad en el futuro.
- Debemos balancear nuestro consumo hoy contra nuestro consumo luego.
- Por ejemplo, ustedes gastan cerca de cuatro horas a la semana viniendo a clases de portafolios, porque esperan que esto contribuya a mejorar su nivel de vida en el futuro.

De manera que los economistas dicen que cada individuo se comporta como el siguiente optimizador:

\begin{align}
\max & \quad\text{Utilidad}\\
\text{s. a.} & \quad\text{Recursos disponibles}
\end{align}

¿Qué tiene que ver todo esto con el curso?
- En este módulo desarrollaremos herramientas para describir las preferencias de los inversionistas cuando se encuentran con decisiones de riesgo y rendimiento.
- Veremos como podemos medir la actitud frente al riesgo, ¿cuánto te gusta o disgusta el riesgo?
- Finalmente, veremos como podemos formular el problema de maximizar la utilidad de un inversionista para tomar la decisión de inversión óptima.
___

## 2. Funciones de utilidad.

¿Cómo tomamos decisiones?
Por ejemplo:
- Ustedes tienen que decidir si venir a clase o quedarse en su casa viendo Netflix, o ir al gimnasio.
- Tienen que decidir entre irse de fiesta cada fin, o ahorrar para salir de vacaciones.

En el caso de un portafolio, la decisión que se debe tomar es **¿cuáto riesgo estás dispuesto a tomar por qué cantidad de rendimiento?**

**¿Cómo evaluarías el "trade-off" entre tener cetes contra una estrategia muy riesgosa con un posible altísimo rendimiento?**

De manera que veremos como tomamos decisiones cuando tenemos distintas posibilidades. Específicamente, hablaremos acerca de las **preferencias**, como los economistas usan dichas preferencias para explicar las decisiones y los "trade-offs" en dichas decisiones.

Usamos las **preferencias** para describir las decisiones que tomamos. Las preferencias nos dicen cómo un individuo evalúa los "trade-offs" entre distintas elecciones.

Por definición, las preferencias son únicas para cada individuo. En el problema de selección de portafolios:
- las preferencias que dictan cuánto riesgo estás dispuesto a asumir por cuánto rendimiento, son específicas para cada uno de ustedes.
- Sus respuestas a esa pregunta pueden ser muy distintas, porque tenemos distintas preferencias.

Ahora, nosotros no podemos *cuantificar* dichas preferencias.
- Por esto usamos el concepto de utilidad, para medir qué tan satisfecho está un individuo con sus elecciones.
- Así que podemos pensar en la utilidad como un indicador numérico que describe las preferencias,
- o un índice que nos ayuda a clasificar diferentes decisiones.
- En términos simples, **la utilidad nos ayuda a transmitir a números la noción de cómo te sientes**;
- mientras más utilidad, mejor te sientes.

**Función de utilidad**: manera sistemática de asignar una medida o indicador numérico para clasificar diferentes escogencias.

El número que da una función de utilidad no tiene significado alguno. Simplemente es una manera de clasificar diferentes decisiones.

**Ejemplo.**

Podemos escribir la utilidad de un inversionista como función de la riqueza,

$$U(W).$$

- $U(W)$ nos da una medida de qué tan satisfechos estamos con el nivel de riqueza que tenemos.
- $U(W)$ no es la riqueza como tal, sino que la función de utilidad traduce la cantidad de riqueza en un índice numérico subjetivo.

¿Cómo luciría gráficamente una función de utilidad de riqueza $U(W)$?

 Ver en el tablero
- ¿Qué caracteristicas debe tener?
- ¿Cómo es su primera derivada?
- ¿Cómo es su segunda derivada?
- Tiempos buenos: riqueza alta (¿cómo es la primera derivada acá?)
- Tiempos malos: poca riqueza (¿cómo es la primera derivada acá?)

## 3. Aversión al riesgo

Una dimensión importante en la toma de decisiones en finanzas y economía es la **incertidumbre**. Probablemente no hay ninguna decisión en economía que no involucre riesgo.

- A la mayoría de las personas no les gusta mucho el riesgo.
- De hecho, estudios del comportamiento humano de cara al riesgo, sugieren fuertemente que los seres humanos somos aversos al riesgo.
- Por ejemplo, la mayoría de hogares poseen seguros para sus activos.
- Así, cuando planteamos el problema de selección óptima de portafolios, suponemos que el inversionista es averso al riesgo.

¿Qué significa esto en términos de preferencias? ¿Cómo lo medimos?

- Como seres humanos, todos tenemos diferentes genes y preferencias, y esto aplica también a la actitud frente al riesgo.
- Por tanto, la aversión al riesgo es clave en cómo describimos las preferencias de un inversinista.
- Individuos con un alto grado de aversión al riesgo valorarán la seguridad a un alto precio, mientras otros no tanto.
- De manera que alguien con alta aversión al riesgo, no querrá enfrentarse a una situación con resultado incierto y querrá pagar una gran prima de seguro para eliminar dicho riesgo.
- O equivalentemente, una persona con alta aversión al riesgo requerirá una compensación alta si se decide a asumir ese riesgo.

El **grado de aversión al riesgo** mide qué tanto un inversionista prefiere un resultado seguro a un resultado incierto.

Lo opuesto a aversión al riesgo es **tolerancia al riesgo**.

 Ver en el tablero gráficamente, cómo se explica la aversión al riesgo desde las funciones de utilidad.

**Conclusión:** la concavidad en la función de utilidad dicta qué tan averso al riesgo es el individuo.

### ¿Cómo medimos el grado de aversión al riesgo de un individuo?

¿Saben cuál es su coeficiente de aversión al riesgo? Podemos estimarlo.

Suponga que se puede participar en la siguiente lotería:
- usted puede ganar $\$1000$ con $50\%$ de probabilidad, o
- puede ganar $\$500$ con $50\%$ de probabilidad.

Es decir, de entrada usted tendrá $\$500$ seguros pero también tiene la posibilidad de ganar $\$1000$.

¿Cuánto estarías dispuesto a pagar por esta oportunidad?

Bien, podemos relacionar tu respuesta con tu coeficiente de aversión al riesgo.

| Coeficiente de aversión al riesgo | Cantidad que pagarías |
| --------------------------------- | --------------------- |
| 0                                 | 750                   |
| 0.5                               | 729                   |
| 1                                 | 707                   |
| 2                                 | 667                   |
| 3                                 | 632                   |
| 4                                 | 606                   |
| 5                                 | 586                   |
| 10                                | 540                   |
| 15                                | 525                   |
| 20                                | 519                   |
| 50                                | 507                   |

La mayoría de la gente está dispuesta a pagar entre $\$540$ (10) y $\$707$ (1). Es muy raro encontrar coeficientes de aversión al riesgo menores a 1. Esto está soportado por una gran cantidad de encuestas.

- En el mundo financiero, los consultores financieros utilizan cuestionarios para medir el coeficiente de aversión al riesgo.

**Ejemplo.** Describir en términos de aversión al riesgo las siguientes funciones de utilidad que dibujaré en el tablero.
___

# Anuncios

## 1. Quiz la siguiente clase.

## 2. Tarea 5 entrega 2 para hoy, lunes 22 de Junio.

Created with Jupyter by Esteban Jiménez Rodríguez.