离散均匀先验——R语言代码段

xiaoxiao2021-02-28  21

Introduction to Bayes using Discrete Priors Jim Albert May 28, 2014 Learning About a Proportion A Discrete Prior Consider a population of “successes” and “failures” where the proportion of suc-cesses is p. Suppose p takes on the discrete set of values 0, .01, ..., .99, 1 and one assigns a uniform prior on these values. We enter the values of p and the associated probabilities into the vectors p and prior, respectively.

p <- seq(0, 1, by = 0.01)

prior <- 1 / 101 + 0 * p #为什么用0×p,自己好好想想,试试

plot(p, prior, type="h", main="Prior Distribution")

Posterior Distribution Suppose one takes a random sample from the population without replacement and observes 20 successes and 12 failiures. The function pdisc in the Learn-Bayes package computes the associated posterior probabilities for p. The inputs to pdisc are the prior (vector of values of p and vector of prior probabilities) and a vector containing the number of successes and failures. > library(LearnBayes) > post <- pdisc(p, prior, c(20, 12))

后验概率的计算

> post  [1] 0.000000e+00 6.604599e-33 6.131079e-27 1.802644e-23 5.019711e-21  [6] 3.839790e-19 1.296532e-17 2.488744e-16 3.158477e-15 2.921265e-14 [11] 2.104429e-13 1.238106e-12 6.160856e-12 2.662700e-11 1.020390e-10 [16] 3.524329e-10 1.111668e-09 3.236977e-09 8.778907e-09 2.234171e-08 [21] 5.369129e-08 1.224997e-07 2.665710e-07 5.554814e-07 1.112275e-06 [26] 2.146641e-06 4.003794e-06 7.233920e-06 1.268746e-05 2.164135e-05 [31] 3.596050e-05 5.829712e-05 9.232740e-05 1.430212e-04 2.169350e-04 [36] 3.225116e-04 4.703637e-04 6.735118e-04 9.475373e-04 1.310612e-03 [41] 1.783360e-03 2.388501e-03 3.150256e-03 4.093469e-03 5.242446e-03 [46] 6.619522e-03 8.243390e-03 1.012725e-02 1.227691e-02 1.468885e-02 [51] 1.734859e-02 2.022929e-02 2.329088e-02 2.647987e-02 2.972989e-02 [56] 3.296310e-02 3.609248e-02 3.902498e-02 4.166539e-02 4.392084e-02 [61] 4.570556e-02 4.694573e-02 4.758401e-02 4.758343e-02 4.693033e-02 [66] 4.563602e-02 4.373708e-02 4.129405e-02 3.838858e-02 3.511933e-02 [71] 3.159657e-02 2.793618e-02 2.425328e-02 2.065601e-02 1.724006e-02 [76] 1.408411e-02 1.124685e-02 8.765418e-03 6.655617e-03 4.913574e-03 [81] 3.518713e-03 2.437629e-03 1.628438e-03 1.045136e-03 6.415782e-04 [86] 3.747121e-04 2.068853e-04 1.071345e-04 5.152909e-05 2.273717e-05 [91] 9.058876e-06 3.191259e-06 9.661855e-07 2.415792e-07 4.705396e-08 [96] 6.521331e-09 5.525463e-10 2.153346e-11 2.037537e-13 6.094358e-17[101] 0.000000e+00

下面是我们通过手工计算看看

