From 671f59031c515cf33e5e58d4189cbb372acdb43f Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Sat, 21 May 2022 14:12:39 +0200 Subject: [PATCH] Ch15 done --- ...Missing Data and Other Opportunities.ipynb | 125 +++++++++++++++--- ... Missing Data and Other Opportunities.html | 105 +++++++++++++++ 2 files changed, 212 insertions(+), 18 deletions(-) diff --git a/15-Chapter 15. Missing Data and Other Opportunities.ipynb b/15-Chapter 15. Missing Data and Other Opportunities.ipynb index f6dd078..bc60539 100644 --- a/15-Chapter 15. Missing Data and Other Opportunities.ipynb +++ b/15-Chapter 15. Missing Data and Other Opportunities.ipynb @@ -1641,7 +1641,7 @@ }, { "cell_type": "markdown", - "id": "1287344d", + "id": "a53f3497", "metadata": {}, "source": [ "Code 15.21" @@ -1650,7 +1650,7 @@ { "cell_type": "code", "execution_count": 28, - "id": "ae9de9c9", + "id": "6968b169", "metadata": {}, "outputs": [ { @@ -2052,7 +2052,7 @@ }, { "cell_type": "markdown", - "id": "5ce07890", + "id": "d2e01f69", "metadata": {}, "source": [ "Code 15.22" @@ -2061,7 +2061,7 @@ { "cell_type": "code", "execution_count": 29, - "id": "27ba1451", + "id": "8beb97ac", "metadata": {}, "outputs": [ { @@ -2139,7 +2139,7 @@ }, { "cell_type": "markdown", - "id": "af59bfc6", + "id": "9908610d", "metadata": {}, "source": [ "Code 15.23" @@ -2148,7 +2148,7 @@ { "cell_type": "code", "execution_count": 30, - "id": "fc7f834a", + "id": "8ba67b3b", "metadata": {}, "outputs": [], "source": [ @@ -2157,7 +2157,7 @@ }, { "cell_type": "markdown", - "id": "11f4dca8", + "id": "08a62cb5", "metadata": {}, "source": [ "Code 15.24" @@ -2166,7 +2166,7 @@ { "cell_type": "code", "execution_count": 32, - "id": "d910e888", + "id": "8db73230", "metadata": {}, "outputs": [ { @@ -2212,7 +2212,7 @@ }, { "cell_type": "markdown", - "id": "9c040ae8", + "id": "57cb4891", "metadata": {}, "source": [ "Code 15.25" @@ -2221,7 +2221,7 @@ { "cell_type": "code", "execution_count": 33, - "id": "8ddc3bc2", + "id": "6b3b6426", "metadata": {}, "outputs": [ { @@ -2244,7 +2244,7 @@ }, { "cell_type": "markdown", - "id": "915e8fe2", + "id": "c66e8146", "metadata": {}, "source": [ "Code 15.26" @@ -2253,7 +2253,7 @@ { "cell_type": "code", "execution_count": 34, - "id": "b8c2b7a7", + "id": "6ea6dd2b", "metadata": {}, "outputs": [ { @@ -5775,7 +5775,7 @@ }, { "cell_type": "markdown", - "id": "bc7f5d56", + "id": "1181d3bd", "metadata": {}, "source": [ "Code 15.27" @@ -5784,7 +5784,7 @@ { "cell_type": "code", "execution_count": 35, - "id": "fd8198a6", + "id": "4470b7ec", "metadata": {}, "outputs": [ { @@ -5810,7 +5810,7 @@ }, { "cell_type": "markdown", - "id": "32258afa", + "id": "16896faf", "metadata": {}, "source": [ "Code 15.28" @@ -5819,7 +5819,7 @@ { "cell_type": "code", "execution_count": 36, - "id": "e0b63a88", + "id": "54b81b46", "metadata": {}, "outputs": [ { @@ -5871,16 +5871,105 @@ }, { "cell_type": "markdown", - "id": "9913d4c2", + "id": "e9fa4fba", "metadata": {}, "source": [ "# 15.3 Categorical errors and discrete absences" ] }, + { + "cell_type": "markdown", + "id": "23437726", + "metadata": {}, + "source": [ + "Code 15.29" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "09f7a634", + "metadata": {}, + "outputs": [], + "source": [ + "Random.seed!(9)\n", + "\n", + "N_houses = 100\n", + "α = 5\n", + "β = -3\n", + "k = 0.5\n", + "r = 0.2\n", + "\n", + "cat = rand(Bernoulli(k), N_houses)\n", + "notes = rand.([Poisson(α + β * c) for c ∈ cat])\n", + "R_C = rand(Bernoulli(r), N_houses)\n", + "\n", + "cat_obs = Vector{Int}(cat)\n", + "cat_obs[R_C] .= -9\n", + "\n", + "dat = (\n", + " notes = notes,\n", + " cat = cat_obs,\n", + " RC = R_C,\n", + " N = N_houses,\n", + ");" + ] + }, + { + "cell_type": "markdown", + "id": "9ee9d9df", + "metadata": {}, + "source": [ + "Code 15.30" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "c6dd2fb2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "┌───────┬──────────────────────────────────────────────────────────┐\n", + "│\u001b[1m param \u001b[0m│\u001b[1m mean \u001b[0m\u001b[1m std \u001b[0m\u001b[1m 5.5% \u001b[0m\u001b[1m 50% \u001b[0m\u001b[1m 94.5% \u001b[0m\u001b[1m histogram \u001b[0m│\n", + "├───────┼──────────────────────────────────────────────────────────┤\n", + "│ a │ 1.55 0.0861 1.413 1.5511 1.6872 ▁▁▅█▅▁▁ │\n", + "│ b │ -0.2694 0.1358 -0.4872 -0.2687 -0.0541 ▁▁▁▂▄▇█▆▃▁▁▁ │\n", + "│ k │ 0.4639 0.0448 0.3926 0.4637 0.5344 ▁▂▇█▄▁▁ │\n", + "└───────┴──────────────────────────────────────────────────────────┘\n" + ] + } + ], + "source": [ + "@model function m15_8(notes, cat, RC, N)\n", + " a ~ Normal()\n", + " b ~ Normal(0, 0.5)\n", + " k ~ Beta(2, 2)\n", + " λ = @. logistic(a + b * cat)\n", + " \n", + " for i ∈ eachindex(cat)\n", + " if !RC[i]\n", + " cat[i] ~ Bernoulli(k)\n", + " notes[i] ~ Poisson(λ[i])\n", + " else\n", + " Turing.@addlogprob! log(k) + poislogpdf(exp(a+b), notes[i])\n", + " Turing.@addlogprob! log(1-k) + poislogpdf(exp(a), notes[i])\n", + " end\n", + " end\n", + "end\n", + "\n", + "m15_8_ch = sample(m15_8(dat...), NUTS(), 10000)\n", + "m15_8_df = DataFrame(m15_8_ch)\n", + "precis(m15_8_df)" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "d0026c60", + "id": "7012eac0", "metadata": {}, "outputs": [], "source": [] diff --git a/docs/15-Chapter 15. Missing Data and Other Opportunities.html b/docs/15-Chapter 15. Missing Data and Other Opportunities.html index 5aa0ca8..a5e1cd0 100644 --- a/docs/15-Chapter 15. Missing Data and Other Opportunities.html +++ b/docs/15-Chapter 15. Missing Data and Other Opportunities.html @@ -19129,6 +19129,111 @@

15.2 Missing data15.3 Categorical errors and discrete absences

+ +
+
+
+

Code 15.29

+ +
+
+
+
+
+
In [37]:
+
+
+
Random.seed!(9)
+
+N_houses = 100
+α = 5
+β = -3
+k = 0.5
+r = 0.2
+
+cat = rand(Bernoulli(k), N_houses)
+notes = rand.([Poisson(α + β * c) for c  cat])
+R_C = rand(Bernoulli(r), N_houses)
+
+cat_obs = Vector{Int}(cat)
+cat_obs[R_C] .= -9
+
+dat = (
+    notes = notes,
+    cat = cat_obs,
+    RC = R_C,
+    N = N_houses,
+);
+
+ +
+
+
+ +
+
+
+
+

Code 15.30

+ +
+
+
+
+
+
In [38]:
+
+
+
@model function m15_8(notes, cat, RC, N)
+    a ~ Normal()
+    b ~ Normal(0, 0.5)
+    k ~ Beta(2, 2)
+    λ = @. logistic(a + b * cat)
+    
+    for i  eachindex(cat)
+        if !RC[i]
+            cat[i] ~ Bernoulli(k)
+            notes[i] ~ Poisson(λ[i])
+        else
+            Turing.@addlogprob! log(k) + poislogpdf(exp(a+b), notes[i])
+            Turing.@addlogprob! log(1-k) + poislogpdf(exp(a), notes[i])
+        end
+    end
+end
+
+m15_8_ch = sample(m15_8(dat...), NUTS(), 10000)
+m15_8_df = DataFrame(m15_8_ch)
+precis(m15_8_df)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
┌───────┬──────────────────────────────────────────────────────────┐
+│ param     mean     std     5.5%      50%    94.5%     histogram │
+├───────┼──────────────────────────────────────────────────────────┤
+│     a │    1.55  0.0861    1.413   1.5511   1.6872       ▁▁▅█▅▁▁ │
+│     b │ -0.2694  0.1358  -0.4872  -0.2687  -0.0541  ▁▁▁▂▄▇█▆▃▁▁▁ │
+│     k │  0.4639  0.0448   0.3926   0.4637   0.5344       ▁▂▇█▄▁▁ │
+└───────┴──────────────────────────────────────────────────────────┘
+
+
+
+ +
+
+