R 圆形区域上的概率
假设z是一个二元正态(高斯)随机变量,平均值为s,协方差矩阵为b^2 I_2。我想得到区域| | x-s | | 0上的概率。我用R软件来计算它。下面是我试图估计概率的一个例子-R 圆形区域上的概率,r,probability,normal-distribution,R,Probability,Normal Distribution,假设z是一个二元正态(高斯)随机变量,平均值为s,协方差矩阵为b^2 I_2。我想得到区域| | x-s | | 0上的概率。我用R软件来计算它。下面是我试图估计概率的一个例子- library(mvtnorm) e2dist=function(x,y) # x is vector, y is matrix { a=sqrt((x[1]-y[,1])^2 + (x[2]-y[,2])^2) return(a) } r=0.5 b=1 s=c(0.1,0.1) x=c(0,0) a1=se
library(mvtnorm)
e2dist=function(x,y) # x is vector, y is matrix
{
a=sqrt((x[1]-y[,1])^2 + (x[2]-y[,2])^2)
return(a)
}
r=0.5
b=1
s=c(0.1,0.1)
x=c(0,0)
a1=seq(x[1]-r, x[1]+r, length.out=1000)
a2=seq(x[2]-r, x[2]+r, length.out=1000)
grid.pts=as.matrix(expand.grid(a1,a2))
ttt=e2dist(s,grid.pts)<=r
tt=which(ttt==T, arr.ind=T)
circle.in.pts=grid.pts[tt,]
mean(dmvnorm(circle.in.pts,s,b*diag(2)))
> [1] 0.1503632
这是不可能的(因为正方形比圆形大)。我知道有什么不对劲,但没能发现。你能帮我找到圆形区域的概率吗
p.S.1)函数“e2dist”计算两点之间的欧几里德距离
2) dmvnorm和pmvnorm都来自包“mvtnorm” 这里有一个蛮力方法: 使用问题中的以下内容:
library(mvtnorm)
e2dist=function(x,y) # x is vector, y is matrix
{
a=sqrt((x[1]-y[,1])^2 + (x[2]-y[,2])^2)
return(a)
}
r=0.5
b=1
s=c(0.1,0.1)
x=c(0,0)
从多元正态分布中抽取大样本,并计算这些样本在感兴趣区域中的比例
y <- rmvnorm(1000000,mean=s, sigma=b*diag(2))
#proportion of mvn distn in circular region (radius r) centered at x
dyx <- e2dist(x,y) #distances between y and x
mean(dyx < r)
>[1] 0.117238
#proportion of mvn distn in circular region (radius r) centered at s
dys <- e2dist(s,y) #distances between y and s
mean(dys < r)
>[1] 0.118308
您可以通过
shotGroups
包获得此概率:
> library(shotGroups)
> pmvnEll(r=0.5, sigma=diag(2), mu=c(0.1,0.1), e=diag(2), x0=c(0,0))
[1] 0.1164051
更一般地说,pmvnEll
函数返回多元(不仅仅是双变量)正态分布的偏移椭球区域的概率。扩展我的评论:
您的方法原则上是正确的,修复两个错误会产生大致正确的结果:
library(mvtnorm)
e2dist=function(x,y) # x is vector, y is matrix
{
a=sqrt((x[1]-y[,1])^2 + (x[2]-y[,2])^2)
return(a)
}
r=0.5
b=1
s=c(0.1,0.1)
x=c(0,0)
a1=seq(x[1]-r, x[1]+r, length.out=1000)
a2=seq(x[2]-r, x[2]+r, length.out=1000)
grid.pts=as.matrix(expand.grid(a1,a2))
ttt=e2dist(x,grid.pts)<=r # circle is centered around x not s
tt=which(ttt==T, arr.ind=T)
circle.in.pts=grid.pts[tt,]
mean(dmvnorm(circle.in.pts,s,b*diag(2))) * pi*r^2 # need to multiply by area
# Output:
# [1] 0.1164057
库(mvtnorm)
e2dist=函数(x,y)#x是向量,y是矩阵
{
a=sqrt((x[1]-y[,1])^2+(x[2]-y[,2])^2)
报税表(a)
}
r=0.5
b=1
s=c(0.1,0.1)
x=c(0,0)
a1=序列(x[1]-r,x[1]+r,长度输出=1000)
a2=序列(x[2]-r,x[2]+r,长度输出=1000)
grid.pts=as.matrix(扩展网格(a1,a2))
ttt=e2dist(x,grid.pts),包从中产生函数e2dist
和dmvnorm
?我相信pmvnorm(下=c(x[1]-r,x[2]-r),上=c(x[1]+r,x[2]+r),均值=s,σ=b*diag(2))[[1]]
返回由下部
和上部
限定的圆形区域中的概率,而不是问题中建议的方形区域。@Pascal我已编辑了问题。抱歉给你添麻烦了。e2dist函数已在许多空间建模的r软件包中使用。但无论如何,我应该解释一下。@ChrisHolbrook很抱歉不同意你的观点。pmvnorm计算具有任意极限和参数的正态分布函数。翻阅这一页,我知道黎曼积分适用于矩形域。我尝试这种方法只是为了看看它有多接近,因为我不知道如何正确计算圆上的概率。
> library(shotGroups)
> pmvnEll(r=0.5, sigma=diag(2), mu=c(0.1,0.1), e=diag(2), x0=c(0,0))
[1] 0.1164051
library(mvtnorm)
e2dist=function(x,y) # x is vector, y is matrix
{
a=sqrt((x[1]-y[,1])^2 + (x[2]-y[,2])^2)
return(a)
}
r=0.5
b=1
s=c(0.1,0.1)
x=c(0,0)
a1=seq(x[1]-r, x[1]+r, length.out=1000)
a2=seq(x[2]-r, x[2]+r, length.out=1000)
grid.pts=as.matrix(expand.grid(a1,a2))
ttt=e2dist(x,grid.pts)<=r # circle is centered around x not s
tt=which(ttt==T, arr.ind=T)
circle.in.pts=grid.pts[tt,]
mean(dmvnorm(circle.in.pts,s,b*diag(2))) * pi*r^2 # need to multiply by area
# Output:
# [1] 0.1164057