> p  [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14 [16] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44 [46] 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74 [76] 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00> p.post <-choose(32,20)*(p^20)*(1-p)^12> p.post  [1] 0.000000e+00 2.001394e-32 1.857903e-26 5.462558e-23 1.521125e-20  [6] 1.163573e-18 3.928886e-17 7.541650e-16 9.571144e-15 8.852319e-14 [11] 6.377057e-13 3.751837e-12 1.866926e-11 8.068787e-11 3.092091e-10 [16] 1.067978e-09 3.368692e-09 9.809022e-09 2.660275e-08 6.770214e-08 [21] 1.627009e-07 3.712113e-07 8.077910e-07 1.683277e-06 3.370529e-06 [26] 6.504973e-06 1.213271e-05 2.192097e-05 3.844685e-05 6.557983e-05 [31] 1.089712e-04 1.766579e-04 2.797800e-04 4.333977e-04 6.573789e-04 [36] 9.773077e-04 1.425345e-03 2.040945e-03 2.871325e-03 3.971553e-03 [41] 5.404120e-03 7.237881e-03 9.546230e-03 1.240445e-02 1.588620e-02 [46] 2.005916e-02 2.497997e-02 3.068865e-02 3.720275e-02 4.451167e-02 [51] 5.257149e-02 6.130087e-02 7.057841e-02 8.024202e-02 9.009057e-02 [56] 9.988818e-02 1.093712e-01 1.182575e-01 1.262588e-01 1.330934e-01 [61] 1.385017e-01 1.422598e-01 1.441940e-01 1.441922e-01 1.422131e-01 [66] 1.382910e-01 1.325366e-01 1.251335e-01 1.163290e-01 1.064222e-01 [71] 9.574719e-02 8.465510e-02 7.349478e-02 6.259398e-02 5.224259e-02 [76] 4.267913e-02 3.408136e-02 2.656187e-02 2.016854e-02 1.488962e-02 [81] 1.066277e-02 7.386756e-03 4.934660e-03 3.167080e-03 1.944176e-03 [86] 1.135491e-03 6.269250e-04 3.246500e-04 1.561488e-04 6.890052e-05 [91] 2.745114e-05 9.670482e-06 2.927835e-06 7.320582e-07 1.425878e-07 [96] 1.976161e-08 1.674383e-09 6.525291e-11 6.174355e-13 1.846775e-16[101] 0.000000e+00> p.sum <-sum(p.post)> p.sum[1] 3.030303> p.sum <-p.sum/101> p.sum[1] 0.030003> print(choose(32,20)*p^20*(1-p)^12/101/p.sum)  [1] 0.000000e+00 6.604599e-33 6.131079e-27 1.802644e-23 5.019711e-21  [6] 3.839790e-19 1.296532e-17 2.488744e-16 3.158477e-15 2.921265e-14 [11] 2.104429e-13 1.238106e-12 6.160856e-12 2.662700e-11 1.020390e-10 [16] 3.524329e-10 1.111668e-09 3.236977e-09 8.778907e-09 2.234171e-08 [21] 5.369129e-08 1.224997e-07 2.665710e-07 5.554814e-07 1.112275e-06 [26] 2.146641e-06 4.003794e-06 7.233920e-06 1.268746e-05 2.164135e-05 [31] 3.596050e-05 5.829712e-05 9.232740e-05 1.430212e-04 2.169350e-04 [36] 3.225116e-04 4.703637e-04 6.735118e-04 9.475373e-04 1.310612e-03 [41] 1.783360e-03 2.388501e-03 3.150256e-03 4.093469e-03 5.242446e-03 [46] 6.619522e-03 8.243390e-03 1.012725e-02 1.227691e-02 1.468885e-02 [51] 1.734859e-02 2.022929e-02 2.329088e-02 2.647987e-02 2.972989e-02 [56] 3.296310e-02 3.609248e-02 3.902498e-02 4.166539e-02 4.392084e-02 [61] 4.570556e-02 4.694573e-02 4.758401e-02 4.758343e-02 4.693033e-02 [66] 4.563602e-02 4.373708e-02 4.129405e-02 3.838858e-02 3.511933e-02 [71] 3.159657e-02 2.793618e-02 2.425328e-02 2.065601e-02 1.724006e-02 [76] 1.408411e-02 1.124685e-02 8.765418e-03 6.655617e-03 4.913574e-03 [81] 3.518713e-03 2.437629e-03 1.628438e-03 1.045136e-03 6.415782e-04 [86] 3.747121e-04 2.068853e-04 1.071345e-04 5.152909e-05 2.273717e-05 [91] 9.058876e-06 3.191259e-06 9.661855e-07 2.415792e-07 4.705396e-08 [96] 6.521331e-09 5.525463e-10 2.153346e-11 2.037537e-13 6.094358e-17[101] 0.000000e+00>

可见pdisc计算出来的post和我们手工计算的结果是一样的

链接如下

https://cran.r-project.org/web/packages/LearnBayes/vignettes/DiscreteBayes.pdf

转载请注明原文地址: https://www.6miu.com/read-2400084.html

最新回复(0)