楼主: ReneeBK
1283 11

[Lecture Notes]Topics in Bayesian statistics [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Topics in Bayesian statistics.pdf (931.21 KB) http://people.bath.ac.uk/masss/ma40189.html

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Statistics statistic Bayesian Lecture Statist

已有 1 人评分经验 收起 理由
oliyiyi + 60 精彩帖子

总评分: 经验 + 60   查看全部评分

沙发
ReneeBK 发表于 2016-6-5 01:45:24 |只看作者 |坛友微信交流群

Step by step illustration of the Gibbs sampler for bivariate normal

  1. gibbs.update=function(start=c(1,1), n=10, rho=0.5, pause=T) {
  2. # use pause=T for small n, to display step by step pressing return
  3. # in between.
  4. # use pause-F to run stragiht through and plot the pictures only
  5. # at the end

  6. # set up vectors for the samples
  7.   x=rep(NA,n)
  8.   y=rep(NA,n)
  9.   x[1]=start[1]
  10.   y[1]=start[2]

  11.   #Set up plots
  12.   xx=seq(-3,3,len=20)
  13.   yy=seq(-3,3,len=20)
  14.   z = outer(xx,yy,f,rho)
  15.   if (pause) {
  16.     layout(matrix(c(0,1,1,0,0,1,1,0,2,2,3,3,2,2,3,3),4,4,byrow=T))
  17.     #plot contours
  18.     contour(xx,yy,z,col="blue",levels=c(0.01,0.02,0.05,0.1,0.14),
  19.             drawlabels=F)
  20.     title(xlab="x",ylab="y")
  21.     #mark starting point:
  22.     points(x[1],y[1],pch=4,col="red")
  23.     #Time series plot so far:
  24.     # X:
  25.     plot(c(0,n),c(min(start[1],-3),max(start[1],3)),
  26.          xlab="iterations",ylab="x",type="n")
  27.     lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  28.     lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")
  29.     # Y:
  30.     plot(c(0,n),c(min(start[2],-3),max(start[2],3)),
  31.          xlab="iterations",ylab="y",type="n")
  32.     lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  33.     lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")
  34.     # Wait for key press
  35.     readLines(n=1)
  36.   }

  37.    
  38.   #Main loop
  39.   for (t in 2:n) {

  40.     # sample x from p(x|y=y[t-1]):
  41.     x[t]=rnorm(1, rho* y[t-1], sqrt(1-rho^2))
  42.     # sample y from p(y|=x[t]):
  43.     y[t]=rnorm(1, rho* x[t], sqrt(1-rho^2))

  44.     if (pause || (t==n)) {
  45.    
  46.       #plot contours (use xx,yy and z from before)
  47.       contour(xx,yy,z ,col="blue",levels=c(0.01,0.02,0.05,0.1,0.14),
  48.               drawlabels=F)
  49.       title(xlab="x",ylab="y")
  50.       #mark starting point:
  51.       points(x[1],y[1],pch=4,col="red")
  52.       #draw path of the chain
  53.       lines(c(x[1],rep(x[2:t],rep(2,t-1))),
  54.             c(rep(y[1:(t-1)],rep(2,t-1)),y[t]),col="red")

  55.       #Time series plot so far:
  56.       # X:
  57.       plot(c(0,n),c(min(start[1],-3),max(start[1],3)),
  58.            xlab="iterations",ylab="x",type="n")
  59.       lines(1:t,x[1:t],col="red")
  60.       lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  61.       lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")
  62.       # Y:
  63.       plot(c(0,n),c(min(start[2],-3),max(start[2],3)),
  64.            xlab="iterations",ylab="y",type="n")
  65.       lines(1:t,y[1:t],col="red")
  66.       lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  67.       lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")
  68.       
  69.     }

  70.     if ((pause)  && (t<n)) {
  71.       # Wait for key press
  72.       readLines(n=1)
  73.     }


  74.   #end main loop
  75.   }
  76. }
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-6-5 01:46:29 |只看作者 |坛友微信交流群
  1. metropolis = function (start=1, n=100, sig.q=1, mu.p=0, sig.p=1) {

  2. #starting value theta_0 = start
  3. #run for n iterations

  4.   #set up vector theta[] for the samples
  5.   theta=rep(NA,n)
  6.   theta[1]=start

  7.   #count the number of accepted proposals
  8.   accept=0

  9.   #Main loop
  10.   for (t in 2:n) {

  11.     #pick a candidate value theta* from N(theta[t-1], sig.q)
  12.     theta.star = rnorm(1, theta[t-1], sqrt(sig.q))
  13.    
  14.     #generate a random number between 0 and 1:
  15.     u = runif(1)

  16.     # calculate acceptance probability:
  17.     r = (dnorm(theta.star,mu.p,sig.p)) / (dnorm(theta[t-1],mu.p,sig.p) )
  18.    
  19.     a=min(r,1)
  20.    

  21.     #if u<a we accept the proposal; otherwise we reject
  22.     if (u<a) {   
  23.         #accept
  24.             accept=accept+1
  25.             theta[t] = theta.star
  26.     }
  27.     else {
  28.         #reject
  29.         theta[t] = theta[t-1]
  30.         }
  31.   #end loop
  32.   }
复制代码

使用道具

板凳
ReneeBK 发表于 2016-6-5 01:47:25 |只看作者 |坛友微信交流群
  1. metropolis.update =function(start=1, n=10, sig.q=1, mu.p=0, sig.p=1) {

  2.   #set up vector theta[] for the samples
  3.   theta=rep(NA,n)
  4.   theta[1]=start

  5.   #count the number of accepted proposals
  6.   accept=0

  7.   #Set up plots
  8.   layout(matrix(c(0,1,1,0,0,1,1,0,2,2,2,2,2,2,2,2),4,4,byrow=T))
  9.   #Selection of points
  10.   plot (c(-5,5),c(0,0.5),type="n",main="",xlab="theta",ylab="density")
  11.   #Target density
  12.   curve(dnorm(x,mu.p,sqrt(sig.p)),add=T,lty=2,col="blue")
  13.   #Proposal density
  14.   curve(dnorm(x,theta[1],sqrt(sig.q)),add=T,col="red")
  15.   #current proposal
  16.   lines(c(theta[1],theta[1]),c(0,0.5),col="red")

  17.   #Time series plot so far:
  18.   plot(c(0,n),c(min(start,-3),max(start,3)),
  19.        xlab="iterations",ylab="theta",type="n")
  20.   lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  21.   lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")

  22.   readLines(n=1)
  23.   
  24.   #Main loop
  25.   for (t in 2:n) {

  26.     #pick a candidate value theta* from N(theta[t-1], sig.q)
  27.     theta.star = rnorm(1, theta[t-1], sqrt(sig.q))
  28.    
  29.     #generate a random number between 0 and 1:
  30.     u = runif(1)

  31.     # calculate acceptance probability:
  32.     r = (dnorm(theta.star,mu.p,sig.p)) / (dnorm(theta[t-1],mu.p,sig.p) )
  33.     a=min(r,1)
  34.    

  35.     #if u<a we accept the proposal; otherwise we reject
  36.     if (u<a) {   
  37.         #accept
  38.             accept=accept+1
  39.             theta[t] = theta.star
  40.             cat("\ttheta*=",theta.star,"\n")
  41.             cat("ACCEPT: theta[t]=",theta[t],"\n")
  42.     }
  43.     else {
  44.         #reject
  45.         theta[t] = theta[t-1]
  46.         cat("\ttheta*=",theta.star,"\n")
  47.         cat("REJECT: theta[t]=",theta[t],"\n")
  48.         }

  49.     # update plots
  50.     layout(matrix(c(0,1,1,0,0,1,1,0,2,2,2,2,2,2,2,2),4,4,byrow=T))
  51.     #Selection of points
  52.     plot (c(-5,5),c(0,0.5),type="n",main="",xlab="theta",ylab="density")
  53.     #Target density
  54.     curve(dnorm(x,mu.p,sqrt(sig.p)),add=T,lty=2,col="blue")
  55.     #Proposal density
  56.     curve(dnorm(x,theta[t-1],sqrt(sig.q)),add=T,col="red")
  57.     #current proposal
  58.     lines(c(theta.star,theta.star),c(0,0.5),col="red",lty=2)
  59.     readLines(n=1)
  60.     #theta[t]
  61.     lines(c(theta[t],theta[t]),c(0,0.5),col="red")

  62.     #Plot the density so far
  63.     if (t>2) lines(density(theta[1:t]),col="green",lty=2)
  64.     #Time series plot so far:
  65.     plot(c(0,n),c(min(start,-3),max(start,3)),
  66.          xlab="iterations",ylab="theta",type="n")
  67.     lines(0:t,theta[1:(t+1)],col="red")
  68.     lines(c(0,n),c(1.96,1.96),lty=2,col="blue")
  69.     lines(c(0,n),-c(1.96,1.96),lty=2,col="blue")

  70.       
  71.     #Wait for keypress
  72.     readLines(n=1)
  73.    
  74.   #end loop
  75.   }


  76. }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-6-5 01:50:51 |只看作者 |坛友微信交流群
Metropolis-Hastings for Bivariate Normals
  1. metropolis1=function( n=1000 , rho=0.99 , start1=-1 , start2=-1 , tau=1 )
  2. {
  3.   x1 <- c(1:n)
  4.   x2 <- c(1:n)
  5.   x1[1] <- start1
  6.   x2[1] <- start2
  7.   par( mfrow=c( 2,2 ) )
  8.   for ( i in 2:n )
  9.    {
  10.     try <- x1[i-1] + rnorm( 1 , 0 , tau )
  11.     temp <- exp( ( try^2 - x1[i-1]^2 - 2.0 * rho * x2[i-1] * ( try - x1[i-1] ) )/(-2.0*(1-rho^2)) )
  12.     if ( runif(1) < temp )
  13.      {
  14.       x1[i] <- try
  15.      }
  16.     else
  17.      {
  18.       x1[i] <- x1[i-1]
  19.      }
  20.     try <- x2[i-1] + rnorm(1,0,tau )
  21.     temp <- exp( ( try^2 - x2[i-1]^2 - 2.0 * rho * x1[i] *( try - x2[i-1]) )/(-2.0*(1-rho^2)) )
  22.     if ( runif(1) < temp )
  23.      {
  24.       x2[i] <- try
  25.      }
  26.     else
  27.      {
  28.       x2[i] <- x2[i-1]
  29.      }


  30.   plot( x1[1:i] , type="l" , xlab="Iteration" ,xlim=c(1,n),ylab="x1")
  31.   plot( x2[1:i] , type="l" , xlab="Iteration",xlim=c(1,n),ylab="x2" )
  32.   plot( x1[1:i] , x2[1:i] , type="p" , pch="." ,xlab="x1",ylab="x2")
  33.   plot( x1[1:i] , x2[1:i] , type="l" ,col="red",xlab="x1",ylab="x2")
  34. }
  35. }
复制代码

使用道具

地板
ReneeBK 发表于 2016-6-5 01:52:12 |只看作者 |坛友微信交流群
Metropolis-Hastings for sampling from a mixture of bivariate normals

  1. metropolis2=function( n=1000 , rho=0.0 , start1=0 , start2=0 , tau=0.25 , sigma2=0.1 )
  2. {
  3.   x1 <- c(1:n)
  4.   x2 <- c(1:n)
  5.   x1[1] <- start1
  6.   x2[1] <- start2
  7.   scale <- -0.5 / ( sigma2* ( 1 - rho^2 ) )
  8.   breaks <- c( -20:(1+20) )/4
  9.   par( mfrow=c( 2,2 ) )
  10.   for ( i in 2:n )
  11.    {
  12.     try <- x1[i-1] + rnorm( 1 , 0 , tau )
  13.     temp <- exp( scale * ( try^2 - 2*rho*try*x2[i-1] +x2[i-1]^2) ) +
  14.             exp( scale * ( (try-1)^2 - 2*rho*(try-1)*(x2[i-1]-1) + (x2[i-1]-1)^2 ) )
  15.     temp <- temp / ( exp( scale * ( x1[i-1]^2 - 2*rho*x1[i-1]*x2[i-1] + x2[i-1]^2 ) ) +
  16.                      exp( scale * ( (x1[i-1]-1)^2 - 2*rho*(x1[i-1]-1)*(x2[i-1]-1) + (x2[i-1]-1)^2 ) ) )
  17.     if ( runif(1) < temp )
  18.      {
  19.       x1[i] <- try
  20.      }
  21.     else
  22.      {
  23.       x1[i] <- x1[i-1]
  24.      }
  25.     try <- x2[i-1] + rnorm(1,0,tau )
  26.     temp <- exp( scale * ( try^2 - 2*rho*try*x1[i] + x1[i]^2) ) +
  27.             exp( scale * ( (try-1)^2 - 2*rho*(try-1)*(x1[i]-1) + (x1[i]-1)^2 ) )
  28.     temp <- temp / ( exp( scale * ( x2[i-1]^2 - 2*rho*x1[i]*x2[i-1] + x1[i]^2 ) ) +
  29.                      exp( scale * ( (x2[i-1]-1)^2 - 2*rho*(x1[i]-1)*(x2[i-1]-1) +(x1[i]-1)^2  ) ) )
  30.     if ( runif(1) < temp )
  31.      {
  32.       x2[i] <- try
  33.      }
  34.     else
  35.      {
  36.       x2[i] <- x2[i-1]
  37.      }


  38.   hist( x1[1:i] , breaks , main="x1" , xlab="x1")
  39.   hist( x2[1:i] , breaks , main="x2" , xlab="x2" )
  40.   plot( x1[1:i] , x2[1:i] , type="p" , pch="." ,xlab="x1",ylab="x2")
  41.   plot( x1[1:i] , x2[1:i] , type="l" ,col="red",xlab="x1",ylab="x2")
  42. }
  43. }
复制代码

使用道具

7
ReneeBK 发表于 2016-6-5 01:54:47 |只看作者 |坛友微信交流群
Gibbs Sampler from Bivariate Normals
  1. gibbs1=function( n=1000 , rho=0.99 , start1=-1 , start2=-1 )
  2. {
  3.   x1 <- c(1:n)
  4.   x2 <- c(1:n)
  5.   x1[1] <- start1
  6.   x2[1] <- start2
  7.   par( mfrow=c( 2,2 ) )
  8.   for ( i in 2:n )
  9.    {
  10.     x1[i] <- rnorm( 1 , x2[i-1] * rho , 1-rho^2 )
  11.     x2[i] <- rnorm( 1, x1[i] * rho , 1 - rho^2 )

  12.   plot( x1[1:i] , type="l" , xlab="Iteration" ,xlim=c(1,n),ylab="x1")
  13.   plot( x2[1:i] , type="l" , xlab="Iteration",xlim=c(1,n),ylab="x2" )
  14.   plot( x1[1:i] , x2[1:i] , type="p" , pch="." ,xlab="x1",ylab="x2")
  15.   plot( x1[1:i] , x2[1:i] , type="l" ,col="red",xlab="x1",ylab="x2")
  16. }
  17. }
复制代码

使用道具

8
oliyiyi 发表于 2016-6-5 09:30:30 |只看作者 |坛友微信交流群
谢谢分享

使用道具

9
偽聖.6.0 发表于 2016-6-5 09:35:13 |只看作者 |坛友微信交流群

使用道具

10
benji427 在职认证  发表于 2016-6-5 10:21:11 |只看作者 |坛友微信交流群

感恩分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 01:26