PSTAT 115 Homework4 课业解析

PSTAT 115 Homework4 课业解析

题意:

蒙特卡洛采样之拒绝采样

解析:

给定一个概率分布p(z)=p~(z)/Zp,p~(z)已知,Zp为归一化常数,为未知数。对该分布进行拒绝采样,我们引入一个简单地参考分布,记作q(x),q(x)分布的采样是易于实现的,比如均匀分布。再引入一个常数k,满足kq(z)>p~(z)。每次采样中首先从q(z)采样一个数值z0,然后在区间[0,kq(z0)]进行均匀采样,得到u0。如果u0<p~(z0),则保留该采样值,否则丢弃该采样值。最后得到的数据就是一个对该分布的近似采样。为了提高接受效率,防止舍弃过多的采样值而导致采样效率低下,k值应该满足在kq(z)>p~(z)的基础上尽可能小。

涉及知识点:

拒绝采样

更多可+薇❤讨论:qing1119X

pdf

Homework 4

PSTAT 115, Fall 2019

Due on November 3, 2019 at 11:59 pm

Note: If you are working with a partner, please submit only one homework per group with both names

and whether you are taking the course for graduate credit or not. Submit your Rmarkdown (.Rmd) and the

compiled pdf on Gauchospace.

1. Rejection Sampling the Beta distribution. Assume we did not have access to the rbeta function for

sampling from a Beta, but we were able to evaluate the density, dbeta. This is a very common setting

in Bayesian statistics, since we can always evaluate the (proportional) posterior density p(θ | y) ∝ p(y |

θ)p(θ) but we don’t have immediate access to a method for sampling from this distribution.

(a) Let p(x) be a Beta(3, 9) density, q1(x) a Uniform(0, 1) density, and q2(x) a Normal(µ = 0.25, σ =

0.15) density.

(b) Use rejection sampling to sample from p(x) by proposing samples from q1(x). To do so, first find

M1 = max

x

p(x)/q1(x) using the optimize function and set lower=0, upper=1, and maximum =

TRUE (since we are maximizing not minimizing, the default). M will be the value in the objective

argument returned by optimize (maximum tells us where the maximum occurs, but not what height

it achieves). Propose 10000 samples and keep only the accepted samples.

(c) Use rejection sampling to sample from p(x) by proposing samples from q2(x). To do this you

need to find M2 = max

x

p(x)/q2(x) as above. Propose 10000 samples and keep only the accepted

samples.

(d) Plot the p(x), M1q1(x) and M2q2(x) all on the same plot and verify visually that the scaled

proposal densities “envelope” the target, p(x). Set the xlimits of the plot from 0 to 1. Use different

color lines for the various densities so are clearly distinguishable.

(e) Which rejection sampler had the higher rejection rate? Why does this make sense given the plot

from the previous part? This means when proposing 10000 samples from each proposal, the Monte

Carlo error of our approximation will be higher when proposing from ____ (choose q1 or q2).

(f) Report the variance of Beta(3, 9) distribution by computing the variance of the beta samples. How

does this compare to the theoretical variance (refer to the probability cheatsheet).

2. Interval estimation with rejection sampling.

(a) Use rejection sampling to sample from the following density:

p(x) = 1

4

|sin(x)| × I{x ∈ [0, 2π]}

Use a proposal density which is uniform from 0 to 2π and generate at least 1000 true samples from

p(x). Compute and report the Monte Carlo estimate of the upper and lower bound for the 50%

quantile interval using the quantile function on your samples. Compare this to the 50% HPD

region calculated on the samples. What are the bounds on the HPD region? Report the length of

the quantile interval and the total length of the HPD region. What explains the difference? Hint:

to compute the HPD use the hdi function from the HDInterval package. As the first argument

pass in density(samples), where samples is the name of your vector of true samples from the

density. Set the allowSplit argument to true and use the credMass argument to set the total

probability mass in the HPD region to 50%.

(b) Plot p(x) using the curve function (base plotting) or stat_function (ggplot). Add lines corresponding to the intervals / probability regions computed in the previous part to your plot using

1

them segments function (base plotting) or geom_segements (ggplot). To ensure that the lines

don’t overlap visually, for the HPD region set the y-value of the segment to 0 and for the quantile

interval set the y-value to to 0.01. Make the segments for HPD region and the segment for quantile

interval different colors. Report the length of the quantile interval and the total length of the HPD

region, verifying that indeed the HPD region is smaller.

2

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,132评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,802评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,566评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,858评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,867评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,695评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,064评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,705评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,915评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,677评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,796评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,432评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,041评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,992评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,223评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,185评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,535评论 2 343

推荐阅读更多精彩内容