使用R在正态分布下收集随机点

时间:2022-04-24 00:31:06

I'm trying to write an R script by first plotting standard normal distribution curve and then uniformly generating dots on the plot. I want to collect all the amount of dots that come under the standard normal PDF. But when I calculate the amount, it is not the same as displayed on the plot.

我试图通过首先绘制标准正态分布曲线然后在图上均匀生成点来编写R脚本。我想收集标准普通PDF下的所有点数。但是当我计算数量时,它与图上显示的不同。

Here is my code,

这是我的代码,

N = 20
normpdf = function(x) {
    f = (1/sqrt(2*pi))*exp(x^2/(-2))
    return(f)
}        

x = seq(-5,5,length=N)
normprobdist = normpdf(x)    
plot(x,normprobdist,type="l")
par(new=TRUE) 
u = NULL
dots_undercurve = NULL
for (i in 1:length(x)) { 
     u[i] = runif(x,0,1)
     if (u[i]<=normprobdist[i]) {
                dots_undercurve[i] = TRUE
     } 
     else {
              dots_undercurve[i] = FALSE
     }

}

sum(dots_undercurve)
plot(x,u,xaxt='n', yaxt = 'n', ann=FALSE)

The sum(dots_undercurve) displays different number of dots under the curve, than seen on the plot. My programming knowledge is quite bad, so please if someone could find whats wrong with my script? Thanks

总和(dots_undercurve)在曲线下显示的点数不同于在图上看到的点数。我的编程知识非常糟糕,所以如果有人能找到我的脚本有什么问题,请谢谢

2 个解决方案

#1


1  

You don't need a for loop or to define the pdf yourself:

您不需要for循环或自己定义pdf:

N <- 1e6
set.seed(42) #for reproducibility of random numbers
x <- runif(N, -5, 5)
y <- runif(N, 0, 1)

yunder <- y < dnorm(x) #which dots are under curve
sum(yunder)/N * (10 * 1) #the expected value is about 1
#[1] 0.99914

curve(dnorm, -5, 5, ylim=c(0,1))
points(x, y, col=c("red", "green")[yunder+1], pch=".")

使用R在正态分布下收集随机点

#2


0  

Many errors here: I thinks what you want is something like that:

这里有很多错误:我认为你想要的是这样的:

rm(list=ls(all=TRUE))
options(warn=-1)
N = 20
normpdf = function(x) {
  f = (1/sqrt(2*pi))*exp(x^2/(-2))
  return(f)
}
x = seq(-5,5,length=N)
normprobdist = normpdf(x)    
par(new=TRUE) 
u = runif(N,0,1)
dots_undercurve = NULL
for (i in 1:length(x)) { 
  if (u[i]<=normprobdist[i]) {
  dots_undercurve[i] = TRUE
} 
else {
  dots_undercurve[i] = FALSE
} 
}
sum(dots_undercurve)
plot(x,u)
lines(x,normprobdist,type="l")

使用R在正态分布下收集随机点

#1


1  

You don't need a for loop or to define the pdf yourself:

您不需要for循环或自己定义pdf:

N <- 1e6
set.seed(42) #for reproducibility of random numbers
x <- runif(N, -5, 5)
y <- runif(N, 0, 1)

yunder <- y < dnorm(x) #which dots are under curve
sum(yunder)/N * (10 * 1) #the expected value is about 1
#[1] 0.99914

curve(dnorm, -5, 5, ylim=c(0,1))
points(x, y, col=c("red", "green")[yunder+1], pch=".")

使用R在正态分布下收集随机点

#2


0  

Many errors here: I thinks what you want is something like that:

这里有很多错误:我认为你想要的是这样的:

rm(list=ls(all=TRUE))
options(warn=-1)
N = 20
normpdf = function(x) {
  f = (1/sqrt(2*pi))*exp(x^2/(-2))
  return(f)
}
x = seq(-5,5,length=N)
normprobdist = normpdf(x)    
par(new=TRUE) 
u = runif(N,0,1)
dots_undercurve = NULL
for (i in 1:length(x)) { 
  if (u[i]<=normprobdist[i]) {
  dots_undercurve[i] = TRUE
} 
else {
  dots_undercurve[i] = FALSE
} 
}
sum(dots_undercurve)
plot(x,u)
lines(x,normprobdist,type="l")

使用R在正态分布下收集随机点