[英]R Proble with Monte Carlo Simulation
How I could solve this exercise:我如何解决这个练习:
Let U come from a uniform (0,1) distribution and Z come from a standard normal distribution.令 U 来自均匀 (0,1) 分布,Z 来自标准正态分布。 Let X = pZ + (1-p)U.令 X = pZ + (1-p)U。 Estimate p when X has a variance of 0.4.当 X 的方差为 0.4 时估计 p。
Maybe I should install.packages('polynom')?也许我应该 install.packages('polynom')?
p is equal to 0,62 because I´ve already calculated it analitically, but I don´t know how to solve it with R using random random numbers generators p 等于 0,62,因为我已经对其进行了分析计算,但我不知道如何使用随机随机数生成器使用 R 来解决它
I will not solve for you the exercice but give you the clue on how to solve it我不会为你解决这个练习,但会给你如何解决它的线索
You need to do as the exercice does:你需要像练习一样做:
Put them together using a value for p使用p的值将它们放在一起
For a given p , you can define a function:对于给定的p ,您可以定义一个 function:
set.seed(234)
computeX <- function(p){
u <- runif(1000)
z <- rnorm(1000)
X = p*z + (1-p)*u
return(X)
}
var(computeX(0.2))
[1] 0.08924463
Now I let you find the routine to get p that is needed for your exercice.现在,我让您找到获得锻炼所需的p的例程。
No need for any external library不需要任何外部库
Here is an example这是一个例子
fobj <- function(p) abs(var(p*rnorm(1e5) + (1-p)*runif(1e5))-0.4)
pval <- optimise(fobj,c(0,1))$minimum
where在哪里
fobj
, ie, distance between variance of X
and 0.4
首先定义您的目标 function fobj
,即X
方差与0.4
之间的距离optimise
minimize the fobj
然后应用optimise
最小化fobj
Result结果
> pval
[1] 0.6223968
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.